DEV Community

Marcelo Martins
Marcelo Martins

Posted on

Tic Tac Toe#️⃣❎0️⃣

Tic Tac Toe game in Python

def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 9)

def check_winner(board, player):
# Check rows, columns, and diagonals
for row in board:
if all(s == player for s in row):
return True
for col in range(3):
if all(board[row][col] == player for row in range(3)):
return True
if all(board[i][i] == player for i in range(3)):
return True
if all(board[i][2 - i] == player for i in range(3)):
return True
return False

def is_full(board):
return all(all(cell != " " for cell in row) for row in board)

def tic_tac_toe():
board = [[" " for _ in range(3)] for _ in range(3)]
current_player = "X"

while True:
print_board(board)
print(f"Player {current_player}'s turn.")
# Ask for move
try:
    row = int(input("Enter row (0-2): "))
    col = int(input("Enter column (0-2): "))
except ValueError:
    print("Please enter a valid number!")
    continue

if row not in range(3) or col not in range(3):
    print("Invalid coordinates! Try again.")
    continue
if board[row][col] != " ":
    print("Cell already taken! Try again.")
    continue

board[row][col] = current_player

if check_winner(board, current_player):
    print_board(board)
    print(f"Player {current_player} wins!")
    break
if is_full(board):
    print_board(board)
    print("It's a tie!")
    break

# Switch player
current_player = "O" if current_player == "X" else "X"
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode




Start the game

tic_tac_toe()

Top comments (0)