2 years ago
#67039
Maddy Walker
Python Tic Tac Toe variables not resetting?
I've just started learning Python, and like a lot of beginners I'm working on a simple two player tic tac toe game.
I've managed to get through most of the code and debug everything EXCEPT for some reason when I try and replay the game, it's not resetting my board variable and essentially clearing the board despite re assigning the variable. I'm assuming its a simple fix and has something to do with scope that I'm just missing but I'd really appreciate if someone could give it a look over and let me know how I'd fix this issue.
Currently the code works, the player picks a symbol and goes back and forth until someone wins, then the code asks if they'd like to replay. If no is chosen, the game ends just fine, but if they select yes, the board isn't cleared from the last games markers.
I feel like I've tried a million different things and nothing seems to be clearing it!
Here's the code
#Display board function
#import clear output function - if don't will show entire history of board moves each time
from IPython.display import clear_output
def view_board(board):
clear_output()
print(display_board[6]+'|'+display_board[7]+"|"+display_board[8])
print('-----')
print(display_board[3]+'|'+display_board[4]+"|"+display_board[5])
print('-----')
print(display_board[0]+'|'+display_board[1]+"|"+display_board[2])
#OK function to ask player one if they want to be X or O
def player_icon():
#VARIABLES
icon = 'wrong'
while icon not in ['X', 'O']:
icon = input('Player 1, please choose a symbol (X or O):')
if icon not in ['X', 'O']:
print('Sorry, that is not a valid choice. Please choose either X or O.')
if icon == 'X':
print('Great! Player 1 will go first!')
elif icon == 'O':
print('Great! Player 2 will go first!')
return icon
#now we do the positioning based on the numbers on the board
#take user input position - use that as index value in blank list - replace that value with their marker
def user_position_X(board):
#board will be blank at first - make board variable
#marker will be their chosen icon from player_icon - starts with X - change to O after first move made
#position will be index value 1-9 - make sure to subtract 1 from chosen value as index will start at 0
#board(position) = marker
#VARIABLES
marker = 'X'
position = 'wrong'
while position not in board:
#ASK FOR USER POSITION
position = input('Please choose a position (1-9): ')
#IF NOT IN ACCEPTABLE VALUES (includes not digits I believe)
if position not in board:
print('Sorry! That is not an acceptable position. Please select an avaliable position.')
#MAKE POSITION INT
position_int = int(position)
#MAKING INPUT
index_position = (position_int)-1
#REPLACING ON BOARD - NOT USING X AND O SO CAN'T BE OVERWRITTEN - X=T O=P
board[index_position] = 'T'
#ADDING MARKER TO DISPLAY BOARD
for location in display_board:
display_board[index_position] = marker
return [board, display_board]
#now we do the positioning based on the numbers on the board
#take user input position - use that as index value in blank list - replace that value with their marker
def user_position_O(board):
#board will be blank at first - make board variable
#marker will be their chosen icon from player_icon - starts with X - change to O after first move made
#position will be index value 1-9 - make sure to subtract 1 from chosen value as index will start at 0
#board(position) = marker
#VARIABLES
marker = 'O'
position = 'wrong'
while position not in board:
#ASK FOR USER POSITION
position = input('Please choose a position (1-9): ')
#IF NOT IN ACCEPTABLE VALUES (includes not digits I believe)
if position not in board:
print('Sorry! That is not an acceptable position. Please select an avaliable position.')
#MAKE POSITION INT
position_int = int(position)
#MAKING INPUT
index_position = (position_int)-1
#REPLACING ON BOARD - Not using X and O so they cannot be replaced - X = T O = P
board[index_position] = 'P'
#ADDING MARKER TO DISPLAY BOARD
for location in display_board:
display_board[index_position] = marker
return [board, display_board]
#CHECK TO SEE IF WON - HOW?
#DICTIONARY OF ALL WINNING POSITIONS?
#run check win after each turn? after display board?
#in final code make player_icon = p1_icon
#check wins should jsut contain those items not be them exacly - check section 6 for the similar ones the 007 thing
#WHEN X AT CERTAIN INDEX POSITIONS WHEN O AT CERTAIN INDEX POSITIONS
#USING T AND P
def check_win(board,player1_icon):
#WHEN X AT CERTAIN INDEX POSITIONS WHEN O AT CERTAIN INDEX POSITIONS
#USING T AND P
#HORIZONTAL X
if (board[0] == 'T'and board[1] == 'T' and board[2] == 'T') or (board[3] == 'T'and board[4] == 'T' and board[5] == 'T') or (board[6] == 'T'and board[7] == 'T' and board[8] == 'T'):
if player1_icon == 'X':
win = 'yes'
print('Congrats, Player 1 has won!')
return win
elif player1_icon != 'X':
win = 'yes'
print('Congrats, Player 2 has won!')
return win
else:
pass
else:
pass
#VERTICAL X
if (board[0] == 'T'and board[3] == 'T' and board[6] == 'T') or (board[1] == 'T'and board[4] == 'T' and board[7] == 'T') or (board[2] == 'T'and board[5] == 'T' and board[8] == 'T'):
if player1_icon == 'X':
win = 'yes'
print('Congrats, Player 1 has won!')
return win
elif player1_icon != 'X':
win = 'yes'
print('Congrats, Player 2 has won!')
return win
else:
pass
else:
pass
#DIAGONAL X
if (board[0] == 'T'and board[4] == 'T' and board[8] == 'T') or (board[2] == 'T'and board[4] == 'T' and board[6] == 'T'):
win = 'yes'
if player1_icon == 'X':
win = 'yes'
print('Congrats, Player 1 has won!')
return win
elif player1_icon != 'X':
win = 'yes'
print('Congrats, Player 2 has won!')
return win
else:
pass
else:
pass
#HORIZONTAL O
if (board[0] == 'P'and board[1] == 'P' and board[2] == 'P') or (board[3] == 'P'and board[4] == 'P' and board[5] == 'P') or (board[6] == 'P'and board[7] == 'P' and board[8] == 'P'):
if player1_icon == 'O':
win = 'yes'
print('Congrats, Player 1 has won!')
return win
elif player1_icon != 'O':
win = 'yes'
print('Congrats, Player 2 has won!')
return win
else:
pass
else:
pass
#VERTICAL O
if (board[0] == 'P'and board[3] == 'P' and board[6] == 'P') or (board[1] == 'P'and board[4] == 'P' and board[7] == 'P') or (board[2] == 'P'and board[5] == 'P' and board[8] == 'P'):
if player1_icon == 'O':
win = 'yes'
print('Congrats, Player 1 has won!')
return win
elif player1_icon != 'O':
win = 'yes'
print('Congrats, Player 2 has won!')
return win
else:
pass
else:
pass
#DIAGONAL O
if (board[0] == 'P'and board[4] == 'P' and board[8] == 'P') or (board[2] == 'P'and board[4] == 'P' and board[6] == 'P'):
if player1_icon == 'O':
win = 'yes'
print('Congrats, Player 1 has won!')
return win
elif player1_icon != 'O':
win = 'yes'
print('Congrats, Player 2 has won!')
return win
else:
pass
else:
win = 'no'
return win
#Would you like to replay
def check_replay():
choice = 'wrong'
while choice not in ['Yes', 'No']:
choice = input('Would you like to play again? (Yes or No) ')
if choice not in ['Yes', 'No']:
print("Sorry, I don't understand. Please choose Yes or No.")
if choice == 'Yes':
return True
#aka game keeps playing
else:
print('Thanks for playing!')
return False
#game stops
#bc boolean values can continue to maintain some while loop
#Put it all together
from IPython.display import clear_output
def play_game():
replay = True
while replay == True:
#VARIABLES
win = 'no'
board = ['1','2','3','4','5','6','7','8','9']
display_board = [' ', ' ',' ',' ',' ',' ',' ',' ',' ']
#DISPLAY BOARD
view_board(display_board)
#ASK PLAYER 1 WHAT ICON
player1_icon = player_icon()
while win == 'no':
clear_output
#X SYMBOL POSITION
x_results = user_position_X(board)
x_results[0] = board
x_results [1] = display_board
#DISPLAY BOARD
view_board(display_board)
#CHECK IF WIN
win = check_win(board,player1_icon)
if win == 'yes':
break
#O SYMBOL POSITION
o_results = user_position_O(board)
o_results[0] = board
o_results [1] = display_board
#DISPLAY BOARD
view_board(display_board)
#CHECK IF WIN
win = check_win(board,player1_icon)
if win == 'yes':
replay = False
replay = check_replay()
if replay:
board = ['1','2','3','4','5','6','7','8','9']
display_board = [' ', ' ',' ',' ',' ',' ',' ',' ',' ']
if not replay:
print('Thanks for Playing!')
#need to have updated list with where answers are and then a second seperate list with available numbers
Sorry if its a bit messy, its my first proper big coding project! Also sorry about my notes it was just the best way for me to keep track of everything!
Like I said, everything seems to work except when check_replay() is executed, for some reason board and display_board aren't resetting despite being defined at the top of the function. I've tried moving the location of board and display_boards assignments, tried re assigning them after checking for replay, tried writing a new function that runs play game as a function and replay as its own thing. I'm really not sure why it's not resetting.
Thanks in advance!!
python
variables
jupyter-notebook
tic-tac-toe
python
variables
jupyter-notebook
tic-tac-toe
0 Answers
Your Answer