-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuperTTT.py
347 lines (299 loc) · 9.5 KB
/
superTTT.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
#!/usr/bin/python3
import numpy as np
import sys
import random
class Board(object):
def __init__(self):
self.board = [['-' for i in range(9)] for i in range(9)]
self.manual = []
def putIN(self, b_num, action, chess):
if self.board[b_num][action] == '-':
self.board[b_num][action] = chess
self.manual.append((b_num,action,chess))
def is_legal(self, b_num, action):
return self.board[b_num][action] == '-'
def showBoard(self):
return self.board[:][:]
def get_ava_Action(self, b_num):
actions = []
win_lines = [self.board[b_num][0:3], self.board[b_num][3:6], self.board[b_num][6:9], self.board[b_num][0::3], self.board[b_num][1::3], self.board[b_num][2::3], self.board[b_num][0::4], self.board[b_num][2:7:2]]
if ['X']*3 not in win_lines and ['O']*3 not in win_lines:
if '-' in self.board[b_num]:
for i in range(9):
if self.board[b_num][i] == '-':
actions.append([b_num, i])
# if board b_num is full, next player can choose wherever to play
else:
list = [0,1,2,3,4,5,6,7,8]
list.remove(b_num)
# print(list)
for i in list:
for j in range(9):
if self.board[i][j] == '-':
actions.append([i,j])
#print(actions)
return actions
def vanishMove(self, b_num, action):
self.board[b_num][action] = '-'
def terminate(self):
win_board = ['-' for i in range(9)]
for i in range(9):
board = self.board
win_lines = [board[i][0:3], board[i][3:6], board[i][6:9], board[i][0::3],board[i][1::3],board[i][2::3],board[i][0::4],board[i][2:7:2]]
if ['X']*3 in win_lines:
win_board[i] = 'X'
elif ['O']*3 in win_lines:
win_board[i] = 'O'
elif '-' not in board[i]:
win_board[i] = 'T'
win_board_lines = [win_board[0:3],win_board[3:6], win_board[6:9], win_board[0::3], win_board[1::3], win_board[2::3], win_board[0::4], board[2:7:2]]
if ['X']*3 in win_board_lines or ['O']*3 in win_board_lines or '-' not in win_board:
return True
return False
def winner(self):
win_board = ['-' for i in range(9)]
for i in range(9):
board = self.board
win_lines = [board[i][0:3], board[i][3:6], board[i][6:9], board[i][0::3],board[i][1::3],board[i][2::3],board[i][0::4],board[i][2:7:2]]
if ['X']*3 in win_lines:
win_board[i] = 'X'
elif ['O']*3 in win_lines:
win_board[i] = 'O'
elif '-' not in board[i]:
win_board[i] = 'T'
win_board_lines = [win_board[0:3],win_board[3:6], win_board[6:9], win_board[0::3], win_board[1::3], win_board[2::3], win_board[0::4], board[2:7:2]]
if ['X']*3 in win_board_lines:
return 1
elif ['O']*3 in win_board_lines:
return -1
elif '-' not in win_board:
return 0
def print_board(self):
board = self.board
sys.stderr.write("\n")
for i in range(3):
for j in range(3):
for k in range(3*i, 3*i+3):
# sys.stderr.write(str(j))
sys.stderr.write(board[j][k])
sys.stderr.write(" ")
sys.stderr.write("|")
sys.stderr.write("\n")
sys.stderr.write("--------------------\n")
for i in range(3):
for j in range(3,6):
for k in range(3*i, 3*i+3):
# sys.stderr.write(str(j))
sys.stderr.write(board[j][k])
sys.stderr.write(" ")
sys.stderr.write("|")
sys.stderr.write("\n")
sys.stderr.write("--------------------\n")
for i in range(3):
for j in range(6,9):
for k in range(3*i, 3*i+3):
# sys.stderr.write(str(j))
sys.stderr.write(board[j][k])
sys.stderr.write(" ")
sys.stderr.write("|")
sys.stderr.write("\n")
sys.stderr.write("--------------------\n")
class chessPlayer(object):
def __init__(self, chess='X'):
self.chess = chess
def think(self, board):
pass
def putChoice(self, b_num, board, action):
sys.stderr.write("The last move is: ")
sys.stderr.flush()
sys.stdout.write(str(b_num+1)+' '+str(action+1))
sys.stdout.flush()
# print("putChoice"+str(b_num)+str(action))
board.putIN(b_num, action, self.chess)
class human(chessPlayer):
def __init__(self, chess):
super().__init__(chess)
def think(self, board, b_num):
# print('think'+ str(b_num))
global board_num
b_num = board_num
while True:
while '-' not in board.board[b_num]:
sys.stderr.write('Board' + str(b_num) + 'full, plase choose choice you want to play on.')
sys.stderr.flush()
choice = sys.stdin.readline()[:-1]
board_num = int(choice[0])-1
b_num = board_num
action = int(choice[2]) - 1
# sys.stderr.write("Please enter your next choice:")
if b_num == -1:
sys.stderr.write("Please enter your choice:")
sys.stderr.flush()
choice = sys.stdin.readline()[:-1]
board_num = int(choice[0])-1
b_num = board_num
action = int(choice[2])-1
else:
choice = [-1, -1];
temp = board_num
while (int(choice[0])-1)!=temp:
sys.stderr.write("Please enter your choice:")
sys.stderr.flush()
choice = sys.stdin.readline()[:-1]
board_num = int(choice[0]) - 1
b_num = board_num
action = int(choice[2]) - 1
# action = sys.stdin.readline()[:-1]
# print(action)
if action in range(9) and board.is_legal(b_num, int(action)):
action_right = int(action)
return [b_num, action_right]
class AI(chessPlayer):
def __init__(self, chess):
super().__init__(chess)
def think(self, board, b_num, depth=8):
global board_num
test = self.max_value(board, board_num, -10000, 10000, depth)
#print("thinkaction"+ str(test))
return test[1]
# return self.max_value(board, b_num, -10000, 10000, depth)[1]
def max_value(self, board, b_num, alpha, beta, depth):
if board.terminate() or depth == 0:
return [self.utility(board),[None,None]]
val = -10000
moves = board.get_ava_Action(b_num)
move = moves[0]
# print(move)
for action in moves:
board.putIN(action[0], action[1], self.chess)
tmp_val = self.min_value(board,action[1], alpha, beta, depth-1)
# print(type(tmp_val))
test = tmp_val[0]
board.vanishMove(action[0],action[1])
if test > val:
move = action
val = test
if val >= beta:
return [val,move]
alpha = max(alpha, val)
# print(move)
return [val,move]
def min_value(self, board, b_num, alpha, beta, depth):
if board.terminate() or depth ==0:
return [self.utility(board),[None,None]]
val = 10000
moves = board.get_ava_Action(b_num)
move = moves[0]
for action in moves:
board.putIN(action[0], action[1], ['X','O'][self.chess == 'X'])
tmp_val = self.max_value(board, action[1], alpha, beta, depth-1)
# print(type(tmp_val))
test = tmp_val[0]
# tmp_val = min(val, self.max_value(board, action[1], alpha, beta, depth-1)[0])
board.vanishMove(action[0],action[1])
if test < val:
move = action
val = test
if val <= alpha:
return [val, move]
beta = min(beta, val)
# print(str(depth)+' '+str(move))
return [val, move]
def utility(self,board):
state = board.board
point = 0
op_chess = ['X','O'][self.chess == 'X']
for i in range(9):
if state[i][i] == self.chess:
point += 1;
elif state[i][i] == op_chess:
point -= 1;
if state[i][4] == self.chess:
point += 1;
elif state[i][4] == op_chess:
point -= 1;
if i == 4:
point += (state[i].count(self.chess) - state[i].count(op_chess)-1)*5
elif i in [0,2,6,8]:
point += (state[i].count(self.chess) - state[i].count(op_chess)-1)*4
else:
point += (state[i].count(self.chess) - state[i].count(op_chess)-1)*3
win_lines = [state[i][0:3],state[i][3:6],state[i][6:9],state[i][0::3],state[i][1::3], state[i][2::3], state[i][0::4], state[i][2:7:2]]
if self.chess*3 in win_lines:
point += 30
elif op_chess*3 in win_lines:
point -= 30
if board.terminate():
if board.winner()==1:
if self.chess == 'X':
point += 60
else:
point -= 60
elif board.winner()==-1:
if self.chess == 'O':
point += 60
else:
point -= 60
return point
class Game(object):
def __init__(self):
self.board = Board()
self.current_Player = None
def playerOrder(self, upper, chess = 'X'):
if upper == 'Human':
return human(chess)
else:
return AI(chess)
def playerChange(self,playerOne,playerTwo):
if self.current_Player is None:
return playerOne
else:
return [playerOne,playerTwo][self.current_Player == playerOne]
def printWinner(self, winner):
if winner == 1:
sys.stderr.write("winner is uppper!\n")
elif winner == -1:
sys.stderr.write("winner is lower! \n")
else:
sys.stderr.write("It is a tie! \n")
def run(self):
global board_num
while True:
sys.stderr.write("choose X or O?")
sys.stderr.flush()
chooseUpper = sys.stdin.readline()[:-1]
if chooseUpper == 'X' or chooseUpper == 'x':
playerOne = self.playerOrder('Human', 'X')
playerTwo = self.playerOrder('AI', 'O')
# sys.stderr.write("Choose the fist board you want to play on.")
# sys.stderr.flush()
# board_num = int(sys.stdin.readline()[:-1]) -1
break
elif chooseUpper == 'O' or chooseUpper == 'o':
playerOne = self.playerOrder('AI', 'X')
playerTwo = self.playerOrder('Human', 'O')
break
else:
sys.stderr.write("Error!")
sys.stderr.flush()
sys.stderr.write("\n Game start \n")
self.board.print_board()
while True:
self.current_Player = self.playerChange(playerOne, playerTwo)
think_result = self.current_Player.think(self.board, b_num = -1)
action = think_result[1]
temp = think_result[0]
self.current_Player.putChoice(temp, self.board, action)
# board_num = action
self.board.print_board()
if self.board.terminate():
winner = self.board.winner()
break
board_num = action
self.printWinner(winner)
sys.stderr.write("Good Game \n")
sys.stderr.flush()
board_num = -1
if __name__ == '__main__':
while True:
Game().run()