-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgo.py
180 lines (120 loc) · 3.98 KB
/
algo.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
from checks_counts import *
from math import inf
import time
def NextMoveNaive(cost, turn, n, arr, depth = 0):
# global Last
temp_cost = []
moves = possibleMoves(arr,n)
if(turn == 0):
for x,y in moves:
arr[x][y] = 0
c = check(arr,n)
if(c == 1):
temp_cost.append([cost-20, x, y])
elif(c == 0):
temp_cost.append([cost, x, y])
else:
val = NextMoveNaive(cost, 1 - turn, n, arr, depth + 1)[0]
temp_cost.append([val,x,y])
arr[x][y] = -1
# Last = temp_cost
return min(temp_cost)
else:
for x,y in moves:
arr[x][y] = 1
c = check(arr,n)
if(c == 1):
temp_cost.append([cost+20,x,y])
elif(c == 0):
temp_cost.append([cost,x,y])
else:
temp_cost.append([NextMoveNaive(cost, 1 - turn, n, arr, depth + 1)[0],x,y])
arr[x][y] = -1
# Last = temp_cost
return max(temp_cost)
def heuristic(arr, n, move, turn):
x,y = move
count, row, col, diag, adiag = 0, 0, 0, 0, 0
# Along row
for i in range(n):
if(arr[x][i] == turn):
row = row + 1
elif(arr[x][i] == 1 - turn):
row = 0
break
# Along col
for i in range(n):
if(arr[i][y] == turn):
col = col + 1
elif(arr[i][y] == 1 - turn):
col = 0
break
# Diagonal
if(x == y):
for i in range(n):
if(arr[i][i] == turn):
diag = diag + 1
elif(arr[i][i] == 1 - turn):
diag = 0
break
if(x + y == n-1):
for i in range(n):
if(arr[i][n-i-1] == turn):
adiag = adiag + 1
elif(arr[i][n-i-1] == 1 - turn):
adiag = 0
break
return row + col + diag + adiag
def removing_initial_moves(arr, n):
if(n>3 and count_moves(arr, n) < 2*n-3):
return 1
return 0
def NextWithPruning(turn, n, arr, alpha, beta, current_depth, depth_limit, time_limit = inf, start_time = inf):
moves = possibleMoves(arr,n)
moves.sort(reverse = True, key = lambda x : heuristic(arr, n, x, turn))
if(time_limit != inf and time.time() - start_time > time_limit):
return [0, moves[0][0], moves[0][1]]
if(removing_initial_moves(arr, n)):
return [0, moves[0][0], moves[0][1]]
# Minimizer
if(turn == 0):
best_val = [inf, -1, -1]
if(current_depth == depth_limit):
return [0, moves[0][0], moves[0][1]]
for x,y in moves:
arr[x][y] = 0
c = check(arr,n)
if(c == 1):
val = -20
elif(c == 0):
val = 0
else:
val = NextWithPruning(1 - turn, n, arr, alpha, beta, current_depth + 1, depth_limit, time_limit, start_time)[0]
arr[x][y] = -1
if(val < best_val[0]):
best_val = [val, x, y]
if(best_val[0] <= alpha):
return best_val
beta = min(beta, best_val[0])
return best_val
else:
best_val = [-inf, -1, -1]
if(current_depth == depth_limit):
return [0, moves[0][0], moves[0][1]]
for x,y in moves:
arr[x][y] = 1
c = check(arr,n)
if(c == 1):
val = 20
elif(c == 0):
val = 0
else:
val = NextWithPruning(1 - turn, n, arr, alpha, beta, current_depth + 1, depth_limit, time_limit, start_time)[0]
arr[x][y] = -1
if(val > best_val[0]):
best_val = [val, x, y]
if(best_val[0] >= beta):
return best_val
alpha = max(alpha, best_val[0])
# print(x,y, current_depth, c, best_val[0])
return best_val