-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomoku.py
276 lines (231 loc) · 6.44 KB
/
gomoku.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
# 用井字棋来练习minmax和alpha beta剪枝
class Move:
def __init__(self, row, col):
self.row = row
self.col = col
class Gomoku:
def __init__(self, rowlen, colen):
self.maxval = 100
self.minval = -100
self.rowlen = rowlen
self.collen = colen
self.goallen = 3
self.board = [["_" for i in range(self.collen)] for i in range(self.rowlen)]
def display(self):
[print(" ".join(x)) for x in self.board]
print("")
def evaluate(self, board):
for row in range(self.rowlen):
for j in range(self.collen - self.goallen + 1):
temp = ''.join(board[row][j:j+self.goallen])
if temp == 'x' * self.goallen :
return self.maxval
elif temp == 'o' * self.goallen :
return self.minval
for col in range(self.collen):
for j in range(self.collen - self.goallen + 1):
temp = ''.join([board[x][col] for x in range(j,self.goallen)])
if temp == 'x' * self.goallen :
return self.maxval
elif temp == 'o' * self.goallen :
return self.minval
for i in range(self.rowlen):
for j in range(self.collen):
count = 0
piece = board[i][j]
if piece == '_': continue
x1, x2 = i, j
while 0 <= x1 < self.rowlen and 0 <= x2 < self.collen:
if board[x1][x2] == piece:
count += 1
x1 += 1
x2 += 1
else:
break
x1, x2 = i - 1, j - 1
count = 0
while 0 <= x1 < self.rowlen and 0 <= x2 < self.collen:
if board[x1][x2] == piece:
count += 1
x1 -= 1
x2 -= 1
else:
break
if count == self.goallen:
return self.maxval if piece == 'x' else self.minval
count = 0
piece = board[i][j]
x1, x2 = i, j
while 0 <= x1 < self.rowlen and 0 <= x2 < self.collen:
if board[x1][x2] == piece:
count += 1
x1 += 1
x2 -= 1
else:
break
x1, x2 = i - 1, j + 1
while 0 <= x1 < self.rowlen and 0 <= x2 < self.collen:
if board[x1][x2] == piece:
count += 1
x1 -= 1
x2 += 1
else:
break
if count == self.goallen:
return self.maxval if piece == 'x' else self.minval
return 0
def minmax(self, board, depth, ismaximizer):
score = self.evaluate(board)
if score == self.maxval: return self.maxval
elif score == self.minval: return self.minval
if ismaximizer:
bestval = -1000
for i in range(self.rowlen):
for j in range(self.collen):
if board[i][j] == '_':
board[i][j] = 'x'
val = self.minmax(board, depth + 1, False)
bestval = max(bestval, val)
board[i][j] = '_'
return bestval
else:
bestval = 1000
for i in range(self.rowlen):
for j in range(self.collen):
if board[i][j] == '_':
board[i][j] = 'o'
val = self.minmax(board, depth + 1, True)
bestval = min(bestval, val)
board[i][j] = '_'
return bestval
return 0
def find_best_move(self, board, depth, ismaximizer):
score = self.evaluate(board)
bestmove = None
if ismaximizer:
bestval = -1000
for i in range(self.rowlen):
for j in range(self.collen):
if board[i][j] == '_':
board[i][j] = 'x'
val = self.minmax(board, depth + 1, False)
# print('move is {},{}, val is {}'.format(i,j,val))
if val > bestval:
bestmove = Move(i,j)
bestval = max(bestval, val)
board[i][j] = '_'
return bestmove
else:
bestval = 1000
for i in range(self.rowlen):
for j in range(self.collen):
if board[i][j] == '_':
board[i][j] = 'o'
val = self.minmax(board, depth + 1, True)
if val < bestval:
bestmove = Move(i,j)
bestval = min(bestval, val)
board[i][j] = '_'
print("bot best move is {} {}, bestval is {}".format(bestmove.row, bestmove.col, bestval))
return bestmove
def is_move_left(self, board):
for i in range(self.rowlen):
for j in range(self.collen):
if board[i][j] == '_': return True
return False
def is_game_end(self):
score = self.evaluate(self.board)
if score == self.maxval or score == self.minval or not self.is_move_left(self.board):
return True
else:
return False
def auto_play(self):
# start from 'x' player
player = True
depth = 0
print("The init board is: ")
self.display()
while not self.is_game_end():
print("Current player is: {}".format(1 if player else 2))
move = self.find_best_move(self.board, depth, player)
player_no = 1 if player else 2
if not move:
print("No best move for player ".format(player_no))
break
print("Player {}'s move is : ({}, {})".format(player_no, move.row, move.col))
if player:
self.board[move.row][move.col] = 'x'
else:
self.board[move.row][move.col] = 'o'
self.display()
player = not player
depth += 1
score = self.evaluate(self.board)
if score == self.maxval:
print("Player 1 Win!")
elif score == self.minval:
print("Player 2 Win!")
else:
print("Draw!")
def play(self):
depth = 0
botmove = game.find_best_move(game.board, depth, False)
depth += 1
game.board[botmove.row][botmove.col] = 'o'
while not game.is_game_end():
while True:
game.display()
suggestmove = game.find_best_move(game.board, depth, True)
if not suggestmove:
print('no suggested move')
else:
print("Suggested move is {} {}".format(suggestmove.row, suggestmove.col))
x, y = list(map(int, input("Please input row and col:\n").split()))
if game.board[x][y] != '_':
print("Invalid postion")
else:
game.board[x][y] = 'x'
depth += 1
break
botmove = game.find_best_move(game.board, depth, False)
if not botmove:
print("No best move for player ".format(player_no))
break
game.board[botmove.row][botmove.col] = 'o'
game.display()
score = game.evaluate(game.board)
if score == self.maxval:
print("You Win!")
elif score == self.minval:
print("You Lose")
else:
print("Draw")
if __name__ == "__main__":
game = Gomoku(4,4)
game.play()
# game.display()
# game.board = [
# '_______________',
# 'x______________',
# 'x______________',
# 'x______________',
# 'x______________',
# 'x______________',
# '_______________',
# '_______________',
# '_______________',
# '_______________',
# '_______________',
# '_______________',
# '_______________',
# '_______________',
# '_______________',
# ]
# game.board = [
# ['o','o','x'],
# ['_','o','x'],
# ['x','_','_']
# ]
# print(game.evaluate(game.board))
# bestmove = game.find_best_move(game.board, 0, True)
# print('next best move is {}, {}'.format(bestmove.row, bestmove.col))