# Define constants for the players
PLAYER_X = "X"
PLAYER_O = "O"
# Initialize an empty board
board = [" " for _ in range(9)]
# Function to print the Tic-Tac-Toe 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]} ")
# Function to check if the game is over
def is_game_over():
# Check rows, columns, and diagonals for a win
win_conditions = [(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)]
for condition in win_conditions:
if board[condition[0]] == board[condition[1]] == board[condition[2]] != " ":
return True
# Check for a draw (no more empty spaces)
if " " not in board:
return True
return False
# Function to play the game
def play_game():
current_player = PLAYER_X
while not is_game_over():
print_board()
move = int(input(f"Player {current_player}, enter your move (1-9): ")) - 1
if 0 <= move < 9 and board[move] == " ":
board[move] = current_player
current_player = PLAYER_X if current_player == PLAYER_O else PLAYER_O
else:
print("Invalid move. Try again.")
print_board()
if " " not in board:
print("It's a draw!")
else:
print("Current player wins!")
# Start the game
play_game()