/// <summary> /// Confirms that the values entered are correct and launches the actual game /// window /// </summary> /// <param name="sender">Object calling the event</param> /// <param name="e">Arguments passed by the event</param> private void BtnStartMenuForm_Click(object sender, EventArgs e) { if (InputTester.IsInteger(TxtBLowerValueMenuForm.Text)) // Test if lower value is correct data type { lowerValue = int.Parse(TxtBLowerValueMenuForm.Text); if (InputTester.IsInteger(TxtBUpperValueMenuForm.Text)) // Test if upper value is correct data type { upperValue = int.Parse(TxtBUpperValueMenuForm.Text); try { // Test if the bounds contain a valid range if (int.Parse(TxtBLowerValueMenuForm.Text) < int.Parse(TxtBUpperValueMenuForm.Text)) { // Launch the game form GameForm gameForm = new GameForm(lowerValue, upperValue); DialogResult gameFormResult = gameForm.ShowDialog(this); this.TxtBLowerValueMenuForm.Text = "1"; // Reset menu form values after game completes this.TxtBUpperValueMenuForm.Text = "10"; } else { throw new InvalidRangeException(); // If the range is invalid, throw exception } } catch (InvalidRangeException ex) // Catch exception and launch exception handler { // with error message Console.WriteLine("The upper value is lower than the lower value"); InputTester.ExceptionHandler(ex, ""); } } } }
/// <summary> /// Start evaluation of guess /// </summary> /// <param name="sender">Object sending the event</param> /// <param name="e">Event details</param> private void BtnOKGameForm_Click(object sender, EventArgs e) { int inputValue = 0; if (InputTester.IsInteger(TxtBGuessGameForm.Text)) // Test if input is correct data type { inputValue = int.Parse(TxtBGuessGameForm.Text); try // Test if entered value is within specified bounds { if ((inputValue < this.LowerValue) || (inputValue > this.UpperValue)) { throw new ValueOutOfBoundsException(); // If not in bounds, throw exception } } catch (ValueOutOfBoundsException ex) // Catch thrown exception and provide feedback to user { Console.WriteLine("The player guess it outside of player defined bounds"); InputTester.ExceptionHandler(ex, "Your guess it outside of the bounds specified," + "please try again."); } if (inputValue > this.GuessValue) // Test if the value is low, high or a winning number { ListBoxGameForm.Items.Add("Your guess of " + inputValue + " was HIGH"); ListBoxGameForm.Items.Add(""); } else if (inputValue < this.GuessValue) { ListBoxGameForm.Items.Add("Your guess of " + inputValue + " was LOW"); ListBoxGameForm.Items.Add(""); } else if (inputValue == this.GuessValue) { ListBoxGameForm.Items.Add("CONGRATULATIONS!!! YOU WON!!!"); ListBoxGameForm.Items.Add(""); this.Timer1.Stop(); DisplayLocalMessage("Win"); // Display message box stating game was won this.Close(); // Close this form } } }