Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can anyone help me fix this game I made?

+13 votes
asked Mar 26, 2021 by Michael Gentry (270 points)
reshown Mar 28, 2021 by Michael Gentry

So I made this little Tic-Tac-Toe game in Python for fun during spare timehttps://onlinegdb.com/ByLi90jEd (Disclaimer: Yes, I know, this code is repetitive and garbage). It doesn't seem to tell you when you win. It will only tell you when the computer wins. Why? How do I fix this?

4 Answers

+3 votes
answered Mar 29, 2021 by Peter Minarik (55,660 points)
selected Mar 29, 2021 by Michael Gentry
 
Best answer

In line 10, change the right-shift operator (>>) to greater-than operator (>).

while Availablespacescounter > 0:

With this, the code seems to work for me.

commented Mar 29, 2021 by Michael Gentry (270 points)
Thanks for the answer! :)
0 votes
answered Apr 5, 2021 by Kyzer Maynard (210 points)
hey Michael it still didn't tell me when I won diagonally
0 votes
answered May 25, 2021 by Shivam Tiwari (180 points)
Use .copy method for AvailableSpaces and save it as  temp list. It's short , you can avoid repetetion by list indexing.

https://www.onlinegdb.com/fork/ByLi90jEd
0 votes
answered Jun 28, 2021 by ITSME (740 points)

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
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...