-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.py
More file actions
387 lines (335 loc) · 12.8 KB
/
Copy pathBoard.py
File metadata and controls
387 lines (335 loc) · 12.8 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import numpy as np
import copy
global corners
corners = [(0, 0), (0, 7), (7, 0), (7, 7)]
global c_square
c_square = [(0, 1), (0, 7), (1, 0), (1, 7), (6, 0), (6, 7), (7, 1), (7, 6)]
global x_square
x_square = [(1, 1), (6, 6), (1, 6), (6, 1)]
class Board:
directions = [(0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
def __init__(self, grid=None, display_mode='basic'):
self.grid = np.zeros((8, 8)) if grid is None else grid
self.grid[3, 3] = self.grid[4, 4] = 1
self.grid[3, 4] = self.grid[4, 3] = -1
self.display_mode = display_mode
def __getitem__(self, item):
return self.grid[item]
def update(self, pos, team_val, in_place=True):
to_update = [8*x+y for x,y in self.check_lines(pos, team_val)]
if in_place:
self.grid.put(to_update, team_val)
else:
grid = copy.deepcopy(self.grid)
grid.put(to_update, team_val)
return Board(grid)
def check_line(self, pos, dir, team_val):
if self.grid[pos] != 0:
return []
i, j = pos
e1, e2 = dir
mem = []
i += e1
j += e2
while 0 <= i < 8 and 0 <= j < 8 and self.grid[i, j] == -team_val:
mem.append((i, j))
i += e1
j += e2
if 0 <= i < 8 and 0 <= j < 8 and self.grid[i, j] == team_val:
return mem
return []
def check_lines(self, pos, team_val):
mem = []
for dir in self.directions:
mem.extend(self.check_line(pos, dir, team_val))
return mem + [pos] if mem else []
def is_move_possible(self, pos, team_val):
if not isinstance(pos, tuple):
return False
for dir in self.directions:
if self.check_line(pos, dir, team_val):
return True
return False
def possible_moves(self, team_val):
moves = []
for i in range(8):
for j in range(8):
if self.is_move_possible((i, j), team_val):
moves.append((i, j))
return moves
def execute_turn(self, pos, team_val, in_place=True):
if not self.is_move_possible(pos, team_val):
raise IndexError("Move {} is not allowed".format(pos))
else:
return self.update(pos, team_val, in_place=in_place)
def count_score(self):
white = - np.sum(self.grid[self.grid == -1])
black = np.sum(self.grid[self.grid == 1])
return black, white
def is_in(self, pos):
x, y = pos
return 0 <= x <= 7 and 0 <= y <= 7
def __str__(self):
if self.display_mode == 'advanced':
numbers = ["one1", "two1", "three1", "four1", "five1", "six1", "seven1", "eight1"]
w = ':white_pawn:'
b = ':black_pawn:'
li = ':black_square_button::aletter::bletter::cletter::dletter::eletter::fletter::gletter::hletter::black_square_button:\n'
for index, line in enumerate(self.grid):
li += ':' + numbers[index] + ':'
for i in line:
if i == -1:
li += w
elif i == 1:
li += b
else:
li += ':white_grid:'
li += ':' + numbers[index] + ':' + '\n'
li += ':black_square_button::aletter::bletter::cletter::dletter::eletter::fletter::gletter::hletter::black_square_button:\n'
return li
elif self.display_mode == 'basic':
b = '\u25CE'
w = '\u25C9'
message = '\u22BF a| b|c|d| e|f|g |h\n'
for index, line in enumerate(self.grid):
li = str(index + 1) + '|'
for i in line:
if i == -1:
li += w + '|'
elif i == 1:
li += b + '|'
else:
li += '\u25A2' + '|'
message += li + '\n'
return message + ' ' + '\u203E' * 16
# returns the number of definitive coins for each side
# rhe flag bool detects if we got into the for loop or not
def definitive_coins(self):
definitive = Board()
definitive.grid[3,3] = definitive.grid[4,4] = 0
definitive.grid[3,4] = definitive.grid[4,3] = 0
# UL
lim1, lim2 = 8, 8
x, y = corners[0][0], corners[0][1]
val = self[x, y]
if val != 0:
while self[x, y] == val:
definitive.grid[x, y] = val
flag = False
for i in range(1, lim1):
flag = True
if self[x + i, y] == val:
definitive.grid[x + i, y] = val
continue
else:
lim1 = i - 1
break
if not flag:
lim1 = 0
flag = False
for j in range(1, lim2):
flag = True
if self[x, y + j] == val:
definitive.grid[x, y + j] = val
continue
else:
lim2 = j - 1
break
if not flag:
lim2 = 0
lim1 -= 1
lim2 -= 1
if min(lim1, lim2) <= 0:
break
x += 1
y += 1
# UR
lim1, lim2 = 8, 8
x, y = corners[1][0], corners[1][1]
val = self[x, y]
if val != 0:
while self[x, y] == val:
definitive.grid[x, y] = val
flag = False
for i in range(1, lim1):
flag = True
if self[x + i, y] == val:
definitive.grid[x + i, y] = val
continue
else:
lim1 = i - 1
break
if not flag:
lim1 = 0
flag = False
for j in range(1, lim2):
flag = True
if self[x, y - j] == val:
definitive.grid[x, y - j] = val
continue
else:
lim2 = j - 1
break
if not flag:
lim2 = 0
lim1 -= 1
lim2 -= 1
if min(lim1, lim2) <= 0:
break
x += 1
y -= 1
# DL
lim1, lim2 = 8, 8
x, y = corners[2][0], corners[2][1]
val = self[x, y]
if val != 0:
while self[x, y] == val:
definitive.grid[x, y] = val
flag = False
for i in range(1, lim1):
flag = True
if self[x - i, y] == val:
definitive.grid[x - i, y] = val
continue
else:
lim1 = i - 1
break
if not flag:
lim1 = 0
flag = False
for j in range(1, lim2):
flag = True
if self[x, y + j] == val:
definitive.grid[x, y + j] = val
continue
else:
lim2 = j - 1
break
if not flag:
lim2 = 0
lim1 -= 1
lim2 -= 1
if min(lim1, lim2) <= 0:
break
x -= 1
y += 1
# UR
lim1, lim2 = 8, 8
x, y = corners[3][0], corners[3][1]
val = self[x, y]
if val != 0:
while self[x, y] == val:
flag = False
definitive.grid[x, y] = val
for i in range(1, lim1):
flag = True
if self[x - i, y] == val:
definitive.grid[x - i, y] = val
continue
else:
lim1 = i - 1
break
if not flag:
lim1 = 0
flag = False
for j in range(1, lim2):
flag = True
if self[x, y - j] == val:
definitive.grid[x, y - j] = val
continue
else:
lim2 = j - 1
break
if not flag:
lim2 = 0
lim1 -= 1
lim2 -= 1
if min(lim1, lim2) <= 0:
break
x -= 1
y -= 1
return definitive
def compute_score(self, team_val, maximize):
black, white = self.count_score()
disc_diff = 100 * (black - white) / (black + white)
black_move, white_move = len(self.possible_moves(1)), len(self.possible_moves(-1))
move_diff = 100 * (black_move - white_move) / (black_move + white_move + 1)
black_corner = white_corner = 0
for corner in corners:
if self[corner[0], corner[1]] == 1:
black_corner += 1
elif self[corner[0], corner[1]] == -1:
white_corner += 1
corner_diff = 100 * (black_corner - white_corner) / (black_corner + white_corner + 1)
definitive = self.definitive_coins()
black_def, white_def = definitive.count_score()
def_diff = 100 * (black_def - white_def) / (black_def + white_def + 1)
c_black = c_white = 0
for i, move in enumerate(c_square):
if self[move] == 1 and corners[i // 2] != 1 and definitive[move] != 1:
c_black += 1
if self[move] == -1 and corners[i // 2] != -1 and definitive[move] != -1:
c_white += 1
c_diff = 100 * (c_black - c_white) / (c_black + c_white + 1)
x_black = x_white = 0
for i, move in enumerate(x_square):
if self[move] == 1 and corners[i] != 1 and definitive[move] != 1:
x_black += 1
if self[move] == -1 and corners[i] != -1 and definitive[move] != -1:
x_white += 1
x_diff = 100 * (x_black - x_white) / (x_black + c_white + 1)
turn = black + white
score = 2 * move_diff * max(1, 12 - turn) + 100 * corner_diff - 100 * x_diff - 50 * c_diff + 30 * def_diff + disc_diff * max(0, turn - 58) * 100
if turn == 64:
score = disc_diff * 100
if maximize:
score = - score * team_val
else:
score = score * team_val
return score
def __eq__(self, board):
if isinstance(board, Board):
return (self.grid == board.grid).all()
else:
return super().__eq__(board)
class Game:
def __init__(self, player1, player2, display_func=print, board=None, cur_team=-1):
self.player1 = player1
self.player1.set_team('white')
self.player2 = player2
self.player2.set_team('black')
self.board = Board() if board == None else board
self.cur_team = cur_team
self.display_func = display_func
if self.display_func:
self.display_func('Starting a new game ! {} vs {}'.format(player1, player2))
self.display_func(self.board)
def next(self):
if not self.board.possible_moves(self.cur_team):
if not self.board.possible_moves(-self.cur_team):
return self.end()
else:
if self.display_func:
self.display_func('{} cannot play'.format('White' if self.cur_team == -1 else 'Black'))
self.cur_team = -self.cur_team
else:
if self.cur_team == -1:
move = self.player1.play(self.board)
else:
move = self.player2.play(self.board)
self.board.execute_turn(move, self.cur_team)
if self.display_func:
self.display_func('{} played {}'.format('White' if self.cur_team == -1 else 'Black',
chr(move[1] + ord('a')) + str(move[0] + 1)))
self.display_func(self.board)
self.cur_team = -self.cur_team
def end(self):
black, white = self.board.count_score()
if self.display_func:
self.display_func('Game is finished. Final score : \nWhite Team ({}) : {}\nBlack team ({}) : {}'.format(self.player1, white, self.player2, black))
return black, white
def rollout(self):
scores = None
while not scores:
scores = self.next()
return scores