-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimaxV1.py
157 lines (149 loc) · 6.93 KB
/
minimaxV1.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import copy
import board as bd
import math
def evaluate(string):
global evalCalls
evalCalls += 1
board = bd.Board(string)
if board.endGame(1):
return -1
if board.endGame(-1):
return 1
scoreW = 0.000
scoreB = 0.000
for i in range(8):
for j in range(8):
if board.board[i][j] > 0:
scoreW += board.board[i][j]
elif board.board[i][j] < 0:
scoreB += -board.board[i][j]
return round(2*((scoreW) / (scoreB + scoreW)) - 1, 5)
def min_value(str, depth = 10, currentV=math.inf):
tempBoard = bd.Board(str)
backupBoard = bd.Board(str)
if depth == 0:
return evaluate(str)
if tempBoard.endGame(-1):
return tempBoard.utility(-1)
v = math.inf
for i in range(8):
for j in range(8):
if tempBoard.board[i][j] == 0:
continue
if tempBoard.moveAllowed(i, j, 'L', -1):
tempBoard.move(i, j, 'L', -1)
v = min(v, max_value(tempBoard.getString(), depth - 1))
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, 'R', -1):
tempBoard.move(i, j, 'R', -1)
v = min(v, max_value(tempBoard.getString(), depth - 1))
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, '-L', -1):
tempBoard.move(i, j, '-L', -1)
v = min(v, max_value(tempBoard.getString(), depth - 1))
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, '-R', -1):
tempBoard.move(i, j, '-R', -1)
v = min(v, max_value(tempBoard.getString(), depth - 1))
tempBoard.board = copy.deepcopy(backupBoard.board)
return v
def max_value(str, depth = 10, currentV=-math.inf):
tempBoard = bd.Board(str)
backupBoard = bd.Board(str)
if depth == 0:
return evaluate(str)
if tempBoard.endGame(1):
return tempBoard.utility(1)
v = -math.inf
for i in range(8):
for j in range(8):
if tempBoard.board[i][j] == 0:
continue
if tempBoard.moveAllowed(i, j, 'L', 1):
tempBoard.move(i, j, 'L', 1)
v = max(v, min_value(tempBoard.getString(), depth - 1,))
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, 'R', 1):
tempBoard.move(i, j, 'R', 1)
v = max(v, min_value(tempBoard.getString(), depth - 1))
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, '-L', 1):
tempBoard.move(i, j, '-L', 1)
v = max(v, min_value(tempBoard.getString(), depth - 1))
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, '-R', 1):
tempBoard.move(i, j, '-R', 1)
v = max(v, min_value(tempBoard.getString(), depth - 1))
tempBoard.board = copy.deepcopy(backupBoard.board)
return v
def minimax(str, depth, turn):
print ('Depth= ', depth)
tempBoard = bd.Board(str)
backupBoard = bd.Board(str)
optimalMove = 'N'
if turn == 1:
v = -math.inf
for i in range(8):
for j in range(8):
if tempBoard.board[i][j] == 0:
continue
if tempBoard.moveAllowed(i, j, 'L', 1):
tempBoard.move(i, j, 'L', 1)
if(min_value(tempBoard.getString(), depth - 1) > v):
v = min_value(tempBoard.getString(), depth - 1)
optimalMove = tempBoard.getString()
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, 'R', 1):
tempBoard.move(i, j, 'R', 1)
if(min_value(tempBoard.getString(), depth - 1) > v):
v = min_value(tempBoard.getString(), depth - 1)
optimalMove = tempBoard.getString()
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, '-L', 1):
tempBoard.move(i, j, '-L', 1)
if(min_value(tempBoard.getString(), depth - 1) > v):
v = min_value(tempBoard.getString(), depth - 1)
optimalMove = tempBoard.getString()
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, '-R', 1):
tempBoard.move(i, j, '-R', 1)
if(min_value(tempBoard.getString(), depth - 1) > v):
v = min_value(tempBoard.getString(), depth - 1)
optimalMove = tempBoard.getString()
tempBoard.board = copy.deepcopy(backupBoard.board)
else:
v = math.inf
for i in range(8):
for j in range(8):
if tempBoard.board[i][j] == 0:
continue
if tempBoard.moveAllowed(i, j, 'L', -1):
tempBoard.move(i, j, 'L', -1)
if(max_value(tempBoard.getString(), depth - 1) < v):
v = max_value(tempBoard.getString(), depth - 1)
optimalMove = tempBoard.getString()
print(i, j, 'L', -1, v)
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, 'R', -1):
tempBoard.move(i, j, 'R', -1)
if(max_value(tempBoard.getString(), depth - 1) < v):
v = max_value(tempBoard.getString(), depth - 1)
optimalMove = tempBoard.getString()
print(i, j, 'R', -1, v)
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, '-L', -1):
tempBoard.move(i, j, '-L', -1)
if(max_value(tempBoard.getString(), depth - 1) < v):
v = max_value(tempBoard.getString(), depth - 1)
optimalMove = tempBoard.getString()
print(i, j, '-L', -1, v)
tempBoard.board = copy.deepcopy(backupBoard.board)
if tempBoard.moveAllowed(i, j, '-R', -1):
tempBoard.move(i, j, '-R', -1)
if(max_value(tempBoard.getString(), depth - 1) < v):
v = max_value(tempBoard.getString(), depth - 1)
optimalMove = tempBoard.getString()
print(i, j, '-R', -1, v)
tempBoard.board = copy.deepcopy(backupBoard.board)
print ('Current evaluation: ' ,v)
return optimalMove