-
Notifications
You must be signed in to change notification settings - Fork 0
/
ids_player.py
58 lines (51 loc) · 1.96 KB
/
ids_player.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
from player_interface import PlayerInterface
from constants import SKIP
import random
"""A player who does iterative deepening search.
Maxes out at depth search 6 (for timing efficiency)
"""
class IterativeDeepeningPlayer(PlayerInterface):
def __init__(self, depth=6):
super().__init__()
self.depth = depth
def get_move(self, board):
"""
Returns the maximal move for this AI player to have the highest score at the
end of this move.
Board -> Posn
"""
for d in range(0, self.depth):
winning_moves = self.__depth_limited(d)
if winning_moves is not False and len(winning_moves) > 0:
return winning_moves[0]
# if no good moves exist to get to goal state, default to a random move
all_moves = self.game_tree.get_actions()
if len(all_moves) == 0:
return SKIP
else:
return random.choice(all_moves)
def __depth_limited(self, depth_cutoff):
initial = (self.game_tree, [])
frontier = [initial]
while len(frontier) > 0:
curr, path = frontier[-1]
frontier = frontier[:-1] #remove curr state from frontier
if self.__goal_test(curr):
return path
else:
if len(path) < depth_cutoff:
moves = self.game_tree.get_actions()
for m in moves:
child = self.game_tree.children[m]
new_path = path[:] #copy the path
new_path.append(m)
frontier.append((child, new_path))
return False
def __goal_test(self, tree):
"""Is the state the goal state?
GameTree -> Boolean
"""
leader = tree.get_leader() == self.color
corners = tree.board.four_corners(self.color) > 0
#TODO determine what a goal state is
return leader or corners