-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTic_tac_toe.py
251 lines (203 loc) · 8.49 KB
/
Tic_tac_toe.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
# coding=utf-8
import random
from game_log import log
def showGameRules():
# This function prints out the rules at the beginning of the game
print("Tic Tac Toe!\n\n")
print("RULES...................\n\n")
print("1. Two players select spaces on the 3×3 grid. The two players alternate when choosing spaces. (First turn should be selected randomly.)\n\n")
print("2. If a player gets three in a row horizontally, vertically, or diagonally in any fashion, this player wins the game.\n\n")
print("3. If the game continues until all the spaces are selected and neither player has three in a row, then the match is a draw/push.\n\n")
print("To select a space, type one of the corresponding indices:\n\n1 | 2 | 3\n---------\n4 | 5 | 6\n---------\n7 | 8 | 9\n")
def askPlayerName(i):
# This function asks for a player name and returns it
# only error is if the player doesent enter a name at all
playerName = input("\nPlayer "+str(i)+" enter your name: ")
while playerName == "":
print("Invalid name entered")
playerName = input("Player "+str(i)+" enter your name: ")
return playerName
def firstMove(player0, player1):
# This function takes the two players as parameters and chooses and returns the first player at random
if(random.randint(0, 1) == 0):
return player0
else:
return player1
def playerPickSymbol(first):
# This function takes the first player as a parameter nd then returns the symbol they choose
playerSymbol = input("\n"+first+", you go first! Type 'X' or 'O' to choose your symbol: ")
# while the player hasnt entered any of the desired symbols
while playerSymbol != "X" and playerSymbol != "x" and playerSymbol != "O" and playerSymbol != "o":
print("\nInvalid Symbol entered. Please type 'X' or 'O':" )
playerSymbol = input("\n"+first+", you go first! Type 'X' or 'O' to choose your symbol: ") # ask them again until they enter a correct symbol
return playerSymbol
def answer():
answer = playerPickSymbol("player")
return answer
def placePiece(player, i, symbols, board):
# This function takes the player, their symbol index, the list of symbols, and the board list as parameters
# The user chooses their space, and the function adds the selection to the board list and returns the board list
#error handling
valid = False
while(not valid):
taken = True
selection = ""
try:
selection = int(input("\nPlease enter the index of the space you would like to select: "))
if(board[selection-1] != " "):
print("\nSorry! This space is already taken. Please take another look at the board and pick a different space.")
outputGameBoard(board)
else:
board[selection-1] = symbols[i]
taken = False
except(ValueError, IndexError):
print("\nSorry! The integer you have entered is not valid or the space is already taken. Please"+
" make sure you are entering an integer between 1 and 9.")
outputGameBoard(board)
if(taken == False):
valid = True
log.info("selection: "+str(selection))
return board
def outputGameBoard(board):
# This function takes the board list as a parameter and prints out the current state of the game board
print("\n", board[0], "|", board[1], "|", board[2], "\n----------\n", board[3], "|",
board[4], "|", board[5], "\n----------\n", board[6], "|", board[7], "|", board[8])
def checkWinner(board, symbols, player0, player1):
# This function takes the board, symbols list, and both players as parameters, checks the game board to see if there is a winner,
# and outputs and returns the winner if there is one
if(board[0] == board[1] == board[2] != " "):
if(board[1] == symbols[0]):
print("\n"+player0, "has won!")
else:
print("\n"+player1, "has won!")
return True
if(board[2] == board[5] == board[8] != " "):
if(board[5] == symbols[0]):
print("\n"+player0, "has won!")
else:
print("\n"+player1, "has won!")
return True
if(board[1] == board[4] == board[7] != " "):
if(board[4] == symbols[0]):
print("\n"+player0, "has won!")
else:
print("\n"+player1, "has won!")
return True
if(board[3] == board[4] == board[5] != " "):
if(board[4] == symbols[0]):
print("\n"+player0, "has won!")
else:
print("\n"+player1, "has won!")
return True
if(board[6] == board[7] == board[8] != " "):
if(board[7] == symbols[0]):
print("\n"+player0, "has won!")
else:
print("\n"+player1, "has won!")
return True
if(board[0] == board[3] == board[6] != " "):
if(board[0] == symbols[0]):
print("\n"+player0, "has won!")
else:
print("\n"+player1, "has won!")
return True
if(board[0] == board[4] == board[8] != " "):
if(board[4] == symbols[0]):
print("\n"+player0, "has won!")
else:
print("\n"+player1, "has won!")
return True
if(board[2] == board[4] == board[6] != " "):
if(board[4] == symbols[0]):
print("\n"+player0, "has won!")
else:
print("\n"+player1, "has won!")
return True
return False
def fullBoard(board):
# This function takes the game board list as a parameter and returns a boolean for if the board is full or not
count = 0
for space in board:
if(space == " "):
break
else:
count += 1
if(count == 9):
print("\nThe board is full so the game ends in a tie.")
return True
return False
def playAgain():
# This function asks the user if they want to play again and returns their answer asn a boolean
valid = True # error handling
validInputs = ['p', 'q']
while(valid): # handles error --> invalid input
again = input("\nType 'p' to play again or type 'q' to quit: ")
if(again.lower() not in validInputs):
print("\n")
else:
if(again.lower() == 'p'):
return True
else:
print("\nThanks for playing!")
valid = False
return False
def play_tic_tac_toe():
# main function from which all other functions are called
# runs through the tic tac toe game in proper order
again = True
while(again):
showGameRules()
player0 = askPlayerName(0)
player1 = askPlayerName(1)
i = 3
first = player1
if(player0 == firstMove(player0, player1)):
i = 2
first = player0
firstSymbol = playerPickSymbol(first).lower()
symbols = [-1, -1]
if(first == player0):
if(firstSymbol == 'x'):
symbols[0] = 'X'
symbols[1] = 'O'
else:
symbols[1] = 'X'
symbols[0] = 'O'
else:
if(firstSymbol == 'x'):
symbols[1] = 'X'
symbols[0] = 'O'
else:
symbols[0] = 'X'
symbols[1] = 'O'
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
full = True
winner = True
turn = 0
while(winner and turn < 9):
if(i % 2 == 0):
board = placePiece(player0, 0, symbols, board)
current = player0
else:
board = placePiece(player1, 1, symbols, board)
current = player1
outputGameBoard(board)
log.info("current player: "+current)
turn = turn + 1
i = i + 1
if(checkWinner(board, symbols, player0, player1)):
winner = False
turn = 9
if(fullBoard(board)):
full = False
winner = False
turn = 9
log.info("winner? "+str(not winner))
log.info("full? "+str(not full))
if(playAgain() is not True):
again = False
break
def main():
play_tic_tac_toe() # starts a game of tic tac toe
if __name__ == "__main__":
main()