Search code examples
pythonturtle-graphicstic-tac-toe

Tic-Tac-Toe problems?


So for my last assignment for my foundations of programming class I'm taking for school you were supposed to use the tools we have learned in class to create a program of your choosing. One of the ideas was rock paper scissors but I decided that was too basic so I tried to make a Tic-Tac-Toe game.

I finally finished and I know there's many better ways I could of programmed this for it to be simpler, less repetitive, more efficient, etc. but there's two main problems with it I want to see if they can be fixed.

I have a problem that a player can go on the same spaces on the board even if that space is taken. I was thinking maybe if I made a board list that recorded where people have gone on the board and then check where they are trying to go against this which I could do the first part but I don't know how I would check it against where they are trying to go.

I also had a problem that once win was true it will still run the rest of the program until it gets back to the start of the while command again. I understand why it does this but is there a way to bypass this or have it automatically stop when while is "true".

I tried to see if two different while commands might work but as I thought it just made more problems. Thanks for the help!

#Jacob Gilger
#4/20/17
#Drawing a house with elif

import turtle

def X1(t):
    t.penup()
    t.setposition(-95,10)
    t.pendown()
    t.left(45)
    t.forward(50)
    t.penup()
    t.setposition(-60,10)
    t.pendown()
    t.left(90)
    t.forward(50)
    t.right(135)    
def X2(t):
    t.penup()
    t.setposition(-45,10)
    t.pendown()
    t.left(45)
    t.forward(50)
    t.penup()
    t.setposition(-10,10)
    t.pendown()
    t.left(90)
    t.forward(50)
    t.right(135)
def X3(t):
    t.penup()
    t.setposition(5,10)
    t.pendown()
    t.left(45)
    t.forward(50)
    t.penup()
    t.setposition(40,10)
    t.pendown()
    t.left(90)
    t.forward(50)
    t.right(135)
def X4(t):
    t.penup()
    t.setposition(-95,60)
    t.pendown()
    t.left(45)
    t.forward(50)
    t.penup()
    t.setposition(-60,60)
    t.pendown()
    t.left(90)
    t.forward(50)
    t.right(135)    
def X5(t):
    t.penup()
    t.setposition(-45,60)
    t.pendown()
    t.left(45)
    t.forward(50)
    t.penup()
    t.setposition(-10,60)
    t.pendown()
    t.left(90)
    t.forward(50)
    t.right(135)
def X6(t):
    t.penup()
    t.setposition(5,60)
    t.pendown()
    t.left(45)
    t.forward(50)
    t.penup()
    t.setposition(40,60)
    t.pendown()
    t.left(90)
    t.forward(50)
    t.right(135)    
def X7(t):
    t.penup()
    t.setposition(-95,110)
    t.pendown()
    t.left(45)
    t.forward(50)
    t.penup()
    t.setposition(-60,110)
    t.pendown()
    t.left(90)
    t.forward(50)
    t.right(135)
def X8(t):
    t.penup()
    t.setposition(-45,110)
    t.pendown()
    t.left(45)
    t.forward(50)
    t.penup()
    t.setposition(-10,110)
    t.pendown()
    t.left(90)
    t.forward(50)
    t.right(135)    
def X9(t):
    t.penup()
    t.setposition(5,110)
    t.pendown()
    t.left(45)
    t.forward(50)
    t.penup()
    t.setposition(40,110)
    t.pendown()
    t.left(90)
    t.forward(50)
    t.right(135)

def O1(t):
    t.penup()
    t.setposition(-77.5,10)
    t.pendown()
    t.circle(17.5)
def O2(t):
    t.penup()
    t.setposition(-27.5,10)
    t.pendown()
    t.circle(17.5)
def O3(t):
    t.penup()
    t.setposition(22.5,10)
    t.pendown()
    t.circle(17.5)
def O4(t):
    t.penup()
    t.setposition(-77.5,60)
    t.pendown()
    t.circle(17.5)
def O5(t):
    t.penup()
    t.setposition(-27.5,60)
    t.pendown()
    t.circle(17.5)
def O6(t):
    t.penup()
    t.setposition(22.5,60)
    t.pendown()
    t.circle(17.5)
def O7(t):
    t.penup()
    t.setposition(-77.5,110)
    t.pendown()
    t.circle(17.5)
def O8(t):
    t.penup()
    t.setposition(-27.5,110)
    t.pendown()
    t.circle(17.5)
def O9(t):
    t.penup()
    t.setposition(22.5,110)
    t.pendown()
    t.circle(17.5)

