/// <summary> /// Generates a new random integer and compares it to the user's guess. /// Increments the RollCounter, Guesses, and WinRatio values. Then updates the values in the form. /// </summary> /// <param name="sender">Event Sender</param> /// <param name="e">Event Object</param> private void RollButton_Click(object sender, EventArgs e) { //Generate random number from 1-6 int roll = r.Next(6) + 1; //Holds the last randomly generated roll in for loop so the "rolling" simulation does not show the same # twice in a row int prev = 0; //Simulate rollinng of the dice for (int i = 0; i < 5; i++) { int value = r.Next(6) + 1; while (value == prev) { value = r.Next(6) + 1; } DiceImage.Image = DetermineDiceImage(r.Next(6) + 1); DiceImage.Refresh(); DiceImage.Visible = true; Thread.Sleep(500); prev = value; } DiceImage.Image = DetermineDiceImage(roll); DiceImage.Refresh(); DiceImage.Visible = true; //Increment the number rolled by 1 as well as what the user guessed by 1 RollCounter[roll - 1]++; Guesses[Convert.ToInt32(GuessEntry.Text) - 1]++; //If they won increment 0 by 1 else increment 1 by 1 WinRatio[Convert.ToInt32(GuessEntry.Text) == roll ? 0 : 1]++; ResultsMessage.Text = Convert.ToInt32(GuessEntry.Text) == roll ? "You Win!" : "You Lost!"; //Update the screen UpdateScreenValues(); }
/// <summary> /// Zeros out the Arrays as well as emptying out the messages that start out blank on the screen. /// Updates the current values on the screen. /// </summary> /// <param name="sender">Event Sender</param> /// <param name="e">Event Object</param> private void ResetButton_Click(object sender, EventArgs e) { //Zero out arrays Array.Clear(RollCounter, 0, RollCounter.Length); Array.Clear(Guesses, 0, Guesses.Length); Array.Clear(WinRatio, 0, WinRatio.Length); //Clear out the various messages as well as the input box. GuessEntry.Text = ""; ResultsMessage.Text = ""; ErrorMessage.Text = ""; //Reset the dice picture DiceImage.Image = Properties.Resources.die1; DiceImage.Refresh(); DiceImage.Visible = true; //Call the method to empty the counters and statistics. UpdateScreenValues(); }