Introduction
There are five simple operations which a grade school student learns: addition, subtraction, multiplication, and that dreaded division. Wait?!? that's only four!
This project is simple to make and easy to play. It has three different skill levels which allows grade school kids to learn and practice their skills while playing a game, and more difficult levels that will challenge almost anyone. As the clock runs down, pressing you for time, you must try to solve the problems one after another.
The game is child's-play.
You have a current value which you try to manipulate into the target value by using the keyboard and the assigned operation. When you click on a numbered button, that number is the second operand in the mathematical operation. In the picture above, pressing the number '2' will result in 43÷2=21 (values are rounded down to the nearest integer). Since this 21 is not the target value 26, the current value of 43 is replaced by 21 and the clock keeps counting down. The list of 'next operations' is shifted over, and the symbol that was on the left most end of that list is the new 'assigned operation'. This continues until the target value is reached and a new level, or problem, is generated and the clock is reset.
It can be a lot of fun, and the Expert level is pretty tough too, so even if you think you're a wiz at math, this is probably better than playing Solitaire at the office.
Remember grade four? When 23÷7=3 remainder 2? Well, in this game, you'll discover that elusive 'fifth operation' called 'modulo'. It basically asks 'if I divide one number by another number what is the remainder?', and so 23 modulo 7 is equal to 2.
Using the code
This project stands by itself. Its just a game, and isn't intended to be made into anything more than an example of an easy-to-make and simple-to-play game which will hone your math skills. Just download the zip file, decompress its contents, then load it in your C# integrated development environment and press F5. You'll be up and running.
Points of interest
There are a few things here for beginners to bite on, but mostly, I'd like to introduce you to something called the "conditional assignment". Its a little known C-language syntax that I have grown fond of. Essentially, it asks a question that results in true or false, and then returns one of two values depending on the result of that question.
It looks like this:
<variable> = <condition> ? <true result> : <false result>;
For example:
x = (y>=0) 1 : -1;
where x is assigned 1, if y is greater than or equal to zero; if not, then x is set to -1.
If we were to write our own C# function that did the same thing, it could look like this:
public int ConditionalAssignment(bool bolCondition,
int intTrueResult, int intFalseResult)
{
if (bolCondition)
return intTrueResult;
else
return intFalseResult;
}
But, the conditional assignment is much much more because it can return anything, and is not limited to any particular type, like this example which takes and returns integers exclusively.
For example:
string strName = (bolInformal ? "Bob" : "Mr.Robert Fourmel");
Here, we ask if the boolean variable 'bolInformal
' is set or not, then assign the correct name to the string variable we just created, strName
.
In the function LevelCompleted()
, you'll find the line:
intScore += intLevelDelay * ((mnuEasy.Checked) ? 1 : (mnuNormal.Checked) ? 2 : 3);
This line embeds a conditional assignment as the false return value, and could also be written as:
int intIncrementFactor;
if (mnuEasy.Checked)
intIncrementFactor = 1;
else if (mnuNormal.Checked)
intIncrementFactor = 2;
else
intIncrementFactor = 3;
intScore = intScore + intLevelDelay * intIncrementFactor;
Both these examples result in the same thing, so neither is wrong. Some may argue that one is easier to read than the other, but won't always agree on which one. You can embed as many of these as you like, though you might want to consider opting for a Switch/Case scenario if you do; but, here's an example:
bool bolFirstCondition = false,
bolSecondCondition = false,
bolThirdCondition = true,
bolFourthCondition = false;
int intExample = (bolFirstCondition ? 1 :
(bolSecondCondition ? 2 :
(bolThirdCondition ? 3 :
(bolFourthCondition ? 4 : 5))));
Since bolThirdCondition
is the first condition encountered that has a 'true' result, our variable intExample
is set to the value 3.
If you like, you can include functions in either the condition part of the conditional assignment or the resultant values. For example:
intScore = (rndGenerator.Next(5) > 2) ? firstFunction() : secondFunction();
Here, we assign intScore
the result of the function firstFunction()
if the random number generator generates a number which is greater than 2, and the results of the function secondFunction()
are assigned to intScore
if the number generated is two or less.
There's lots you can do with this.