-
Notifications
You must be signed in to change notification settings - Fork 7
/
board.py
297 lines (262 loc) · 9.97 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
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
from copy import deepcopy
from config import EMPTY, BLACK, WHITE
import numpy as np
from ctypes import *
class Board:
LIBFUNCTIONS = cdll.LoadLibrary("./libfunctions.so")
def __init__(self, board = None):
if board is not None:
self.board = board
self.count_pieces()
else:
self.board = np.zeros((8, 8), dtype=np.integer) # 8 by 8 empty board
self.board[3][4] = BLACK
self.board[4][3] = BLACK
self.board[3][3] = WHITE
self.board[4][4] = WHITE
self.white_pieces = 2
self.black_pieces = 2
self.empty_spaces = 60
self.valid_moves = []
self.now_playing = BLACK
def get_possible_moves(self, row, column, color):
if color == BLACK:
other = WHITE
else:
other = BLACK
moves = []
if row < 0 or row > 7 or column < 0 or column > 7:
return moves
# north
i = row - 1
if i >= 0 and self.board[i][column] == other:
i = i - 1
while i >= 0 and self.board[i][column] == other:
i = i - 1
if i >= 0 and self.board[i][column] == 0:
moves = moves + [( i, column)]
# northeast
i = row - 1
j = column + 1
if i >= 0 and j < 8 and self.board[i][j] == other:
i = i - 1
j = j + 1
while i >= 0 and j < 8 and self.board[i][j] == other:
i = i - 1
j = j + 1
if i >= 0 and j < 8 and self.board[i][j] == 0:
moves = moves + [(i, j)]
# east
j = column + 1
if j < 8 and self.board[row][j] == other:
j = j + 1
while j < 8 and self.board[row][j] == other:
j = j + 1
if j < 8 and self.board[row][j] == 0:
moves = moves + [(row, j)]
# southeast
i = row + 1
j = column + 1
if i < 8 and j < 8 and self.board[i][j] == other:
i = i + 1
j = j + 1
while i < 8 and j < 8 and self.board[i][j] == other:
i = i + 1
j = j + 1
if i < 8 and j < 8 and self.board[i][j] == 0:
moves = moves + [(i, j)]
# south
i = row + 1
if i < 8 and self.board[i][column] == other:
i = i + 1
while i < 8 and self.board[i][column] == other:
i = i + 1
if i < 8 and self.board[i][column] == 0:
moves = moves + [(i, column)]
# southwest
i = row + 1
j = column - 1
if i < 8 and j >= 0 and self.board[i][j] == other:
i = i + 1
j = j - 1
while i < 8 and j >= 0 and self.board[i][j] == other:
i = i + 1
j = j - 1
if i < 8 and j >= 0 and self.board[i][j] == 0:
moves = moves + [(i, j)]
# west
j = column - 1
if j >= 0 and self.board[row][j] == other:
j = j - 1
while j >= 0 and self.board[row][j] == other:
j = j - 1
if j >= 0 and self.board[row][j] == 0:
moves = moves + [(row, j)]
# northwest
i = row - 1
j = column - 1
if i >= 0 and j >= 0 and self.board[i][j] == other:
i = i - 1
j = j - 1
while i >= 0 and j >= 0 and self.board[i][j] == other:
i = i - 1
j = j - 1
if i >= 0 and j >= 0 and self.board[i][j] == 0:
moves = moves + [(i, j)]
return moves
def get_valid_moves(self, color):
v = Board.LIBFUNCTIONS.get_valid_moves(c_void_p(self.board.ctypes.data), color)
c_int_p_p = POINTER(POINTER(c_int))
moves = cast(v, c_int_p_p)
valid_moves = [None] * moves[0][0]
for i in range(moves[0][0]):
valid_moves[i] = (moves[i+1][0], moves[i+1][1])
self.valid_moves = valid_moves
Board.LIBFUNCTIONS.free_moves(v, moves[0][0])
return valid_moves
def get_valid_moves_python(self, color):
if color == BLACK:
num_pieces = self.black_pieces
else:
num_pieces = self.white_pieces
if num_pieces < self.empty_spaces:
return self.get_valid_moves_by_colored_squares(color)
else:
return self.get_valid_moves_by_empty_squares(color)
def get_valid_moves_by_colored_squares(self, color):
valid_moves = []
for i in xrange(8):
for j in xrange(8):
if self.board[i][j] == color:
valid_moves = valid_moves + self.get_possible_moves(i, j, color)
valid_moves = list(set(valid_moves)) # Make each move in valid_moves unique
self.valid_moves = valid_moves
return valid_moves
def get_valid_moves_by_empty_squares(self, color):
moves = []
# For each empty space on the board, check if there are
# any of the opponents pieces available to flip
for i in xrange(8):
for j in xrange(8):
if self.board[i][j] == EMPTY:
for direction in xrange(1,9):
(num, valid) = self.pieces_to_flip_in_row((i, j), color, direction)
if num > 0:
moves = moves + [(i, j)]
break
self.valid_moves = moves
return moves
def apply_move(self, move, color):
self.board[move[0]][move[1]] = color
if color == BLACK:
self.black_pieces += 1
else:
self.white_pieces += 1
self.empty_spaces -= 1
self.flip_pieces(move, color)
def flip_pieces(self, position, color):
for direction in xrange(1,9): # Flip row for each of the 8 possible directions
(num_pieces, pieces_to_flip) = self.pieces_to_flip_in_row(position, color, direction)
for i in range(num_pieces):
self.board[pieces_to_flip[i][0]][pieces_to_flip[i][1]] = color
if color == BLACK:
self.black_pieces += num_pieces
self.white_pieces -= num_pieces
else:
self.black_pieces -= num_pieces
self.white_pieces += num_pieces
def pieces_to_flip_in_row(self, position, color, direction):
row_inc = 0
col_inc = 0
if direction >= 5:
direction += 1 # Have directions correspond to numberpad
if direction == 1 or direction == 2 or direction == 3:
row_inc = -1
elif direction == 7 or direction == 8 or direction == 9:
row_inc = 1
if direction == 1 or direction == 4 or direction == 7:
col_inc = -1
elif direction == 3 or direction == 6 or direction == 9:
col_inc = 1
pieces = [None] * 8
pieces_flipped = 0
i = position[0] + row_inc
j = position[1] + col_inc
if color == WHITE:
other = BLACK
else:
other = WHITE
if i in xrange(8) and j in xrange(8) and self.board[i][j] == other:
# assures there is at least one piece to flip
pieces[pieces_flipped] = (i,j)
pieces_flipped += 1
i = i + row_inc
j = j + col_inc
while i in xrange(8) and j in xrange(8) and self.board[i][j] == other:
# search for more pieces to flip
pieces[pieces_flipped] = (i,j)
pieces_flipped += 1
i = i + row_inc
j = j + col_inc
if i in xrange(8) and j in xrange(8) and self.board[i][j] == color:
# found a piece of the right color to flip the pieces between
return (pieces_flipped, pieces)
return (0, [])
def count_pieces(self):
self.white_pieces = 0
self.black_pieces = 0
self.empty_spaces = 64
for i in xrange(8):
for j in xrange(8):
if self.board[i][j] == WHITE:
self.white_pieces += 1
self.empty_spaces -= 1
elif self.board[i][j] == BLACK:
self.black_pieces += 1
self.empty_spaces -= 1
def game_won(self):
# Game Won if One Player Has No Pieces on the Board
if self.white_pieces == 0:
return BLACK
elif self.black_pieces == 0:
return WHITE
# Game also over if no valid moves for both players or no empty spaces left on board
elif (self.get_valid_moves(BLACK) == [] and self.get_valid_moves(WHITE) == []) or self.empty_spaces == 0:
if self.white_pieces > self.black_pieces:
return WHITE
elif self.black_pieces > self.white_pieces:
return BLACK
else:
return EMPTY # returning EMPTY denotes a tie
return None
def child_nodes(self, color):
moves = self.get_valid_moves(color)
children = [None]*len(moves)
for (i, move) in enumerate(moves):
child = Board()
child.now_playing = self.now_playing
child.board = np.copy(self.board)
child.valid_moves = self.valid_moves
child.white_pieces = self.white_pieces
child.black_pieces = self.black_pieces
child.empty_spaces = self.empty_spaces
child.apply_move(move, color)
children[i] = (child, move)
return children
# Function to print board for text based game
def print_board(self):
print ' ',
for i in xrange(8):
print ' ', i,
print
for i in xrange(8):
print i, ' |',
for j in xrange(8):
if self.board[i][j] == BLACK:
print 'B',
elif self.board[i][j] == WHITE:
print 'W',
else:
print ' ',
print '|',
print