-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminimax.py
207 lines (169 loc) · 4.46 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#chaos play
#global color_prob_arr=[]
import os, sys
sys.path.insert(0, os.path.realpath('../utils'))
from log import *
N=5
avail_colors=N*N
num_colors=[]
for i in xrange(N):
num_colors.append(N)
def prob(colour):
if (colour=='-'):
return 0
index = ord(colour) - ord('A')
return num_colors[index]/avail_colors
Inf_min=-100
Inf_max=100
def getPossibleOrderMoves(x, y):
possibleMoves = []
for iterator in range(x-1,-1,-1):
if board[iterator][y]=='-':
possibleMoves.append((iterator,y))
else:
break
for iterator in range(y-1,-1,-1):
if board[x][iterator]=='-':
possibleMoves.append((x,iterator))
else:
break
for iterator in range(x+1,N):
if board[iterator][y]=='-':
possibleMoves.append((iterator,y))
else:
break
for iterator in range(y+1,N):
if board[x][iterator]=='-':
possibleMoves.append((x,iterator))
else:
break
return possibleMoves
def Expectiminimax_decision_chaos(board, Color):
alpha=100
global avail_colors
avail_colors -= 1
index = ord(Color) - ord('A')
num_colors[index] -= 1
(actionx,actiony)=(-1,-1)
for x in xrange(N):
for y in xrange(N):
if board[x][y]=="-":
board[x][y]=Color
value=Expectiminimax_value(board,0,"max",Color)
alpha=min(alpha,value)
if(alpha==value):
(actionx,actiony)=(x,y)
board[x][y]="-"
return (actionx,actiony)
def Expectiminimax_decision_order():
beta=Inf_min
max_move=(0,0,0,0)
Ordermoves=Omoveshelper(board)
for move in Ordermoves:
(a,b,c,d)=move
board[c][d] = board[a][b]
board[a][b] = '-'
value=Expectiminimax_value(board,0,"chance",'A')
beta=max(beta,value)
if(value==beta):
max_move=move
board[a][b] = board[c][d]
board[c][d] = '-'
return max_move
def Omoveshelper(board):
capturedSquares=[]
for x in xrange(N):
for y in xrange(N):
if board[x][y]!="-":
capturedSquares.append((x,y,board[x][y]))
capturedSquares = sorted(capturedSquares, key=lambda t:(t[0],t[1]))
i=0
ans=[]
while(i<len(capturedSquares)):
fromPosition = capturedSquares[i]
i=i+1
possibleMoves = getPossibleOrderMoves(fromPosition[0], fromPosition[1])
if len(possibleMoves)!=0:
j=0
while(j<len(possibleMoves)):
mv =possibleMoves[j]
j+=1
ans.append((fromPosition[0], fromPosition[1], mv[0], mv[1]))
return ans
COLORS = [bcolors.OKRED, bcolors.OKCYAN, bcolors.OKGREEN, bcolors.OKBLUE, bcolors.OKYELLOW, bcolors.OKWHITE]
TEXTCONV = {'A': 'R', 'B': 'C', 'C': 'G','D':'B', 'E':'Y', '-':'-'}
def color(tile): # character
#print tile
index = ord(tile) - ord('A')
if (tile == '-'):
index = 5
return COLORS[index] + TEXTCONV[tile] + bcolors.ENDC
def scoreHelp(row):
MAX = len(row)
isOk = lambda x: True if x >= 0 and x < MAX and row[x] != '-' else False
score = 0
for ind in range(1, MAX):
# epicenter b/w ind-1 and ind
length = 0
scoreX = 0
right = ind
left = ind - 1
while isOk(right) and isOk(left) and row[left] == row[right]:
scoreX += (length+2); length += 2; right += 1; left -= 1
score += scoreX
# epicenter at ind
length = 1
scoreX = 0
right = ind + 1
left = ind - 1
while isOk(right) and isOk(left) and row[left] == row[right]:
scoreX += (length + 2); length += 2; right += 1; left -= 1
score += scoreX
return score
def calculateScore():
score = 0
for rowList in board:
score += scoreHelp(rowList)
for col in range(0, N):
colList = []
for row in range(0, N):
colList.append(board[row][col])
score += scoreHelp(colList)
return score
def evaluation(board):
return 1
def Expectiminimax_value(board,depth,player,Color):
cutoff=3
if(depth==cutoff):
return calculateScore()
else:
if(player=="max"):
beta=Inf_min
Ordermoves=Omoveshelper(board)
for move in Ordermoves:
(a,b,c,d)=move
board[c][d] = board[a][b]
board[a][b] = '-'
beta=max(beta, Expectiminimax_value(board,depth+1,"chance",Color))
board[a][b] = board[c][d]
board[c][d] = '-'
return beta
elif(player=="chance"):
mycolor=['A','B','C','D','E','-']
chance_sum=0
for char in mycolor:
chance_sum=chance_sum+prob(char)*Expectiminimax_value(board,depth+1,"min",char)
return chance_sum
elif(player=="min"):
alpha=Inf_max
for x in xrange(N):
for y in xrange(N):
if board[x][y]=="-":
board[x][y]=Color
alpha=min(alpha,Expectiminimax_value(board,depth+1,"max",Color))
board[x][y]="-"
return alpha
def printBoard():
for x in xrange(N):
print >>sys.stderr, "".join( list( map( lambda x: color(x), board[x] ) ) )
print >>sys.stderr, '\n'