Manishkumarsingh41 / project

2 stars 0 forks source link

Roll a Dice #1

Open Manishkumarsingh41 opened 9 months ago

Manishkumarsingh41 commented 9 months ago

import random

def roll(): min_value = 1 max_value = 6 roll = random.randint(min_value, max_value)

return roll

while True: players = input("Enter the number of players (2 - 4): ") if players.isdigit(): players = int(players) if 2 <= players <= 4: break else: print("Must be between 2 - 4 players.") else: print("Invalid, try again.")

max_score = 40 playerscores = [0 for in range(players)]

while max(player_scores) < max_score: for player_idx in range(players): print("\nPlayer number", player_idx + 1, "turn has just started!") print("Your total score is:", player_scores[player_idx], "\n") current_score = 0

    while True:
        should_roll = input("Would you like to roll (y)? ")
        if should_roll.lower() != "y":
            break

        value = roll()
        if value == 1:
            print("You rolled a 1! Turn done!")
            current_score = 0
            break
        else:
            current_score += value
            print("You rolled a:", value)

        print("Your score is:", current_score)

    player_scores[player_idx] += current_score
    print("Your total score is:", player_scores[player_idx])

max_score = max(player_scores) winning_idx = player_scores.index(max_score) print("Player number", winning_idx + 1, "is the winner with a score of:", max_score)

Manishkumarsingh41 commented 9 months ago

The given code is a simple implementation of a dice game where players take turns rolling a dice. The game ends when a player reaches a score of 40 or more. The code uses a while loop to continuously ask players for their input and to decide whether they want to roll the dice. The game ends when the maximum score is reached.

breakdown of the code:

The roll() function generates a random number between 1 and 6, representing the result of rolling a dice.

The game starts by asking the user to input the number of players. The input must be a digit between 2 and 4.

The player_scores list is initialized with zeros, representing the initial scores of all players.

The game loop starts. It continues until the maximum score (40) is reached.

In each iteration of the game loop, the code iterates over each player.

For each player, the game prompts the player to roll the dice. The player can choose to roll the dice by typing 'y' or stop rolling by typing anything else.

If the player chooses to roll the dice, the game generates a random number between 1 and 6.

If the player rolls a 1, the game ends their turn and resets their current score to 0.

If the player rolls any other number, the game adds the rolled number to the player's current score.

The player's current score is added to their total score.

The game loop continues until the maximum score is reached.

After the game loop ends, the code finds the index of the player with the maximum score and prints the winner's name and score.