forked from dz14/NIne-Men-Morris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlphaBeta.py
137 lines (96 loc) · 2.95 KB
/
AlphaBeta.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
from BoardLogic import *
from Utility import *
pruned = 0
states_reached = 0
class evaluator():
def __init__(self):
self.evaluator = 0
self.board = []
def alphaBetaPruning(board, depth, player1, alpha, beta, isStage1, heuristic):
finalEvaluation = evaluator()
global states_reached
states_reached += 1
if depth != 0:
currentEvaluation = evaluator()
if player1:
if isStage1:
possible_configs = stage1Moves(board)
else:
possible_configs = stage23Moves(board)
else:
if isStage1:
possible_configs = generateInvertedBoardList(stage1Moves(InvertedBoard(board)))
else:
possible_configs = generateInvertedBoardList(stage23Moves(InvertedBoard(board)))
for move in possible_configs:
if player1:
currentEvaluation = alphaBetaPruning(move, depth - 1, False, alpha, beta, isStage1, heuristic)
if currentEvaluation.evaluator > alpha:
alpha = currentEvaluation.evaluator
finalEvaluation.board = move
else:
currentEvaluation = alphaBetaPruning(move, depth - 1, True, alpha, beta, isStage1, heuristic)
if currentEvaluation.evaluator < beta:
beta = currentEvaluation.evaluator
finalEvaluation.board = move
if alpha >= beta:
global pruned
pruned += 1
break
if player1:
finalEvaluation.evaluator = alpha
else:
finalEvaluation.evaluator = beta
else:
if player1:
finalEvaluation.evaluator = heuristic(board, isStage1)
else:
finalEvaluation.evaluator = heuristic(InvertedBoard(board), isStage1)
return finalEvaluation
def minimax(board, depth, player1, alpha, beta, isStage1, heuristic):
finalEvaluation = evaluator()
global states_reached
states_reached += 1
if depth != 0:
currentEvaluation = evaluator()
if player1:
if isStage1:
possible_configs = stage1Moves(board)
else:
possible_configs = stage23Moves(board)
else:
if isStage1:
possible_configs = generateInvertedBoardList(stage1Moves(InvertedBoard(board)))
else:
possible_configs = generateInvertedBoardList(stage23Moves(InvertedBoard(board)))
for move in possible_configs:
if player1:
currentEvaluation = minimax(move, depth - 1, False, alpha, beta, isStage1, heuristic)
if currentEvaluation.evaluator > alpha:
alpha = currentEvaluation.evaluator
finalEvaluation.board = move
else:
currentEvaluation = minimax(move, depth - 1, True, alpha, beta, isStage1, heuristic)
if currentEvaluation.evaluator < beta:
beta = currentEvaluation.evaluator
finalEvaluation.board = move
if player1:
finalEvaluation.evaluator = alpha
else:
finalEvaluation.evaluator = beta
else:
if player1:
finalEvaluation.evaluator = heuristic(board, isStage1)
else:
finalEvaluation.evaluator = heuristic(InvertedBoard(board), isStage1)
return finalEvaluation
def getPruned():
global pruned
x = pruned
pruned = 0
return x
def getStatesReached():
global states_reached
x = states_reached
states_reached = 0
return x