-
Notifications
You must be signed in to change notification settings - Fork 22
/
tic_tac_toe.py
35 lines (33 loc) · 917 Bytes
/
tic_tac_toe.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
squares = [' ']*9
players = 'XO'
board = '''
0 1 2
{0} | {1} | {2}
-----------
3 {3} | {4} | {5} 5
-----------
{6} | {7} | {8}
6 7 8
'''
win_conditions = [
(0, 1, 2), (3, 4, 5), (6, 7, 8), # horizontals
(0, 3, 6), (1, 4, 7), (2, 5, 8), # verticals
(0, 4, 8), (2, 4, 6) # diagonals
]
def check_win(player):
for a, b, c in win_conditions:
if {squares[a], squares[b], squares[c]} == {player}:
return True
while True:
print(board.format(*squares))
if check_win(players[1]):
print(f'{players[1]} is the winner!')
break
if ' ' not in squares:
print('Cats game!')
break
move = input(f'{players[0]} to move [0-8] > ')
if not move.isdigit() or not 0 <= int(move) <= 8 or squares[int(move)] != ' ':
print('Invalid move!')
continue
squares[int(move)], players = players[0], players[::-1]