-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrlrMinMaxAlphaBeta.py
57 lines (44 loc) · 1.46 KB
/
ctrlrMinMaxAlphaBeta.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
#! /usr/bin/python2
import time
from ctrlrMinMax import ControllerMinMax
from state import State
class ControllerMinMaxAlphaBeta(ControllerMinMax):
"""docstring for ControllerMinMaxAlphaBeta"""
def __init__(self, name, player_ordinality, precalc_utivals_enabled=True, move_randomize=True):
super(ControllerMinMaxAlphaBeta, self).__init__(name, player_ordinality, precalc_utivals_enabled, move_randomize)
def minmax(self, state, stack_depth, alpha=float("-inf"), beta=float("inf")):
self.stats["n"] += 1
if stack_depth > self.stats["d"]:
self.stats["d"] = stack_depth
if State.is_state_terminal(state):
return State.utility_value(state)
else:
utival_best = None
player_cur = None
if state.player_last == State.PLAYER_A:
utival_best = float("inf")
player_cur = State.PLAYER_B
else:
utival_best = float("-inf")
player_cur = State.PLAYER_A
move_positions = State.get_move_positions(state)
for pos in move_positions:
state_new = State.get_state_on_move(state, (player_cur,pos))
utival = self.minmax(state_new, stack_depth+1, alpha, beta)
# max node
if player_cur == State.PLAYER_A:
if utival > utival_best:
utival_best = utival
if utival > beta:
return utival
if utival > alpha:
alpha = utival
# min node
else:
if utival < utival_best:
utival_best = utival
if utival < alpha:
return utival
if utival < beta:
beta = utival
return utival_best