The question: Create an application that simulates rolling a pair of dice. When the user clicks a button, the application should generate two random numbers, each in the range of 1 through 6, to represent the value of the dice. Use PictureBox controls to display the dice.
I currently have 6 picture boxes with the picture boxes named “dice1PictureBox”, “dice2PictureBox” etc, up to 6.
Here is the code I have written so far. I am completely lost at this point. I am also very new at programming, any help is greatly appreciated. Thank you in advance.
private void rollButton_Click(object sender, EventArgs e)
{
int diceOne;
int diceTwo;
Random rand = new Random();
diceOne = rand.Next(3);
if (diceOne == 0)
{
diceOne.Visible = true;
}
else (diceOne == 1)
{
diceOne.Visible = true;
}
else (diceOne == 2)
{
diceOne.Visible = true;
}
diceTwo = rand.Next(4) + 6;
if (diceOne == 3)
{
diceOne.Visible = true;
}
else (diceOne == 4)
{
diceOne.Visible = true;
}
else (diceOne == 5)
{
diceOne.Visible = true;
}
}
}
}
Let’s go over the general idea here. I’m not going to even bother with how you’re doing things right now, cause it’s not going to do what you want. Period.
All you need is two PictureBoxes, one for each die. You then have 6 images, one for each possible value. I’d suggest keeping the images in an array or perhaps an ImageList (either way, let’s call it
images); it will make things much simpler.When you roll, for each die, you’ll say something like
roll = rand.Next(6);.rollwill then correspond to the index of the image in the array. You set theImageof the corresponding PictureBox toimages[roll](orimages.Images[roll]if you’re using an ImageList). No need to mess withVisible; the two PictureBoxes will always be visible.Just be aware that when you consider the actual value of
roll, it will be from 0 to 5. Add 1 to get the value people expect to see.