-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam24.py
204 lines (185 loc) · 5.47 KB
/
team24.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
import random
import copy
import math
INF=float("inf")
class Player24():
def __init__(self):
pass
def evaluate(self,board,block,flag):
first=[[0 for p in range(4)] for q in range(4)]
second=[[0 for p in range(4)] for q in range(4)]
came=0
for i in range(0,4):
for j in range(0,4):
came=0
if block[i][j]=='-':
came=1
smallblock=[['-' for p in range(4)] for q in range(4)]
for l in range(4*i,4*i+4):
for m in range(4*j,4*j+4):
smallblock[l-4*i][m-4*j]=board[l][m]
if flag=='x':
if came==1:
first[i][j]=self.findprobabilities(smallblock,'x')
second[i][j]=self.findprobabilities(smallblock,'o')
else:
if came==1:
first[i][j]=self.findprobabilities(smallblock,'o')
second[i][j]=self.findprobabilities(smallblock,'x')
if came==0:
if block[i][j]==flag:
first[i][j]=1
second[i][j]=0
elif block[i][j]!='-':
second[i][j]=1
first[i][j]=0
if first[i][j]==1:
second[i][j]=0
if second[i][j]==1:
first[i][j]=0
present=self.findheuristic(first)
opponent=self.findheuristic(second)
return present-opponent
def findprobabilities(self,smallblock,flag):
value=[[0 for p in range(4)] for q in range(4)]
for i in range(0,4):
for j in range(0,4):
if smallblock[i][j]==flag:
value[i][j]=1
elif smallblock[i][j]=='-':
value[i][j]=0.5
else:
value[i][j]=0
heuristic_value=self.findheuristic(value)
return heuristic_value
def findheuristic(self,value):
heuristic=[0,0,0,0,0,0,0,0,0,0]
count=0
for i in range(0,4):
p=1
for j in range(0,4):
p=p*value[i][j]
heuristic[count]=p
count+=1
for j in range(0,4):
p=1
for i in range(0,4):
p=p*value[i][j]
heuristic[count]=p
count+=1
heuristic[8]=value[0][0]*value[1][1]*value[2][2]*value[3][3]
heuristic[9]=value[0][3]*value[1][2]*value[2][1]*value[3][0]
flag1=0
heuristic_value_avg=0
for i in range(0,10):
if heuristic[i]==1:
flag1=1
heuristic_value_avg+=heuristic[i]
if flag1==1:
return 1
else:
heuristic_value_avg=heuristic_value_avg
return heuristic_value_avg/10
def move(self, board, old_move, flag):
#You have to implement the move function with the same signature as this
#Find the list of valid cells allowed
[a,b,val]=self.dfs(board,0,old_move,flag,flag,-INF,INF,-1,-1)
return a,b
def dfs(self,board,depth,old_move,flag,original_flag,alpha,beta,best_row,best_col):
if depth==3:
val=self.evaluate(board.board_status,board.block_status,original_flag)
return best_row,best_col,val
count=0
children=self.find_valid_cells(board.block_status,board.board_status,old_move)
if len(children)==0:
val=self.evaluate(board.board_status,board.block_status,original_flag)
return old_move[0],old_move[1],val
for x in children:
copy_block_status = [['-' for i in range(4)] for j in range(4)]
copy_board_status = [['-' for i in range(16)] for j in range(16)]
for i in range(0,4):
for j in range(0,4):
copy_block_status[i][j]=board.block_status[i][j]
for i in range(0,16):
for j in range(0,16):
copy_board_status[i][j]=board.board_status[i][j]
board.board_status[x[0]][x[1]]=flag
board.block_status=self.updateboardstatus(board.block_status,board.board_status,x,flag)
if flag=='o':
util=self.dfs(board,depth+1,x,'x',original_flag,alpha,beta,best_row,best_col)
else:
util=self.dfs(board,depth+1,x,'o',original_flag,alpha,beta,best_row,best_col)
utility=util[2]
if depth%2==1 and utility<beta:
beta=utility
best_row=x[0]
best_col=x[1]
elif depth%2==0 and utility>alpha:
alpha=utility
best_row=x[0]
best_col=x[1]
for i in range(0,16):
for j in range(0,16):
board.board_status[i][j]=copy_board_status[i][j]
for i in range(0,4):
for j in range(0,4):
board.block_status[i][j]=copy_block_status[i][j]
if alpha>=beta:
break
if depth==0:
if best_row==-1 or best_col==-1:
best_row=children[0][0]
best_col=children[0][1]
if depth%2==0:
return best_row,best_col,alpha
else:
return best_row,best_col,beta
def updateboardstatus(self,block_status,board_status,move,flag):
row=move[0]/4
col=move[1]/4
ans=0
for i in range(0,4):
c=0
for j in range(0,4):
if board_status[4*row+i][4*col+j]==flag:
c+=1
if c==4:
ans=1
for j in range(0,4):
c=0
for i in range(0,4):
if board_status[4*row+i][4*col+j]==flag:
c+=1
if c==4:
ans=1
c=0
for i in range(0,4):
if board_status[4*row+i][4*col+i]==flag:
c+=1
if c==4:
ans=1
c=0
for i in range(0,4):
if board_status[4*row+i][4*col+3-i]==flag:
c+=1
if c==4:
ans=1
if ans==1:
block_status[row][col]=flag
return block_status
def find_valid_cells(self,block_status,board_status,old_move):
#returns the valid cells allowed given the last move and the current board state
allowed_cells = []
allowed_block = [old_move[0]%4, old_move[1]%4]
#checks if the move is a free move or not based on the rules
if old_move != (-1,-1) and block_status[allowed_block[0]][allowed_block[1]] == '-':
for i in range(4*allowed_block[0], 4*allowed_block[0]+4):
for j in range(4*allowed_block[1], 4*allowed_block[1]+4):
if board_status[i][j] == '-':
allowed_cells.append((i,j))
else:
for i in range(16):
for j in range(16):
if board_status[i][j] == '-' and block_status[i/4][j/4] == '-':
allowed_cells.append((i,j))
return allowed_cells