-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
78 lines (64 loc) · 2.78 KB
/
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
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
game_board = {'(1, 1)': ' ', '(1, 2)': ' ', '(1, 3)': ' ',
'(2, 1)': ' ', '(2, 2)': ' ', '(2, 3)': ' ',
'(3, 1)': ' ', '(3, 2)': ' ', '(3, 3)': ' '}
def print_board(game_board):
print(' 1 2 3')
print('1 ' + game_board['(1, 1)'] + '|' + game_board['(1, 2)'] + '|' + game_board['(1, 3)'])
print(' --+--+--')
print('2 ' + game_board['(2, 1)'] + '|' + game_board['(2, 2)'] + '|' + game_board['(2, 3)'])
print(' --+--+--')
print('3 ' + game_board['(3, 1)'] + '|' + game_board['(3, 2)'] + '|' + game_board['(3, 3)'])
def game():
turn = ' X'
count = 0
for i in range(10):
print_board(game_board)
print("Enter " + turn + " move (row,column no spaces)>")
move = str(tuple(map(int, raw_input().split(','))))
if str(game_board[move]) == ' ':
game_board[move] = turn
count += 1
else:
print("Invalid move, try again.")
continue
if count >= 5:
if game_board['(1, 1)'] == game_board['(1, 2)'] == game_board['(1, 3)'] != ' ':
print_board(game_board)
print(turn + " is the winner!")
break
elif game_board['(3, 2)'] == game_board['(2, 2)'] == game_board['(1, 2)'] != ' ':
print_board(game_board)
print(turn + " is the winner!")
break
elif game_board['(3, 3)'] == game_board['(2, 3)'] == game_board['(1, 3)'] != ' ':
print_board(game_board)
print(turn + " is the winner!")
break
elif game_board['(1, 1)'] == game_board['(2, 2)'] == game_board['(3, 3)'] != ' ':
print_board(game_board)
print(turn + " is the winner!")
break
elif game_board['(3, 1)'] == game_board['(2, 2)'] == game_board['(1, 3)'] != ' ':
print_board(game_board)
print(turn + " is the winner!")
break
elif game_board['(2, 1)'] == game_board['(2, 2)'] == game_board['(2, 3)'] != ' ':
print_board(game_board)
print(turn + " is the winner!")
break
elif game_board['(3, 1)'] == game_board['(3, 2)'] == game_board['(3, 3)'] != ' ':
print_board(game_board)
print(turn + " is the winner!")
break
elif game_board['(3, 1)'] == game_board['(2, 1)'] == game_board['(1, 1)'] != ' ':
print_board(game_board)
print(turn + " is the winner!")
break
if count == 9:
print("There are no more moves. It's a tie!")
if turn == ' X':
turn = ' O'
else:
turn = ' X'
if __name__ == "__main__":
game()