forked from 905t/Python-Chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
580 lines (432 loc) · 18.3 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import pieces
import numpy as np
class Board(object):
"""
The chess board where the piece movement will occur.
=== Public Attributes ===
curTurn:
Represents the player who has the current move. ('B' or 'W')
board:
Represents the game board in a 8x8 Python list.
check_state:
Represents which player is in check, empty string if none.
"""
def __init__(self):
"""
Initializes the Chess Board to value self.board.
The first player to begin the game will be 'W'.
"""
self.cur_turn = 'W'
self.board = self.create_board()
self.check = ""
def get_valid_moves(self, coordinate):
"""
Returns a list of valid moves for the piece at a given coordinate
"""
coord_piece = self.return_valid_piece(coordinate)
if isinstance(coord_piece, pieces.Pawn):
return self.get_pawn_moves(coordinate)
if isinstance(coord_piece, pieces.Knight):
return self.get_knight_moves(coordinate)
if isinstance(coord_piece, pieces.Rook):
return self.get_rook_moves(coordinate)
if isinstance(coord_piece, pieces.Bishop):
return self.get_bishop_moves(coordinate)
if isinstance(coord_piece, pieces.Queen):
return self.get_queen_moves(coordinate)
if isinstance(coord_piece, pieces.King):
return self.get_king_moves(coordinate)
def get_pawn_moves(self, coordinate):
"""
Returns a list of valid moves for the pawn piece at a given coordinate.
"""
cur_x = coordinate[0]
cur_y = coordinate[1]
cur_pawn = self.board[cur_x][cur_y]
valid_moves = []
# if player Black
if self.cur_turn == 'B':
# Case for pawn's first move
if cur_pawn.start:
for i in range(cur_x + 1, cur_x + 3):
potential_coord = (i, cur_y)
if not self.is_blocked(potential_coord, True) and \
self.is_boundary(potential_coord):
valid_moves.append(potential_coord)
# Case for pawn's 2nd move and beyond
else:
potential_coord = (cur_x+1, cur_y)
if not self.is_blocked(potential_coord, True) and \
self.is_boundary(potential_coord):
valid_moves.append(potential_coord)
# If there is a black piece located diagonally:
if self.is_boundary((cur_x + 1, cur_y + 1)):
potential_piece = self.return_valid_piece((cur_x+1, cur_y + 1))
if potential_piece != 0 and \
potential_piece.colour != self.cur_turn:
valid_moves.append((cur_x + 1, cur_y + 1))
if self.is_boundary((cur_x + 1, cur_y - 1)):
potential_piece = self.return_valid_piece((cur_x+1, cur_y - 1))
if potential_piece != 0 and \
potential_piece.colour != self.cur_turn:
valid_moves.append((cur_x + 1, cur_y - 1))
# If player white
else:
# Case for pawn's first move
if cur_pawn.start:
for i in range(cur_x - 2, cur_x):
potential_coord = (i, cur_y)
if not self.is_blocked(potential_coord, True) and \
self.is_boundary(potential_coord):
valid_moves.append(potential_coord)
# Case for pawn's 2nd move and beyond
else:
potential_coord = (cur_x-1, cur_y)
if not self.is_blocked(potential_coord, True) and \
self.is_boundary(potential_coord):
valid_moves.append(potential_coord)
# If there is a black piece located diagonally:
if self.is_boundary((cur_x-1, cur_y -1)):
potential_piece = self.return_valid_piece((cur_x-1, cur_y-1))
if potential_piece != 0 and \
potential_piece.colour != self.cur_turn:
valid_moves.append((cur_x-1, cur_y-1))
if self.is_boundary((cur_x-1, cur_y +1)):
potential_piece = self.return_valid_piece((cur_x-1, cur_y+1))
if potential_piece != 0 and \
potential_piece.colour != self.cur_turn:
valid_moves.append((cur_x-1, cur_y+1))
return valid_moves
def get_knight_moves(self, coordinate):
"""
Returns a list of valid moves for the knight piece at a given coordinate.
"""
cur_x = coordinate[0]
cur_y = coordinate[1]
valid_moves = []
# fwd left cases
if self.is_boundary((cur_x-2, cur_y-1)) and \
not self.is_blocked((cur_x-2, cur_y-1), False):
valid_moves.append((cur_x-2, cur_y-1))
if self.is_boundary((cur_x-1, cur_y-2)) and \
not self.is_blocked((cur_x-1, cur_y-2), False):
valid_moves.append((cur_x-1, cur_y-2))
# fwd right cases
if self.is_boundary((cur_x-2, cur_y+1)) and \
not self.is_blocked((cur_x-2, cur_y+1), False):
valid_moves.append((cur_x-2, cur_y+1))
if self.is_boundary((cur_x-1, cur_y+2)) and \
not self.is_blocked((cur_x-1, cur_y+2), False):
valid_moves.append((cur_x-1, cur_y+2))
# bwrd left cases
if self.is_boundary((cur_x+2, cur_y-1)) and \
not self.is_blocked((cur_x+2, cur_y-1), False):
valid_moves.append((cur_x+2, cur_y-1))
if self.is_boundary((cur_x+1, cur_y-2)) and \
not self.is_blocked((cur_x+1, cur_y-2), False):
valid_moves.append((cur_x+1, cur_y-2))
# bwrd right cases
if self.is_boundary((cur_x+2, cur_y+1)) and \
not self.is_blocked((cur_x+2, cur_y+1), False):
valid_moves.append((cur_x+2, cur_y+1))
if self.is_boundary((cur_x+1, cur_y+2)) and \
not self.is_blocked((cur_x+1, cur_y+2), False):
valid_moves.append((cur_x+1, cur_y+2))
return valid_moves
def get_rook_moves(self, coordinate):
"""
Returns a list of valid moves for the rook piece at a given coordinate.
"""
cur_x = coordinate[0]
cur_y = coordinate[1]
valid_moves = []
# x-axis movement
if self.cur_turn == 'B':
for i in range(0, cur_x):
if self.is_blocked((i, cur_y), False):
break
else:
valid_moves.append((i, cur_y))
if isinstance(self.board[i][cur_y], pieces.Piece):
break
for i in range(cur_x + 1, 8):
if self.is_blocked((i, cur_y), False):
break
else:
valid_moves.append((i, cur_y))
if isinstance(self.board[i][cur_y], pieces.Piece):
break
else:
for i in range(cur_x-1, -1, -1):
if self.is_blocked((i, cur_y), False):
break
else:
valid_moves.append((i, cur_y))
if isinstance(self.board[i][cur_y], pieces.Piece):
break
for i in range(cur_x+1, 8):
if self.is_blocked((i, cur_y), False):
break
else:
valid_moves.append((i, cur_y))
if isinstance(self.board[i][cur_y], pieces.Piece):
break
# y-axis movement
for j in range(0, cur_y):
j = cur_y - 1 - j
if self.is_blocked((cur_x, j), False):
break
else:
valid_moves.append((cur_x, j))
if isinstance(self.board[cur_x][j], pieces.Piece):
break
for j in range(cur_y+1, 8):
if self.is_blocked((cur_x, j), False):
break
else:
valid_moves.append((cur_x, j))
if isinstance(self.board[cur_x][j], pieces.Piece):
break
return valid_moves
def get_bishop_moves(self, coordinate):
"""
Returns a list of valid moves for the bishop piece at a given coordinate.
"""
cur_x = coordinate[0]
cur_y = coordinate[1]
valid_moves = []
# top left movement
top_left_movement = (-1, -1)
potential_coord = tuple(map(sum, zip(coordinate, top_left_movement)))
while self.is_boundary(potential_coord) and \
not self.is_blocked(potential_coord, False):
valid_moves.append(potential_coord)
if isinstance(self.board[potential_coord[0]][potential_coord[1]],
pieces.Piece):
break
potential_coord = tuple(map(sum, zip(potential_coord,
top_left_movement)))
# top right movement
top_right_movement = (1, 1)
potential_coord = tuple(map(sum, zip(coordinate, top_right_movement)))
while self.is_boundary(potential_coord) and \
not self.is_blocked(potential_coord, False):
valid_moves.append(potential_coord)
if isinstance(self.board[potential_coord[0]][potential_coord[1]],
pieces.Piece):
break
potential_coord = tuple(map(sum, zip(potential_coord,
top_right_movement)))
# bottom left movement
bot_left_movement = (1, -1)
potential_coord = tuple(map(sum, zip(coordinate, bot_left_movement)))
while self.is_boundary(potential_coord) and \
not self.is_blocked(potential_coord, False):
valid_moves.append(potential_coord)
if isinstance(self.board[potential_coord[0]][potential_coord[1]],
pieces.Piece):
break
potential_coord = tuple(map(sum, zip(potential_coord,
bot_left_movement)))
# bottom right movement
bot_right_movement = (-1, 1)
potential_coord = tuple(map(sum, zip(coordinate, bot_right_movement)))
while self.is_boundary(potential_coord) and \
not self.is_blocked(potential_coord, False):
valid_moves.append(potential_coord)
if isinstance(self.board[potential_coord[0]][potential_coord[1]],
pieces.Piece):
break
potential_coord = tuple(map(sum, zip(potential_coord,
bot_right_movement)))
return valid_moves
def get_queen_moves(self, coordinate):
"""
Returns a list of valid moves for the queen piece at a given coordinate
"""
bishop_moves = self.get_bishop_moves(coordinate)
rook_moves = self.get_rook_moves(coordinate)
valid_moves = bishop_moves + rook_moves
return valid_moves
def basic_king_moves(self, coordinate):
"""
Returns the basic movement of King - not including Check positions
"""
cur_x = coordinate[0]
cur_y = coordinate[1]
valid_moves = []
# Left/right side movement
for i in range(-1, 2):
potential_coord_left = (cur_x+i, cur_y - 1)
potential_coord_right = (cur_x+i, cur_y + 1)
if i != 0:
potential_coord_center = (cur_x+i, cur_y)
if self.is_boundary(potential_coord_center) and \
not self.is_blocked(potential_coord_center, False):
valid_moves.append(potential_coord_center)
if self.is_boundary(potential_coord_left) and \
not self.is_blocked(potential_coord_left, False):
valid_moves.append(potential_coord_left)
if self.is_boundary(potential_coord_right) and \
not self.is_blocked(potential_coord_right, False):
valid_moves.append(potential_coord_right)
return valid_moves
def get_king_moves(self, coordinate):
"""
Returns a list of valid moves for the King piece at a given coordinate
"""
if self.cur_turn == 'W':
opp_moves = self.get_all_moves('W')['B']
else:
opp_moves = self.get_all_moves('B')['W']
king_movement = self.basic_king_moves(coordinate)
valid_moves = [x for x in king_movement if x not in opp_moves]
return valid_moves
def get_all_moves(self, current_piece=None):
"""
Returns a dictionary of all moves.
Keys are the player and the values are the moves themselves.
"""
white_moves = []
black_moves = []
for i in range(8):
for j in range(8):
if isinstance(self.return_valid_piece((i, j)), pieces.Piece):
piece = self.board[i][j]
coord = (i, j)
if current_piece is not None and \
isinstance(piece, pieces.King) and \
piece.colour == current_piece:
break
current_turn = self.cur_turn
if piece.colour == 'W':
self.cur_turn = 'W'
if isinstance(piece, pieces.King):
for move in self.basic_king_moves(coord):
white_moves.append(move)
self.cur_turn = current_turn
break
for move in self.get_valid_moves(coord):
white_moves.append(move)
self.cur_turn = current_turn
else:
self.cur_turn = 'B'
if isinstance(piece, pieces.King):
for move in self.basic_king_moves(coord):
black_moves.append(move)
self.cur_turn = current_turn
break
for move in self.get_valid_moves(coord):
black_moves.append(move)
self.cur_turn = current_turn
return {'B': black_moves, 'W': white_moves}
def is_blocked(self, coordinate, is_pawn):
"""
Check to see if piece at coordinate is a potential blocking move.
"""
cur_x = coordinate[0]
cur_y = coordinate[1]
if cur_x < 8 and cur_y < 8:
piece = self.board[cur_x][cur_y]
else:
return False
# If nothing at coordinate, then it's a safe move.
if piece == 0:
return False
else:
# Action passed if its a pawn - it can't attack straight on.
if is_pawn:
return True
elif piece.colour == self.cur_turn:
return True
return False
def is_boundary(self, coordinate):
"""
Checks to see if coordinate is in boundary of the board
"""
cur_x = coordinate[0]
cur_y = coordinate[1]
if cur_x < 0 or cur_x > 7 or cur_y < 0 or cur_y > 7:
return False
else:
return True
def return_valid_piece(self, coordinate):
"""
Returns a piece at the coordinate - if it exists.
Returns 0 otherwise
"""
cur_x = coordinate[0]
cur_y = coordinate[1]
# If coordinate contains a piece
if self.board[cur_x][cur_y] != 0:
return self.board[cur_x][cur_y]
else:
return 0
def move(self, old_coord, new_coord):
"""
Moves the piece from cur_coord (x, y) to new_coord (x, y).
"""
old_x = old_coord[0]
old_y = old_coord[1]
new_x = new_coord[0]
new_y = new_coord[1]
self.board[new_x][new_y] = self.board[old_x][old_y]
self.board[old_x][old_y] = 0
if isinstance(self.board[new_x][new_y], pieces.Pawn):
self.board[new_x][new_y].made_first_move()
if self.cur_turn == 'B':
self.cur_turn = 'W'
else:
self.cur_turn = 'B'
def can_capture(self, old_cord, new_cord):
"""
called by move and gui to check if the tile that the player is tying
to move to is occupied by an opponents piece that can be captured.
"""
if self.return_valid_piece(old_cord) == 0:
return False
if self.return_valid_piece(old_cord).colour != \
self.return_valid_piece(new_cord).colour \
and new_cord in self.get_valid_moves(old_cord):
return True
return False
def __repr__(self):
"""
Prints the Chess Board in matrix format.
Utilized numpy array to format 2d list to matrix format
"""
return str(np.matrix(self.board))
def create_board(self):
"""
Creates the initial game board required for Chess.
Populated spaces are filled with respective pieces from pieces.py.
Vacant spaces are filled with 0s.
#TODO replace the strings with actual pieces from the pieces class
"""
board = [[0 for _ in range(8)] for _ in range(8)]
# Placing the Rooks
board[0][0] = pieces.Rook('B')
board[0][7] = pieces.Rook('B')
board[7][0] = pieces.Rook('W')
board[7][7] = pieces.Rook('W')
# Placing the Knights
board[0][1] = pieces.Knight('B')
board[0][6] = pieces.Knight('B')
board[7][1] = pieces.Knight('W')
board[7][6] = pieces.Knight('W')
# Placing the Bishops
board[0][2] = pieces.Bishop('B')
board[0][5] = pieces.Bishop('B')
board[7][2] = pieces.Bishop('W')
board[7][5] = pieces.Bishop('W')
# Placing the Queens
board[0][3] = pieces.Queen('B')
board[7][3] = pieces.Queen('W')
# Placing the Kings
board[0][4] = pieces.King('B')
board[7][4] = pieces.King('W')
# Placing the Pawns
for i in range(8):
board[1][i] = pieces.Pawn('B')
board[6][i] = pieces.Pawn('W')
return board