Pergunta

It is simple roll the dice application (pig game) in windows form.

After I click the "Roll the dice" botton, if the I rolled dice 1 ,the player's total will revert to 0

And I also want to remove my array I know it is pointless, but a lot of errors appear after I removed array.

How do I achieve this?

 public partial class PigForm : Form {

        Image[] diceImages;
        int[] dice = new int[1] { 0 };
        Random roll = new Random();

        public PigForm() {
            InitializeComponent();
        }

        private void cancelGameBotton_Click(object sender, EventArgs e) {

            this.DialogResult = DialogResult.OK; // This hides the form, and causes ShowDialog() to return in WhichGame form
        }

        private void rollDieBotton_Click(object sender, EventArgs e) {

            RollDice();
            Pig.Hold();

        }

        private void playAnotherGameLabel_Click(object sender, EventArgs e) {

        }

        private void RollDice() {

            for (int i = 0; i < dice.Length; i++){
                var currentRoll = roll.Next(1, 7);
                dice[i] += currentRoll;
                dicePictureBox.Image = diceImages[currentRoll-1];
                playersTotal.Text = String.Format("{0}", dice[i]);
                }//end for

        } // end RollDice

        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;
        } 

    } //end class

} //end namespace
Foi útil?

Solução

Errors occur when removing the array because you probably just remove the brackets and the value at the declaration but you are not modifying the code where you are using the array.

From what i've understood, I removed your array and made your label 'revert' to 0 with the message box appearing if the dice rolled 1. If there are some misconceptions please do not mind to comment.

Image[] diceImages;
int dice = 0; // array removed because it is not needed
Random roll = new Random(); 

private void rollDieBotton_Click(object sender, EventArgs e) {

    RollDice();
    Pig.Hold();

    if (dice == 1)
    {
    playAnotherGameLabel.Text = "0";
    MessageBox.Show("Sorry you have thrown a 1. your turn is over!");
    }

}


private void RollDice() {

    // for loop is not needed because it is no longer an array
        var currentRoll = roll.Next(1, 7);
        dice[i] += currentRoll; // this in my opinion should be = not += because the result of the next roll will be absurd
        dicePictureBox.Image = diceImages[currentRoll-1];
        playersTotal.Text = String.Format("{0}", dice[i]);

} // end RollDice

Outras dicas

I am not sure I understand your question completely, but here is a suggestion:

    private void RollDice()
    {
        bool gameComplete = false;
        for (int i = 0; i < dice.Length; i++)
        {
            var currentRoll = roll.Next(1, 7);
            if (currentRoll == 1)
            {
                dice = new int[1] { 0 };
                gameComplete = true;
            }
            else
            {
                dice[i] += currentRoll;
                dicePictureBox.Image = diceImages[currentRoll - 1];
            }
            playersTotal.Text = String.Format("{0}", dice[i]);
        }//end for

        if (gameComplete)
        {
            MessageBox.Show("Sorry you have thrown a 1. your turn is over!");
        }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top