I just got all the bugs fixed and my code is not the best. So, how can I restructure my tic-tac-toe code so that it does not take as many resources while being readable?
#!/usr/bin/env python3
#Create the grid.
def drawGrid(grid):
#Show the grid to the user.
for rows in grid:
print("+---+---+---+")
#Print each item in the row.
for row in rows:
print(row, end='')
#Formatting
print()
print("+---+---+---+")
#Edit the Tic Tac Toe grid.
def editGrid(grid, selectedRow, selectedColumn, player):
row = grid[selectedRow - 1]
#Determine which player is editing the
#grid.
if player == " X ":
#Determine what to do based on the column selected.
if selectedColumn == 1:
#Check to see if a player is in the spot selected.
if row[selectedColumn] == " O " or row[selectedColumn] == " X ":
print("\nA player is already there.\n")
else:
row[selectedColumn] = player
elif selectedColumn == 2:
if row[selectedColumn + 1] == " O " or row[selectedColumn + 1] == " X ":
print("\nA player is already there.\n")
else:
row[selectedColumn + 1] = player
elif selectedColumn == 3:
if row[selectedColumn + 2] == " O " or row[selectedColumn + 2] == " X ":
print("\nA player is already there.\n")
else:
row[selectedColumn + 2] = player
else:
#Determine what to do based on the column selected.
if selectedColumn == 1:
#Check to see if a player is in the spot selected.
if row[selectedColumn] == " O " or row[selectedColumn] == " X ":
print("\nA player is already there.\n")
else:
row[selectedColumn] = player
elif selectedColumn == 2:
if row[selectedColumn + 1] == " O " or row[selectedColumn + 1] == " X ":
print("\nA player is already there.\n")
else:
row[selectedColumn + 1] = player
elif selectedColumn == 3:
if row[selectedColumn + 2] == " O " or row[selectedColumn + 2] == " X ":
print("\nA player is already there.\n")
else:
row[selectedColumn + 2] = player
#Determine if there are any winners.
def checkWinner(grid, player):
if (grid[0][1] == player and grid[0][3] == player) and grid[0][5] == player:
print("\n" + player + "wins!")
return True
elif (grid[1][1] == player and grid[1][3] == player) and grid[1][5] == player:
print("\n" + player + "wins!")
return True
elif (grid[2][1] == player and grid[2][3] == player) and grid[2][5] == player:
print("\n" + player + "wins!")
return True
elif (grid[0][1] == player and grid[1][3] == player) and grid[2][5] == player:
print("\n" + player + "wins!")
return True
elif (grid[0][3] == player and grid[1][3] == player) and grid[2][3] == player:
print("\n" + player + "wins!")
return True
elif (grid[0][5] == player and grid[1][5] == player) and grid[2][5] == player:
print("\n" + player + "wins!")
return True
elif (grid[0][1] == player and grid[1][3] == player) and grid[2][5] == player:
print("\n" + player + "wins!")
return True
elif (grid[0][5] == player and grid[1][3] == player) and grid[2][1] == player:
print("\n" + player + "wins!")
return True
elif (grid[0][1] == player and grid[1][1] == player) and grid[2][1] == player:
print("\n" + player + "wins!")
return True
else:
return False
#Ask the user which column to occupy.
def getRow():
while True:
#Catch any invalid data.
try:
row = int(input("Pick a row (1, 2, 3): "))
#Check the validity of the input
#data.
while row < 1 or row > 3:
print("Please enter an integer bewteen 1 and 3. Try again.")
row = int(input("Pick a row (1, 2, 3): "))
return row
except ValueError:
print("Please input an integer between 1 and 3. Try again.")
#Ask the user which column to occupy.
def getColumn():
while True:
#Catch any invalid data.
try:
column = int(input("Pick a row (1, 2, 3): "))
#Check the validity of the input
#data.
while column < 1 or column > 3:
print("Please enter an integer bewteen 1 and 3. Try again.")
column = int(input("Pick a row (1, 2, 3): "))
return column
except ValueError:
print("Please input an integer between 1 and 3. Try again.")
def main():
print("Welcome to Tic Tac Toe\n")
#Assume the user wants to play.
userChoice = "y"
while userChoice.lower() == "y":
#Form the grid
grid = [["|"," ","|", " ","|", " ","|"],
["|"," ","|", " ","|", " ","|"],
["|"," ","|", " ","|", " ","|"]]
drawGrid(grid)
#Set the default number of turns.
numberOfTurns = 1
isWon= False
isTie = False
#Run until someone wins or it ties.
while isWon == False and isTie == False:
#Catch any errors with the input.
if numberOfTurns % 2 != 0:
print("\nX's Turn")
#Set who goes this turn and what
#marker to use.
player = " X "
#Get input.
row = getRow()
column = getColumn()
else:
print("\nO's Turn")
#Set who goes this turn and what
#marker to use.
player = " O "
#Get input.
row = getRow()
column = getColumn()
print()
#Edit the gird with the values given.
editGrid(grid, row, column, player)
#Draw the edited grid.
drawGrid(grid)
#Determine if the programs needs to terminate.
isWon = checkWinner(grid, player)
#Check the status of the game.
if isWon == False and isTie == False:
#Keep track of how many spaces
#are taken up.
tokenCounter = 0
for rows in grid:
for space in rows:
#Determine if all there spaces
#are taken up.
if space == " X " or space == " O ":
tokenCounter += 1
#Determine if there was a tie.
if tokenCounter == 9 and isWon == False:
isTie = True
print("\nIt is a tie!\n")
#Next players goes.
numberOfTurns += 1
print("Round over!\n")
userChoice = input("Do you want to play another round? (y / n) ")
while userChoice.lower() != "y" and userChoice.lower() != "n":
print("Please type \"y\" or \"n\". Try again.")
userChoice = input("Do you want to play another round? (y / n) ")
print()
print("Bye!")
if __name__ == "__main__":
main()