-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
board.py
58 lines (50 loc) · 2.05 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
from rook import Rook
from knight import Knight
from bishop import Bishop
from queen import Queen
from king import King
from pawn import Pawn
from color import Color
class Board:
def __init__(self):
self.board = [[None] * 8 for _ in range(8)]
self._initialize_board()
def _initialize_board(self):
# Initialize white pieces
self.board[0][0] = Rook(Color.WHITE, 0, 0)
self.board[0][1] = Knight(Color.WHITE, 0, 1)
self.board[0][2] = Bishop(Color.WHITE, 0, 2)
self.board[0][3] = Queen(Color.WHITE, 0, 3)
self.board[0][4] = King(Color.WHITE, 0, 4)
self.board[0][5] = Bishop(Color.WHITE, 0, 5)
self.board[0][6] = Knight(Color.WHITE, 0, 6)
self.board[0][7] = Rook(Color.WHITE, 0, 7)
for i in range(8):
self.board[1][i] = Pawn(Color.WHITE, 1, i)
# Initialize black pieces
self.board[7][0] = Rook(Color.BLACK, 7, 0)
self.board[7][1] = Knight(Color.BLACK, 7, 1)
self.board[7][2] = Bishop(Color.BLACK, 7, 2)
self.board[7][3] = Queen(Color.BLACK, 7, 3)
self.board[7][4] = King(Color.BLACK, 7, 4)
self.board[7][5] = Bishop(Color.BLACK, 7, 5)
self.board[7][6] = Knight(Color.BLACK, 7, 6)
self.board[7][7] = Rook(Color.BLACK, 7, 7)
for i in range(8):
self.board[6][i] = Pawn(Color.BLACK, 6, i)
def get_piece(self, row, col):
return self.board[row][col]
def set_piece(self, row, col, piece):
self.board[row][col] = piece
def is_valid_move(self, piece, dest_row, dest_col):
if piece is None or dest_row < 0 or dest_row > 7 or dest_col < 0 or dest_col > 7:
return False
dest_piece = self.board[dest_row][dest_col]
return (dest_piece is None or dest_piece.color != piece.color) and \
piece.can_move(self, dest_row, dest_col)
def is_checkmate(self, color):
# TODO: Implement checkmate logic
return False
def is_stalemate(self, color):
# TODO: Implement stalemate logic
return False