board = ["0", "1", "2", "3", "4", "5", "6", "7", "8"]
def print_board():
print(f"{board[0]} | {board[1]} | {board[2]}")
print("----------")
print(f"{board[3]} | {board[4]} | {board[5]}")
print("----------")
print(f"{board[6]} | {board[7]} | {board[8]}")
print_board()
def game():
point_x = 0
point_o = 0
while True:
player_x = input("Enter where you'd like to place X: ")
position_x = int(player_x)
if board[position_x] != "X":
board[position_x] = 'X'
print_board()
print()
if board[0] == board[1] == board[2] or board[3] == board[4] == board[5] or board[6] == board[7] == board[8] or board[0] == board[3] == board[6] or board[1] == board[4] == board[7] or board[2] == board[5] == board[8] or board[0] == board[4] == board[8] or board[2] == board[4] == board[6]:
print("X won!")
point_x = point_x + 1
print(f"X has {point_x} points")
play_game_again()
else:
print("That position is already taken.")
player_0 = input("Enter where you'd like to place O: ")
position_o = int(player_0)
if board[position_o] != 'O':
board[position_o] = 'O'
print_board()
print()
if board[0] == board[1] == board[2] or board[3] == board[4] == board[5] or board[6] == board[7] == board[8] or board[0] == board[3] == board[6] or board[1] == board[4] == board[7] or board[2] == board[5] == board[8] or board[0] == board[4] == board[8] or board[2] == board[4] == board[6]:
print("O won!")
point_o = point_o + 1
print(f"O has {point_o} points")
play_game_again()
else:
print("That position is already taken.")
def play_game_again():
global board
play_game = input("Would you like to play the game again? ")
if play_game.lower() == "yes":
board = ["0", "1", "2", "3", "4", "5", "6", "7", "8"]
if 'X' or 'O' in board:
def print_board():
print(f"{board[0]} | {board[1]} | {board[2]}")
print("----------")
print(f"{board[3]} | {board[4]} | {board[5]}")
print("----------")
print(f"{board[6]} | {board[7]} | {board[8]}")
print_board()
game()
else:
print("Thanks for playing!")
quit()
game()
I’m finding error in re-setting the board. It displays the new board, but the positions are still occupied by X or O. It’s not seen in the program/output, but probably in the memory.
Can someone run this program on their machine and let me know how I could re-set the board so that the user can play the game again?
Thank you so much!
I tried re-setting the board somewhere below in the program (only when the user wants to play the game again) but it does not work.
I’m expecting the code to re-set the board when the user wants to play again, so that the user can place the position of ‘X’ or ‘O’ in places that were used before.
But in the current code, it does not happen.
Afreen Hossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.