0% found this document useful (0 votes)
16 views2 pages

Tic Tac Toe by Ross (Totally Not From Google)

The document contains a Python implementation of a Tic-Tac-Toe game. It includes functions to print the game board, check for a winner, and determine if the board is full. The main function manages player turns and game flow until a player wins or the game ends in a tie.

Uploaded by

charanm.s
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)
16 views2 pages

Tic Tac Toe by Ross (Totally Not From Google)

The document contains a Python implementation of a Tic-Tac-Toe game. It includes functions to print the game board, check for a winner, and determine if the board is full. The main function manages player turns and game flow until a player wins or the game ends in a tie.

Uploaded by

charanm.s
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):

for row in board:


print(" | ".join(row))
print("-" * 5)

def check_winner(board):
# Check rows and columns
for i in range(3):
if board[i][0] == board[i][1] == board[i][2] != ' ' or \
board[0][i] == board[1][i] == board[2][i] != ' ':
return True

# Check diagonals
if board[0][0] == board[1][1] == board[2][2] != ' ' or \
board[0][2] == board[1][1] == board[2][0] != ' ':
return True

return False

def is_board_full(board):
for row in board:
if ' ' in row:
return False
return True

def main():
board = [[' ' for _ in range(3)] for _ in range(3)]
current_player = 'X'

while True:
print_board(board)

# Get player move


while True:
row = int(input(f"Player {current_player}, enter row (0, 1, or 2): "))
col = int(input(f"Player {current_player}, enter column (0, 1, or 2):
"))

if board[row][col] == ' ':


break
else:
print("Invalid move. Try again.")

# Make the move


board[row][col] = current_player

# Check for a winner


if check_winner(board):
print_board(board)
print(f"Player {current_player} wins!")
break

# Check for a tie


if is_board_full(board):
print_board(board)
print("It's a tie!")
break

# Switch to the other player


current_player = 'O' if current_player == 'X' else 'X'

if __name__ == "__main__":
main()

You might also like