Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project_1 #1

Open
Sandhyateju opened this issue Mar 25, 2024 · 0 comments
Open

Project_1 #1

Sandhyateju opened this issue Mar 25, 2024 · 0 comments

Comments

@Sandhyateju
Copy link
Owner

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

def check_winner(board):
# Check rows
for row in board:
if row[0] == row[1] == row[2] != ' ':
return True

# Check columns
for col in range(3):
    if board[0][col] == board[1][col] == board[2][col] != ' ':
        return True

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

return False

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

def tic_tac_toe():
board = [[' ' for _ in range(3)] for _ in range(3)]
player = 'X'

print("Let's play Tic Tac Toe!")

while True:
    print_board(board)
    row = int(input("Enter row (0, 1, or 2): "))
    col = int(input("Enter column (0, 1, or 2): "))

    if board[row][col] != ' ':
        print("Cell already taken. Try again.")
        continue

    board[row][col] = player

    if check_winner(board):
        print_board(board)
        print(f"Player {player} wins!")
        break
    elif is_board_full(board):
        print_board(board)
        print("It's a tie!")
        break

    player = 'O' if player == 'X' else 'X'

tic_tac_toe()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant