-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboard.py
269 lines (235 loc) · 7.19 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
from typing import List, Tuple
from constants import *
import protocol
from protocol import Piece, coord
class Board:
def __init__(self, pieces):
self.state = []
self.moving = {}
self.moving[WHITE] = []
self.moving[BLACK] = []
for i in range(8):
self.state.append([None] * 8)
self.moving[WHITE].append([False] * 8)
self.moving[BLACK].append([False] * 8)
for pi in range(len(pieces)):
if pieces[pi] == "":
continue
piece = Piece(pieces[pi])
a, i = coord(piece.pos)
if piece.moving:
assert (not self.moving[piece.color][a][i])
self.moving[piece.color][a][i] = True
else:
assert (self.state[a][i] is None)
self.state[a][i] = piece
def is_valid_position(self, pos):
a, i = coord(pos)
return a is not None
def has_piece(self, pos):
a, i = coord(pos)
return self.state[a][i] is not None
def piece(self, pos):
a, i = coord(pos)
return self.state[a][i]
def piece_name(self, pos):
a, i = coord(pos)
return str(self.state[a][i])
def is_white(self, pos):
a, i = coord(pos)
return self.state[a][i].color == WHITE
def clear_path(self, fa, fi, ta, ti):
da = ta - fa
di = ti - fi
if da != 0:
da = int(da / abs(da))
if di != 0:
di = int(di / abs(di))
i = fi
a = fa
while True:
i += di
a += da
if a == ta and i == ti:
break
if not self.empty_or_moving(a, i):
return False
return True
def empty(self, a, i):
"""Square is empty and no piece is on its way there."""
state = self.state[a][i]
return state is None
def _opposing_standing(self, a, i, own_color):
"""Square has an enemy standing in the square."""
state = self.state[a][i]
if state is None:
return False
return state.color != own_color and not state.moving
def empty_or_opposing(self, a, i, own_color):
"""Square is empty or occupied by the opposing color or
a piece of the opposing color is on its way there."""
state = self.state[a][i]
if state is None:
return True
return state.color != own_color
def empty_or_moving(self, a, i):
"""Square is empty or have pieces moving there. No piece
is standing there."""
state = self.state[a][i]
if state is None:
return True
return state.moving
def is_valid_move(self, from_pos, to_pos):
fa, fi = coord(from_pos)
ta, ti = coord(to_pos)
if fa is None or ta is None:
return False
if fa == ta and fi == ti:
return False
piece = self.state[fa][fi]
if not piece:
return False
elif piece.sleeping:
return False
# Moving pieces are not on the board.
assert not piece.moving
# Is a piece of the same color moving here?
if self.moving[piece.color][ta][ti]:
return False
if piece.type == PAWN:
d = 1
if piece.color == BLACK:
d = -1
if fa == ta and ti - fi == d and self.empty(ta, ti):
return True
if fa == ta and self.empty(fa, fi + d) and self.empty(ta, ti):
if piece.color == WHITE and fi == 1 and ti == 3:
return True
if piece.color == BLACK and fi == 6 and ti == 4:
return True
# Capture
if abs(fa - ta) == 1 and ti - fi == d and self._opposing_standing(
ta, ti, piece.color):
return True
return False
elif piece.type == ROOK:
if fa != ta and fi != ti:
return False
if not self.clear_path(fa, fi, ta, ti):
return False
return self.empty_or_opposing(ta, ti, piece.color)
elif piece.type == BISHOP:
if abs(fa - ta) != abs(fi - ti):
return False
if not self.clear_path(fa, fi, ta, ti):
return False
return self.empty_or_opposing(ta, ti, piece.color)
elif piece.type == QUEEN:
if fa != ta and fi != ti and abs(fa - ta) != abs(fi - ti):
return False
if not self.clear_path(fa, fi, ta, ti):
return False
return self.empty_or_opposing(ta, ti, piece.color)
elif piece.type == KING:
if abs(fa - ta) > 1 or abs(fi - ti) > 1:
return False
return self.empty_or_opposing(ta, ti, piece.color)
elif piece.type == KNIGHT:
da = abs(fa - ta)
di = abs(fi - ti)
if (da != 1 and da != 2) or (di != 1 and di != 2) or (
da == 1 and di != 2) or (da == 2 and di != 1):
return False
return self.empty_or_opposing(ta, ti, piece.color)
else:
return False
# This method is not used for the game, only as a helper method
# for the AI.
def get_possible_moves(self, color: int) -> List[Tuple[str, List[str]]]:
result = []
for a in range(8):
for i in range(8):
if self.state[a][i] is not None and self.state[a][i].color == color:
pos = protocol.pos(a, i)
moves = self.get_moves(pos)
if moves:
result.append((pos, moves))
return result
# This method is not used for the game, only as a helper method
# for the AI.
def get_moves(self, from_pos: str) -> List[str]:
a, i = protocol.coord(from_pos)
piece = self.state[a][i]
if not piece:
return []
moves = [] # type: List[str]
if piece.type == PAWN:
d = 1
if piece.color == BLACK:
d = -1
possible = [
protocol.pos(a, i + d),
protocol.pos(a, i + 2 * d),
protocol.pos(a + 1, i + d),
protocol.pos(a - 1, i + d)
]
moves = [to for to in possible if self.is_valid_move(from_pos, to)]
elif piece.type == ROOK:
self._add_all_moves_in_line(moves, a, i, 1, 0)
self._add_all_moves_in_line(moves, a, i, 0, 1)
self._add_all_moves_in_line(moves, a, i, -1, 0)
self._add_all_moves_in_line(moves, a, i, 0, -1)
elif piece.type == BISHOP:
self._add_all_moves_in_line(moves, a, i, 1, 1)
self._add_all_moves_in_line(moves, a, i, -1, -1)
self._add_all_moves_in_line(moves, a, i, -1, 1)
self._add_all_moves_in_line(moves, a, i, 1, -1)
elif piece.type == QUEEN:
self._add_all_moves_in_line(moves, a, i, 1, 1)
self._add_all_moves_in_line(moves, a, i, -1, -1)
self._add_all_moves_in_line(moves, a, i, -1, 1)
self._add_all_moves_in_line(moves, a, i, 1, -1)
self._add_all_moves_in_line(moves, a, i, 1, 0)
self._add_all_moves_in_line(moves, a, i, 0, 1)
self._add_all_moves_in_line(moves, a, i, -1, 0)
self._add_all_moves_in_line(moves, a, i, 0, -1)
elif piece.type == KNIGHT:
possible = [
protocol.pos(a + 1, i + 2),
protocol.pos(a - 1, i + 2),
protocol.pos(a + 1, i - 2),
protocol.pos(a - 1, i - 2),
protocol.pos(a + 2, i + 1),
protocol.pos(a - 2, i + 1),
protocol.pos(a + 2, i - 1),
protocol.pos(a - 2, i - 1),
]
moves = [to for to in possible if self.is_valid_move(from_pos, to)]
elif piece.type == KING:
possible = [
protocol.pos(a - 1, i + 1),
protocol.pos(a - 1, i),
protocol.pos(a - 1, i - 1),
protocol.pos(a, i + 1),
protocol.pos(a, i - 1),
protocol.pos(a + 1, i + 1),
protocol.pos(a + 1, i),
protocol.pos(a + 1, i - 1),
]
moves = [to for to in possible if self.is_valid_move(from_pos, to)]
return moves
# This method is not used for the game, only as a helper method
# for the AI.
def _add_all_moves_in_line(self, moves: List[str], a: int, i: int, da: int,
di: int) -> None:
color = self.state[a][i].color
while True:
a += da
i += di
if a < 0 or a >= 8 or i < 0 or i >= 8:
break
if self.state[a][i] is not None and self.state[a][i].color == color:
break
moves.append(protocol.pos(a, i))
if self.state[a][i] is not None:
break