forked from ayushv/entropy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimax.py
148 lines (122 loc) · 3.23 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
#chaos play
#global color_prob_arr=[]
import os, sys
sys.path.insert(0, os.path.realpath('../utils'))
from log import *
N=5
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
(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 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 evaluation(board):
return 1
def Expectiminimax_value(board,depth,player,Color):
cutoff=3
if(depth==cutoff):
return evaluation(board)
else:
if(player=="max"):
beta=-100
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
Prob=0.2
for char in mycolor:
chance_sum=chance_sum+Prob*Expectiminimax_value(board,depth+1,"min",char)
return chance_sum
elif(player=="min"):
alpha=100
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'
board = []
for i in range(0, N):
boardRow = []
for j in range(0, N):
boardRow.append('-')
board.append(boardRow)
printBoard()
(x,y)=Expectiminimax_decision_chaos(board,'A')
print (x,y)
board[x][y]='A'
(x,y)=Expectiminimax_decision_chaos(board,'B')
print (x,y)
board[x][y]='B'
printBoard()