-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNiyaButFaster.py
More file actions
354 lines (283 loc) · 9.44 KB
/
Copy pathNiyaButFaster.py
File metadata and controls
354 lines (283 loc) · 9.44 KB
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
from random import randint
import time
import dis
def PrintMainMenu():
print("\n What do you want to do?")
print(" Play a game : play")
# print("Generate boards : generate")
print(" Exit : exit")
def PrintBoard(board):
for b in board:
print(' ' + str(b))
def PrintPlayableBoard(board, player, lastPlay):
print("\n %s's turn."%('X' if player == 0 else 'O'))
if lastPlay is not None:
print(" Last Play: %s"%lastPlay)
else:
print(" Can only play on the edge")
PrintBoard(board)
print()
return
def CreateBoard():
board = [['','','',''],
['','','',''],
['','','',''],
['','','','']]
# 我是一棵樹
# When the scorching sun arise, my leaves won't wither and die
# I am a tree with roots by the river of the Lord
emptySpaces = 16
for let in ['A', 'B', 'C', 'D']:
for num in ['1', '2', '3', '4']:
addToHere = randint(0, emptySpaces - 1)
here = 0
for r in range(4):
for c in range(4):
if board[r][c] == '':
if here == addToHere:
board[r][c] = let + num
emptySpaces -= 1
here += 1
return board
def CreateBoardHash(board, lastTurn):
boardHash = str(lastTurn)
for row in board:
for col in row:
boardHash += col
return boardHash
def CheckRow(board):
for row in board:
if row[0] == row[1] and \
row[0] == row[2] and \
row[0] == row[3]:
return True
return False
def CheckCol(board):
for col in range(4):
for spot in range(1, 4):
if board[0][col] != board[spot][col]:
break
else:
return True
return False
def CheckDia(board):
for i in range(1, 4):
if board[3][0] != board[3 - i][i]:
break
else:
return True
for i in range(1, 4):
if board[0][0] != board[i][i]:
break
else:
return True
return False
def CheckBox(board):
for row in range(3):
for col in range(3):
# for i in range(1,4):
# if board[row][col] != board[row + (i % 2)][col + (0 if i < 2 else 1)]:
# break
# else:
# return True
if board[col*4+row] == board[row][col + 1] and \
board[col*4+row] == board[row + 1][col] and \
board[col*4+row] == board[row + 1][col + 1]:
return True
return False
def FindPlayable(board, lastPlay):
i = 0
playable = [None,None,None,None,None,None]
try:
let = lastPlay[0]
num = lastPlay[1]
r = 0
for row in board:
c = 0
for col in row:
if let in col or num in col:
playable[i] = [r, c]
i += 1
c += 1
r += 1
except:
# Only executes if it is the first move
playable = []
for row in range(4):
for col in range(4):
if row % 3 == 0 or col % 3 == 0:
playable.append([row, col])
return playable
return [] if i == 0 else playable
def PlayPiece(board, player, play):
temp = board[play[0]][play[1]]
board[play[0]][play[1]] = "XX" if player == 0 else "OO"
return temp
def UnplayPiece(board, play, lastPlay):
board[play[0]][play[1]] = lastPlay
def NoPlays(board, player):
count = 0
playerMarker = "XX" if player == 0 else "OO"
for row in board:
for col in row:
if col in playerMarker:
count += 1
if count <= 7:
return 1 - player
else:
return 2
def AddSmartScore(myScore, otherScore, player):
myScore[player] += otherScore[player]
# Other player can't win, 1. correct my score then 2. correct-add theirs
if otherScore[1 - player] == 0:
if myScore[1 - player] != 0:
myScore[player] += myScore[1 - player]
myScore[1 - player] = 0
myScore[player] += otherScore[1 - player]
elif myScore[1 - player] == 0 and myScore[player] != 0:
myScore[player] += otherScore[1 - player]
# If other player can win just add their score normally
else:
myScore[1 - player] += otherScore[1 - player]
myScore[2] += otherScore[2]
return
gameBoards = {}
def Solve(board, player = 0, lastPlay = None):
try:
playOptions = [0, 0, 0]
playable = FindPlayable(board, lastPlay)
if playable == []:
playOptions[NoPlays(board, player)] += 1
boardHash = ''
for play in playable:
if play is None:
break
lastPlayTemp = PlayPiece(board, player, play)
boardHash = CreateBoardHash(board, lastPlayTemp)
if boardHash in gameBoards:
AddSmartScore(playOptions, gameBoards[boardHash], player)
elif CheckRow(board) or CheckDia(board) or CheckCol(board) or CheckBox(board):
AddSmartScore(playOptions, [1,0,0] if player == 0 else [0,1,0], player)
else:
AddSmartScore(playOptions, Solve(board, (1 - player), lastPlayTemp), player)
UnplayPiece(board, play, lastPlayTemp)
gameBoards[CreateBoardHash(board, lastPlay)] = playOptions
except Exception as ex:
print(ex)
return playOptions
def CreateGame():
board = CreateBoard()
PrintBoard(board)
print('\nGenerating AI; please wait.')
begin = time.time()
Solve(board)
end = time.time()
print('Took %.2f seconds to generate AI.\n'%(end - begin))
return board
def GetWinChances(board, playable, player, turn, lastPlay):
last = []
win = [0.0, None]
tie = [0.0, None]
for play in playable:
tempPlay = PlayPiece(board, player, play)
hash = CreateBoardHash(board, tempPlay)
if hash in gameBoards:
last = [play, gameBoards[hash]]
else:
last = [play, Solve(board, player, lastPlay)]
winChance = last[1][player] / sum(last[1])
tieChance = last[1][2] / sum(last[1])
print('%s:%s - %s'%(play, tempPlay, hash))
print('Win: %.2f Tie: %.2f\n'%(winChance, tieChance))
if winChance > win[0]:
win = [winChance, last[0]]
if tieChance > tie[0]:
tie = [tieChance, last[0]]
UnplayPiece(board, play, tempPlay)
return win, tie, last
def PlayerChoice(board, player, turn, lastPlay):
plays = {}
for p in FindPlayable(board, lastPlay):
plays[board[p[0]][p[1]]] = p
while True:
choice = input("Pick a play: ")
choice = choice.upper()
if choice in plays:
return PlayPiece(board, player, plays[choice])
elif 'CH' in choice:
GetWinChances(board, FindPlayable(board, lastPlay), player, turn, lastPlay)
elif 'RE' in choice:
PrintPlayableBoard(board, player, lastPlay)
elif choice in gameBoards:
print(gameBoards[choice])
else:
print("Can't make that play.\n")
return
def AIChoice(board, player, turn, lastPlay):
# Once was painful trying
# Now it's perfect trust
win, tie, default = GetWinChances(board, FindPlayable(board, lastPlay), player, turn, lastPlay)
choice = default[0]
if win[0] > 0.0:
choice = win[1]
elif tie[0] > 0.0:
choice = tie[1]
print( 'AI picks %s'%(board[choice[0]][choice[1]]) )
time.sleep(2)
return PlayPiece(board, player, choice)
def PlayCheckWin(board, player, lastPlay):
if CheckRow(board) or CheckDia(board) or CheckCol(board) or CheckBox(board):
return player
if FindPlayable(board, lastPlay) == []:
return NoPlays(board, player)
return -1
def PlayGame(board, _player=0, _turn=1, lastPlay=None):
playerXO = 0 if input('Do you want to be X\'s or O\'s: ').upper() == 'X' else 1
player, turn, lastPlay = _player, _turn, lastPlay
winCheck = -1
while True:
PrintPlayableBoard(board, player, lastPlay)
if playerXO == player:
lastPlay = PlayerChoice(board, player, turn, lastPlay)
else:
lastPlay = AIChoice(board, player, turn, lastPlay)
winCheck = PlayCheckWin(board, player, lastPlay)
if winCheck != -1:
break
player = 1 - player
turn += 1
print('\n------------------------')
if winCheck != 2:
print("\n%s win!!"%("XX" if winCheck == 0 else "OO"))
else:
print("\nGame was a tie.")
return
def main():
global gameBoards
print("\n Welcome to Niya!!!")
while True:
PrintMainMenu()
response = input("\nDecision: ").upper()
print()
if 'P' in response:
del gameBoards
gameBoards = {}
board = CreateGame()
# board = [['A4','B2','C2','D2'], # , 1, 6, 'C3'
# ['C1','D3','OO','A2'],
# ['OO','A3','D4','XX'],
# ['C4','B4','XX','XX']]
PlayGame(board) #, 1, 6, 'C3')
elif response in gameBoards:
print(gameBoards[response])
# elif 'g' in responce:
# GenerateBoards()
elif 'X' in response:
break
print("\nPlay again soon!!!")
return
def SolveWrapper():
Solve(CreateBoard())
main()
# SolveWrapper()
# dis.dis(CheckRow)