-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
223 lines (187 loc) · 7.59 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
from helpers import glue_notation
from move import Move, SCORE_PIECE
from copy import deepcopy
EMPTY_BOARD = [
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--']
]
START_BOARD = [
["bR", "bN", "bB", "bQ", "bK", "bB", "bN", "bR"],
["bP", "bP", "bP", "bP", "bP", "bP", "bP", "bP"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["wP", "wP", "wP", "wP", "wP", "wP", "wP", "wP"],
["wR", "wN", "wB", "wQ", "wK", "wB", "wN", "wR"]
]
KNI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N_SCORES = [[0.0, 0.2, 0.2, 0.3, 0.3, 0.2, 0.2, 0.0],
[0.2, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.2],
[0.2, 0.4, 0.5, 0.5, 0.5, 0.5, 0.4, 0.2],
[0.3, 0.4, 0.5, 0.5, 0.5, 0.5, 0.4, 0.3],
[0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.4, 0.3],
[0.2, 0.5, 0.5, 0.5, 0.5, 0.5, 0.4, 0.2],
[0.2, 0.4, 0.5, 0.4, 0.4, 0.4, 0.4, 0.2],
[0.0, 0.2, 0.2, 0.3, 0.3, 0.2, 0.2, 0.0]]
PAWN_SCORES = [[0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8],
[0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7],
[0.3, 0.3, 0.4, 0.5, 0.5, 0.4, 0.3, 0.3],
[0.25, 0.25, 0.3, 0.45, 0.45, 0.3, 0.25, 0.25],
[0.2, 0.2, 0.2, 0.4, 0.4, 0.2, 0.2, 0.2],
[0.25, 0.15, 0.1, 0.2, 0.2, 0.1, 0.15, 0.25],
[0.25, 0.3, 0.3, 0.0, 0.0, 0.3, 0.3, 0.25],
[0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2]]
PIECE_POSITION_SCORES = {"wN": KNIGHT_SCORES,
"bN": KNIGHT_SCORES[::-1],
"wB": BISHOP_SCORES,
"bB": BISHOP_SCORES[::-1],
"wQ": QUEEN_SCORES,
"bQ": QUEEN_SCORES[::-1],
"wR": ROOK_SCORES,
"bR": ROOK_SCORES[::-1],
"wP": PAWN_SCORES,
"bP": PAWN_SCORES[::-1]}
class Board:
def __init__(self, is_test_board: bool = False, console_moves: bool = False):
if is_test_board:
self.board = deepcopy(EMPTY_BOARD)
else:
self.board = deepcopy(START_BOARD)
self.game_log = []
self.console_moves = console_moves
# Location Format: (i, j).
# Must be up-to-date.
self.wk_pos = (4, 7)
self.bk_pos = (4, 0)
def __repr__(self) -> str:
return '\n' + '\n'.join([' '.join(row) for row in self.board]) + '\n'
def clear_board(self):
self.board = deepcopy(EMPTY_BOARD)
def make_move(self, move: Move):
"""
Moves a piece from (i, j) to (ni, nj).
Handles promotions, piece capturing, etc.
"""
piece = self.board[move.j][move.i]
self.board[move.nj][move.ni] = piece
self.board[move.j][move.i] = '--'
p_color, p_type = piece[0], piece[1]
if p_type == 'K':
if p_color == 'b':
self.bk_pos = (move.ni, move.nj)
else:
self.wk_pos = (move.ni, move.nj)
# Consider promotion.
if p_type == 'P':
if p_color == 'w':
promotion_row = 0
else:
promotion_row = 7
if move.nj == promotion_row:
self.board[move.nj][move.ni] = p_color + 'Q'
def undo_move(self, move: Move):
"""
Undo a move from (ni, nj) to (i, j).
Undoes promotions, king movement, captured pieces.
"""
piece = self.board[move.nj][move.ni]
self.board[move.j][move.i] = piece
self.board[move.nj][move.ni] = '--' if move.captured_piece is None else move.captured_piece
p_color, p_type = piece[0], piece[1]
if p_type == 'K':
if p_color == 'b':
self.bk_pos = (move.i, move.j)
else:
self.wk_pos = (move.i, move.j)
# Undo promotion when applicable.
if p_type == 'Q':
if p_color == 'w':
promotion_row = 0
else:
promotion_row = 7
if move.nj == promotion_row and move.promote:
self.board[move.j][move.i] = p_color + 'P'
def score_board(self) -> int:
"""
Score the current board, user pieces are scored positively, AI pieces are scored as neg 1.
"""
score = 0
for i in range(8):
for j in range(8):
piece = self.board[j][i]
if piece != '--':
score += SCORE_PIECE[piece[1]] if piece[0] == 'w' else -SCORE_PIECE[piece[1]]
if 'K' not in piece:
score += PIECE_POSITION_SCORES[piece][j][i]
return score
@staticmethod
def to_chess_notation(i, j) -> dict[str, str]:
files = 'abcdefgh'
ranks = '87654321'
return {'file': files[i], 'rank': ranks[j]}
def log_move(self, move: Move, in_check: bool = False, checkmate: bool = False):
"""
Logs the provided move into the game log.
Must be called after the move is complete.
source: https://www.chessstrategyonline.com/content/tutorials/basic-chess-concepts-chess-notation
"""
notation = ''
piece = self.board[move.nj][move.ni]
destination_pos = glue_notation(self.to_chess_notation(move.ni, move.nj))
if piece[1] == 'P' or (piece[1] == 'Q' and move.promote):
if move.captured_piece:
initial_file = self.to_chess_notation(move.i, move.j)['file']
notation = initial_file + 'x'
# TODO: Once we handle under-promotions, we need to revise this case.
if move.promote:
destination_pos += '=Q'
notation += destination_pos
else:
notation = piece[1]
if move.captured_piece:
notation += 'x'
notation += destination_pos
if checkmate:
notation += '#'
elif in_check:
notation += '+'
self.game_log.append(notation)
return notation
def log_game(self):
move_step = 1
for i in range(0, len(self.game_log), 2):
if i + 1 < len(self.game_log):
print(move_step, self.game_log[i], self.game_log[i + 1])
else:
print(move_step, self.game_log[i])
move_step += 1