-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBoard.py
308 lines (243 loc) · 11.6 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
"""
@author: Sam Ragusa
NOTES:
-Should store moves as array of locations e.g.: [[x1,y1],[x2,y2],[x3,y3]]
which is showing the piece at [x1,x2] goes to [x2,y2] then [x3,y3] as one move
-0 is empty spot, 1 is p1, 2 is p2, 3 is p1 king, 4 is p2 king
-if self.player_turn == True then it is player 1's turn
"""
import math
import copy
from functools import reduce
class Board:
"""
A class to represent and play an 8x8 game of checkers.
"""
EMPTY_SPOT = 0
P1 = 1
P2 = 2
P1_K = 3
P2_K = 4
BACKWARDS_PLAYER = P2
HEIGHT = 8
WIDTH = 4
def __init__(self, old_spots=None, the_player_turn=True):
"""
Initializes a new instance of the Board class. Unless specified otherwise,
the board will be created with a start board configuration.
NOTE:
Maybe have default parameter so board is 8x8 by default but nxn if wanted.
"""
self.player_turn = the_player_turn
if old_spots is None:
self.spots = [[j, j, j, j] for j in [self.P1, self.P1, self.P1, self.EMPTY_SPOT, self.EMPTY_SPOT, self.P2, self.P2, self.P2]]
else:
self.spots = old_spots
def reset_board(self):
"""
Resets the current configuration of the game board to the original
starting position.
"""
self.spots = Board().spots
def empty_board(self):
"""
Removes any pieces currently on the board and leaves the board with nothing but empty spots.
"""
self.spots = [[j, j, j, j] for j in [self.EMPTY_SPOT] * self.HEIGHT] # Make sure [self.EMPTY_SPOT]*self.HEIGHT] has no issues
def is_game_over(self):
"""
Finds out and returns weather the game currently being played is over or
not.
"""
if not self.get_possible_next_moves():
return True
return False
def not_spot(self, loc):
"""
Finds out of the spot at the given location is an actual spot on the game board.
"""
if len(loc) == 0 or loc[0] < 0 or loc[0] > self.HEIGHT - 1 or loc[1] < 0 or loc[1] > self.WIDTH - 1:
return True
return False
def get_spot_info(self, loc):
"""
Gets the information about the spot at the given location.
NOTE:
Might want to not use this for the sake of computational time.
"""
return self.spots[loc[0]][loc[1]]
def forward_n_locations(self, start_loc, n, backwards=False):
"""
Gets the locations possible for moving a piece from a given location diagonally
forward (or backwards if wanted) a given number of times(without directional change midway).
"""
if n % 2 == 0:
temp1 = 0
temp2 = 0
elif start_loc[0] % 2 == 0:
temp1 = 0
temp2 = 1
else:
temp1 = 1
temp2 = 0
answer = [[start_loc[0], start_loc[1] + math.floor(n / 2) + temp1], [start_loc[0], start_loc[1] - math.floor(n / 2) - temp2]]
if backwards:
answer[0][0] = answer[0][0] - n
answer[1][0] = answer[1][0] - n
else:
answer[0][0] = answer[0][0] + n
answer[1][0] = answer[1][0] + n
if self.not_spot(answer[0]):
answer[0] = []
if self.not_spot(answer[1]):
answer[1] = []
return answer
def get_simple_moves(self, start_loc):
"""
Gets the possible moves a piece can make given that it does not capture any opponents pieces.
PRE-CONDITION:
-start_loc is a location with a players piece
"""
if self.spots[start_loc[0]][start_loc[1]] > 2:
next_locations = self.forward_n_locations(start_loc, 1)
next_locations.extend(self.forward_n_locations(start_loc, 1, True))
elif self.spots[start_loc[0]][start_loc[1]] == self.BACKWARDS_PLAYER:
next_locations = self.forward_n_locations(start_loc, 1, True) # Switched the true from the statement below
else:
next_locations = self.forward_n_locations(start_loc, 1)
possible_next_locations = []
for location in next_locations:
if len(location) != 0:
if self.spots[location[0]][location[1]] == self.EMPTY_SPOT:
possible_next_locations.append(location)
return [[start_loc, end_spot] for end_spot in possible_next_locations]
def get_capture_moves(self, start_loc, move_beginnings=None):
"""
Recursively get all of the possible moves for a piece which involve capturing an opponent's piece.
"""
if move_beginnings is None:
move_beginnings = [start_loc]
answer = []
if self.spots[start_loc[0]][start_loc[1]] > 2:
next1 = self.forward_n_locations(start_loc, 1)
next2 = self.forward_n_locations(start_loc, 2)
next1.extend(self.forward_n_locations(start_loc, 1, True))
next2.extend(self.forward_n_locations(start_loc, 2, True))
elif self.spots[start_loc[0]][start_loc[1]] == self.BACKWARDS_PLAYER:
next1 = self.forward_n_locations(start_loc, 1, True)
next2 = self.forward_n_locations(start_loc, 2, True)
else:
next1 = self.forward_n_locations(start_loc, 1)
next2 = self.forward_n_locations(start_loc, 2)
for j in range(len(next1)):
if (not self.not_spot(next2[j])) and (not self.not_spot(next1[j])) : # if both spots exist
if self.get_spot_info(next1[j]) != self.EMPTY_SPOT and self.get_spot_info(next1[j]) % 2 != self.get_spot_info(start_loc) % 2: # if next spot is opponent
if self.get_spot_info(next2[j]) == self.EMPTY_SPOT: # if next next spot is empty
temp_move1 = copy.deepcopy(move_beginnings)
temp_move1.append(next2[j])
answer_length = len(answer)
if self.get_spot_info(start_loc) != self.P1 or next2[j][0] != self.HEIGHT - 1:
if self.get_spot_info(start_loc) != self.P2 or next2[j][0] != 0:
temp_move2 = [start_loc, next2[j]]
temp_board = Board(copy.deepcopy(self.spots), self.player_turn)
temp_board.make_move(temp_move2, False)
answer.extend(temp_board.get_capture_moves(temp_move2[1], temp_move1))
if len(answer) == answer_length:
answer.append(temp_move1)
return answer
def get_possible_next_moves(self):
"""
Gets the possible moves that can be made from the current board configuration.
"""
piece_locations = []
for j in range(self.HEIGHT):
for i in range(self.WIDTH):
if (self.player_turn == True and (self.spots[j][i] == self.P1 or self.spots[j][i] == self.P1_K)) or (self.player_turn == False and (self.spots[j][i] == self.P2 or self.spots[j][i] == self.P2_K)):
piece_locations.append([j, i])
try: #Should check to make sure if this try statement is still necessary
capture_moves = list(reduce(lambda a, b: a + b, list(map(self.get_capture_moves, piece_locations)))) # CHECK IF OUTER LIST IS NECESSARY
if len(capture_moves) != 0:
return capture_moves
return list(reduce(lambda a, b: a + b, list(map(self.get_simple_moves, piece_locations)))) # CHECK IF OUTER LIST IS NECESSARY
except TypeError:
return []
def make_move(self, move, switch_player_turn=True):
"""
Makes a given move on the board, and (as long as is wanted) switches the indicator for
which players turn it is.
"""
if abs(move[0][0] - move[1][0]) == 2:
for j in range(len(move) - 1):
if move[j][0] % 2 == 1:
if move[j + 1][1] < move[j][1]:
middle_y = move[j][1]
else:
middle_y = move[j + 1][1]
else:
if move[j + 1][1] < move[j][1]:
middle_y = move[j + 1][1]
else:
middle_y = move[j][1]
self.spots[int((move[j][0] + move[j + 1][0]) / 2)][middle_y] = self.EMPTY_SPOT
self.spots[move[len(move) - 1][0]][move[len(move) - 1][1]] = self.spots[move[0][0]][move[0][1]]
if move[len(move) - 1][0] == self.HEIGHT - 1 and self.spots[move[len(move) - 1][0]][move[len(move) - 1][1]] == self.P1:
self.spots[move[len(move) - 1][0]][move[len(move) - 1][1]] = self.P1_K
elif move[len(move) - 1][0] == 0 and self.spots[move[len(move) - 1][0]][move[len(move) - 1][1]] == self.P2:
self.spots[move[len(move) - 1][0]][move[len(move) - 1][1]] = self.P2_K
else:
self.spots[move[len(move) - 1][0]][move[len(move) - 1][1]] = self.spots[move[0][0]][move[0][1]]
self.spots[move[0][0]][move[0][1]] = self.EMPTY_SPOT
if switch_player_turn:
self.player_turn = not self.player_turn
def get_potential_spots_from_moves(self, moves):
"""
Get's the potential spots for the board if it makes any of the given moves.
If moves is None then returns it's own current spots.
"""
if moves is None:
return self.spots
answer = []
for move in moves:
original_spots = copy.deepcopy(self.spots)
self.make_move(move, switch_player_turn=False)
answer.append(self.spots)
self.spots = original_spots
return answer
def insert_pieces(self, pieces_info):
"""
Inserts a set of pieces onto a board.
pieces_info is in the form: [[vert1, horz1, piece1], [vert2, horz2, piece2], ..., [vertn, horzn, piecen]]
"""
for piece_info in pieces_info:
self.spots[piece_info[0]][piece_info[1]] = piece_info[2]
def get_symbol(self, location):
"""
Gets the symbol for what should be at a board location.
"""
if self.spots[location[0]][location[1]] == self.EMPTY_SPOT:
return " "
elif self.spots[location[0]][location[1]] == self.P1:
return "o"
elif self.spots[location[0]][location[1]] == self.P2:
return "x"
elif self.spots[location[0]][location[1]] == self.P1_K:
return "O"
else:
return "X"
def print_board(self):
"""
Prints a string representation of the current game board.
"""
norm_line = "|---|---|---|---|---|---|---|---|"
print(norm_line)
for j in range(self.HEIGHT):
if j % 2 == 1:
temp_line = "|///|"
else:
temp_line = "|"
for i in range(self.WIDTH):
temp_line = temp_line + " " + self.get_symbol([j, i]) + " |"
if i != 3 or j % 2 != 1: # should figure out if this 3 should be changed to self.WIDTH-1
temp_line = temp_line + "///|"
print(temp_line)
print(norm_line)