-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework.py
More file actions
384 lines (286 loc) · 12.8 KB
/
homework.py
File metadata and controls
384 lines (286 loc) · 12.8 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# Python Standard Library imports
import sys
import time
import math
# Custom module imports
from board import Board
from tile import Tile
class Halma():
def __init__(self, b_size=16, t_limit=60, c_player=Tile.P_RED):
# Create initial board
board = [[None] * b_size for _ in range(b_size)]
for row in range(b_size):
for col in range(b_size):
if row + col < 4:
element = Tile(2, 2, 0, row, col)
elif row + col > 2 * (b_size - 3):
element = Tile(1, 1, 0, row, col)
else:
element = Tile(0, 0, 0, row, col)
board[row][col] = element
# Save member variables
self.b_size = b_size
self.t_limit = t_limit
self.c_player = c_player
self.board_view = Board(board)
self.board = board
self.current_player = Tile.P_GREEN
self.selected_tile = None
self.valid_moves = []
self.computing = False
self.total_plies = 0
self.ply_depth = 2
self.ab_enabled = True
self.r_goals = [t for row in board
for t in row if t.tile == Tile.T_RED]
self.g_goals = [t for row in board
for t in row if t.tile == Tile.T_GREEN]
self.board_view.set_status_color("#E50000" if
self.current_player == Tile.P_RED else "#007F00")
if self.c_player == self.current_player:
self.execute_computer_move()
self.board_view.add_click_handler(self.tile_clicked)
self.board_view.draw_tiles(board=self.board) # Refresh the board
# Print initial program info
print("Halma Solver Basic Information")
print("==============================")
print("AI opponent enabled:", "no" if self.c_player is None else "yes")
print("A-B pruning enabled:", "yes" if self.ab_enabled else "no")
print("Turn time limit:", self.t_limit)
print("Max ply depth:", self.ply_depth)
print()
self.board_view.mainloop() # Begin tkinter main loop
def tile_clicked(self, row, col):
if self.computing: # Block clicks while computing
return
new_tile = self.board[row][col]
# If we are selecting a friendly piece
if new_tile.piece == self.current_player:
self.outline_tiles(None) # Reset outlines
# Outline the new and valid move tiles
new_tile.outline = Tile.O_MOVED
self.valid_moves = self.get_moves_at_tile(new_tile,
self.current_player)
self.outline_tiles(self.valid_moves)
# Update status and save the new tile
self.board_view.set_status("Tile `" + str(new_tile) + "` selected")
self.selected_tile = new_tile
self.board_view.draw_tiles(board=self.board) # Refresh the board
# If we already had a piece selected and we are moving a piece
elif self.selected_tile and new_tile in self.valid_moves:
self.outline_tiles(None) # Reset outlines
self.move_piece(self.selected_tile, new_tile) # Move the piece
# Update status and reset tracking variables
self.selected_tile = None
self.valid_moves = []
self.current_player = (Tile.P_RED
if self.current_player == Tile.P_GREEN else Tile.P_GREEN)
self.board_view.draw_tiles(board=self.board) # Refresh the board
# If there is a winner to the game
winner = self.find_winner()
if winner:
self.board_view.set_status("The " + ("green"
if winner == Tile.P_GREEN else "red") + " player has won!")
self.current_player = None
elif self.c_player is not None:
self.execute_computer_move()
else:
self.board_view.set_status("Invalid move attempted")
def minimax(self, depth, player_to_max, max_time, a=float("-inf"),
b=float("inf"), maxing=True, prunes=0, boards=0):
# Bottomed out base case
if depth == 0 or self.find_winner() or time.time() > max_time:
return self.utility_distance(player_to_max), None, prunes, boards
# Setup initial variables and find moves
best_move = None
if maxing:
best_val = float("-inf")
moves = self.get_next_moves(player_to_max)
else:
best_val = float("inf")
moves = self.get_next_moves((Tile.P_RED
if player_to_max == Tile.P_GREEN else Tile.P_GREEN))
# For each move
for move in moves:
for to in move["to"]:
# Bail out when we're out of time
if time.time() > max_time:
return best_val, best_move, prunes, boards
# Move piece to the move outlined
piece = move["from"].piece
move["from"].piece = Tile.P_NONE
to.piece = piece
boards += 1
# Recursively call self
val, _, new_prunes, new_boards = self.minimax(depth - 1,
player_to_max, max_time, a, b, not maxing, prunes, boards)
prunes = new_prunes
boards = new_boards
# Move the piece back
to.piece = Tile.P_NONE
move["from"].piece = piece
if maxing and val > best_val:
best_val = val
best_move = (move["from"].loc, to.loc)
a = max(a, val)
if not maxing and val < best_val:
best_val = val
best_move = (move["from"].loc, to.loc)
b = min(b, val)
if self.ab_enabled and b <= a:
return best_val, best_move, prunes + 1, boards
return best_val, best_move, prunes, boards
def execute_computer_move(self):
# Print out search information
current_turn = (self.total_plies // 2) + 1
print("Turn", current_turn, "Computation")
print("=================" + ("=" * len(str(current_turn))))
print("Executing search ...", end=" ")
sys.stdout.flush()
# self.board_view.set_status("Computing next move...")
self.computing = True
self.board_view.update()
max_time = time.time() + self.t_limit
# Execute minimax search
start = time.time()
_, move, prunes, boards = self.minimax(self.ply_depth,
self.c_player, max_time)
end = time.time()
# Print search result stats
print("complete")
print("Time to compute:", round(end - start, 4))
print("Total boards generated:", boards)
print("Total prune events:", prunes)
# Move the resulting piece
self.outline_tiles(None) # Reset outlines
move_from = self.board[move[0][0]][move[0][1]]
move_to = self.board[move[1][0]][move[1][1]]
self.move_piece(move_from, move_to)
self.board_view.draw_tiles(board=self.board) # Refresh the board
winner = self.find_winner()
if winner:
self.board_view.set_status("The " + ("green"
if winner == Tile.P_GREEN else "red") + " player has won!")
self.board_view.set_status_color("#212121")
self.current_player = None
self.current_player = None
print()
print("Final Stats")
print("===========")
print("Final winner:", "green"
if winner == Tile.P_GREEN else "red")
print("Total # of plies:", self.total_plies)
else: # Toggle the current player
self.current_player = (Tile.P_RED
if self.current_player == Tile.P_GREEN else Tile.P_GREEN)
self.computing = False
print()
def get_next_moves(self, player=1):
moves = [] # All possible moves
for col in range(self.b_size):
for row in range(self.b_size):
curr_tile = self.board[row][col]
# Skip board elements that are not the current player
if curr_tile.piece != player:
continue
move = {
"from": curr_tile,
"to": self.get_moves_at_tile(curr_tile, player)
}
moves.append(move)
return moves
def get_moves_at_tile(self, tile, player, moves=None, adj=True):
if moves is None:
moves = []
row = tile.loc[0]
col = tile.loc[1]
# List of valid tile types to move to
valid_tiles = [Tile.T_NONE, Tile.T_GREEN, Tile.T_RED]
if tile.tile != player:
valid_tiles.remove(player) # Moving back into your own goal
if tile.tile != Tile.T_NONE and tile.tile != player:
valid_tiles.remove(Tile.T_NONE) # Moving out of the enemy's goal
# Find and save immediately adjacent moves
for col_delta in range(-1, 2):
for row_delta in range(-1, 2):
# Check adjacent tiles
new_row = row + row_delta
new_col = col + col_delta
# Skip checking degenerate values
if ((new_row == row and new_col == col) or
new_row < 0 or new_col < 0 or
new_row >= self.b_size or new_col >= self.b_size):
continue
# Handle moves out of/in to goals
new_tile = self.board[new_row][new_col]
if new_tile.tile not in valid_tiles:
continue
if new_tile.piece == Tile.P_NONE:
if adj: # Don't consider adjacent on subsequent calls
moves.append(new_tile)
continue
# Check jump tiles
new_row = new_row + row_delta
new_col = new_col + col_delta
# Skip checking degenerate values
if (new_row < 0 or new_col < 0 or
new_row >= self.b_size or new_col >= self.b_size):
continue
# Handle returning moves and moves out of/in to goals
new_tile = self.board[new_row][new_col]
if new_tile in moves or (new_tile.tile not in valid_tiles):
continue
if new_tile.piece == Tile.P_NONE:
moves.insert(0, new_tile) # Prioritize jumps
self.get_moves_at_tile(new_tile, player, moves, False)
return moves
def move_piece(self, from_tile, to_tile):
# Handle trying to move a non-existant piece and moving into a piece
if from_tile.piece == Tile.P_NONE or to_tile.piece != Tile.P_NONE:
self.board_view.set_status("Invalid move")
return
# Move piece
to_tile.piece = from_tile.piece
from_tile.piece = Tile.P_NONE
# Update outline
to_tile.outline = Tile.O_MOVED
from_tile.outline = Tile.O_MOVED
self.total_plies += 1
self.board_view.set_status_color("#007F00" if
self.current_player == Tile.P_RED else "#E50000")
self.board_view.set_status("Piece moved from `" + str(from_tile) +
"` to `" + str(to_tile) + "`, " + ("green's" if
self.current_player == Tile.P_RED else "red's") + " turn...")
def find_winner(self):
if all(g.piece == Tile.P_GREEN for g in self.r_goals):
return Tile.P_GREEN
elif all(g.piece == Tile.P_RED for g in self.g_goals):
return Tile.P_RED
else:
return None
def outline_tiles(self, tiles=[], outline_type=Tile.O_SELECT):
if tiles is None:
tiles = [j for i in self.board for j in i]
outline_type = Tile.O_NONE
for tile in tiles:
tile.outline = outline_type
def utility_distance(self, player):
def point_distance(p0, p1):
return math.sqrt((p1[0] - p0[0])**2 + (p1[1] - p0[1])**2)
value = 0
for col in range(self.b_size):
for row in range(self.b_size):
tile = self.board[row][col]
if tile.piece == Tile.P_GREEN:
distances = [point_distance(tile.loc, g.loc) for g in
self.r_goals if g.piece != Tile.P_GREEN]
value -= max(distances) if len(distances) else -50
elif tile.piece == Tile.P_RED:
distances = [point_distance(tile.loc, g.loc) for g in
self.g_goals if g.piece != Tile.P_RED]
value += max(distances) if len(distances) else -50
if player == Tile.P_RED:
value *= -1
return value
if __name__ == "__main__":
halma = Halma()