0% found this document useful (0 votes)
12 views3 pages

Document

This document contains a Python implementation of a simple Tic-Tac-Toe game. It includes functions to display the game board, check for a win condition, and manage player turns. The game continues until a player wins or the board is full, resulting in a tie.

Uploaded by

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

Document

This document contains a Python implementation of a simple Tic-Tac-Toe game. It includes functions to display the game board, check for a win condition, and manage player turns. The game continues until a player wins or the board is full, resulting in a tie.

Uploaded by

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

def display_board(board):

print('-------------')

for row in board:

print('|', end='')

for cell in row:

print(f' {cell} |', end='')

print('\n-------------')

def check_win(board, player):

# Check rows

for row in board:

if all(cell == player for cell in row):

return True

# Check columns

for col in range(3):

if all(board[row][col] == player for row in range(3)):

return True

# Check diagonals

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 main():

board = [[' ' for _ in range(3)] for _ in range(3)]

current_player = 'X'

while True:

display_board(board)

row = int(input(f"Player {current_player}, enter row (1-3): ")) - 1

col = int(input(f"Player {current_player}, enter column (1-3): ")) - 1

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

board[row][col] = current_player

if check_win(board, current_player):

display_board(board)

print(f"Player {current_player} wins!")

break

if all(cell != ' ' for row in board for cell in row):

display_board(board)

print("It's a tie!")

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

else:

print("That spot is already taken. Try again.")

if __name__ == "__main__":

main()

You might also like