-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimax_player.py
72 lines (62 loc) · 2.32 KB
/
minimax_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from player_interface import PlayerInterface
from constants import SKIP
import math
from operator import gt, lt
"""A minimax player who maximizes their move assuming
others are adversarial players. Depth of 2 by default.
"""
class MinimaxPlayer(PlayerInterface):
def __init__(self, depth=2):
super().__init__()
self.depth = depth
def get_move(self, board):
"""
Returns the maximal move for this AI player.
Board -> Posn
"""
tree = self.game_tree
best_action = self.get_minimax_score(tree, self.depth * 2) # *2 for num players
return best_action[0]
def get_minimax_score(self, tree, depth):
"""
Compute the minimax move and score for this player.
Depth is mumber of layers of tree to look through, and number of turns for this player.
GameTree Natural -> (Move, Natural)
"""
# the game has ended
if tree.is_game_over():
return (None, tree.get_score(self.color))
moves = tree.get_actions()
# Skip if there are no possible moves
if (len(moves) == 0):
return (SKIP, tree.get_score(self.color))
move_score = []
maximize = tree.curr_turn == self.color
if depth == 1:
for m in moves:
next_game = tree.children[m]
next_score = next_game.get_score(self.color)
move_score.append((m, next_score))
else:
for m in moves:
next_game = tree.children[m]
next_move_score = self.get_minimax_score(next_game, depth - 1)
next_score = next_move_score[1]
move_score.append((m, next_score))
return self.get_best_action(maximize, move_score)
def get_best_action(self, maximize, move_scores):
"""
Get the best move, either maximize or minimize the score
Boolean [Listof (Move, Natural)] -> (Move, Natural)
"""
best_score = math.inf
best_move = None
comparator = gt if maximize else lt
if maximize:
best_score *= -1
for move_score in move_scores:
move, score = move_score
if comparator(score, best_score):
best_score = score
best_move = move
return (best_move, best_score)