Game Development Reference
In-Depth Information
Whenever you find yourself using a lot of the same text over and over again in any of your programs,
try storing it in a variable: you'll save yourself a lot of typing and a lot of trouble in the long run.
Using uint vs. int variables
Did you try making more than the ten guesses the game said you had remaining? If you did, you would
have noticed that the output text field displayed something like this: Guesses Remaining: 4294967295 .
Figure 5-16 shows an example.
Figure 5-16. Make too many guesses, and you'll see output like this.
At the moment. you haven't programmed the game to end when the player has run out of guesses, so
the game keeps on subtracting 1 from the cqaooaoNai]ejejc variable even after the 10 guesses are
used up. After it passes 0, it suddenly jumps to this crazy long number: 4294967295. No, your program
isn't about to join a secret botnet of computers plotting to overthrow the human race; there is actu-
ally very interesting logic behind it.
You decided to declare the cqaooaoNai]ejejc variable as a qejp type. qejp variables store whole
numbers that are only positive. They can never be negative. When a variable declared as a qejp type
does try to become less than zero, exactly the opposite thing happens: it flips to the maximum pos-
sible number, which happens to be 4294967295.
This is very important to know because sometimes you need or want to know whether a number has
run into negative territory. In those cases, make sure that you declare the variable as an ejp type. (As
mentioned before, ejp stands for integer , which is a whole number that can be positive or negative.)
To see the effect that changing the cqaooaoNai]ejejc variable to an ejp type has on your program,
try it and see what happens!
1. Change the line of code near the top of your program that declares the cqaooaoNai]ejejc
variable so that its type is set to ejp:
r]ncqaooaoNai]ejejc6 ejp 7
2. Save the I]ej*]o file and test the program.
3. Click the Guess button more than ten times. You should see the Guesses Remaining become
negative, as shown in Figure 5-17.
 
Search Nedrilad ::




Custom Search