0% found this document useful (0 votes)
8 views

Python code

The document contains a Python implementation of a tic-tac-toe game. It includes functions to print the board, check for a winner, check if the board is full, and manage the game flow. The game alternates between two players, X and O, until one wins or the game ends in a tie.

Uploaded by

Abdullah Kashif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python code

The document contains a Python implementation of a tic-tac-toe game. It includes functions to print the board, check for a winner, check if the board is full, and manage the game flow. The game alternates between two players, X and O, until one wins or the game ends in a tie.

Uploaded by

Abdullah Kashif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

def print_board(board): # Function to print the tic-tac-toe board

for row in board:


print(" | ".join(row)) # Join each element in the row with " | " and print
print("-" * 5) # Print a line of dashes for visual separation

# This function checks if a player has won the game


def check_winner(board):
# Check rows
for row in board:
if row.count(row[0]) == len(row) and row[0] != ' ': # If all elements in
the row are the same and not empty
return True # Return True, indicating a win

# Check columns
for col in range(len(board[0])):
if board[0][col] == board[1][col] == board[2][col] and board[0][col] != '
': # If all elements in the column are the same and not empty
return True # Return True, indicating a win

# Check diagonals
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != ' ': # If all
elements in the diagonal are the same and not empty
return True # Return True, indicating a win
if board[0][2] == board[1][1] == board[2][0] and board[0][2] != ' ': # If all
elements in the diagonal are the same and not empty
return True # Return True, indicating a win

return False # Return False, indicating no win

# This function checks if the board is full


def is_board_full(board):
for row in board:
if ' ' in row: # If there is an empty space in any row
return False # Return False, indicating the board is not full
return True # Return True, indicating the board is full

# This is the main game function


def tic_tac_toe():
board = [[' ' for _ in range(3)] for _ in range(3)] # Initialize an empty
board
player = 'X' # Player X starts the game

while True:
print_board(board) # Print the current board
try:
row = int(input(f"Player {player}, enter row number (0, 1, 2): ")) #
Get user input for row
col = int(input(f"Player {player}, enter column number (0, 1, 2): "))
# Get user input for column
except ValueError:
print("Invalid input! Please enter a number.") # Handle invalid input
continue

if row < 0 or row > 2 or col < 0 or col > 2: # If input is out of bounds
print("Invalid input! Please enter a number between 0 and 2.") # Print
error message
continue

if board[row][col] == ' ': # If the selected spot is empty


board[row][col] = player # Place the player's symbol on the board

if check_winner(board): # Check if the player has won


print_board(board)
print(f"Player {player} wins!")
return
elif is_board_full(board): # Check if the board is full
print_board(board)
print("It's a tie!")
return
else:
player = 'O' if player == 'X' else 'X' # Switch players
else:
print("That spot is already taken! Try again.") # Inform the player
that the spot is taken

tic_tac_toe() # Start the game

You might also like