Problem Generator
by David Higgins · in Torque Game Builder · 02/09/2007 (6:38 am) · 2 replies
Ok, I whipped this up last night for my 6 year old ... I intend to extend it, but figured I'd post what I have thus far in case anyone had curiosities about how to do this, or something similar.
math.cs
math.cs
$MAX_NUMBER = 10;
function newProblem()
{
if(isObject($problem)) $problem.delete();
%left = getRandom(0, $MAX_NUMBER);
%right = getRandom(0, $MAX_NUMBER);
%rnd = getRandom(0,1);
switch(%rnd)
{
case 0: %op = "+";
case 1:
%op = "-";
// make sure we aren't doing negative math
%right = getRandom(0, %left);
}
%obj = new t2dTextObject() {
canSaveDynamicFields = "1";
size = "31.750 10.000";
text = %left SPC %op SPC %right SPC "=" SPC "";
font = "Arial";
wordWrap = "0";
hideOverflow = "1";
textAlign = "Left";
lineHeight = "10";
aspectRatio = "1";
lineSpacing = "0";
characterSpacing = "0";
autoSize = "1";
fontSizes = "80";
textColor = "1 1 1 1";
hideOverlap = "0";
mountID = "2";
sceneGraph = sceneWindow2D.GetSceneGraph();
answer = "";
left = %left;
right = %right;
op = %op;
};
error("OBJECT: " @ %obj);
return %obj;
}
function keyPress(%key)
{
echo("KEY: " @ %key);
if(%key == -2) // enter
{
%left = $problem.left;
%right = $problem.right;
%answer = $problem.answer;
switch$($problem.op)
{
case "+":
if((%left + %right) == %answer)
{
$problem = newProblem();
return;
}
case "-":
if((%left - %right) == %answer)
{
$problem = newProblem();
return;
}
case "x":
case "/":
}
// we haven't returned yet, because the solution was incorrect
error("INCORRECT ANSWER!");
$problem.answer = "";
setText();
return;
}
if(%key == -1) // backspace
{
$problem.answer = getSubStr($problem.answer, 0, strlen($problem.answer)-1);
setText();
}
else
{
if(%key == -3) %key = "-";
$problem.answer = $problem.answer @ %key;
}
setText();
}
function setText()
{
%left = $problem.left;
%right = $problem.right;
%op = $problem.op;
%answer = $problem.answer;
$problem.text = %left SPC $problem.op SPC %right SPC "=" SPC %answer;
}About the author
#2
02/09/2007 (6:40 am)
Oh and, the reason for the case statement and getRandom(0,1) for the operator is so I can extend it to include multiplication and division ... just didn't add that yet ...
Associate David Higgins
DPHCoders.com
//--------------------------------------------------------------------------------------------- // Torque Game Builder // Copyright (C) GarageGames.com, Inc. //--------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------- // startGame // All game logic should be set up here. This will be called by the level builder when you // select "Run Game" or by the startup process of your game to load the first level. //--------------------------------------------------------------------------------------------- $problem = false; function startGame(%level) { // Set The GUI. Canvas.setContent(mainScreenGui); Canvas.setCursor(DefaultCursor); moveMap.push(); moveMap.bindCmd("keyboard", "1", "keyPress(1);",""); moveMap.bindCmd("keyboard", "2", "keyPress(2);",""); moveMap.bindCmd("keyboard", "3", "keyPress(3);",""); moveMap.bindCmd("keyboard", "4", "keyPress(4);",""); moveMap.bindCmd("keyboard", "5", "keyPress(5);",""); moveMap.bindCmd("keyboard", "6", "keyPress(6);",""); moveMap.bindCmd("keyboard", "7", "keyPress(7);",""); moveMap.bindCmd("keyboard", "8", "keyPress(8);",""); moveMap.bindCmd("keyboard", "9", "keyPress(9);",""); moveMap.bindCmd("keyboard", "0", "keyPress(0);",""); moveMap.bindCmd("keyboard", "backspace", "keyPress(-1);",""); moveMap.bindCmd("keyboard", "enter", "keyPress(-2);", ""); moveMap.bindCmd("keyboard", "-", "keyPress(-3);", ""); exec("./math.cs"); if( isFile( %level ) || isFile( %level @ ".dso")) sceneWindow2D.loadLevel(%level); $problem = newProblem(); } //--------------------------------------------------------------------------------------------- // endGame // Game cleanup should be done here. //--------------------------------------------------------------------------------------------- function endGame() { sceneWindow2D.endLevel(); moveMap.pop(); }There is an "untitled.t2d" level in the level directory, and it's "empty"
My next step is to add a "help" or "hint" feature, where she can click a GUI control or something and have the number of items from the left and right appear on the screen, so she can count the objects --
I added a 'non-negative' math check to subtraction, and the largest number that can be worked with currently is "10" -- which I also intend to make an option at "start up" so I can simplify it for my younger kids and make it harder as my 6 year old progresses.
Does this already exist? Sure it does ... tons of math games and programs for kids ... why'd I make it? I was bored ...