-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.py
144 lines (122 loc) · 4.09 KB
/
tictactoe.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from os import system # Used for clearing the outputs
from time import sleep # Used for delay
# Initializing board
row1 = [' ', ' ', ' ']
row2 = [' ', ' ', ' ']
row3 = [' ', ' ', ' ']
next = 0 # Indicates next player
x1 = False # Player1 is X
x2 = False # Player2 is X
# Clears the outputs
def clear():
_ = system('clear') # Change to 'cls' if running on windows
# Starts the game, asks what will be player1's symbol
def beginGame():
global next, x1, x2, name1, name2
print('\nWelcome to Tic Tac Toe!\n')
choice = 'wrong' # Placeholder value
name1 = input('Player 1: Insert your name: ')
name2 = input('Player 2: Insert your name: ')
while choice not in ['O', 'X', 'o', 'x']:
choice = input(f'\n{name1}: Do you want to be X or O? ')
if choice.lower() == 'x':
print(f'{name1} will go first.')
next = 1
x1 = True
elif choice.lower() == 'o':
print(f'{name2} will go first.')
next = 2
x2 = True
else:
continue
# Prints and updates the board
def show_board():
sleep(0.5)
clear()
print(f'\n {row1[0]} | {row1[1]} | {row1[2]} ')
print('\n' + '------------------')
print(f'\n {row2[0]} | {row2[1]} | {row2[2]} ')
print('\n' + '------------------')
print(f'\n {row3[0]} | {row3[1]} | {row3[2]} ')
# lets each player decide where to draw an X or O
def turn(name, marker):
global row1, row2, row3
turn = 'wrong' # Placeholder value
while True:
turn = input(f'\n{name}: choose your position ({marker}): (1-9)\n')
if turn.isdigit() == False: # Check if the input is not a digit
print('\nInvalid input, try again\n')
continue
turn = int(turn)
# Updates the values of the board
if turn in range(1, 4):
row3[turn-1] = marker
break
elif turn in range(4, 7):
row2[turn-4] = marker
break
elif turn in range(7, 10):
row1[turn-7] = marker
break
else:
print('\nInvalid input, try again\n')
# Check if someone won the game
def check_win(mark):
return ((row1[0] == mark and row1[1] == mark and row1[2] == mark) or
(row2[0] == mark and row2[1] == mark and row2[2] == mark) or
(row3[0] == mark and row3[1] == mark and row3[2] == mark) or
(row1[0] == mark and row2[0] == mark and row3[0] == mark) or
(row1[1] == mark and row2[1] == mark and row3[1] == mark) or
(row1[2] == mark and row2[2] == mark and row3[2] == mark) or
(row1[0] == mark and row2[1] == mark and row3[2] == mark) or
(row1[2] == mark and row2[1] == mark and row3[0] == mark))
# Check if the board is full
def check_full():
for x in row1:
if x == ' ':
return False
for x in row2:
if x == ' ':
return False
for x in row3:
if x == ' ':
return False
return True
beginGame()
# Runs the game until someone wins or the board is full
while True:
show_board()
if next == 1 and x1:
turn(name1, 'X')
next = 2
elif next == 1 and x2:
turn(name1, 'O')
next = 2
elif next == 2 and x2:
turn(name2, 'X')
next = 1
else:
turn(name2, 'O')
next = 1
if check_win('X'):
if x1:
show_board()
print(f'\n{name1} is the winner !!!')
break
else:
show_board()
print(f'\n{name2} is the winner !!!')
break
elif check_win('O'):
if x2:
show_board()
print(f'\n{name1} is the winner !!!')
break
else:
show_board()
print(f'\n{name2} is the winner !!!')
break
elif check_full():
show_board()
print('\nGame over !!!')
break