This repository has been archived by the owner on Sep 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameBoard.py
455 lines (335 loc) · 12.2 KB
/
GameBoard.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
from os import system, name
import random
from time import sleep
import PyInquirer as inquirer
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
import webbrowser
class GameBoard:
def __init__(self):
self.clear()
self.UserChar = 'X'
self.round = 1
self.turn = 'X'
self.__Board = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
]
self.done = False
self.map = {
1: [0, 0],
2: [0, 1],
3: [0, 2],
4: [1, 0],
5: [1, 1],
6: [1, 2],
7: [2, 0],
8: [2, 1],
9: [2, 2],
}
self.printTitle()
self.lastMove = ''
difficultyQuestion = [
{
'type': 'list',
'name': 'diffQ',
'message': 'Choose your difficulty: ',
'choices': ['Easy', 'Medium', 'Hard']
}
]
self.difficulty = inquirer.prompt(difficultyQuestion)['diffQ']
print()
print('Coin Flipping To Choose Who Start First ...')
headsOrTails = ['Heads', 'Tails']
coinFlipQuestion = [
{
'type': 'list',
'name': 'coinFlip',
'message': 'Choose heads or tails: ',
'choices': headsOrTails
}
]
userCoin = inquirer.prompt(coinFlipQuestion)['coinFlip']
flip = random.randint(0, 10)
flip = 1 if flip >= 5 else 0
winner = 'You' if userCoin == headsOrTails[flip] else 'Bot'
self.UserChar = 'X' if winner == 'You' else 'O'
self.botChar = 'X' if winner == 'Bot' else 'O'
print()
# It's Tails. Bot won! Bot will start first.
print('+--------------------------------------------+')
print(f'|{" "*44}|')
print(f'| It\'s {headsOrTails[flip]:^5}. {winner:^3} won! {winner:^3} will start first. |')
print(f'| Your character is {self.UserChar:<1}. |')
print(f'|{" "*44}|')
print('+--------------------------------------------+')
print()
input('Press [Enter] to start the game')
def rickRoll(self):
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
# Get current volume
currentVolumeDb = volume.GetMasterVolumeLevel()
volume.SetMute(0, None)
volume.SetMasterVolumeLevel(-0.0, None)
webbrowser.open('https://www.youtube.com/watch?v=YddwkMJG1Jo')
def printTitle(self):
print('=============================================')
print('= The Impossible TicTacToe =')
print('= Made by: Josh =')
print('=============================================')
print('\n Let\' Start!')
print()
return
def clear(self):
if name == 'nt':
system('cls')
else:
system('clear')
def getCords(self, boxChosen: int):
row = self.map[boxChosen][0]
col = self.map[boxChosen][1]
return [row, col]
def ask(self):
if (self.done):
return
self.turn = self.botChar
while (True):
boxChosen = input('Where do you want to play? ')
if (boxChosen.isdigit()):
boxChosen = int(boxChosen)
else:
continue
if (boxChosen < 1 or boxChosen > 9):
continue
row, col = self.getCords(boxChosen)
if (self.__Board[row][col] in 'XO'):
print('Box Filled')
continue
self.__Board[row][col] = self.UserChar
self.lastMove = boxChosen
break
self.draw()
def getOutcomes(self, char: str, filter: bool = True):
board = self.__Board
char = self.UserChar if char == 'usr' else self.botChar
enemychar = 'O' if char == 'X' else 'X'
outcomes = []
for rows in board:
if (filter):
if (enemychar not in set(rows)):
outcomes.append(set(rows))
else:
outcomes.append(set(rows))
# Check Vertically
for i in range(1, 4):
vert = []
for j in range(i, 10, 3):
row, col = self.getCords(j)
vert.append(board[row][col])
if filter:
if (enemychar not in set(vert)):
outcomes.append(set(vert))
else:
outcomes.append(set(vert))
dr = [board[i][i] for i in range(3)]
dl = [board[i][2 - i] for i in range(3)]
if filter:
if (enemychar not in set(dr)):
outcomes.append(set(dr))
if (enemychar not in set(dl)):
outcomes.append(set(dl))
else:
outcomes.append(set(dr))
outcomes.append(set(dl))
# Sort the outcomes by the shortest length
outcomes = sorted(outcomes, key=len)
return outcomes
def botInit(self):
self.turn = self.UserChar
if (self.difficulty == 'Easy'):
return self.easyBot()
if (self.difficulty == 'Medium'):
return self.mediumBot()
if (self.difficulty == 'Hard'):
return self.hardBot()
self.draw()
def easyBot(self):
if (self.done):
return
board = self.__Board
print('Bot Choosing ...')
# Sleep for a bit so user can see what's happening
sleep(.5)
while (True):
randomBox = random.randint(1, 9)
row, col = self.getCords(randomBox)
if (board[row][col] in 'XO'):
continue
else:
self.lastMove = board[row][col]
self.__Board[row][col] = self.botChar
break
self.draw()
def mediumBot(self):
if (self.done):
return
if (self.round < 2):
return self.easyBot()
print('Bot Choosing ...')
# Sleep for a bit so user can see what's happening
sleep(.5)
outcomes = self.getOutcomes('bot')
if (len(outcomes) < 1):
return self.easyBot()
# Get the first outcome (which should be the best)
decided = [x for x in outcomes[0] if x.isdigit()][0]
self.lastMove = decided
# get row and column
row, col = self.getCords(int(decided))
# Apply the bot's character into the box
self.__Board[row][col] = self.botChar
self.draw()
def hardBot(self):
if (self.done):
return
print('Bot Choosing ...')
# Sleep for a bit so user can see what's happening
sleep(.5)
if (self.round == 1):
row, col = self.getCords(5)
if (self.UserChar not in self.__Board[row][col]):
self.__Board[row][col] = self.botChar
self.lastMove = '5'
self.draw()
return
else:
None
def getBox(_outcomes):
return [x for x in _outcomes[0] if x.isdigit()][0]
usrOutcomes = self.getOutcomes('usr')
botOutcomes = self.getOutcomes('bot')
# If the bot has a guaranteed win take the win
if (len(botOutcomes) > 0 and len(botOutcomes[0]) == 2):
winBox = getBox(botOutcomes)
row, col = self.getCords(int(winBox))
self.__Board[row][col] = self.botChar
self.lastMove = winBox
# OTHERWISE
# Check if the user have a guaranteed win
# if they do, block it
elif (len(usrOutcomes) > 0 and len(usrOutcomes[0]) == 2):
blockingBox = getBox(usrOutcomes)
row, col = self.getCords(int(blockingBox))
self.__Board[row][col] = self.botChar
self.lastMove = blockingBox
# OTHERWISE
# ? Code to check
# # If the bot character doesn't exist in the board
# elif (not any(self.botChar in sl for sl in self.__Board)):
# # If the user character exist in the board
# if (any(self.UserChar in sl for sl in self.__Board)):
# # Get all the blocking outcomes
# # Get all the outcomes unfiltered
# # Then get all the outcomes that have the user character in it
# blockingOutcomes = [list(x) for x in self.getOutcomes(self.UserChar, False) if self.UserChar in x ]
# # From all the blocking outcomes, pick a random one
# randomBlock = blockingOutcomes[random.randint(0, len(blockingOutcomes) - 1)]
# # Get the largest number (so it will pick the farthest block from the user)
# biggestNum = max([ int(x) for x in randomBlock if x.isdigit() ])
# # Get rows and columns
# row, col = self.getCords(biggestNum)
# # Apply
# self.__Board[row][col] = self.botChar
# self.lastMove = biggestNum
# # OTHERWISE (The user didn't start)
# else:
# # Run the medium bot
# self.mediumBot()
# # Just try to win
else:
self.mediumBot()
self.draw()
def move(self):
if (not self.done):
# If the user is the 'x'
if (self.UserChar == 'X'):
self.ask()
self.botInit()
else:
self.botInit()
self.ask()
self.round += 1
else:
return
def draw(self):
self.clear()
turn = 'Your' if self.turn == self.UserChar else 'Bot\'s'
print(f' It\'s {turn} turn ')
print('=======================')
print()
for idx, Row in enumerate(self.__Board):
# Top Part
print(f'{" ":^7}|{" ":^7}|{" ":^7}', end="")
print()
# Second Part | This is where the Character lives
print(f'{Row[0]:^7}|{Row[1]:^7}|{Row[2]:^7}', end="")
print()
# Bottom Part
print(f'{" ":^7}|{" ":^7}|{" ":^7}', end="")
print()
# If it's not the last line, print the separator
if (idx < 2):
print('-------+-------+-------')
if (self.lastMove != ''):
lastOne = "X" if self.turn == "O" else "O"
print('+-------------------+')
print(f'|{" "*19}|')
print(f'| {"You" if lastOne == self.UserChar else "Bot":^1} chose box {self.lastMove:^1} |')
print(f'|{" "*19}|')
print('+-------------------+')
print()
if (self.round > 2):
self.check()
def won(self, charWon):
print(f'{charWon} WON!')
if charWon == self.botChar:
self.rickRoll()
self.done = True
def checkLine(self, line):
# Put it in a set which removes duplicates
lineSet = set(line)
if (len(lineSet) > 1):
return None
winning = 'X' if 'X' in lineSet else 'O'
self.won(winning)
def check(self):
# Copy the current board array
board = self.__Board
# Check each row if there is a winner
for rows in board:
self.checkLine(rows)
for i in range(1, 4):
vert = []
for j in range(i, 10, 3):
row, col = self.getCords(j)
vert.append(board[row][col])
self.checkLine(vert)
# These 2 lines will check diagonally
self.checkLine([board[i][i] for i in range(3)])
self.checkLine([board[i][2 - i] for i in range(3)])
tie = True
# Check for ties
for r in board:
for c in r:
if c.isdigit():
tie = False
if tie:
print('Tie')
self.done = True
self.rickRoll()
return