def menu():
    print("-----------------------------")
    print("|       ~Tic-Tac-Toe~       |")
    print("|      1 ~ Bottom Left      |")
    print("|     2 ~ Bottom Center     |")
    print("|     3 ~ Bottom Right      |")
    print("|      4 ~ Middle Left      |")
    print("|     5 ~ Middle Center     |")
    print("|     6 ~ Middle Right      |")
    print("|       7 ~ Top Left        |")
    print("|      8 ~ Top Center       |")
    print("|       9 ~ Top Right       |")
    print("-----------------------------")

def main():

    t=turtle.Turtle()
    t.speed(0)
    t.hideturtle()

    x=-50
    y=100

    for loop in range(2):
        t.penup()
        t.setposition(-100,y)
        t.pendown()
        t.forward(150)
        y=y-50
    t.right(90)
    for loop in range(2):
        t.penup()
        t.setposition(x, 150)
        t.pendown()
        t.forward(150)
        x=x+50
    t.left(90)

    playerOne=[]
    playerTwo=[]
    winPossible=[1,2,3]
    winPossible2=[4,5,6]
    winPossible3=[7,8,9]
    winPossible4=[1,4,7]
    winPossible5=[2,5,8]
    winPossible6=[3,6,9]
    winPossible7=[1,5,9]
    winPossible8=[3,5,7]

    menu()

    win="false"
    while(win != "true"):

        option=input("Choose where to draw an X.")
        if(option=="1"):
            X1(t)
            playerOne.append(1)
        elif(option=="2"):
            X2(t)
            playerOne.append(2)
        elif(option=="3"):
            X3(t)
            playerOne.append(3)
        elif(option=="4"):
            X4(t)
            playerOne.append(4)
        elif(option=="5"):
            X5(t)
            playerOne.append(5)
        elif(option=="6"):
            X6(t)
            playerOne.append(6)
        elif(option=="7"):
            X7(t)
            playerOne.append(7)
        elif(option=="8"):
            X8(t)
            playerOne.append(8)
        elif(option=="9"):
            X9(t)
            playerOne.append(9)
        elif(option=="end"):
            win="true"

        option=input("Choose where to draw an O.")
        if(option=="1"):
            O1(t)
            playerTwo.append(1)
        elif(option=="2"):
            O2(t)
            playerTwo.append(2)
        elif(option=="3"):
            O3(t)
            playerTwo.append(3)
        elif(option=="4"):
            O4(t)
            playerTwo.append(4)
        elif(option=="5"):
            O5(t)
            playerTwo.append(5)
        elif(option=="6"):
            O6(t)
            playerTwo.append(6)
        elif(option=="7"):
            O7(t)
            playerTwo.append(7)
        elif(option=="8"):
            O8(t)
            playerTwo.append(8)
        elif(option=="9"):
            O9(t)
            playerTwo.append(9)
        elif(option=="end"):
            win="true"

        t.penup()
        t.forward(10)
        #pointless forward so O will be drawn now instead of later

        if(set(playerOne)>=set(winPossible)):
            win="true"
        elif(set(playerOne)>=set(winPossible2)):
            win="true"
        elif(set(playerOne)>=set(winPossible3)):
            win="true"
        elif(set(playerOne)>=set(winPossible4)):
            win="true"
        elif(set(playerOne)>=set(winPossible5)):
            win="true"
        elif(set(playerOne)>=set(winPossible6)):
            win="true"
        elif(set(playerOne)>=set(winPossible7)):
            win="true"
        elif(set(playerOne)>=set(winPossible8)):
            win="true"

        if(set(playerTwo)>=set(winPossible)):
            win="true"
        elif(set(playerTwo)>=set(winPossible2)):
            win="true"
        elif(set(playerTwo)>=set(winPossible3)):
            win="true"
        elif(set(playerTwo)>=set(winPossible4)):
            win="true"
        elif(set(playerTwo)>=set(winPossible5)):
            win="true"
        elif(set(playerTwo)>=set(winPossible6)):
            win="true"
        elif(set(playerTwo)>=set(winPossible7)):
            win="true"
        elif(set(playerTwo)>=set(winPossible8)):
            win="true"

main()

Solution

  • I have a problem that a player can go on the same spaces on the board even if that space is taken.

    Here's a bandaid you can use. Where this code is:

    option=input("Choose where to draw an X.")
    

    Instead do:

    option = None
    while not option or int(option) in playerOne or int(option) in playerTwo:
        option = input("Choose where to draw an X.")
    

    Do the equivalent, but slightly modified, fix where you find:

    option=input("Choose where to draw an O.")
    

    This isn't optimal but gets you over this particular hurdle.

    I also had a problem that once win was true it will still run the rest of the program until it gets back to the start of the while command again.

    This one requires you to encapsulate your testing logic into a function, as others have noted in their comments, and apply it after each player's move rather than after both have gone.