Question

Currently the dice image will change after I click the button "Click to roll the die".
And I also want to update the label "Player's total" when I click the "Click to roll the dice" bottom.

How do I achieve this?

private void rollDieBotton_Click(object sender, EventArgs e) 
{
    RollDice();
}       

private void RollDice() 
{
    for (int i = 0; i < dice.Length; i++)
        dice[i] = roll.Next(1, 6);

    dicePictureBox.Image = diceImages[dice[0]];
}

private void PigForm_Load(object sender, EventArgs e) 
{
    diceImages = new Image[6];
    diceImages[0] = Properties.Resources.Alea_1;
    diceImages[1] = Properties.Resources.Alea_2;
    diceImages[2] = Properties.Resources.Alea_3;
    diceImages[3] = Properties.Resources.Alea_4;
    diceImages[4] = Properties.Resources.Alea_5;
    diceImages[5] = Properties.Resources.Alea_6;

    dice = new int[1] { 0 };

    roll = new Random();
} 
Was it helpful?

Solution

You could use the dice value to sum the rolls instead of just store the current roll and do something like:

private void RollDice() 
{
    for (int i = 0; i < dice.Length; i++)
    {
        var currenRoll = roll.Next(1, 6);
        dice[i] += currentRoll;
        dicePictureBox.Image = diceImages[currentRoll];

        playerTotalLabel.Text = String.Format("Total: {0}", dice[i]);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top