There are many optimal ways but i corrected it by a simple method
#You can ignore this. This part just sets up up some elements we're going to use during the game
import random
Availablespaces = ["11", "12", "13", "21", "22", "23", "31", "32", "33"]
Availablespacescounter = len(Availablespaces)
PlayerSpaces = [""]
CpuSpaces = [""]
PlayerVictory = False
CpuVictory = False
#Loops the game until the board runs out of spaces or someone wins
while True:
if Availablespacescounter >0:
#User's turn
print("Available Spaces:", Availablespaces)
PlayerX = input("Enter the X coordinate for your piece: ")
PlayerY = input("Enter the Y coordinate for your piece: ")
if PlayerX == "1" and PlayerY == "1" and "11" in Availablespaces:
Availablespaces.remove("11")
PlayerSpaces.append("11")
elif PlayerX == "1" and PlayerY == "2" and "12" in Availablespaces:
Availablespaces.remove("12")
PlayerSpaces.append("12")
elif PlayerX == "1" and PlayerY == "3" and "13" in Availablespaces:
Availablespaces.remove("13")
PlayerSpaces.append("13")
elif PlayerX == "2" and PlayerY == "1" and "21" in Availablespaces:
Availablespaces.remove("21")
PlayerSpaces.append("21")
elif PlayerX == "2" and PlayerY == "2" and "22" in Availablespaces:
Availablespaces.remove("22")
PlayerSpaces.append("23")
elif PlayerX == "2" and PlayerY == "3" and "23" in Availablespaces:
Availablespaces.remove("23")
PlayerSpaces.append("23")
elif PlayerX == "3" and PlayerY == "1" and "31" in Availablespaces:
Availablespaces.remove("31")
PlayerSpaces.append("31")
elif PlayerX == "3" and PlayerY == "2" and "32" in Availablespaces:
Availablespaces.remove("32")
PlayerSpaces.append("32")
elif PlayerX == "3" and PlayerY == "3" and "33" in Availablespaces:
Availablespaces.remove("33")
PlayerSpaces.append("33")
print("You chose the space:", PlayerX, PlayerY)
#Checks if the user has gotten three in-a-row yet
if "11" in PlayerSpaces and "12" in PlayerSpaces and "13" in PlayerSpaces:
print("You won!")
Availablespacescounter = 0
break
elif "21" in PlayerSpaces and "22" in PlayerSpaces and "23" in PlayerSpaces:
print("You won!")
Availablespacescounter = 0
break
elif "31" in PlayerSpaces and "32" in PlayerSpaces and "33" in PlayerSpaces:
print("You won!")
Availablespacescounter = 0
break
elif "11" in PlayerSpaces and "21" in PlayerSpaces and "31" in PlayerSpaces:
print("You won!")
Availablespacescounter = 0
break
elif "12" in PlayerSpaces and "22" in PlayerSpaces and "32" in PlayerSpaces:
print("You won!")
Availablespacescounter = 0
break
elif "13" in PlayerSpaces and "23" in PlayerSpaces and "33" in PlayerSpaces:
print("You won!")
Availablespacescounter = 0
break
elif "11" in PlayerSpaces and "22" in PlayerSpaces and "33" in PlaySpaces:
print("You won!")
Availablespacescounter = 0
break
elif "13" in PlayerSpaces and "22" in PlayerSpaces and "31" in PlayerSpaces:
print("You won!")
Availablespacescounter = 0
break
#The computer's turn
CpuChoice = random.choice(Availablespaces)
#Compares the computer's choice to the list available spaces and removes items acordingly
if CpuChoice == "11":
Availablespaces.remove("11")
CpuSpaces.append("11")
print("The computer chose the space: 1, 1")
elif CpuChoice == "12":
Availablespaces.remove("12")
CpuSpaces.append("12")
print("The computer chose the space: 1, 2")
elif CpuChoice == "13":
Availablespaces.remove("13")
CpuSpaces.append("13")
print("The computer chose the space: 1, 3")
elif CpuChoice == "21":
Availablespaces.remove("21")
CpuSpaces.append("21")
print("The computer chose the space: 2, 1")
elif CpuChoice == "22":
Availablespaces.remove("22")
CpuSpaces.append("22")
print("The computer chose the space: 2, 2")
elif CpuChoice == "23":
Availablespaces.remove("23")
CpuSpaces.append("23")
print("The computer chose the space: 2, 3")
elif CpuChoice == "31":
Availablespaces.remove("31")
CpuSpaces.append("31")
print("The computer chose the space: 3,1")
elif CpuChoice == "32":
Availablespaces.remove("32")
CpuSpaces.append("32")
print("The computer chose the space: 3, 2")
elif CpuChoice == "33":
Availablespaces.remove("33")
CpuSpaces.append("33")
print("The computer chose the space: 3, 3")
#Checks to see if the computer has gotten 3 in-a-row yet
if "11" in CpuSpaces and "12" in CpuSpaces and "13" in CpuSpaces:
print("The computer won!")
Availablespacescounter = 0
break
elif "21" in CpuSpaces and "22" in CpuSpaces and "23" in CpuSpaces:
print("The computer won!")
Availablespacescounter = 0
break
elif "31" in CpuSpaces and "32" in CpuSpaces and "33" in CpuSpaces:
print("The computer won!")
Availablespacescounter = 0
break
elif "11" in CpuSpaces and "21" in CpuSpaces and "31" in CpuSpaces:
print("The computer won!")
Availablespacescounter = 0
break
elif "12" in CpuSpaces and "22" in CpuSpaces and "32" in CpuSpaces:
print("The computer won!")
Availablespacescounter = 0
break
elif "13" in CpuSpaces and "23" in CpuSpaces and "33" in CpuSpaces:
print("The computer won!")
Availablespacescounter = 0
break
elif "11" in CpuSpaces and "22" in CpuSpaces and "33" in CpuSpaces:
print("The computer won!")
Availablespacescounter = 0
break
elif "13" in CpuSpaces and "22" in CpuSpaces and "31" in CpuSpaces:
print("The computer won!")
Availablespacescounter = 0
break
else :
break