-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
60 lines (55 loc) · 1.85 KB
/
game.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
board = [0, 1, 2,
3, 4, 5,
6, 7, 8]
win = False
def displayBoard():
#display board neatly on command line
print(board[0], '|', board[1], '|', board[2])
print('----------')
print(board[3], '|', board[4], '|', board[5])
print('----------')
print(board[6], '|', board[7], '|', board[8])
def whoWon():
winStates = [['x', 'x', 'x'], ['o', 'o', 'o']]
boardStates = [[board[0], board[1], board[2]], [board[3], board[4], board[5]], [board[6], board[7], board[8]], [board[0], board[3], board[6]], [board[1], board[4], board[7]], [board[2], board[5], board[8]], [board[0], board[4], board[8]], [board[2], board[4], board[6]]]
for i in range(0, 8):
for j in range(0, 2):
if winStates[j] == boardStates[i]:
return [True, winStates[j]]
return [False, None]
displayBoard()
while True:
while True:
try:
print('Player 1 pick a spot on the board: ')
spot = int(input())
if (board[spot] != 'x' and board[spot] != 'o'):
board[spot] = 'x'
displayBoard()
break
else:
print('\nSpot is taken\n')
except Exception as e:
print('Wrong input')
displayBoard()
winner = whoWon()
if winner[0]:
print(winner[1][0], 'won!')
break
while True:
try:
print('Player 2 pick a spot on the board: ')
spot = int(input())
if (board[spot] != 'x' and board[spot] != 'o'):
board[spot] = 'o'
displayBoard()
break
else:
print('\nSpot is taken\n')
except Exception as e:
print('Wrong input')
displayBoard()
winner = whoWon()
if winner[0]:
print(winner[1], 'won!')
break