-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathboard.py
282 lines (254 loc) · 9.76 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
'''
Contains all of the information for an instance of the game.
'''
from pieces import Rook, Knight, Bishop, Queen, Pawn, King
class Board:
'''
Stores boardstate of game.
'''
def __init__(self):
'''
Sets up all of the pieces that are initially on the board
when the game starts
'''
self.board = []
self.set_initial_state()
def get_raw(self):
''' Get the raw data from the board to send to the user
'''
string_board = '['
for x_pos in range(8):
string_board += "["
for y_pos in range(8):
string_board += '"' + str(self.board[x_pos][y_pos]) + '"'
if y_pos != 7:
string_board += ','
string_board += "]"
if x_pos != 7:
string_board += ","
string_board += ']'
return string_board
def __str__(self):
'''
Returns a string representation of the current board
Used for debugging
'''
string_board = ''
for x_pos in range(8):
for y_pos in range(8):
string_board += str(self.board[x_pos][y_pos]) + ' '
string_board += '<br>'
return string_board
def get_moveset_moves(self, coord, move_set, piece, turn):
''' Get all of the possible moves from a moveset '''
center_x, center_y = 0, 0
moves = []
coord_x = int(coord[0])
coord_y = int(coord[1])
for rownum, row in enumerate(move_set):
for colnum in range(len(row)):
if move_set[rownum][colnum] == 0:
center_x, center_y = rownum, colnum
for rownum, row in enumerate(move_set):
for colnum in range(len(row)):
if move_set[rownum][colnum] == 1:
moves += [[colnum-center_x+coord_x,
rownum-center_y+coord_y]]
elif move_set[rownum][colnum] == 3:
if not piece.hasMoved:
moves += [[colnum-center_x+coord_x,
rownum-center_y+coord_y]]
elif move_set[rownum][colnum] == 2:
piece2 = self.board[colnum-center_x+coord_x][rownum-center_y+coord_y]
if piece2 != 0 and piece2.get_color() != int(turn):
moves += [[colnum-center_x+coord_x,
rownum-center_y+coord_y]]
return self.check_pieces(moves, turn)
def get_move(self, coord, turn=-1):
'''
Gets all of the available squares that the piece at the given input
coordinate can move to.
'''
# How we store the squares the piece can move to
allowed_squares = []
# First, we get the piece on the current board
piece = self.board[int(coord[0])][int(coord[1])]
# Make sure the player is not trying to move a piece of the wrong color
if turn != -1 and piece.get_color() != turn:
return []
# Obtain the moveset for the given piece
allowed_moves = piece.get_allowed_moves()
allowed_squares += self.get_moveset_moves(coord, allowed_moves[-1], piece, turn)
coord_x = int(coord[0])
coord_y = int(coord[1])
# Up Allowed
if allowed_moves[0]:
for x_pos in range(coord_y+1, 8):
if x_pos != coord_y:
piece = self.board[coord_x][x_pos]
# Left Allowed
if allowed_moves[1]:
allowed_squares += [[x_pos, coord_y] for x_pos in range(8) if x_pos != coord_x]
# Right Allowed
if allowed_moves[2]:
pass
# Down Allowed
if allowed_moves[3]:
for x_pos in range(0, coord_y):
if x_pos != coord_y:
allowed_squares.append([coord_x, x_pos])
# Diag Left
if allowed_moves[4]:
allowed_squares += [[coord_x-1-x_pos, coord_y-1-x_pos] for x_pos in
range(min(coord_x, coord_y))]
allowed_squares += [[coord_x+1+x_pos, coord_y+1+x_pos] for x_pos in
range(min(7-coord_x, 7-coord_y))]
# Diag Right
if allowed_moves[5]:
allowed_squares += [[coord_x-1-x_pos, coord_y+1+x_pos] for x_pos in range(coord_x)]
allowed_squares += [[coord_x+1+x_pos, coord_y-1-x_pos] for x_pos in
range(7-coord_x)]
return self.check_pieces(allowed_squares, turn)
def is_piece(self, coord):
'''
TODO: use this
'''
piece = self.board[coord[0]][coord[1]]
if piece == 0:
return False
return True
def check_pieces(self, coords, turn):
'''
Given a set of available moves, removes all of the pieces that are of
the same color as your piece
'''
out_l = []
for coord in coords:
piece = self.board[coord[0]][coord[1]]
if piece == 0:
out_l.append(coord)
else:
if piece.getColor() != int(turn):
out_l.append(coord)
else:
pass
return out_l
def move(self, current_square, next_square, turn):
'''
Takes in two locations. Moves piece from current
square to next Square and replaces the piece with a zero
'''
piece = self.board[current_square[0]][current_square[1]]
target = self.board[next_square[0]][next_square[1]]
# if we try to move an empty square or move a piece no squares,
# or on top of a piece of our own color
if piece == 0:
raise ValueError("No Piece Selected")
if current_square == next_square:
raise ValueError("Cannot Move to Same Position")
else:
if target != 0:
if target.getColor() == piece.getColor():
raise ValueError("Cannot Take a Piece of Same Color")
if piece.getColor() != turn:
raise ValueError("Cannot move your opponent's piece")
# check if given a piece and a different starting/ending square,
# it is a valid move
if self.is_valid_move(piece, current_square, next_square):
self.board[current_square[0]][current_square[1]] = 0
self.board[next_square[0]][next_square[1]] = piece
if hasattr(piece, 'hasMoved'):
piece.hasMoved = True
return f"Made Move: {current_square, next_square} \
for piece {piece} <br>"
raise ValueError("Move Not in Moveset")
def check_for_mate(self):
'''
Temporary
'''
print(self)
return True
# TODO
# def check_for_mate(self):
# ''' Looks at the current board state and returns True if there
# is a checkmate
# '''
#
# # TODO: First, check if the king is in check
# # If the king is in check, check if it has any available moves
#
# # Check if the king is on the board. If not, it is mate
# king_count = 0
# for row in self.board:
# for col in row:
# if 1 == 1:
# king_count += 1
# if king_count == 2:
# return True
#
# return False
def is_valid_move(self, piece, current_square, next_square):
''' temporary method for testing '''
print(piece)
print(current_square)
print(next_square)
if piece == 123:
self.is_valid_move(piece, current_square, next_square)
# def is_valid_move(self, piece, current_square, next_square):
# '''
# TODO
# '''
# move_l = self.get_move(current_square)
# if next_square in move_l:
# return True
# return False
#
# # then, we check the difference between the next_square and
# # current_square
# # x_diff = next_square[0] - current_square[0]
# # y_diff = next_square[1] - current_square[1]
# TODO
# def check_winner(self):
# '''
# Checks winner by determining if there are two kings on the
# board
# '''
# king_list = []
# for col in self.board:
# for row in self.board:
# if str(row) == "King":
# king_list.append(row)
# if len(king_list) == 1:
# return king_list[0].getColor()
def get_board(self):
'''
Returns the current board
'''
return self.board
def set_initial_state(self):
'''
Creates and initializes the board
'''
self.board = [[Rook(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Rook(0)],
[Knight(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Knight(0)],
[Bishop(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Bishop(0)],
[Queen(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Queen(0)],
[King(1), Pawn(1), 0, 0, 0, 0, Pawn(0), King(0)],
[Bishop(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Bishop(0)],
[Knight(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Knight(0)],
[Rook(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Rook(0)]]
def get_total_score(self):
'''
Count the total score for black and white and returns
[WhiteScore, BlackScore]
'''
total_white_score = 0
total_black_score = 0
for i in self.board:
for j in i:
if j != 0:
if j.get_color() == 0:
total_white_score += j.get_value()
elif j.get_color() == 1:
total_black_score += j.get_value()
return [total_white_score, total_black_score]