Categories
Mastering Development

Tic Tac Toe game using python [closed]

  1. If you look at line 12 I need to make a restriction so that you can not overlap each other. (for example I don’t want "X" to go on a spot where "O" already is. (And yes I have to use a while loop) I have tried different ways but it does not seem to work.

  2. I want my code to look cleaner so that I am not re pasting the same thing in every if statement.

     for i in range(0,9,3):
     print(board[i], "|", board[i+1], "|", board[i+2])
     if (i<6):
         print("------------")
    

I want to reduce this code above so that I am not just repasting it in each if statement ^^.

Here is my entire code I have so far that I want to improve:

board=[1,2,3,4,5,6,7,8,9]
playerturn="X"

while True:
    for i in range(0,9,3):
        print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")

    choose=int(input("Where do you want to play your" + " " + playerturn + ":"))

    while[Need help making restriction here]
    print("That spot is already occupied")
    choose=int(input("Choose another spot:"))

    board[choose-1]=playerturn
    if playerturn =="X":
        playerturn="O"
    else:
        playerturn="X"

    count=0

    if (board[0]==board[1]) and (board[1]==board[2]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[0]
    print("The winner is", winner)
    break

    elif (board[3]==board[4]) and (board[4]==board[5]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[3]
    print("The winner is", winner)
    break

    elif (board[6]==board[7]) and (board[7]==board[8]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[6]
    print("The winner is", winner)
    break

    elif (board[0]==board[3]) and (board[3]==board[6]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[0]
    print("The winner is", winner)
    break

    elif (board[1]==board[4]) and (board[4]==board[7]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[0]
    print("The winner is", winner)
    break

    elif (board[2]==board[5]) and (board[5]==board[8]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[2]
    print("The winner is", winner)
    break

    elif (board[0]==board[4]) and (board[4]==board[8]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[0]
    print("The winner is", winner)
    break

    elif (board[2]==board[4]) and (board[4]==board[6]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[2]
    print("The winner is", winner)
    break

    for item in board:
        if (item != "X") and (item != "O"):
            count +=1

    if count==0:
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
        if (i<6):
            print("------------")
            print("Tie game")
            break

Leave a Reply

Your email address will not be published. Required fields are marked *