-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.py
312 lines (267 loc) · 9.22 KB
/
env.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
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
#!/usr/bin/env python
""" game.py Humberto Henrique Campos Pinheiro
Game logic.
"""
from copy import deepcopy
import numpy as np
from helper import *
import time
EMPTY = 0
BLACK = 1
WHITE = -1
DRAW = 0
BLACK_WIN = 1
WHITE_WIN = -1
class Board:
""" Rules of the game """
def __init__(self):
self.board = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
self.board[3][4] = BLACK
self.board[4][3] = BLACK
self.board[3][3] = WHITE
self.board[4][4] = WHITE
self.valid_moves = []
def reset(self):
self.board = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
self.board[3][4] = BLACK
self.board[4][3] = BLACK
self.board[3][3] = WHITE
self.board[4][4] = WHITE
self.valid_moves = []
def __getitem__(self, i, j):
return self.board[i][j]
def set_black_winning_board(self):
W=-1
self.board = [[0, 0, 1, 0, 0, 0, 0, 0],
[0, W, 1, 0, 0, 0, W, 0],
[1, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 1, W, W, 0, 0, W],
[W, 1, 1, W, 1, 1, W, W],
[W, W, W, 1, W, W, W, W],
[W, W, W, W, W, W, W, 1],
[0, 0, 1, W, W, 1, 1, 1]]
self.valid_moves = []
def lookup(self, row, column, color):
"""Returns the possible positions that there exists at least one
straight (horizontal, vertical, or diagonal) line between the
piece specified by (row, column, color) and another piece of
the same color.
"""
if color == BLACK:
other = WHITE
else:
other = BLACK
places = []
if row < 0 or row > 7 or column < 0 or column > 7:
return places
# For each direction search for possible positions to put a piece.
for (x, y) in [
(-1, 0),
(-1, 1),
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(0, -1),
(-1, -1)
]:
pos = self.check_direction(row, column, x, y, other)
if pos:
places.append(pos)
return places
def check_direction(self, row, column, row_add, column_add, other_color):
i = row + row_add
j = column + column_add
if (i >= 0 and j >= 0 and i < 8 and j < 8 and self.board[i][j] == other_color):
i += row_add
j += column_add
while (i >= 0 and j >= 0 and i < 8 and j < 8 and self.board[i][j] == other_color):
i += row_add
j += column_add
if (i >= 0 and j >= 0 and i < 8 and j < 8 and self.board[i][j] == EMPTY):
return (i, j)
def get_valid_moves(self, color):
"""Get the avaiable positions to put a piece of the given color. For
each piece of the given color we search its neighbours,
searching for pieces of the other color to determine if is
possible to make a move. This method must be called before
apply_move.
"""
if color == BLACK:
other = WHITE
else:
other = BLACK
places = []
for i in range(8):
for j in range(8):
if self.board[i][j] == color:
places = places + self.lookup(i, j, color)
places = list(set(places))
self.valid_moves = places
return places
def play(self, move, color):
""" Determine if the move is correct and apply the changes in the game.
"""
current_board = deepcopy(self.board)
# DEBUG
#print("INSIDE board.play")
#print("curr_board")
#for b in current_board:
# print(b)
#print(self.valid_moves)
if move in self.valid_moves:
self.board[move[0]][move[1]] = color
for i in range(1, 9):
self.flip(i, move, color)
new_board = deepcopy(self.board)
# DEBUG
#print("new board")
#for b in new_board:
# print(b)
if self.game_ended():
white, black, empty = self.count_stones()
if white==black:
return 0.5
elif white>black:
return 1.0
else:
return 0.0
else:
return 0.0 # replace with reward func in future
def flip(self, direction, position, color):
""" Flips (capturates) the pieces of the given color in the given direction
(1=North,2=Northeast...) from position. """
if direction == 1:
# north
row_inc = -1
col_inc = 0
elif direction == 2:
# northeast
row_inc = -1
col_inc = 1
elif direction == 3:
# east
row_inc = 0
col_inc = 1
elif direction == 4:
# southeast
row_inc = 1
col_inc = 1
elif direction == 5:
# south
row_inc = 1
col_inc = 0
elif direction == 6:
# southwest
row_inc = 1
col_inc = -1
elif direction == 7:
# west
row_inc = 0
col_inc = -1
elif direction == 8:
# northwest
row_inc = -1
col_inc = -1
places = [] # pieces to flip
i = position[0] + row_inc
j = position[1] + col_inc
if color == WHITE:
other = BLACK
else:
other = WHITE
if i in range(8) and j in range(8) and self.board[i][j] == other:
# assures there is at least one piece to flip
places = places + [(i, j)]
i = i + row_inc
j = j + col_inc
while i in range(8) and j in range(8) and self.board[i][j] == other:
# search for more pieces to flip
places = places + [(i, j)]
i = i + row_inc
j = j + col_inc
if i in range(8) and j in range(8) and self.board[i][j] == color:
# found a piece of the right color to flip the pieces between
for pos in places:
# flips
self.board[pos[0]][pos[1]] = color
def get_changes(self):
""" Return black and white counters. """
whites, blacks, empty = self.count_stones()
return (self.board, blacks, whites)
def game_ended(self):
""" Is the game ended? """
# board full or wipeout
whites, blacks, empty = self.count_stones()
if whites == 0 or blacks == 0 or empty == 0:
return True
# no valid moves for both players
if self.get_valid_moves(BLACK) == [] and \
self.get_valid_moves(WHITE) == []:
return True
return False
def print_board(self):
for i in range(8):
print(i, ' |', end=' ')
for j in range(8):
if self.board[i][j] == BLACK:
print('B', end=' ')
elif self.board[i][j] == WHITE:
print('W', end=' ')
else:
print(' ', end=' ')
print('|', end=' ')
print()
def print_state(s):
for i in range(8):
print(i, ' |', end=' ')
for j in range(8):
if s[i][j] == BLACK:
print('B', end=' ')
elif s[i][j] == WHITE:
print('W', end=' ')
else:
print(' ', end=' ')
print('|', end=' ')
print()
def count_stones(self):
""" Returns the number of white pieces, black pieces and empty squares, in
this order.
"""
whites = 0
blacks = 0
empty = 0
for i in range(8):
for j in range(8):
if self.board[i][j] == WHITE:
whites += 1
elif self.board[i][j] == BLACK:
blacks += 1
else:
empty += 1
return whites, blacks, empty
def next_states(self, color):
"""Given a player's color return all the boards resulting from moves
that this player cand do. It's implemented as an iterator.
"""
valid_moves = self.get_valid_moves(color)
for move in valid_moves:
newBoard = deepcopy(self)
newBoard.play(move, color)
yield newBoard
def get_current_state(self):
return np.array(deepcopy(self.board), dtype=np.float32).flatten()