private void CheckAnswer() { // Hide the "press the enter key" text promptLabel.Hide(); try { // Evaluate answer if (float.Parse(answerText.Text).Equals(operation.Perform(factor1, factor2))) { // Correct answer TemporaryTitleColor(MetroColorStyle.Green); correct++; } else { // Incorrect answer TemporaryTitleColor(MetroColorStyle.Red); } // Ask next question or exit AskQuestion(); } catch { // Respond to an invalid input FilthyMessage.Show("That's not a real number!"); } }
private void Start() { // Pre-game starting logic string username = usernameText.Text; int questions = (int)questionsNumeric.Value; List <Operation> operations = GetOperations(); if (username.Equals("")) { // Name not set FilthyMessage.Show("You need to type a name!"); } else { // Start game Hide(); FilthyGame game = new FilthyGame(username, questions, operations); game.ShowDialog(); // Return to menu Show(); Activate(); } }
private void AskQuestion() { // Check how many questions have already been answered out of desired total if (question >= questions) { // Notify of game end and close FilthyMessage.Show("Well done! You got " + correct + " out of " + questions + " questions correct."); Close(); } // Next question question++; Random rand = new Random(); // Set label text and invalidate to enforce redraw answerText.Text = ""; Text = "Question " + question; Invalidate(); // Generate factors for new question, ensuring the answer is not decimal (43 / 52 = 0.8269230769230769, for instance) operation = operations[rand.Next(operations.Count)]; int factorMinimum = operation.FactorMinimum; int factorMaximum = operation.FactorMaximum; float answer = 0; do // Do this stuff and repeat if the condition below is met { // Generate factors and store in type-instance scope variables factor1 = rand.Next(factorMinimum, factorMaximum); factor2 = rand.Next(factorMinimum, factorMaximum); answer = operation.Perform(factor1, factor2); // Use a method to apply the operation rather than a switch or similar. This is nice. } while (answer % 1 != 0); // Remainder when divided by 1 greater than zero will cause this loop to repeat until a non-decimal answer is found. // Show the question questionLabel.Text = factor1 + " " + operation.Identifier + " " + factor2; }