# Function to print the Tic Tac Toe board def print_board(board): for row in board: print(" | ".join(row)) print("-" * 9) # Function to check if any player has won the game def check_winner(board): # Check rows for row in board: if row[0] == row[1] == row[2] != " ": return row[0] # Check columns for col in range(3): if board[0][col] == board[1][col] == board[2][col] != " ": return board[0][col] # Check diagonals if board[0][0] == board[1][1] == board[2][2] != " ": return board[0][0] if board[0][2] == board[1][1] == board[2][0] != " ": return board[0][2] return None # Function to play the Tic Tac Toe game def play_game(): board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]] current_player = "X" moves = 0 while True: print_board(board) # Get player's move while True: row = int(input("Enter the row (0-2): ")) col = int(input("Enter the column (0-2): ")) if 0 <= row <= 2 and 0 <= col <= 2 and board[row][col] == " ": board[row][col] = current_player break else: print("Invalid move. Try again.") moves += 1 # Check if the current player has won winner = check_winner(board) if winner: print_board(board) print(f"Player {winner} wins!") break # Check if the game is a draw if moves == 9: print_board(board) print("It's a draw!") break # Switch to the other player current_player = "O" if current_player == "X" else "X" # Play the Tic Tac Toe game play_game()