Create Python Tic Tac Toe Game
Tic-Tac-Toe is a two-player game played on a 3x3 grid. Players take turns marking X or O in empty cells, and the goal is to get three of their marks in a row, column, or diagonal.
# Initialize the board
board = [[" " for_in range(3)] for_in range(3)]
# Function to print the board
def print_board():
for row in board:
print("|".join(row))
print("-----")
# Function to check if there's a winner
def check_winner():
# 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
print("Let's play Tic-Tac-Toe!")
player = "X"
while True:
print_board()
row = int(input("Enter the row (0-2): "))
col = int(input("Enter the column (0-2): "))
if board[row][col] == " ":
board[row][col] = player
winner = check_winner()
if winner:
print_board()
print("Player", winner, "wins!")
break
if player == "X":
player = "O"
else:
player = "X"
else:
print("That cell is already filled. Try again!")