Categorized | Math Panic, Tutorials

How-to Guide: Numbers and Operators in 2D Math Panic

Posted on 26 November 2011 by scAdmin

In response to a fan post on the Facebook page, here is a code snippet of how to generate random numbers and equations in 2D Math Panic.

Things to note:

  • Refer to the book  Learning XNA 4.0 before following this example
  • The code below is from the “SpriteManager” class, a DrawableGameComponent
  • Initialize a static Random() object once, then use it when needed
  • In the Update() method, generate the operands and operators for the equation
  • Finally, calculate the correct answer!
The sample code below is a stripped-down version, to illustrate the points above. You will need a Draw() method to draw the equations on the screen, within an existing XNA project as shown in the referenced book.
using System;

public class SpriteManager : DrawableGameComponent
{
    // the 2 numbers used in each equation
    int num1 = 1;
    int num2 = 1;
    // string version of the operator, to show on the screen
    string mathOp = "+";
    // the correct answer, which will be re-calculated every time
    int correctAnswer = 2;
    // the static "random maker"
    static Random randomMaker;

    // set of Math operators
    public enum MathOpSet
    {
        Addition,
        SubtractionEasy,
        SubtractionHard,
        Multiplication,
        Division
    }
    // current choice of Math operator, initialized to addition
    public MathOpSet CurrentMathOp = MathOpSet.Addition;

    // constants
    int minNumber = 1;
    int maxNumber = 9;
    int minAnswer = 2;
    int maxAnswer = 20;

    public override void Initialize()
    {
        // initialize static random maker just once
        randomMaker = new Random();
    }

    public override void Update(GameTime gameTime)
    {
        // generate random numbers to use in equation
        var randomNum1 = randomMaker.Next(minNumber, maxNumber);
        var randomNum2 = randomMaker.Next(minNumber, maxNumber);

        // choose an operator from the set of operators
        int opChoice = randomMaker.Next(1, 6); // 1 through 5
        switch (opChoice)
        {
            case (1):
                {
                    CurrentMathOp = MathOpSet.Addition;
                    mathOp = "+";
                    num1 = randomNum1;
                    num2 = randomNum2;
                    correctAnswer = num1 + num2;
                }
                break;
            case (2):
                {
                    CurrentMathOp = MathOpSet.SubtractionEasy;
                    mathOp = "-";
                    num1 = Math.Max(randomNum1, randomNum2);
                    num2 = Math.Min(randomNum1, randomNum2);
                    correctAnswer = num1 - num2;
                }
                break;
            case (3):
                {
                    CurrentMathOp = MathOpSet.SubtractionHard;
                    mathOp = "-";
                    num1 = Math.Min(randomNum1, randomNum2);
                    num2 = Math.Max(randomNum1, randomNum2);
                    correctAnswer = num1 - num2;
                }
                break;
            case (4):
                {
                    CurrentMathOp = MathOpSet.Multiplication;
                    mathOp = "x";
                    num1 = randomNum1;
                    num2 = randomNum2;
                    correctAnswer = num1 * num2;
                }
                break;
            case (5):
                {
                    CurrentMathOp = MathOpSet.Division;
                    mathOp = "/";
                    num1 = randomNum1 * randomNum2;
                    num2 = randomNum2;
                    correctAnswer = randomNum1;
                }
                break;
            default:
                {
                    CurrentMathOp = MathOpSet.SubtractionEasy;
                    mathOp = "-";
                    num1 = Math.Max(randomNum1, randomNum2);
                    num2 = Math.Min(randomNum1, randomNum2);
                    correctAnswer = num1 - num2;
                }
                break;
        }
    }
}
Email your feedback to games@OnekSoft.com



List Price: $39.99 USD
New From: $21.61 In Stock
Used from: $12.40 In Stock

NCX AZNC 2DMP