-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
166 lines (129 loc) · 5.14 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import random
board = [["?","?","?"],["?","?","?"],["?","?","?"]]
def boardDisplay(board): # Displays the board in terminal
print(board[0][0] + "|" + board[1][0] + "|" + board[2][0])
print("—————")
print(board[0][1] + "|" + board[1][1] + "|" + board[2][1])
print("—————")
print(board[0][2] + "|" + board[1][2] + "|" + board[2][2])
def boardUpdate(board, move_x, move_y, letter): # Adds move to board
board[move_x][move_y] = letter
def checkWinner():
i = 0
while i <= 2:
if board[i][0] == board[i][1] == board[i][2] != "?": # Checks if three are in a row vertically
return True
elif board[0][i] == board[1][i] == board[2][i] != "?": # Check if three are in a row horizontally
return True
elif board[0][0] == board[1][1] == board[2][2] != "?": # Checks the top left to bottom right diagonal
return True
elif board[0][2] == board[1][1] == board[2][0] != "?" : # Checks the bottom left to top right diagonal
return True
else:
i += 1
return False
def checkDraw(): # Checks if game ended in draw
return not any("?" in subboard for subboard in board)
print("The coordinates start at [0][0] at the top left square to [2][2] in the bottom right.")
def computer():
game = True
while game: # game loop
if checkDraw():
print("Draw.")
game += False
break
boardDisplay(board)
while True:
try:
move_x1 = int(input("Player 1, enter your x coordinate:"))
move_y1 = int(input("Player 1, enter your y coordinate:"))
if not 0 <= move_x1 <= 2 or not 0<= move_y1 <= 2: # checks if user input is valid
print("Enter integers between 0 and 2.")
elif not board[move_x1][move_y1] == "?": # checks if selected square has been occupied
print("That square is occupied. Retry.")
else:
break
except ValueError:
print("Enter integers between 0 and 2.")
print("Player 1 made their move.")
boardUpdate(board, move_x1, move_y1, "X")
boardDisplay(board)
if checkWinner():
print("Player 1 won.")
game += False
break
if checkDraw():
print("Draw.")
game += False
break
move_x2 = 0
move_y2 = 0
while board[move_x2][move_y2] != "?":
move_x2 = random.randint(0, 2)
move_y2 = random.randint(0, 2)
print("Computer made their move.")
boardUpdate(board, move_x2, move_y2, "O")
boardDisplay(board)
if checkWinner():
print("Computer won.")
game += False
break
print("-------------------------------------------------------------")
def two_player():
game = True
while game: # game loop
if checkDraw():
print("Draw.")
game += False
break
boardDisplay(board)
while True:
try:
move_x1 = int(input("Player 1, enter your x coordinate:"))
move_y1 = int(input("Player 1, enter your y coordinate:"))
if not 0 <= move_x1 <= 2 or not 0<= move_y1 <= 2: # checks if user input is valid
print("Enter integers between 0 and 2.")
elif not board[move_x1][move_y1] == "?": # checks if selected square has been occupied
print("That square is occupied. Retry.")
else:
break
except ValueError:
print("Enter integers between 0 and 2.")
print("Player 1 made their move.")
boardUpdate(board, move_x1, move_y1, "X")
boardDisplay(board)
if checkWinner():
print("Player 1 won.")
game += False
break
if checkDraw():
print("Draw.")
game += False
break
while True:
try:
move_x2 = int(input("Player 2, enter your x coordinate:"))
move_y2 = int(input("Player 2, enter your y coordinate:"))
if not 0 <= move_x2 <= 2 or not 0<= move_y2 <= 2:
print("Enter integers between 0 and 2.")
elif not board[move_x2][move_y2] == "?":
print("That square is occupied. Retry.")
else:
break
except ValueError:
print("Enter integers between 0 and 2.")
print("Player 2 made their move.")
boardUpdate(board, move_x2, move_y2, "O")
boardDisplay(board)
if checkWinner():
print("Player 2 won.")
game += False
break
print("-------------------------------------------------------------")
def menu():
option = input("Enter 1 for two players. Enter 2 to play against a computer:")
if option == "1":
two_player()
elif option == "2":
computer()
menu()