So I started learning to code and followed a tutorial making a simple tic tac toe game. Later that day, I wanted to do it again, but without help this time. I've gone through everything, but it didn't work as intended. So I took the code I made with the tutorial and compared them. After a few simplifying steps, I still didn't know why it wasn't working as well as the other... So I just renamed all my variables and functions, so that they are the same in both codes, and suddenly, it worked!
I literally didn't do anything else than renaming my variables and functions (and making some lines and comments for better looks). So, can anyone explain to me, why this could have happened?
Sadly I cannot post the code pre-renaming, as it is already overwritten and so on... (I, the dumb***, didn't think about making an extra savestate... uff)
PS: I coded in Visual Studio Code. Somehow when I press "Run", it does not run the code from the beginning, if another "game" is still running. Is that because it uses console commands to run the script? (Because my game is asking for input until it's finished)
For those interested, here's the working code:
To describe the problem before renaming: You got 3 X in a row (or column or diagonal), but it would still ask O to put it's sign before declaring X the winner... (sorry for that spelling and grammar...)
I literally didn't do anything else than renaming my variables and functions (and making some lines and comments for better looks). So, can anyone explain to me, why this could have happened?
Sadly I cannot post the code pre-renaming, as it is already overwritten and so on... (I, the dumb***, didn't think about making an extra savestate... uff)
PS: I coded in Visual Studio Code. Somehow when I press "Run", it does not run the code from the beginning, if another "game" is still running. Is that because it uses console commands to run the script? (Because my game is asking for input until it's finished)
For those interested, here's the working code:
# ------ Global Var ------ # Game Board board = ["-", "-", "-", "-", "-", "-", "-", "-", "-",] # If game ist still going game_still_going = True #Who won? Or tie? winner = None # Whose turn is it? current_player = "X" # ------- Functions -------- # Default Board def display_board(): print(board[0] + "|" + board[1] + "|" + board[2]) print(board[3] + "|" + board[4] + "|" + board[5]) print(board[6] + "|" + board[7] + "|" + board[8]) # Actual Game def play_game(): # Setup global variables (because of "Wanna play again") global game_still_going global winner global board global current_player # Display initial board display_board() # While game is still going while game_still_going: # Handle a single turn of an abitrary player handle_turn(current_player) # Check if the game has ended check_if_game_over() # Switch to other player switch_player() # The game has ended if winner == "X" or winner == "O": print(winner + " won.") elif winner == None: print("Tie.") # Replay? (Selfmade) print("Want to play again? Enter 'Yes' or 'No'. ") replay = input() while replay not in ["Yes", "No"]: replay = input("Input valid answer.") if replay == "Yes": print(" ") print("Okay, then lets go!") game_still_going = True winner = None current_player = "X" for x in range(8): board[x] = "-" play_game() elif replay == "No": print("Alright, your choice!") game_still_going = False # Handle a single turn of an abitrary player def handle_turn(player): print(player + "'s turn.") position = input("Choose a position from 1 - 9: ") # Input validation valid = False while not valid: while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]: position = input("Choose a position from 1-9: ") position = int(position) - 1 if board[position] == "-": valid = True else: print("Can't go there. Go again.") # Input happening board[position] = player # Display updated board display_board() # Check if the game has ended def check_if_game_over(): check_for_winner() check_if_tie() # Check if a player has won def check_for_winner(): #Setup global variables global winner #check rows row_winner = check_rows() #check culumns column_winner = check_columns() #check diagonals diagonal_winner = check_diagonals() if row_winner: winner = row_winner elif column_winner: winner = column_winner elif diagonal_winner: winner = diagonal_winner else: winner = None def check_rows(): #Setup global variables global game_still_going #Check any rows row_1 = board[0] == board[1] == board[2] != "-" row_2 = board[3] == board[4] == board[5] != "-" row_3 = board[6] == board[7] == board[8] != "-" #Any row -> flag as win if row_1 or row_2 or row_3: game_still_going = False #Return winner (X or O) if row_1: return board[0] if row_2: return board[3] if row_3: return board[6] def check_columns(): #Setup global variables global game_still_going #Check any column column_1 = board[0] == board[3] == board[6] != "-" column_2 = board[1] == board[4] == board[7] != "-" column_3 = board[2] == board[5] == board[8] != "-" #Any column -> flag as win if column_1 or column_2 or column_3: game_still_going = False #Return winner (X or O) if column_1: return board[0] if column_2: return board[1] if column_3: return board[2] def check_diagonals(): #Setup global variables global game_still_going #Check any diagonal diagonal_1 = board[0] == board[4] == board[8] != "-" diagonal_2 = board[6] == board[4] == board[2] != "-" #Any diagonal -> flag as win if diagonal_1 or diagonal_2: game_still_going = False #Return winner (X or O) if diagonal_1: return board[0] if diagonal_2: return board[2] # Check if there is a tie def check_if_tie(): # Setup global variables global game_still_going # If board is filled up, then Tie if "-" not in board: game_still_going = False # Switch to other player def switch_player(): # Setup global variables global current_player # Change to opposite player if current_player == "X": current_player = "O" elif current_player == "O": current_player = "X" # ------------- actual Code -------------- LMAO play_game()
To describe the problem before renaming: You got 3 X in a row (or column or diagonal), but it would still ask O to put it's sign before declaring X the winner... (sorry for that spelling and grammar...)