-1

I am currently learning python as my first programming language and thought I had the knowledge and wanted to challenge myself to create tic-tac-toe. I have the code working to the point where it starts the game and the first move is requested but when I try to print the new board it doesn't print with he space filled out.

L1 = " "

def board():
  print("     |     |        ")
  print(" ",L1," | ",M1," | ",R1," ")
  print("     |     |   ")
  print("-----------------")
  print("     |     |   ")
  print(" ",L2," | ",M2," | ",R2," ")
  print("     |     |   ")
  print("-----------------")
  print("     |     |   ")
  print(" ",L3," | ",M3," | ",R3," ")
  print("     |     |   ")

Xmove = input("Where does X want to go? ")

def xmove(Xmove):

   if Xmove == ("L1"):
     L1 = "X"
     board()

xmove(Xmove)

This should be printing the new board with the top left space now an "X" but it isn't. It just prints a blank board.

  • We will likely need more information to solve this issue. Could you post enough code to reproduce your issue? Right now I can't see what `board()` does and suspect your issue might be in that function. – Hoog Apr 24 '19 at 19:37
  • 2
    There's not really enough code here for me to get a good idea of what's going on. But I suspect there's a problem because you have a global L1 and a local L1. Setting L1 in xmove only changes L1 in the scope of the funtion, but it does not change the global L1. Classes should be used to avoid global variables since they're frowned upon, but you might be able to get it to work by declaring L1 as a global variable inside the xmove scope. Add `global L1` to the top of your xmove definition and board definition. – SyntaxVoid Apr 24 '19 at 19:40
  • Duplicate of [10588317](https://stackoverflow.com/questions/10588317/python-function-global-variables) and [12665994](https://stackoverflow.com/questions/12665994/function-not-changing-global-variable), probably among others – B. Shefter Apr 24 '19 at 19:45

3 Answers3

1

You may want to move some stuff around and use a dict, so it is easier to keep track of. This also avoids the need for globals. This also conveniently handles all your potential moves, with simple code.

def board():
    print("     |     |        ")
    print("   "+pm['L1']+" |   "+pm['M1']+"  | "+pm['R1']+" ")
    print("     |     |   ")
    print("-----------------")
    print("     |     |   ")
    print("   "+pm['L2']+" |   "+pm['M2']+"  | "+pm['R2']+" ")
    print("     |     |   ")
    print("-----------------")
    print("     |     |   ")
    print("   "+pm['L3']+" |   "+pm['M3']+"  | "+pm['R3']+" ")
    print("     |     |   ")


pm = {'L1': '', 'M1': '', 'R1': '',
      'L2': '', 'M2': '', 'R2': '',
      'L3': '', 'M3': '', 'R3': '', }


def xmove():
    Xmove = input("Where does X want to go? ")
    pm[Xmove] = 'X'
    board()

xmove()
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20
0

Right now you have L1 set up as a global variable. You can access global variables inside functions, but to change them you need to use the global keyword, something like this:

def xmove(Xmove):
    global L1
    if Xmove == ("L1"):
        L1 = "X"
        board()
B. Shefter
  • 877
  • 7
  • 19
0

I hope you can see why it might be tedious to use global variables like this-- you have to declare them as global variables every time you want to use them. In your case, you'd have to declare 9 global variables in each function that needs to access them. Instead, you could group them together use a class approach. Then all you have to do is pass the class around. It might look something like this:

class BoardClass:
    def __init__(self):
        # This is the constructor function that is run when you create a
        # a new instance of BoardClass. All we'll do is set the default 
        # values. 'self' is exactly what it sounds like. *itself*
        self.L1 = " "
        self.M1 = " "
        self.R1 = " "
        self.L2 = " "
        self.M2 = " "
        self.R2 = " "
        self.L3 = " "
        self.M3 = " "
        self.R3 = " "

def board(b):
    # b is the BoardClass object. You can refer to its members with b.varname
    print("     |     |        ")
    print(" ",b.L1," | ",b.M1," | ",b.R1," ")
    print("     |     |   ")
    print("-----------------")
    print("     |     |   ")
    print(" ",b.L2," | ",b.M2," | ",b.R2," ")
    print("     |     |   ")
    print("-----------------")
    print("     |     |   ")
    print(" ",b.L3," | ",b.M3," | ",b.R3," ")
    print("     |     |   ")

def xmove(b, Xmove):
    # Again, b is the BoardClass object.
    if Xmove == ("L1"):
        b.L1 = "X" # You can set the parameters of b like this
        board(b)

# Create your board
b = BoardClass() 

# Ask user for move
Xmove = input("Where does X want to go? ")

# Make the move and print
xmove(b, Xmove)

This only makes one move though, you'll have to come up with your own logic to get the game to loop and switch turns.

SyntaxVoid
  • 2,501
  • 2
  • 15
  • 23