Search code examples
pythontic-tac-toe

Tic-tac-toe Python - switch the user


I am writing the python code for the Tic Tac Tow game. I stuck by switching the user from Palyerx to playero. Below is part of my code. I am not sure which part I did wrong that after playero generate a valid result, it did not switch to playerx. I really appreciate your help! Thank you all in advance.

board_list=["0","1","2","3","4","5","6","7","8"]

playerLetter = 'o'

# Print out the tic tac toe board
# Input: list representing the tic tac toe board
# Return value: none
def printUglyBoard(board_list):
    print()
    counter = 0
    for i in range(3):
        for j in range(3):
        print(board_list[counter], end="  ")
        counter += 1
    print()


def isValidMove(board_list,spot):
    if 0<= spot <= 8 and board_list[spot]!='x' and board_list[spot]!='o':
        print("True")
        return True
    else:
        print("Invaild. Enter another value.")
        return False

def updateBoard(board_list,spot,playerLetter):
    result=isValidMove(board_list,spot)
    if result==True and playerLetter=='x':
        board_list[spot]='x'
        playerLetter='o'
    elif result==True and playerLetter=='o':
        board_list[spot]='o'
        playerLetter='x'
    print(board_list)
    print(playerLetter)
    printUglyBoard(board_list)




def play():
    printUglyBoard(board_list)
    while True:
        if playerLetter=='x':
            spot = int(input('Player x,enter the value:'))
        elif playerLetter=='o':
            spot = int(input('Player o,enter the value:'))
        isValidMove(board_list,spot)
        updateBoard(board_list,spot,playerLetter)



play()

Solution

  • Variables inside functions are local. Basically you need to return the playerLetter to be used in the next while Loop. Try this:

    board_list=["0","1","2","3","4","5","6","7","8"]
    
    # Print out the tic tac toe board
    # Input: list representing the tic tac toe board
    # Return value: none
    def printUglyBoard(board_list):
        print()
        counter = 0
        for i in range(3):
            for j in range(3):
                print(board_list[counter], end="  ")
                counter += 1
        print()
    
    
    def isValidMove(board_list,spot):
        if 0<= spot <= 8 and board_list[spot]!='x' and board_list[spot]!='o':
            print("True")
            return True
        else:
            print("Invaild. Enter another value.")
            return False
    
    def updateBoard(board_list,spot,playerLetter):
        result=isValidMove(board_list,spot)
        if result==True and playerLetter=='x':
            board_list[spot]='x'
            playerLetter='o'
        elif result==True and playerLetter=='o':
            board_list[spot]='o'
            playerLetter='x'
        print(board_list)
        print(playerLetter)
        printUglyBoard(board_list)
        return playerLetter  ############### <<------- CHANGED
    
    def play():
        playerLetter = 'o'  ############### <<------- CHANGED
        printUglyBoard(board_list)
        while True:
            if playerLetter=='x':
                spot = int(input('Player x,enter the value:'))
            elif playerLetter=='o':
                spot = int(input('Player o,enter the value:'))
            isValidMove(board_list,spot)
            playerLetter = updateBoard(board_list,spot,playerLetter) ###### <<---- CHANGED
    
    play()
    

    Furthermore:

    You can do various changes here. Consider this for example, 4lines become 1:

    if playerLetter=='x':
        spot = int(input('Player x,enter the value:'))
    elif playerLetter=='o':
        spot = int(input('Player o,enter the value:'))
    

    Could be changed to this:

    spot = int(input('Player {},enter the value:'.format(playerLetter))