I am creating a dice program to further my learning. The program will roll 5 dice and assign random number values to an array of 5 and print out the values. After that I am wanting the player to select the die or dice that he or she would like to keep, display those values and later I will create a method to roll the remaining dice.
I am having a problem with the selecting multiple dice part.
This is what I have so far:
using System;
class HelloWorld {
static void Main() {
Random random = new Random();
int[] diceEach = new int[5];
int diceCount = 1;
for(int i = 0; i < 5; i++)
{
diceEach[i] = random.Next(1, 7);
Console.WriteLine("Dice " + diceCount +": " + diceEach[i]);
diceCount++;
}
Console.WriteLine("To roll again hit R");
Console.WriteLine("Please type the dice numbers that you would like to keep...");
string diceKept = Console.ReadLine();
if(diceKept == "1")
{
Console.Write(diceEach[0]);
}
else if(diceKept == "1")
{
Console.WriteLine(diceEach[1]);
}
else if(diceKept == "3")
{
Console.WriteLine(diceEach[2]);
}
else if(diceKept == "4")
{
Console.WriteLine(diceEach[3]);
}
else if(diceKept == "5")
{
Console.WriteLine(diceEach[4]);
}
else if(diceKept == "r")
{
Console.WriteLine("You have chosen to roll again");
}
Console.ReadLine();
}
}
I have it currently printing out one dice value that you select but I wasn't sure how to print out multiple selections. The only way I can think of is typing out all the options. But that doesn't seem right and would make my code so long.
I was thinking some sort of a loop might work? But I can see how.
This is my first time posting here so hopefully I did it right. Thanks in advance!
Another way you could solve it would be to loop until the user is done selecting dice to keep and wants to re-roll using a do...while loop.
string diceKept;
do
{
Console.WriteLine("Please type the dice number that you would like to keep...and type 'r' to when finished to re-roll remaining dice");
diceKept = Console.ReadLine();
if(int.TryParse(diceKept, out var diceNumber))
{
// I am making an assumption here that the user will always input a number between 1-6.
// It would be best to put some logic to catch that scenario though.
Console.WriteLine(diceEach[diceNumber - 1]);
}
} while (diceKept.ToLower() != "r");
Console.WriteLine("Now re-rolling the rest....");
// Do some re-relling stuff here.
Console.Read()