-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
312 lines (272 loc) · 10.8 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from UltimateMetaTTT import NumTicTacToe
from UltimateMetaTTT import ClassicTicTacToe
from UltimateMetaTTT import MetaTicTacToe
def playNum(num, player, numbersEntered, counter):
"""
This function asks the player for the row, column and input numbers for the numbers tic tac toe game. It
validates the chosen rows, columns, and inputs before returning them. This function differentiates between odds
and evens for player 1 and player 2 respectively.
Parameters: The current numbers tic tac toe board initialized earlier and the current player.
Returns: The row, column, and the input number (odd/even).
"""
notEmpty = True
odds = [1, 3, 5, 7, 9]
evens = [2, 4, 6, 8]
ask = True
while ask:
if counter % 2 != 0:
playerNumber = input('Player ' + str(player) + ', please enter an odd number (1-9): ')
elif counter % 2 == 0:
playerNumber = input('Player ' + str(player) + ', please enter an even number (2-8): ')
if playerNumber.isdigit():
playerNumber = int(playerNumber)
if counter % 2 != 0:
if playerNumber in odds and playerNumber not in numbersEntered:
numbersEntered.append(playerNumber)
ask = False
elif playerNumber in numbersEntered:
print("Error: that number has already been entered. ", end='')
else:
print("Error: entry not odd. ", end='')
elif counter % 2 == 0:
if playerNumber in evens and playerNumber not in numbersEntered:
numbersEntered.append(playerNumber)
ask = False
elif playerNumber in numbersEntered:
print("Error: that number has already been entered. ", end='')
else:
print("Error: entry not even. ", end='')
else:
print("Please enter a valid number!")
while notEmpty:
ask = True
while ask:
row = input('Player ' + str(player) + ', please enter a row: ')
if row.isdigit():
row = int(row)
if 0 <= row <= 2:
ask = False
else:
print("Error: column not in correct range.", end='')
else:
print("Error: row not in correct range.", end='')
ask = True
while ask:
column = input('Player ' + str(player) + ', please enter a column: ')
if column.isdigit():
column = int(column)
if 0 <= column <= 2:
ask = False
else:
print("Error: column not in correct range.", end='')
else:
print("Error: row not in correct range.", end='')
if num.squareIsEmpty(row, column):
return row, column, playerNumber
else:
print("Error: could not make move! This spot is already filled!")
def playClassic(classic, player):
"""
This function asks the player for row and column numbers for the classical tic tac toe game. It validates the chosen
rows and columns before returning them.
Parameters: The current classic tic tac toe board initialized earlier and the current player.
Returns: The row, column, and the corresponding player symbol.
"""
notEmpty = True
while notEmpty:
ask = True
while ask:
row = input('Player ' + str(player) + ', please enter a row: ')
if row.isdigit():
row = int(row)
if 0 <= row <= 2:
ask = False
else:
print("Error: row not in correct range.", end='')
else:
print("Error: row not in correct range.", end='')
ask = True
while ask:
column = input('Player ' + str(player) + ', please enter a column: ')
if column.isdigit():
column = int(column)
if 0 <= column <= 2:
ask = False
else:
print("Error: column not in correct range.", end='')
else:
print("Error: column not in correct range.", end='')
if classic.squareIsEmpty(row, column):
return row, column
else:
print("Error: could not make move! This spot is already filled!")
def classicGame(classic, player):
"""
This function initializes the classic tic tac toe game. It updates the classic tic tac toe board as the game
progresses and checks after every player turn if a player has won or if a tie has occurred.
Parameters: The classic game board that was initialized and the current player
Returns: the player who won or in the case of a tie it returns "tie"
"""
dashes = '----------------------------------'
ask = True
playerOne = 'X'
playerTwo = 'O'
counter = 1
print(dashes)
print('This is a Classical Tic Tac Toe.')
classic.drawBoard()
while ask:
currentPlayer = playClassic(classic, player)
currentPlayerRow = currentPlayer[0]
currentPlayerColumn = currentPlayer[1]
# currentPlayerNumber = currentPlayer[2]
if counter % 2 != 0:
classic.update(currentPlayerRow, currentPlayerColumn, playerOne)
elif counter % 2 == 0:
classic.update(currentPlayerRow, currentPlayerColumn, playerTwo)
classic.drawBoard()
if classic.isWinner():
print("Player " + str(player) + " wins. Congrats!")
return player
elif classic.boardFull() and not classic.isWinner():
print("Its a tie!")
winner = 'Tie'
return winner
else:
if player == 1:
player += 1
else:
player -= 1
counter += 1
def numGame(num, player):
"""
This function initializes the numbers tic tac toe game. It updates the numbers tic tac toe board as the game
progresses and checks after every player turn if a player has won or if a tie has occurred.
Parameters: The numbers game board that was initialized and the current player
Returns: the player who won or in the case of a tie it returns "tie"
"""
dashes = '----------------------------------'
ask = True
numbersEntered = []
counter = 1
print(dashes)
print('This is Numerical Tic Tac Toe.')
num.drawBoard()
while ask:
currentPlayer = playNum(num, player, numbersEntered, counter)
currentPlayerRow = currentPlayer[0]
currentPlayerColumn = currentPlayer[1]
currentPlayerNumber = currentPlayer[2]
counter += 1
num.update(currentPlayerRow, currentPlayerColumn, currentPlayerNumber)
num.drawBoard()
if num.isWinner():
print("Player " + str(player) + " wins. Congrats!")
return player
elif num.boardFull() and not num.isWinner():
print("Its a tie!")
winner = 'Tie'
return winner
else:
if player == 1:
player += 1
else:
player -= 1
def playerDeciding(num, classic, meta, player):
"""
This function asks the current player to choose a spot on the meta board and then runs the appropriate functions
based on the chosen square. The function also updates and draws the meta board once a player wins or ties a
sub-tic tac toe game.
Parameters: num and classic are the numbers/classic tic tac toe objects initialized in the main function, meta is
current state of the meta class object created at the very start of the game, and player is the current
player.
Returns: N/A
"""
winnerNum = ''
winnerClassic = ''
playerOneSymbol = 'X'
playerTwoSymbol = 'O'
correctRow = True
correctCol = True
while correctRow:
row = input('Player ' + str(player) + ', please enter a row: ')
if row.isdigit():
row = int(row)
if 0 <= row <= 2:
correctRow = False
else:
print("Please enter a valid row number from 0 to 2!")
else:
print("Please enter a valid row number from 0 to 2!")
while correctCol:
col = input('Player ' + str(player) + ', please enter a column: ')
if col.isdigit():
col = int(col)
if 0 <= col <= 2:
correctCol = False
else:
print("Please enter a valid column number from 0 to 2!")
else:
print("Please enter a valid column number from 0 to 2!")
if meta.squareIsEmpty(row, col):
if meta.getLocalBoard(row, col) == 'n':
winnerNum = numGame(num, player)
elif meta.getLocalBoard(row, col) == 'c':
winnerClassic = classicGame(classic, player)
else:
print('This spot is already filled!')
playerDeciding(num, classic, meta, player)
if winnerNum == 1 or winnerClassic == 1:
meta.update(row, col, playerOneSymbol)
meta.drawBoard()
elif winnerNum == 2 or winnerClassic == 2:
meta.update(row, col, playerTwoSymbol)
meta.drawBoard()
elif winnerClassic == 'Tie' or winnerNum == 'Tie':
tie = 'D'
print('', end='\n')
meta.update(row, col, tie)
meta.drawBoard()
def main():
# create a board (object) for each class
meta = MetaTicTacToe('MetaTTTconfig.txt')
dashes = '----------------------------------'
player = 1
# game setup
print(dashes)
print('Starting new Meta Tic Tac Toe game')
print(dashes)
meta.drawBoard()
# begin the game with player 1
ask = True
while ask:
if player == 1:
num = NumTicTacToe()
classic = ClassicTicTacToe()
playerDeciding(num, classic, meta, player)
player += 1
else:
num = NumTicTacToe()
classic = ClassicTicTacToe()
playerDeciding(num, classic, meta, player)
player -= 1
if meta.isWinner():
if player == 1:
print("Player " + str(player + 1) + " wins the Meta Tic Tac Toe game. GOOD GAME!")
return
elif player == 2:
print("Player " + str(player - 1) + " wins this game!")
return
elif meta.boardFull():
print("Its a tie!")
main()
if __name__ == "__main__":
while True:
playAgain = input("Do you want to player another game? (Y/N)")
if playAgain == "Y" or "N":
if playAgain == "Y":
main()
elif playAgain == "N":
print('Thanks for playing! Goodbye.')
else:
print("Please enter 'Y' for yes or 'N' for no")