-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSolver.py
377 lines (279 loc) · 11.4 KB
/
Solver.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: utf-8 -*-
from __future__ import print_function
from time import time
class Solver(object):
# This solver object takes gameboard state
# bestMoveSearch f(x) uses search f(x) to find the solution
# bestMoveRule f(x) uses rule f(x) to find the solution
board = None
colors = ["o", "x"]
def __init__(self, board, color):
self.board = [x[:] for x in board]
self.AIcolor = color
def bestMoveSearch(self, depth, board, currentPlayer, phase):
# return the best column number and the corresponding alpha value using search (option 1)
if currentPlayer == self.colors[0]:
enemyPlayer = self.colors[1]
else:
enemyPlayer = self.colors[0]
currPhase = phase
best_alpha = -999999
best_move = None
start = time()
columns = [3, 2, 4, 1, 5, 0, 6]
if currPhase <= 6:
legal_moves = {3: 5, 2: 0, 4: 0, 1: 0, 5: 0, 0: -5, 6: -5}
else:
legal_moves = {3: 0, 2: 0, 4: 0, 1: 0, 5: 0, 0: 0, 6: 0}
for column in columns: # 0~6
print("Searching column number : ", end="")
print(column + 1)
if not self.isLegalMove(column, board):
legal_moves[column] = -9999999
if self.isLegalMove(column, board) and time() - start < 100:
temp = self.makeMove(board, column, currentPlayer)
legal_moves[column] += -self.search(depth, -999, 999, temp, enemyPlayer) # puts in the alpha values of each choice
print("Alpha : ", end="")
print(legal_moves.items())
print("Time passed : ", end="")
print(time() - start)
if time() - start > 119:
break
for move, alpha in legal_moves.items():
if alpha >= best_alpha:
best_alpha = alpha
best_move = move
return best_move, best_alpha
def search(self, depth, alpha, beta, board, currentPlayer):
# return the max value
columns = [3, 2, 4, 1, 5, 0, 6]
legal_moves = []
for i in columns:
if self.isLegalMove(i, board):
temp = self.makeMove(board, i, currentPlayer)
legal_moves.append(temp)
if currentPlayer == self.colors[0]:
enemyPlayer = self.colors[1]
else:
enemyPlayer = self.colors[0]
if depth == 0 or len(legal_moves) == 0 or self.gameIsOver(board):
return self.value(board, currentPlayer)
bestMoveValue = -9999999
for child in legal_moves:
if child is None:
print("No children")
currValue = -self.search(depth - 1, -beta, -alpha, child, enemyPlayer)
bestMoveValue = max(bestMoveValue, currValue)
alpha = max(alpha, currValue)
if alpha >= beta:
break
return bestMoveValue
def value(self, board, tile):
# evaluate the fitness of each board state for the player
# connect4 = 10000 points, connect3 = 100 points, connect2 = 1 point
# enemy point is minus points
if tile == self.colors[0]:
enemyTile = self.colors[1]
else:
enemyTile = self.colors[0]
connectFour = self.checkForStreak(board, tile, 4)
connectThree = self.checkForStreak(board, tile, 3)
connectTwo = self.checkForStreak(board, tile, 2)
enemyConnectFour = self.checkForStreak(board, enemyTile, 4)
enemyConnectThree = self.checkForStreak(board, enemyTile, 3)
enemyConnectTwo = self.checkForStreak(board, enemyTile, 2)
result = (connectFour * 10000 + connectThree * 100 + connectTwo) - \
(enemyConnectFour * 10000 + enemyConnectThree * 100 + enemyConnectTwo)
return result
def removeDefaultRule(self, legal_moves, rule):
temp_list = [v[rule] for k, v in legal_moves.items()]
if 0 not in temp_list:
for k, v in legal_moves.items():
v[rule] = 0
def removeRedundancyRule(self, legal_moves, rule):
temp_list = [v[rule] for k, v in legal_moves.items()]
if all(item == temp_list[0] for item in temp_list):
for k, v in legal_moves.items():
v[rule] = 0
# if k == 0:
# v[rule] -= v[rule]
# v[rule+2] -= v[rule+2]+1
# v[rule+4] -= v[rule+4]+1
# elif k == 2:
# v[rule] -= v[rule]
# v[rule+2] -= v[rule]+1
# elif k == 4:
# v[rule] -= v[rule]
def bestMoveRule(self, board, currentPlayer, phase):
legal_moves = {} # dictionary where keys: column numbers, values: a 8 number tuple which denotes the rules applied
for column in range(7): # iterate all columns
if self.isLegalMove(column, board):
selected_row, temp = self.make_move2(board, column, currentPlayer)
legal_moves[column] = self.rule_checking_flags(temp, currentPlayer, selected_row, column) # RULE BASED ALGORITHM
# temp2 = self.make_move_rulebased(temp, column, enemyPlayer, temp.local_point) #depth 2
# legal_moves[column] = self.rule_enemy(temp2, enemyPlayer)
# legal_moves[3] = [1, 1, 2, 4, 5, 1..])
# {3: [1, 2,, 1],
# 4: ...
# 5}
for rule_num in [1, 3, 5]:
self.removeDefaultRule(legal_moves, rule_num)
for rule_num in [0,2,4]:
self.removeRedundancyRule(legal_moves,rule_num)
get_value = lambda key: legal_moves[key]
best_point = max(legal_moves, key=get_value) # column값(key) 나옴
# print(best_point, legal_moves[best_point])
messages = {
0: "Rule 1: If there is a winning move, take it.",
1: "Rule 2: Avoid the situation that the opponent can make a winning move.",
2: "Rule 3: If my square can create or extend the streak, make it",
3: "Rule 4: Avoid the situation that the opponent can connect 3.",
4: "Rule 3: If my square can create or extend the streak, make it.",
5: "Rule 5: Avoid the situation that the opponent can connect 2.",
6: "Rule 6: Put the stone in a square at odd row (except for the first)",
7: "Rule 7: Place a stone at center, or corner if not possible"
}
for rule in range(8):
if legal_moves[best_point][rule]:
print(messages[rule])
return best_point
def rule_checking_flags(self, board, currentPlayer, row, column):
flag = [0] * 8
if currentPlayer == self.colors[0]:
enemyPlayer = self.colors[1]
else:
enemyPlayer = self.colors[0]
connectFour = self.checkForStreak(board, currentPlayer, 4)
connectThree = self.checkForStreak(board, currentPlayer, 3)
connectTwo = self.checkForStreak(board, currentPlayer, 2)
if connectFour:
flag[0] += connectFour
if connectThree:
flag[2] += connectThree
if connectTwo:
flag[4] += connectTwo
if row == 2 or row == 4:
flag[6] += 1
if column == 3 :
flag[7] += 2
elif column == 0 or column == 6:
flag[7] += 1
flag[1], flag[3], flag[5] = 1, 1, 1
for column in range(7):
if self.isLegalMove(column, board):
_, temp_board = self.make_move2(board, column, enemyPlayer)
rule_enemy_tuples = ((1, 4), (3, 3), (5,2))
for rule, consecutive in rule_enemy_tuples:
if self.checkForStreak(temp_board, enemyPlayer, consecutive) != 0:
flag[rule] = 0
return flag
def isLegalMove(self, column, board):
# return boolean value if the move is legal
for i in range(6): # 0-5
if board[i][column] == ' ':
return True
return False
def gameIsOver(self, board):
if self.checkForStreak(board, self.colors[0], 4) >= 1:
return True
elif self.checkForStreak(board, self.colors[1], 4) >= 1:
return True
else:
return False
def makeMove(self, board, column, color):
# return a temporary state after the move is updated
temp = [x[:] for x in board]
for i in range(6):
if temp[i][column] == ' ':
temp[i][column] = color
return temp
def make_move2(self, board, column, color):
temp = [x[:] for x in board]
for row in range(6):
if temp[row][column] == ' ':
temp[row][column] = color
return row, temp
def checkForStreak(self, board, tile, streak):
# return the count of streaks
count = 0
for i in range(6):
for j in range(7):
if board[i][j] == tile:
# vertical check
count += self.verticalCheck(i, j, board, streak)
# horizontal check
count += self.horizontalCheck(i, j, board, streak)
# diagonal check /, \
count += self.diagonalCheck(i, j, board, streak)
else:
continue
return count
def verticalCheck(self, row, column, board, streak):
line = 0
# color = board[row][column]
# if row >0:
# if board[row-1][column] == color:
# return 0
for i in range(row, 6):
if board[i][column].lower() == board[row][column].lower():
line += 1
else:
break
if line >= streak:
return 1
else:
return 0
def horizontalCheck(self, row, column, board, streak):
line = 0
# color = board[row][column]
#
# if column > 0:
# if board[row][column-1] == color:
# return 0
for j in range(column, 7):
if board[row][j].lower() == board[row][column].lower():
line += 1
else:
break
if line >= streak:
return 1
else:
return 0
def diagonalCheck(self, row, column, board, streak):
total = 0
# line = 0
# color = board[row][column]
#
# if row > 0 and column > 0:
# if board[row-1][column-1] == color:
# line = 0
# else:
line = 0
j = column
for i in range(row, 6):
if j > 6:
break
elif board[i][j].lower() == board[row][column].lower():
line += 1
else:
break
j += 1 # increment column when row is incremented
if line >= streak:
total += 1
# if row < 6 and column > 0:
# if board[row+1][column-1] == color:
# line = 0
# else:
line = 0
j = column
for i in range(row, 6):
if j > 6:
break
elif board[i][j].lower() == board[row][column].lower():
line += 1
else:
break
j -= 1 # decrement column when row is incremented
if line >= streak:
total += 1
return total