Untitled

 avatar
unknown
plain_text
5 months ago
1.8 kB
2
Indexable
import random

# Function to simulate the dice roll (1 to 6)
def roll_dice():
    return random.randint(1, 6)

# Board setup: Dictionary with key as start square, value as the destination square
snakes = {
    16: 6,
    47: 26,
    49: 11,
    56: 53,
    62: 19,
    64: 60,
    87: 24,
    93: 73,
    95: 75,
    98: 78
}

ladders = {
    1: 38,
    4: 14,
    9: 31,
    21: 42,
    28: 84,
    36: 44,
    51: 67,
    71: 91,
    80: 100
}

# Function to move the player
def move_player(position):
    roll = roll_dice()
    print(f"Rolled a {roll}")
    position += roll
    
    # Check if player lands on a snake or ladder
    if position in snakes:
        print(f"Oops! Bitten by a snake! Moving down to {snakes[position]}")
        position = snakes[position]
    elif position in ladders:
        print(f"Yay! Climbed a ladder! Moving up to {ladders[position]}")
        position = ladders[position]
    
    return position

# Main function for playing the game
def play_game():
    player1_pos = 0
    player2_pos = 0
    turn = 1

    # Loop until one player reaches or exceeds 100
    while player1_pos < 100 and player2_pos < 100:
        if turn % 2 != 0:
            print("\nPlayer 1's turn:")
            player1_pos = move_player(player1_pos)
            print(f"Player 1 is now on square {player1_pos}")
        else:
            print("\nPlayer 2's turn:")
            player2_pos = move_player(player2_pos)
            print(f"Player 2 is now on square {player2_pos}")
        
        # Check if anyone has won
        if player1_pos >= 100:
            print("\nPlayer 1 wins!")
            break
        elif player2_pos >= 100:
            print("\nPlayer 2 wins!")
            break
        
        turn += 1
        print("-" * 30)  # Separator for turns

# Start the game
if __name__ == "__main__":
    play_game()
Editor is loading...
Leave a Comment