Python Tic-Tac-Toe Game Code
Python Tic-Tac-Toe Game Code
The CheckWin() function evaluates the current state of the board to determine if there is a winning combination, a draw, or if the game should continue running. It checks for all possible winning combinations by comparing the marks in the rows, columns, and diagonals. If a winning combination is detected, it sets the game state to 'Win'; if the board is full without any winner, it sets the game state to 'Draw'; otherwise, it keeps the game running.
The global keyword in the CheckWin() function allows the function to modify the 'Game' variable, which is defined outside the function's local scope. By declaring 'Game' as global, changes made to its value inside the function affect the variable's value throughout the program, allowing the function's outcomes to directly influence the game's state.
The code differentiates between Player 1 and Player 2 using the 'player' variable. Player 1 is associated with odd values of 'player', where their mark is 'X', while Player 2 is associated with even values, where their mark is 'O'. This differentiation ensures correct alternation of turns and appropriate assignment of marks on the board.
The code clears the screen using os.system('cls'), which is a call to the operating system's command to clear the console on Windows systems. This step is necessary to prevent clutter on the console, making the updates to the game board clear and readable after each player's move. It helps maintain a clean interface, ensuring players can easily follow the game's progress.
The DrawBoard() function uses placeholders like '%c' to format the output string when printing the game board. This formatting approach enables dynamic substitution of the board's current state, displaying the marks ('X' or 'O') or an empty space for each position, creating a visual representation of the board in its current state.
The specific winning combinations considered in this tic-tac-toe implementation are: horizontal lines (positions [1, 2, 3], [4, 5, 6], [7, 8, 9]), vertical lines (positions [1, 4, 7], [2, 5, 8], [3, 6, 9]), and the two main diagonals (positions [1, 5, 9] and [3, 5, 7]). These combinations are checked in the CheckWin() function, and if any are completed by the same player's mark ('X' or 'O'), it sets the game state to 'Win'.
The game identifies a 'draw' condition when none of the winning combinations are met and all board positions are filled, i.e., none are ' '. If these conditions are satisfied, the CheckWin() function sets the 'Game' state to 'Draw', which leads to an exit from the game loop, and the message "Game Draw" is printed. This mechanism ensures that the game correctly identifies a draw situation and terminates accordingly.
The game employs the CheckPosition() function to ensure that players can only place their mark on available board positions. This function checks if the specified board position is empty (' ') and returns True if it is available. The game loop only allows a mark to be placed if CheckPosition() returns True, thereby preventing overwriting an already occupied position.
The game loop manages player turns using the 'player' variable. It utilizes a simple loop structure where each iteration corresponds to a player's move. It alternates turns by incrementing the 'player' variable; odd numbers signify Player 1's turn ('X'), and even numbers signify Player 2's turn ('O'). After each valid move, the player number increments, effectively switching turns.
The time.sleep(3) function is used to pause the game for 3 seconds, allowing the players to read introductory information before the screen is cleared and the game starts. This enhances the user experience by giving players a moment to prepare after initial instructions are displayed, ensuring they do not miss any important information before the game begins.