-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
158 lines (136 loc) · 4.75 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
import traceback
import time
import json
import random
import time
#from PIL import Image
import client_bot
class Board(object):
EMPTY = 0
P_1 = 1
P_2 = 2
def __init__(self, x_size, y_size):
self.x_size, self.y_size = x_size, y_size
self.locations = {}
def __repr__(self):
return str(self.locations)
def __getitem__(self, pos):
if pos in self.locations:
return self.locations[pos]
return Board.EMPTY
def __setitem__(self, pos, val):
if val == Board.EMPTY:
if pos in self.locations:
del self.locations[pos]
elif 0<=pos[0]<self.y_size:
if 0<=pos[1]<self.x_size:
self.locations[pos] = val
def save(self, filename = "board.png"):
im = Image.new("L", (200, 200))
for i in xrange(200):
for j in xrange(200):
im.putpixel((i,j), self[i,j]*127)
print("saving")
im.save(filename)
im.close()
def get_neighbours(self, x, y):
return self[x+1,y], self[x-1,y], self[x,y+1], self[x,y-1], \
self[x+1,y+1], self[x-1,y-1], self[x-1,y+1], self[x+1,y-1],
def get_neighbour_locs(self, x, y):
return (x+1,y), (x-1,y), (x,y+1), (x,y-1), \
(x+1,y+1), (x-1,y-1), (x+1,y+1), (x-1,y-1)
def tick(self):
updated = {}
for loc in self.locations:
player = self.locations[loc]
neighbours = self.get_neighbours(*loc)
friendly_count = neighbours.count(player)
if friendly_count < 2:
#Starve
updated[loc] = Board.EMPTY
if friendly_count > 3:
#Overpoputation
updated[loc] = Board.EMPTY
for loc in self.get_neighbour_locs(*loc):
if self[loc] == Board.EMPTY:
#Up for reclaiming
neighbours = self.get_neighbours(*loc)
set_neighbours = set(neighbours) - {Board.EMPTY}
if len(set_neighbours) == 1 and neighbours.count(list(set_neighbours)[0])==3:
updated[loc] = list(set_neighbours)[0]
for loc in updated:
self[loc] = updated[loc]
return updated
def update(self, positions, player_id):
rtn = {}
for loc in positions:
loc = tuple(loc)
if self[loc] == Board.EMPTY:
self[loc] = player_id
rtn[loc] = player_id
return rtn
class Game(object):
x_size = 1000
y_size = 1000
no_generations = 1000
def __init__(self, bot_1, bot_2, stop_event, autostart = False):
cur_time = str(time.time())
self.log = open("log_%s.log"%cur_time, "w")
self.stop_event = stop_event
bots = [bot_1, bot_2]
random.shuffle(bots)
self.bot_1,self.bot_2 = bots
self.ended = False
if autostart:
self.play()
def play(self):
try:
self.setup()
except Exception as e:
self.log.write("--- Start game setup exception ---\n")
traceback.print_exc()
self.log.write("--- End game setup exception ---\n")
while not self.ended:
try:
self.tick()
except Exception as e:
self.log.write("--- Start game exception ---\n")
traceback.print_exc()
self.log.write("--- End game exception ---\n")
if self.tick_id == Game.no_generations:
self.end_time()
self.log.write("Finished!\n")
self.stop_event.set()
def setup(self):
self.log.write("SETUP GAME %s %s\n"%(self.bot_1, self.bot_2))
self.setup_board()
self.tick_id = 0
def setup_board(self):
self.board = Board(self.x_size, self.y_size)
def tick(self):
self.log.write("TICK\n")
#self.log.flush()
rtn = {}
bot_1 = self.get_move(self.bot_1, 1)
bot_2 = self.get_move(self.bot_2, 2)
#self.board.save("board_%s.png"%(self.tick_id))
tick = self.board.tick()
self.tick_id += 1
rtn.update(bot_1)
rtn.update(bot_2)
rtn.update(tick)
return rtn
def get_move(self, bot, bid):
return self.board.update(bot.get_move(bid, self.board, self.tick_id), bid)
def end_time(self):
self.ended = True
locs = [0, 0]
for loc in self.board.locations:
locs[self.board[loc]-1]+=1
if locs[0] >= locs[1]:
self.bot_1.inc_wins(self.bot_2)
if locs[0] <= locs[1]:
self.bot_2.inc_wins(self.bot_1)
if __name__ == "__main__":
pass