-
Notifications
You must be signed in to change notification settings - Fork 8
/
board.py
259 lines (222 loc) · 8.02 KB
/
board.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
#!/usr/bin/env python
""" game.py Humberto Henrique Campos Pinheiro
Game logic.
"""
from config import WHITE, BLACK, EMPTY
from copy import deepcopy
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 __getitem__(self, i, j):
return self.board[i][j]
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 apply_move(self, move, color):
""" Determine if the move is correct and apply the changes in the game.
"""
if move in self.valid_moves:
self.board[move[0]][move[1]] = color
for i in range(1, 9):
self.flip(i, move, color)
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 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 compare(self, otherBoard):
"""Return a board containing only the squares that are empty in one
of the boards and not empty on the other.
"""
diffBoard = Board()
diffBoard.board[3][4] = 0
diffBoard.board[3][3] = 0
diffBoard.board[4][3] = 0
diffBoard.board[4][4] = 0
for i in range(8):
for j in range(8):
if otherBoard.board[i][j] != self.board[i][j]:
diffBoard.board[i][j] = otherBoard.board[i][j]
return otherBoard
def get_adjacent_count(self, color):
"""Return how many empty squares there are on the board adjacent to
the specified color."""
adjCount = 0
for x, y in [(a, b) for a in range(8) for b in range(8) if self.board[a][b] == color]:
for i, j in [(a, b) for a in [-1, 0, 1] for b in [-1, 0, 1]]:
if 0 <= x + i <= 7 and 0 <= y + j <= 7:
if self.board[x + i][y + j] == EMPTY:
adjCount += 1
return adjCount
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.apply_move(move, color)
yield newBoard