-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
310 lines (217 loc) · 7.22 KB
/
game.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import numpy as np
from typing import Callable, Dict, List, NoReturn, Tuple
"""Type alias for m by n location/index values"""
Location = Tuple[int, int]
"""Type alias for list of legal actions"""
ActionList = List[Tuple[Callable, Location]]
"""Type alias for game state tuple"""
GameState = Tuple[int, np.ndarray]
def actions_check_advance(state: np.ndarray, src: Location) -> bool:
"""Check if advance is a legal move.
Args:
state (np.ndarray): Current state
src (Location): m by n index values for pawn
Returns:
bool: True if advance is a legal move
"""
direction = state[1][src[0], src[1]]
dst = (src[0] + direction, src[1])
if not is_valid_index(dst):
return False
if state[1][dst[0], dst[1]]:
return False
return True
def actions_check_capture(state: np.ndarray, src: Location, target: int) -> bool:
"""Check if capture in the target direction is a legal move.
Args:
state (np.ndarray): Current state
src (Location): m by n index values for pawn
target (int): -1 or 1 for left or right capture
Returns:
bool: True if capture is a legal move in target direction.
"""
direction = state[1][src[0], src[1]]
dst = (src[0] + direction, src[1] + target)
if not is_valid_index(dst):
return False
src_id = state[1][src[0], src[1]]
dst_id = state[1][dst[0], dst[1]]
if src_id * dst_id == -1:
return True
return False
def actions(state: np.ndarray) -> ActionList:
"""Generate a list of actions for the current player.
Iterate through the current player's pawns. For each,
query possible moves and add to list of legal moves if
valid.
Args:
state (np.ndarray): Current state
Returns:
ActionList: List of valid moves of the form (Callable, Location)
"""
action_list = []
pawn_id = to_move(state)
for m in range(3):
for n in range(3):
if state[1][m][n] == pawn_id:
adv = actions_check_advance(state, (m, n))
cleft = actions_check_capture(state, (m, n), -1)
cright = actions_check_capture(state, (m, n), 1)
if adv:
action_list.append((advance, (m, n)))
if cleft:
action_list.append((capture_left, (m, n)))
if cright:
action_list.append((capture_right, (m, n)))
return action_list
def advance(state: np.ndarray, src: Location) -> np.ndarray:
"""Advance a pawn at the src location.
Args:
state (np.ndarray): Current state
src (Location): Location of pawn
Returns:
np.ndarray: New board state
"""
direction = state[1][src[0], src[1]]
dst = (src[0] + direction, src[1])
return finish_action(state, dst, src)
def capture_left(state: np.ndarray, src: Location) -> np.ndarray:
"""Capture the pawn at the left diagonal to the src location.
Args:
state (np.ndarray): Current state
src (Location): Location of pawn
Returns:
np.ndarray: New board state
"""
direction = state[1][src[0], src[1]]
dst = (src[0] + direction, src[1] - 1)
return finish_action(state, dst, src)
def capture_right(state: np.ndarray, src: Location) -> np.ndarray:
"""Capture the pawn at the right diagonal to the src location.
Args:
state (np.ndarray): Current state
src (Location): Location of pawn
Returns:
np.ndarray: New board state
"""
direction = state[1][src[0], src[1]]
dst = (src[0] + direction, src[1] + 1)
return finish_action(state, dst, src)
def create_statespace() -> List[GameState]:
"""Create the 70 non-terminal, unique game states
Returns:
List[GameState]: List of game states
"""
space = set()
g = new_game()
state_queue = [g]
while state_queue:
s = state_queue.pop()
if is_terminal(s):
continue
acts = actions(s)
space.add(tuple(to_vector(s)))
for a in acts:
state_queue.append(result(s, a))
return space
def finish_action(state: np.ndarray, dst: Location, src: Location) -> np.ndarray:
"""Complete an action by moving the pawn from src to dst and backfilling with 0.
Args:
state (np.ndarray): Current state
dst (Location): Ending location
src (Location): Starting location
Returns:
np.ndarray: New board state
"""
val = state[1][src[0], src[1]]
new_board = np.copy(state[1])
new_board[src[0], src[1]] = 0
new_board[dst[0], dst[1]] = val
return new_board
def is_terminal(state: np.ndarray) -> bool:
"""Check for terminal game state.
Args:
state (np.ndarray): Current state
Returns:
bool: Return true if game over
"""
if utility(state):
return True
return False
def is_valid_index(idx: Location) -> bool:
"""Validate index is within bounds of board.
Args:
idx (Location): m by n index of pawn
Returns:
bool: Return true if location is valid
"""
for i in idx:
if i not in [0, 1, 2]:
return False
return True
def new_game() -> GameState:
"""Generate new board and place pawns.
Board is flipped upside down from writeup to allow
-1 or 1 to encode direction of travel.
Returns:
np.ndarray: Initial board state
"""
board = np.zeros((3, 3), dtype=int)
board[0, :] += 1
board[2, :] -= 1
return (-1, board)
def print_state(state: np.ndarray) -> NoReturn:
"""Print board state in more legible format.
Args:
state (np.ndarray): Current state
Returns:
NoReturn:
"""
pd = {-1: "Min", 1: "Max"}
print(f"Current Player: {pd[state[0]]}\n{state[1]}")
def result(state: np.ndarray, act: Tuple[Callable, Location]) -> Tuple[int, np.ndarray]:
"""Apply an action to the current board and return the resulting state.
Args:
state (np.ndarray): Current state
act (Tuple[Callable, Location]): Tuple of action function and pawn location
Returns:
Tuple[int, np.ndarray]: New state after applying action
"""
new_turn = state[0] * -1
new_board = act[0](state, act[1])
return (new_turn, new_board)
def to_move(state: np.ndarray) -> int:
"""Return the current player.
Args:
state (np.ndarray): Current state
Returns:
int: Int representing the current player
"""
return state[0]
def to_vector(state: np.ndarray) -> np.ndarray:
"""Convert tuple state to vector format.
Args:
state (np.ndarray): Current state
Returns:
np.ndarray: Vector representation of state
"""
return np.concatenate((np.array(state[0]), state[1]), axis=None)
def utility(state: np.ndarray) -> int:
"""Calculate state utility.
Args:
state (np.ndarray): Current state
Returns:
int: Utility is element of [-1, 0, 1]
"""
# Min in Top Row
if -1 in state[1][0, :]:
return -1
# Max in Bottom Row
elif 1 in state[1][2, :]:
return 1
# No Moves for Current Player
# action list empty == False
elif not actions(state):
return -1 * state[0]
else:
return 0