# Tic-Tac-Toe Game in Python
def print_board(board):
"""
Print the current state of the Tic-Tac-Toe board.
"""
print("-------------")
for i in range(3):
print("|", end=" ")
for j in range(3):
print(board[i][j], "|", end=" ")
print()
print("-------------")
def get_move(player):
"""
Ask the player for their next move.
"""
print(f"Player {player}, make your move (row, col): ")
row = int(input())
col = int(input())
return (row, col)
def is_valid_move(board, move):
"""
Check if the given move is valid.
"""
row, col = move
if row < 0 or row > 2 or col < 0 or col > 2:
return False
if board[row][col] != " ":
return False
return True
def is_winner(board, player):
"""
Check if the given player has won the game.
"""
for i in range(3):
if board[i][0] == player and board[i][1] == player and board[i][2] == player:
return True
if board[0][i] == player and board[1][i] == player and board[2][i] == player:
return True
if board[0][0] == player and board[1][1] == player and board[2][2] == player:
return True
if board[0][2] == player and board[1][1] == player and board[2][0] == player:
return True
return False
def is_tie(board):
"""
Check if the game has ended in a tie.
"""
for i in range(3):
for j in range(3):
if board[i][j] == " ":
return False
return True
def play_game():
"""
Play a game of Tic-Tac-Toe.
"""
board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
players = ["X", "O"]
current_player = 0
while True:
print_board(board)
move = get_move(players[current_player])
if not is_valid_move(board, move):
print("Invalid move. Try again.")
continue
row, col = move
board[row][col] = players[current_player]
if is_winner(board, players[current_player]):
print_board(board)
print(f"Player {players[current_player]} wins!")
break
if is_tie(board):
print_board(board)
print("Tie game!")
break
current_player = (current_player + 1) % 2
play_game()