-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic-tac-toe.py
86 lines (69 loc) · 2.26 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
78
79
80
81
82
83
84
85
86
def valid_moves(board):
valid = []
for slots in board:
try:
slot = int(slots)
valid.append(slot)
except:
continue
return valid
def is_valid_move(board, pos, player):
try:
section = board[pos]
except IndexError:
return False
if section == 'X' or section == 'O':
return False
return True
def move(board, pos, player):
''' '''
if player == 'Player 1':
board[pos] = 'X'
else:
board[pos] = 'O'
return board
def print_board(board):
print('+-----------+\n', end='')
print('| ', ' | '.join(board[0:3]), ' |', sep='')
print('|---|---|---|')
print('| ', ' | '.join(board[3:6]), ' |', sep='')
print('|---|---|---|')
print('| ', ' | '.join(board[6:9]), ' |', sep='')
print('+-----------+\n')
def is_game_over(board, winning_combo):
for combo in winning_combo:
sample = board[combo[0]]
if sample == board[combo[0]] and sample == board[combo[1]] and sample == board[combo[2]]:
if sample == 'X':
print('Player 1 has won!')
elif sample == 'O':
print('Player 2 has won!')
return True
return False
def main():
is_game_won = False
winning_combo = [(0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8),
(6, 4, 2), (3, 4, 5), (0, 1, 2), (6, 7, 8)]
board = ['0', '1', '2',
'3', '4', '5',
'6', '7', '8']
player = 'Player 1'
print_board(board)
valid = valid_moves(board)
while not is_game_won:
pos = int(input(player + ', ender index: '))
if is_valid_move(board, pos, player):
if player == 'Player 1':
move(board, pos, player)
player = 'Player 2'
else:
move(board, pos, player)
player = 'Player 1'
is_game_won = is_game_over(board, winning_combo)
valid = valid_moves(board)
if len(valid) == 0:
print('No one won.')
break
print_board(board)
if __name__ == '__main__':
main()