-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimax.py
129 lines (107 loc) · 4.16 KB
/
Minimax.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
import copy
import game
class Solver:
def __init__(self,
depth: int = 2,
prev_board: list = None,
board: list = None,
player: int = -1,):
self.depth = depth
self.prev_board = copy.deepcopy(prev_board)
self.board = copy.deepcopy(board)
self.player = player
self.opponent = -1 * player
self.start = None
self.end = None
def evaluate(self, board):
result = sum(map(sum, board))
if self.player == -1:
result *= -1
return result
def play(self, node, dp, alpha = -100, beta = 100):
if dp > self.depth:
return
# LEAF NODE
if dp == self.depth:
return self.evaluate(node.board)
cg = game.CoGanh()
trap = None
# PLAYER
if dp % 2 == 0:
if node.prev_board != None:
trap = cg.checkTrap(node.prev_board, node.board, self.opponent)
successor = []
pos = cg.getPosition(node.board, self.player)
for p in pos:
ans = cg.move_gen(node, p, trap)
if ans != None:
successor += cg.move_gen(node, p, trap)
isTrapped = False
if len(successor) > 0:
for s in successor:
if s[3]:
isTrapped = True
break
for s in successor:
if isTrapped:
if not s[3]:
continue
if self.player == -1:
if cg.X_win(s[0].board):
if dp == 0:
self.start = s[2]
self.end = s[1]
return 75
else:
if cg.O_win(s[0].board):
if dp == 0:
self.start = s[2]
self.end = s[1]
return 75
value = self.play(s[0], dp + 1, alpha, beta)
if value > alpha:
alpha = value
if dp == 0:
self.start = s[2]
self.end = s[1]
if alpha >= beta:
return alpha
return alpha
# OPPONENT
else:
if node.prev_board != None:
trap = cg.checkTrap(node.prev_board, node.board, self.player)
successor = []
pos = cg.getPosition(node.board, self.opponent)
for p in pos:
ans = cg.move_gen(node, p, trap)
if ans != None:
successor += cg.move_gen(node, p, trap)
isTrapped = False
if len(successor) > 0:
for s in successor:
if s[3]:
isTrapped = True
break
for s in successor:
if isTrapped:
if not s[3]:
continue
if self.player == -1:
if cg.O_win(s[0].board):
return -50
else:
if cg.X_win(s[0].board):
return -50
value = self.play(s[0], dp + 1, alpha, beta)
if value < beta:
beta = value
if beta <= alpha:
return beta
return beta
def solv(self):
node = game.Node_1(self.board, self.prev_board)
score = self.play(node, 0)
if self.start == None or self.end == None:
return None
return (self.start, self.end)