-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
356 lines (295 loc) · 12.2 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
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
# AI
#
# Myanna Harris, Jasmine Jans, and Carol Joplin ([email protected] submitter)
# 3-10-17
#
# Othello
# Board: Class that represents the board used in the game of othello.
# Board is 8x8 with letters across the columns and numbers down the rows.
#
class Board(object):
# constructor to set up board with first 4 tokens based
# on the config parameter
def __init__(self, config):
# set up scores
self.black = 2
self.white = 2
# set up empty board
# array
# row = 0, 8, 16, ... = 8 * row
# column = 0, 1, 2, 3, ... = col
# position in grid = 8 * row + col
self.board = ["-" for x in range(0, 64)]
# center of board is 3,3 3,4 4,3 4,4
if config == 1:
# WB
# BW
self.board[8 * 3 + 3] = 'W'
self.board[8 * 3 + 4] = 'B'
self.board[8 * 4 + 3] = 'B'
self.board[8 * 4 + 4] = 'W'
else:
# BW
# WB
self.board[8 * 3 + 3] = 'B'
self.board[8 * 3 + 4] = 'W'
self.board[8 * 4 + 3] = 'W'
self.board[8 * 4 + 4] = 'B'
# Returns a copy of the board array
def getBoard(self):
copyBoard = []
for x in self.board:
copyBoard.append(x)
return copyBoard
# displays board, aka prints out the current score and
# current board configuration
def printBoard(self):
# Print score
print("")
print("White: " + str(self.white))
print("Black: " + str(self.black))
print("")
# Print board
print(" A B C D E F G H")
for row in range(0, 8):
printStr = str(row + 1)
for col in range(0, 8):
printStr += " " + self.board[8 * row + col]
print(printStr)
print("")
# display checking board, aka board that checks the move
# wanting to be places
def printCheckBoard(self, checkBoard):
# Print board
print(" A B C D E F G H")
for row in range(0, 8):
printStr = str(row + 1)
for col in range(0, 8):
printStr += " " + checkBoard[8 * row + col]
print(printStr)
# Checks the next black move to see if it is legal and
# creates a new "check board" to be printed to verify the move
def checkBlackMove(self, row, col):
copy = self.board[:]
copy[8 * row + col] = 'B'
#check all of the surrounding squares of the move
self.checkUpLeft(copy, row-1, col-1, "B", "X")
self.checkRight(copy, row, col+1, "B", "X")
self.checkLeft(copy, row, col-1, "B", "X")
self.checkUp(copy, row-1, col, "B", "X")
self.checkBottom(copy, row+1, col, "B", "X")
self.checkUpRight(copy, row-1, col+1, "B", "X")
self.checkBottomRight(copy, row+1, col+1, "B", "X")
self.checkBottomLeft(copy, row+1, col-1, "B", "X")
#print out the checking board
self.printCheckBoard(copy)
# Checks the next white move to see if it is legal and
# creates a new "check board" to be printed to verify the move
def checkWhiteMove(self, row, col):
copy = self.board[:]
copy[8 * row + col] = 'W'
#check all of the surrounding squares of the move
self.checkUpLeft(copy, row-1, col-1, "W", "X")
self.checkRight(copy, row, col+1, "W", "X")
self.checkLeft(copy, row, col-1, "W", "X")
self.checkUp(copy, row-1, col, "W", "X")
self.checkBottom(copy, row+1, col, "W", "X")
self.checkUpRight(copy, row-1, col+1, "W", "X")
self.checkBottomRight(copy, row+1, col+1, "W", "X")
self.checkBottomLeft(copy, row+1, col-1, "W", "X")
#print out the checking board
self.printCheckBoard(copy)
#the following 8 "check" methods check in all the directions for certain
#pieces that will flip colors if a given move is made. It then
#replaces those pieces with the given newItem
# check upLeft items
def checkUpLeft(self, currBoard, row, col, currColor, newItem):
if (row < 0 or row > 7 or col < 0 or col > 7 or
currBoard[8 * row + col] == "-"):
return False
if currColor == currBoard[8 * row + col]:
return True
if self.checkUpLeft(currBoard, row-1, col-1, currColor, newItem):
currBoard[8 * row + col] = newItem
return True
return False
# check upRight items
def checkUpRight(self, currBoard, row, col, currColor, newItem):
if (row < 0 or row > 7 or col < 0 or col > 7 or
currBoard[8 * row + col] == "-"):
return False
if currColor == currBoard[8 * row + col]:
return True
if self.checkUpRight(currBoard, row-1, col+1, currColor, newItem):
currBoard[8 * row + col] = newItem
return True
return False
# check bottomLeft items
def checkBottomLeft(self, currBoard, row, col, currColor, newItem):
if (row < 0 or row > 7 or col < 0 or col > 7 or
currBoard[8 * row + col] == "-"):
return False
if currColor == currBoard[8 * row + col]:
return True
if self.checkBottomLeft(currBoard, row+1, col-1, currColor, newItem):
currBoard[8 * row + col] = newItem
return True
return False
# check BottomRight items
def checkBottomRight(self, currBoard, row, col, currColor, newItem):
if (row < 0 or row > 7 or col < 0 or col > 7 or
currBoard[8 * row + col] == "-"):
return False
if currColor == currBoard[8 * row + col]:
return True
if self.checkBottomRight(currBoard, row+1, col+1, currColor, newItem):
currBoard[8 * row + col] = newItem
return True
return False
# check up items
def checkUp(self, currBoard, row, col, currColor, newItem):
if (row < 0 or row > 7 or col < 0 or col > 7 or
currBoard[8 * row + col] == "-"):
return False
if currColor == currBoard[8 * row + col]:
return True
if self.checkUp(currBoard, row-1, col, currColor, newItem):
currBoard[8 * row + col] = newItem
return True
return False
# check right items
def checkRight(self, currBoard, row, col, currColor, newItem):
if (row < 0 or row > 7 or col < 0 or col > 7 or
currBoard[8 * row + col] == "-"):
return False
if currColor == currBoard[8 * row + col]:
return True
if self.checkRight(currBoard, row, col+1, currColor, newItem):
currBoard[8 * row + col] = newItem
return True
return False
# check left items
def checkLeft(self, currBoard, row, col, currColor, newItem):
if (row < 0 or row > 7 or col < 0 or col > 7 or
currBoard[8 * row + col] == "-"):
return False
if currColor == currBoard[8 * row + col]:
return True
if self.checkLeft(currBoard, row, col-1, currColor, newItem):
currBoard[8 * row + col] = newItem
return True
return False
# check bottom items
def checkBottom(self, currBoard, row, col, currColor, newItem):
if (row < 0 or row > 7 or col < 0 or col > 7 or
currBoard[8 * row + col] == "-"):
return False
if currColor == currBoard[8 * row + col]:
return True
if self.checkBottom(currBoard, row+1, col, currColor, newItem):
currBoard[8 * row + col] = newItem
return True
return False
# Add black move, make the new move on the actual board
# and update the score
def blackMove(self, row, col):
self.board[8 * row + col] = 'B'
self.checkUpLeft(self.board, row-1, col-1, "B", "B")
self.checkRight(self.board, row, col+1, "B", "B")
self.checkLeft(self.board, row, col-1, "B", "B")
self.checkUp(self.board, row-1, col, "B", "B")
self.checkBottom(self.board, row+1, col, "B", "B")
self.checkUpRight(self.board, row-1, col+1, "B", "B")
self.checkBottomRight(self.board, row+1, col+1, "B", "B")
self.checkBottomLeft(self.board, row+1, col-1, "B", "B")
self.setScore()
# Add white move, make the new move on the actual board
# and update the score
def whiteMove(self, row, col):
self.board[8 * row + col] = 'W'
self.checkUpLeft(self.board, row-1, col-1, "W", "W")
self.checkRight(self.board, row, col+1, "W", "W")
self.checkLeft(self.board, row, col-1, "W", "W")
self.checkUp(self.board, row-1, col, "W", "W")
self.checkBottom(self.board, row+1, col, "W", "W")
self.checkUpRight(self.board, row-1, col+1, "W", "W")
self.checkBottomRight(self.board, row+1, col+1, "W", "W")
self.checkBottomLeft(self.board, row+1, col-1, "W", "W")
self.setScore()
# checks if game is done, aka if there are no legal moves left
def isDone(self, color):
for i in range(0, 8):
for j in range(0, 8):
if self.board[8 * i + j] == "-":
if self.isLegal(i, j, color):
return False
return True
# gets the item (b, w, -) at the given coordinates
def getItem(self, row, col):
if row < 0 or row > 7 or col < 0 or col > 7:
return ""
return self.board[8 * row + col]
# checks the legality of given coordinates
def isLegal(self, row, col, currColor):
# get current color and opposing color
token = "B"
if currColor == 2:
token = "W"
nextToken = "W"
if currColor == 2:
nextToken = "B"
#checks to see that its adjacent to an opposite piece
if self.getItem(row + 1, col - 1) == nextToken:
#checks to see that another same color piece sandwiches
if self.checkForNextColor(row+2, col-2, 1, -1, token):
return True
if self.getItem(row + 1, col + 1) == nextToken:
if self.checkForNextColor(row+2, col+2, 1, 1, token):
return True
if self.getItem(row + 1, col) == nextToken:
if self.checkForNextColor(row+2, col, 1, 0, token):
return True
if self.getItem(row, col + 1) == nextToken:
if self.checkForNextColor(row, col+2, 0, 1, token):
return True
if self.getItem(row, col - 1) == nextToken:
if self.checkForNextColor(row, col-2, 0, -1, token):
return True
if self.getItem(row - 1, col - 1) == nextToken:
if self.checkForNextColor(row-2, col-2, -1, -1, token):
return True
if self.getItem(row - 1, col) == nextToken:
if self.checkForNextColor(row-2, col, -1, 0, token):
return True
if self.getItem(row - 1, col + 1) == nextToken:
if self.checkForNextColor(row-2, col+2, -1, 1, token):
return True
return False
# recursively checks for a valid move by searching for the current color
# to sandwhich the opposite color in a given direction (by incRow, incCol)
def checkForNextColor(self, row, col, incRow, incCol, checkColor):
if (row < 0 or row > 7 or col < 0 or col > 7
or self.board[8 * row + col] == "-"):
return False
if checkColor == self.board[8 * row + col]:
return True
if self.checkForNextColor(row+incRow, col+incCol, incRow, incCol, checkColor):
return True
return False
# set new score for board, counts all the black and white pieces on the board
def setScore(self):
self.white = 0
self.black = 0
for item in self.board:
if item == "B":
self.black += 1
elif item == "W":
self.white += 1
# prints the winner of the game
def printWinner(self):
if self.white > self.black:
print("White wins")
elif self.black > self.white:
print("Black wins")
else:
print("Tie")