-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.py
181 lines (151 loc) · 7.17 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
'''
https://github.com/Dirk94/ChessAI/blob/master/board.py
'''
import pieces, ai, boardControl
class Board:
WIDTH = 8
HEIGHT = 8
def __init__(self, chesspieces, white_king_moved, black_king_moved):
self.chesspieces = chesspieces
self.white_king_moved = white_king_moved
self.black_king_moved = black_king_moved
@classmethod
def clone(cls, chessboard):
chesspieces = [[0 for x in range(Board.WIDTH)] for y in range(Board.HEIGHT)]
for x in range(Board.WIDTH):
for y in range(Board.HEIGHT):
piece = chessboard.chesspieces[x][y]
if (piece != 0):
chesspieces[x][y] = piece.clone()
return cls(chesspieces, chessboard.white_king_moved, chessboard.black_king_moved)
@classmethod
def new(cls):
chess_pieces = [[0 for x in range(Board.WIDTH)] for y in range(Board.HEIGHT)]
# Create pawns.
for x in range(Board.WIDTH):
chess_pieces[x][Board.HEIGHT-2] = pieces.Pawn(x, Board.HEIGHT-2, pieces.Piece.WHITE)
chess_pieces[x][1] = pieces.Pawn(x, 1, pieces.Piece.BLACK)
# Create rooks.
chess_pieces[0][Board.HEIGHT-1] = pieces.Rook(0, Board.HEIGHT-1, pieces.Piece.WHITE)
chess_pieces[Board.WIDTH-1][Board.HEIGHT-1] = pieces.Rook(Board.WIDTH-1, Board.HEIGHT-1, pieces.Piece.WHITE)
chess_pieces[0][0] = pieces.Rook(0, 0, pieces.Piece.BLACK)
chess_pieces[Board.WIDTH-1][0] = pieces.Rook(Board.WIDTH-1, 0, pieces.Piece.BLACK)
# Create Knights.
chess_pieces[1][Board.HEIGHT-1] = pieces.Knight(1, Board.HEIGHT-1, pieces.Piece.WHITE)
chess_pieces[Board.WIDTH-2][Board.HEIGHT-1] = pieces.Knight(Board.WIDTH-2, Board.HEIGHT-1, pieces.Piece.WHITE)
chess_pieces[1][0] = pieces.Knight(1, 0, pieces.Piece.BLACK)
chess_pieces[Board.WIDTH-2][0] = pieces.Knight(Board.WIDTH-2, 0, pieces.Piece.BLACK)
# Create Bishops.
chess_pieces[2][Board.HEIGHT-1] = pieces.Bishop(2, Board.HEIGHT-1, pieces.Piece.WHITE)
chess_pieces[Board.WIDTH-3][Board.HEIGHT-1] = pieces.Bishop(Board.WIDTH-3, Board.HEIGHT-1, pieces.Piece.WHITE)
chess_pieces[2][0] = pieces.Bishop(2, 0, pieces.Piece.BLACK)
chess_pieces[Board.WIDTH-3][0] = pieces.Bishop(Board.WIDTH-3, 0, pieces.Piece.BLACK)
# Create King & Queen.
chess_pieces[4][Board.HEIGHT-1] = pieces.King(4, Board.HEIGHT-1, pieces.Piece.WHITE)
chess_pieces[3][Board.HEIGHT-1] = pieces.Queen(3, Board.HEIGHT-1, pieces.Piece.WHITE)
chess_pieces[4][0] = pieces.King(4, 0, pieces.Piece.BLACK)
chess_pieces[3][0] = pieces.Queen(3, 0, pieces.Piece.BLACK)
return cls(chess_pieces, False, False)
def get_possible_moves(self, color):
moves = []
for x in range(Board.WIDTH):
for y in range(Board.HEIGHT):
piece = self.chesspieces[x][y]
if (piece != 0):
if (piece.color == color):
moves += piece.get_possible_moves(self)
return moves
def perform_move(self, move):
piece = self.chesspieces[move.xfrom][move.yfrom]
piece.x = move.xto
piece.y = move.yto
self.chesspieces[move.xto][move.yto] = piece
self.chesspieces[move.xfrom][move.yfrom] = 0
if (piece.piece_type == pieces.Pawn.PIECE_TYPE):
if (piece.y == 0 or piece.y == Board.HEIGHT-1):
self.chesspieces[piece.x][piece.y] = pieces.Queen(piece.x, piece.y, piece.color)
if (move.castling_move):
if (move.xto < move.xfrom):
rook = self.chesspieces[move.xfrom][0]
rook.x = 2
self.chesspieces[2][0] = rook
self.chesspieces[0][0] = 0
if (move.xto > move.xfrom):
rook = self.chesspieces[move.xfrom][Board.HEIGHT-1]
rook.x = Board.WIDTH-4
self.chesspieces[Board.WIDTH-4][Board.HEIGHT-1] = rook
self.chesspieces[move.xfrom][Board.HEIGHT-1] = 0
if (piece.piece_type == pieces.King.PIECE_TYPE):
if (piece.color == pieces.Piece.WHITE):
self.white_king_moved = True
else:
self.black_king_moved = True
def perform_move_board(self, move, turn):
piece = self.chesspieces[move.xfrom][move.yfrom]
piece.x = move.xto
piece.y = move.yto
self.chesspieces[move.xto][move.yto] = piece
self.chesspieces[move.xfrom][move.yfrom] = 0
try:
boardControl.move_pieces([move.xfrom,move.yfrom,move.xto,move.yto], turn)
except:
pass
if (piece.piece_type == pieces.Pawn.PIECE_TYPE):
if (piece.y == 0 or piece.y == Board.HEIGHT-1):
self.chesspieces[piece.x][piece.y] = pieces.Queen(piece.x, piece.y, piece.color)
if (move.castling_move):
if (move.xto < move.xfrom):
rook = self.chesspieces[move.xfrom][0]
rook.x = 2
self.chesspieces[2][0] = rook
self.chesspieces[0][0] = 0
boardControl.move_pieces["qc", turn]
if (move.xto > move.xfrom):
rook = self.chesspieces[move.xfrom][Board.HEIGHT-1]
rook.x = Board.WIDTH-4
self.chesspieces[Board.WIDTH-4][Board.HEIGHT-1] = rook
self.chesspieces[move.xfrom][Board.HEIGHT-1] = 0
boardControl.move_pieces["kc", turn]
if (piece.piece_type == pieces.King.PIECE_TYPE):
if (piece.color == pieces.Piece.WHITE):
self.white_king_moved = True
else:
self.black_king_moved = True
# Returns if the given color is checked.
def is_check(self, color):
other_color = pieces.Piece.WHITE
if (color == pieces.Piece.WHITE):
other_color = pieces.Piece.BLACK
for move in self.get_possible_moves(other_color):
copy = Board.clone(self)
copy.perform_move(move)
king_found = False
for x in range(Board.WIDTH):
for y in range(Board.HEIGHT):
piece = copy.chesspieces[x][y]
if (piece != 0):
if (piece.color == color and piece.piece_type == pieces.King.PIECE_TYPE):
king_found = True
if (not king_found):
return True
return False
# Returns piece at given position or 0 if: No piece or out of bounds.
def get_piece(self, x, y):
if (not self.in_bounds(x, y)):
return 0
return self.chesspieces[x][y]
def in_bounds(self, x, y):
return (x >= 0 and y >= 0 and x < Board.WIDTH and y < Board.HEIGHT)
def to_string(self):
string = " A B C D E F G H\n"
string += " -----------------------\n"
for y in range(Board.HEIGHT):
string += str(8 - y) + " | "
for x in range(Board.WIDTH):
piece = self.chesspieces[x][y]
if (piece != 0):
string += piece.to_string()
else:
string += ".. "
string += "\n"
return string + "\n"