Sure! Here's a basic implementation of Tic Tac Toe game in Python:

# Tic Tac Toe

# Create the board
board = [' ' for _ in range(9)]

# Function to display the board
def display_board():
    print(f' {board[0]} | {board[1]} | {board[2]} ')
    print('---+---+---')
    print(f' {board[3]} | {board[4]} | {board[5]} ')
    print('---+---+---')
    print(f' {board[6]} | {board[7]} | {board[8]} ')

# Function to check if a player has won
def check_win(player):
    # Check rows
    for i in range(0, 9, 3):
        if board[i] == board[i+1] == board[i+2] == player:
            return True
    # Check columns
    for i in range(3):
        if board[i] == board[i+3] == board[i+6] == player:
            return True
    # Check diagonals
    if board[0] == board[4] == board[8] == player:
        return True
    if board[2] == board[4] == board[6] == player:
        return True
    return False

# Function to check if the board is full
def check_full():
    return ' ' not in board

# Function to play the game
def play_game():
    current_player = 'X'
    while True:
        display_board()
        position = int(input(f"Player {current_player}, choose a position (1-9): "))
        position -= 1
        if board[position] == ' ':
            board[position] = current_player
            if check_win(current_player):
                display_board()
                print(f"Player {current_player} wins!")
                break
            elif check_full():
                display_board()
                print("It's a tie!")
                break
            current_player = 'O' if current_player == 'X' else 'X'
        else:
            print("Invalid move. Try again.")

# Start the game
play_game()

You can run this code in your Python environment and play Tic Tac Toe by entering the position number (1-9) when prompted. Enjoy the game

write me tic tac toe game in python

原文地址: https://www.cveoy.top/t/topic/hXyS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录