-1

I've created a simple Tic-Tac-Toe game using python3. The gameplay itself works exactly as it should, the only problem I'm having is that the reset function is not properly wiping the board. It sets the value, prints it out, and yet on the next operation uses the old value.

This crux of the matter is that when you select, yes, you would like to play again, the game instantly ends due to it using the finalized board from the previous game. I've tried a couple different things, but end up with the same result. Is it a problem with scope an obvious mistake in the code. The problem should be contained in the reset() function, everything else is there for context.

#Tic-Tac-Toe Simulator

from random import *

board = [' ']*9
end = 0
abc = ['A', 'B', 'C']
count = 0
action = str

def render():
    print('3 ' + board[0] + '|' + board[1] + '|' + board[2])
    print('  -----')
    print('2 ' + board[3] + '|' + board[4] + '|' + board[5])
    print('  -----')
    print('1 ' + board[6] + '|' + board[7] + '|' + board[8])
    print('  A B C\n')

def winCheck():
    print(board)
    if board[0] == board[1] == board[2] != ' ':
        return board[0]
    elif board[3] == board[4] == board[5] != ' ':
        return board[3]
    elif board[6] == board[7] == board[8] != ' ':
        return board[6]
    elif board[0] == board[3] == board[6] != ' ':
        return board[0]
    elif board[1] == board[4] == board[7] != ' ':
        return board[1]
    elif board[2] == board[5] == board[8] != ' ':
        return board[2]
    elif board[0] == board[4] == board[8] != ' ':
        return board[0]
    elif board[2] == board[4] == board[6] != ' ':
        return board[2]
    else:
        return 0

def convert(location):
    coords = list(location.upper())
    if coords[0] == 'A' and coords[1] == '3':
        return 0
    if coords[0] == 'A' and coords[1] == '2':
        return 3
    if coords[0] == 'A' and coords[1] == '1':
        return 6
    if coords[0] == 'B' and coords[1] == '3':
        return 1
    if coords[0] == 'B' and coords[1] == '2':
        return 4
    if coords[0] == 'B' and coords[1] == '1':
        return 7
    if coords[0] == 'C' and coords[1] == '3':
        return 2
    if coords[0] == 'C' and coords[1] == '2':
        return 5
    if coords[0] == 'C' and coords[1] == '1':
        return 8

def move(letter, location):
    board[convert(location)] = letter.upper()

def spotCheck(location):
    if board[convert(location)] == ' ':
        return False
    else:
        return True

def reset():
    board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
    print(board)
    end = 0
    abc = ['A', 'B', 'C']
    count = 0
    action = str

def main():
    count = 0
    letter = str
    turn = 'x'
    while letter not in ['x', 'o']:
        letter = str(input("Would you like to play as 'X' or 'O': ")).lower()
    print("Please enter moves in the form of coordinates, such as A2 or C1")
    render()
    while winCheck() == 0:
        if letter == turn:
            action = str(input('Enter move: '))
            while spotCheck(action) == True:
                action = str(input('Space occupied, try again: '))
            move(letter, action)
        else:
            x = randint(0, 2)
            y = randint(1, 3)
            x = abc[x]
            action = x + str(y)
            while spotCheck(action) == True:
                x = randint(0, 2)
                y = randint(1, 3)
                x = abc[x]
                action = x + str(y)
            move(turn, action)
        render()
        if turn == 'x':
            turn = 'o'
        else:
            turn = 'x'
        count = count + 1
        if count == 9:
            break
    if winCheck() == 'X':
        print('X has won the game!')
    if winCheck() == 'O':
        print('O has won the game!')
    if winCheck() == 0:
        print('The game is a tie!')

main()
if input("Would you like to play again? ").lower() in ['y','yes']:
    reset()
    main()
Encryptonite
  • 3
  • 1
  • 3

1 Answers1

0

You are creating new local variables that have the same names as your global ones. Reading variables by name (if you do not declare them in function scope with same name) works, if you want to modify them, "explain" to python you mean the global ones:

def reset():
    global board  
    global end  
    global abc  
    global count  
    global action 
    board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
    print(board)
    end = 0
    abc = ['A', 'B', 'C']
    count = 0
    action = str   # where does str come from?

Read more here: Use of "global" keyword in Python

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69