-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtic_tac_toe.py
More file actions
62 lines (49 loc) · 1.46 KB
/
tic_tac_toe.py
File metadata and controls
62 lines (49 loc) · 1.46 KB
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
from os import system
board = [str(i) + " " for i in range(1, 10)]
user = '❌'
win_lines = [
[0, 1, 2], [0, 3, 6],
[3, 4, 5], [1, 4, 7],
[6, 7, 8], [2, 5, 8],
[2, 4, 6], [0, 4, 8]
]
def show_board():
print("┌──┬──┬──┐")
for i in range(3):
print(f"│{board[i*3]}│{board[i*3+1]}│{board[i*3+2]}│")
if (i != 2):
print("├──┼──┼──┤")
print("└──┴──┴──┘")
def prompt():
answer = int(input('Position: '))
while (1 > answer or answer > 9):
answer = int(input('Try again: '))
return answer-1
def set_board(index):
if board[index] != '❌' and board[index] != '⭕':
board[index] = user
return True
return False
def changeTurn(user):
return '⭕' if user == '❌' else "❌"
def did_win(user, board, win_lines):
for line in win_lines:
if board[line[0]]== user and board[line[1]]== user and board[line[2]]== user:
return True
return False
turns = 0
while(True):
system('cls')
print('Turn: ' + user)
show_board()
if turns >= 9:
print("It's a draw. Both of you suck!")
break
if set_board(prompt()):
if (did_win(user, board, win_lines)):
system('cls')
show_board()
print(user + ' won!')
break
user = changeTurn(user)
turns += 1