0

I wanted to challenge myself and make a tictactoe game from my beginner knowledge of Python. I'm most of the way through the code, after I get these variables to change and it to appear on the board I can add in o registration as well and get it on a while command or something to keep the game going until someone wins. The only problem I'm having is the variables wont change even though the conditions I've set are being met. If you start the game, go to the x team, and make a move to spot 1 or 2 then it should show x on that spot on the board. Although even when I print the variable afterwards outside of the board to see if it was something wrong with the it, it will still show the original string that was connected to the x2 or x1 variable. How would I go about fixing this?(importing random is for when I add AI to play against the player)

import random
row = "   |   |   "
intersection = "---|---|---"
winner = None
draw = False
playerx = False
playero = False
x1 = " "
x2 = " "
x3 = " "
x4 = " "
x5 = " "
x6 = " "
x7 = " "
x8 = " "
x9 = " "


print("Welcome to my TicTacToe, which side would you like to be on? ")
def start():
    player = str(input())
    if player == "x":
        print("You are x!")
        playerx = True
        return "x"
    elif player == "o":
        print("You are o!")
        playero = True
        return "o"
        
        

if start() == "x" or "o": 
    print("1-9 correspond to their spots on the board, input your move here: ")
def action():
    x10 = int(input())
    if x10 <= 9:
        return x10  
    elif x10 >= 10:
        print("Not a spot on the board! Try again!")
        action()

print(action())

if start() == "x":
    if action() == 1:
        x1 = "x"
    elif action() == 2:
        x2 = "x"
    elif action() == 3:
        x3 = "x"
    elif action() == 4:
        x4 = "x"
    elif action() == 5:
        x5 = "x"
    elif action() == 6:
        x6 = "x"
    elif action() == 7:
        x7 = "x"
    elif action() == 8:
        x8 = "x"
    elif action() == 9:
        x9 = "x"
    else:
        print("Game broke, goodbye!")
    
def board(): 
    print(f" {x1} | {x2} | {x3} ")
    print(f"---|---|---")   
    print(f" {x4} | {x5} | {x6} ")
    print(f"---|---|---")
    print(f" {x7} | {x8} | {x9} ")
board()

I think I'm gonna need to add some timing code into this as well because sometimes it will bug out like it's trying to compute everything too fast as well but that's just something I haven't gotten to yet. Tips would also be appreciated.

DrizzyDog
  • 1
  • 1
  • Wouldn't it be easier if you would create your board as a 1D/2D list or a dictionary? – David Dec 04 '22 at 18:11
  • Can you please add an explanation of a step-by-step of what should happen? I try to run it on my machine and it makes no sense. – David Dec 04 '22 at 18:17
  • Alright, sorry for the message above. I have figured out what are your problems. First of all, you should delete all the random code that not helps the game like the spring statements of the functions or the row & intersection. Second, wherever you have an `if` followed by some `elifs` don't call the function all over again. call it once and save it into a variable. and check the variable. I recommend reading my 1st comment and remodeling your game. good luck developing. – David Dec 04 '22 at 18:26

0 Answers0