-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.py
117 lines (101 loc) · 2.81 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
def printBoard(gameMat):
"""
prints the current game
"""
print(gameMat[0]) #row 1
print(gameMat[1]) #row 2
print(gameMat[2]) #row 3
def checkWin(letter,board):
"""
algorithm to check for the wins
"""
#checks for horizontal wins
count = 0
for r in range(3):
count = 0
for k in range(3):
if board[r][k] == letter:
count = count + 1
if count == 3:
print(letter + " wins")
return False
#checks for vertical wins
count = 0
for k in range(3):
count = 0
for r in range(3):
if board[r][k] == letter:
count = count + 1
if count == 3:
print(letter + " wins")
return False
#checks for skewed right diagonals
count = 0
r = 0
for k in range(3):
if board[r][k] == letter:
count = count + 1
if count == 3:
print(letter + " wins")
return False
r = r + 1
#checks for skewed left diagonals
count = 0
r = 2
for k in range(3):
if board[r][k] == letter:
count = count + 1
if count == 3:
print(letter + " wins")
return False
r = r - 1
#checks for a tie in the game
count = 0
for r in range(3):
for k in range(3):
if board[r][k] != '-':
count = count + 1
if count == 9:
print(letter + " tie")
return False
return True
def game():
"""
changes the board to x and o
uses printBoard to update the users ui and checkWin to find winner
"""
#intializes the board
board = [['-','-','-'],['-','-','-'],['-','-','-']]
printBoard(board)
x = True #initialize x as True to loop the game, False when there is a winner
#user inputs
while x:
n = input("Please enter 'x' or 'o':")
while n.strip() != 'x' and n.strip() != 'o':
n = input("Please enter 'x' or 'o':")
m = input("Please enter position y:")
p = input("Please enter position x:")
if int(m)-1 < len(board) and int(p)-1 < len(board):
board[int(m)-1][int(p)-1] = n
printBoard(board)
x = checkWin(n,board)
w = input("rematch? yes?: ")
if w == "yes":
game()
else:
print("FINE THEN")
print("GOOD GAME BISH!!!!")
#message after user chooses the option to quit the game
def quitGame():
print("Hope you had fun!!")
#first initialized as the game's main menu
print("Tic Tac Toe")
print("play")
print("quit")
a = input("Chose: ")
while a != "play" and a != "quit":
a = input("Chose: ")
if a == "play":
game()
elif a == "quit":
quitGame()