الرجوع
تسجيل الدخول
أنت تتصفح نسخة مؤرشفة من الموقع اضغط للانتقال إلى الموقع الجديد بكامل المزايا

https://lucid.app/documents

كود بايثون للعبة اكس او (للفائدة فقط):

# Tic-Tac-Toe (Noughts and Crosses) in Python

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

def print_board():
    """Print the game board."""
    print("\n")
    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]}")
    print("\n")

def check_winner(player):
    """Check if the player has won the game."""
    win_combinations = [
        [0, 1, 2], [3, 4, 5], [6, 7, 8],  # Rows
        [0, 3, 6], [1, 4, 7], [2, 5, 8],  # Columns
        [0, 4, 8], [2, 4, 6]              # Diagonals
    ]
    for combo in win_combinations:
        if board[combo[0]] == board[combo[1]] == board[combo[2]] == player:
            return True
    return False

def is_draw():
    """Check if the game is a draw."""
    return ' ' not in board

def play_game():
    """Main game loop."""
    current_player = 'X'
    
    while True:
        print_board()
        print(f"Player {current_player}'s turn.")
        
        try:
            move = int(input(f"Choose a position (1-9) for {current_player}: ")) - 1
            if board[move] == ' ':
                board[move] = current_player
            else:
                print("Invalid move, position already taken.")
                continue
        except (IndexError, ValueError):
            print("Invalid input. Please choose a number between 1 and 9.")
            continue
        
        if check_winner(current_player):
            print_board()
            print(f"Player {current_player} wins!")
            break
        
        if is_draw():
            print_board()
            print("It's a draw!")
            break
        
        # Switch player
        current_player = 'O' if current_player == 'X' else 'X'

# Start the game
if __name__ == "__main__":
    play_game()