This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
252 lines (220 loc) · 8.84 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
from collections import defaultdict
from enum import IntEnum
class Citadels(IntEnum):
L = 10
LC = 11
R = 20
RC = 21
U = 30
UC = 31
D = 40
DC = 41
T = 50
class Color(IntEnum):
WHITE = 0
BLACK = 1
KING = 2
EMPTY = 3
class Board:
coords_noenter = {
(3, 0): Citadels.L, # left citadels
(4, 0): Citadels.LC,
(5, 0): Citadels.L,
(4, 1): Citadels.L,
(3, 8): Citadels.R, # right citadels
(4, 8): Citadels.RC,
(5, 8): Citadels.R,
(4, 7): Citadels.R,
(0, 3): Citadels.U, # upper citadels
(0, 4): Citadels.UC,
(0, 5): Citadels.U,
(1, 4): Citadels.U,
(8, 3): Citadels.D, # lower citadels
(8, 4): Citadels.DC,
(8, 5): Citadels.D,
(7, 4): Citadels.D,
(4, 4): Citadels.T, # throne
}
history_table = {Color.WHITE: {}, Color.BLACK: {}} # {COLOR: {move: value}}
def __init__(self):
self.color_coords = defaultdict(set) # BLACK|WHITE|KING -> {(i,j), ...}
self.coords_color = {} # (i,j) -> BLACK|WHITE|KING
def get_available_moves(self, color):
if color == Color.WHITE:
pieces = (Color.KING, Color.WHITE)
else:
pieces = (Color.BLACK,)
moves = []
for piece in pieces:
coords = self.color_coords[piece]
for i, j in coords:
# Horizontal backwards
for j_back in range(j - 1, -1, -1):
if self.coords_color.get(
(i, j_back), None
) is None and self.is_valid(
(i, j), (i, j_back)
): # before calling function, check if destination coord is empty
moves.append((i, j, i, j_back))
else:
break
# Horizontal forward
for j_forward in range(j + 1, 9):
if self.coords_color.get(
(i, j_forward), None
) is None and self.is_valid((i, j), (i, j_forward)):
moves.append((i, j, i, j_forward))
else:
break
# Vertical backwards
for i_back in range(i - 1, -1, -1):
if self.coords_color.get(
(i_back, j), None
) is None and self.is_valid((i, j), (i_back, j)):
moves.append((i, j, i_back, j))
else:
break
# Vertical forward
for i_forward in range(i + 1, 9):
if self.coords_color.get(
(i_forward, j), None
) is None and self.is_valid((i, j), (i_forward, j)):
moves.append((i, j, i_forward, j))
else:
break
heuristic_moves = [] # (rating, move)
for m in moves:
rating = self.history_table[color.value].get(m, 0)
i = 0
while i < len(heuristic_moves):
if rating > heuristic_moves[i][0]:
heuristic_moves.insert(i, (rating, m))
break
i += 1
if i == len(heuristic_moves):
heuristic_moves.append((rating, m))
return [move for (_, move) in heuristic_moves]
# Updates board state
def update(self, board):
self.color_coords = defaultdict(
set
) # BLACK|WHITE|KING -> {(i,j), ...} (str -> set(tuple))
self.coords_color = {} # (i,j) -> BLACK|WHITE|KING (tuple -> str )
for i in range(9):
for j in range(9):
if board[i][j] == "WHITE":
self.color_coords[Color.WHITE].add((i, j))
elif board[i][j] == "BLACK":
self.color_coords[Color.BLACK].add((i, j))
elif board[i][j] == "KING":
self.color_coords[Color.KING].add((i, j))
self.coords_color = {
coord: color
for color, coords in self.color_coords.items()
for coord in coords
}
# Checks if move is valid
def is_valid(self, start_coords, stop_coords):
start_noenter, stop_noenter = self.coords_noenter.get(
start_coords, None
), self.coords_noenter.get(stop_coords, None)
if start_noenter is None: # checker outside of citadel/throne
return stop_noenter is None # moves to a non-citadel/non-throne -> True
elif (
stop_noenter != None
): # checker in citadel/throne (wants to move in citadel)
return (
start_noenter == stop_noenter or abs(start_noenter - stop_noenter) == 1
) # moves in its own citadel (can't be throne) -> True
return True # checker moves from citadel/throne to a non-citadel/non-throne
def move_piece(self, move):
stop_row, stop_col = move[2], move[3]
# start updating __coords_color
color = self.coords_color[(stop_row, stop_col)] = self.coords_color[
(move[0], move[1])
]
# update __color_coords
self.color_coords[color].remove((move[0], move[1]))
self.color_coords[color].add((stop_row, stop_col))
# end updating __coords_color
del self.coords_color[(move[0], move[1])]
if color != Color.BLACK:
color = Color.WHITE
self.check_capture(color, stop_row, stop_col, 0, 1) # Right capture
self.check_capture(color, stop_row, stop_col, 0, -1) # Left capture
self.check_capture(color, stop_row, stop_col, -1, 0) # Top capture
self.check_capture(color, stop_row, stop_col, 1, 0) # Down capture
def check_capture(self, color, stop_row, stop_col, inc_row, inc_col):
# Check and capture pieces
next_coords = (stop_row + inc_row, stop_col + inc_col)
next_next_coords = (stop_row + (inc_row * 2), stop_col + (inc_col * 2))
next_square = self.coords_color.get(next_coords, Color.EMPTY)
if (
color == Color.BLACK
and next_square == Color.KING
and next_coords in ((4, 4), (5, 4), (4, 5), (4, 3), (3, 4))
):
if next_coords == (4, 4): # king on throne
required_for_capture = 4
else:
required_for_capture = 3
close_blacks = 0
if (
self.coords_color.get((next_coords[0] - 1, next_coords[1]), None)
== Color.BLACK
): # Up square check
close_blacks += 1
if (
self.coords_color.get((next_coords[0] + 1, next_coords[1]), None)
== Color.BLACK
): # Down square check
close_blacks += 1
if (
self.coords_color.get((next_coords[0], next_coords[1] - 1), None)
== Color.BLACK
): # Left square check
close_blacks += 1
if (
self.coords_color.get((next_coords[0], next_coords[1] + 1), None)
== Color.BLACK
): # Right square check
close_blacks += 1
if required_for_capture - close_blacks == 0:
del self.coords_color[next_coords]
self.color_coords[next_square].remove(next_coords)
# color in Color.WHITE, Color.BLACK
# next_square in Color.WHITE, Color.BLACK, Color.KING, Color.EMPTY
elif next_square != Color.EMPTY and color == 1 - (next_square % 2):
next_next_square = self.coords_noenter.get(
next_next_coords, self.coords_color.get(next_next_coords, Color.EMPTY)
) # will be a citadel, pawn or empty
if next_next_square == color or next_next_square in (
Citadels.L,
Citadels.R,
Citadels.D,
Citadels.U,
Citadels.T,
):
if next_square == Color.KING and (0 in next_coords or 8 in next_coords):
return # Removes problem of king being captured after escape
else:
del self.coords_color[next_coords]
self.color_coords[next_square].remove(next_coords)
def get_king_coords(self):
# Returns king position tuple
temp = self.color_coords[Color.KING].copy()
try:
return temp.pop()
except KeyError:
return None
def __repr__(self):
res = ""
for i in range(9):
for j in range(9):
color = self.coords_color.get((i,j), None)
if color is None:
res += f"{'*':^6}"
else:
res += f"{color.name:^6}"
res += '\n'
return res