I am a beginner at C# and I have a homework assignment where I have to write a dice roll simulator.

The user can chose a maximum of 5 die and then as they roll each one the sum is added up with the one rolled previously. At the end the number of rolls and the the total sum is presented.

However, if the user rolls a 6 then this result is not added to the sum but the die is rolled twice more and then the sum is added.

I have a few points where I have gotten stuck and/or would like to know a better way of writing it. I have included a snippet below.

  1. Below I have stated my Random class every time the dice rolls but is it possible for me to do this only once and then use throughout my code?

  2. Is there a simple way to write the part with what happens if you roll a 6? I mean I guess I could write another if statement every time the die rolls but seems that this would very long?

if (intnumberofdie == 1)

            {
                Random rnd1 = new Random();
                int dice1 = rnd1.Next(1, 7);

                Console.WriteLine("You have rolled " + dice1);


            else if (intnumberofdie == 2)

            {
                Random rnd1 = new Random();
                int dice1 = rnd1.Next(1, 7);

                Console.WriteLine("The first die rolled " + dice1);

                Console.ReadKey();

                Random rnd2 = new Random();
                int dice2 = rnd2.Next(1, 7);

                Console.WriteLine("The second die has rolled " + (dice2));

                Console.ReadKey();

                Console.WriteLine("The total sum so far is " + (dice1 + dice2));

}

2 Answers

0
Joelius On

Since you are a beginner I'll try to explain a few things first. If you have a number of times you have to do the same thing, you should never handle each case yourself. This means you shouldn't use the if else if else.. but instead use a loop like for or while.

I have written a short function which should do what you want.

private int GetNumber(int amountDie) {
    if (amountDie <= 0 || amountDie > 5) return 0;

    int total = 0;

    for (int i = 0; i < amountDie; i++)
    {
        int roll = rnd.Next(1, 7);

        if (roll == 6)
        {
            int specialRoll1 = rnd.Next(1, 7);
            int specialRoll2 = rnd.Next(1, 7);

            total += specialRoll1;
            total += specialRoll2;
        }
        else {
            total += roll;
        }
    }

    return total;
}

Now some more explaining: Since you said that you can't have more than 5 die, we will first check if the value given was valid, otherwise we'll just return 0 (there are better things to do in this case but for simplicity we'll just say they get back 0).
After that we declare a total to keep track of how many points we have to return.
Then we start the loop. For each dice, we roll once (first line in the for-construct). Since we need to check if the user rolled a 6, we have this if there. Inside it we roll two die and add both results to the total. Note, that for these two rolls 6 is just like any other number and not handled differently (if you need that, it would be a bit more complicating).
If the user didn't roll a 6, we just add whatever they rolled to the total.

After we've done that for all die we simply return the total we got.

You'll see that if you just copy paste this code, it will not compile. The reason for that is that you are missing a Random-object. In your code you always created a new one everytime you rolled. This is a problem since the Random class isn't as random as you might think and you should always create just one for many random numbers.
Thats why I added this at the top of my class:
private static readonly Random rnd = new Random();

This random-object is only created once and then used for every die. This gives you a lot more randomness and prevents that every user always rolls the same value.

I hope this shows you how you can create a method like this. Of course you can add the console-output etc like you did in your code.

1
Enigmativity On

Give this one a go:

var rnd1 = new Random();
var rollsRemaining = 6;
var sum = 0;
while (rollsRemaining > 0)
{
    int dice1 = rnd1.Next(1, 7);
    Console.WriteLine("You have rolled " + dice1);
    if (dice1 == 6)
    {
        rollsRemaining += 2;    
    }
    else
    {
        sum += dice1;
        rollsRemaining -= 1;
    }
}
Console.WriteLine("The sum is " + sum);