Game Development Reference
The aj`C]ia method looks to see whether the game has been won or lost by checking whether the
c]iaSkj variable is pnqa or b]hoa. It then displays the appropriate message. (Can you figure out how
string concatenation is being used to display these messages? Compare the code with what you see in
the game's output text field. I'm sure you can do it!)
eb statements work by running their directives if the condition in the parentheses is
pnqa. With >kkha]j variables, there are two ways to check whether they're pnqa or
b]hoa. You can use an equality operator (a double-equal sign) and compare it with
a pnqa or b]hoa value, like this:
If c]iaSkj is b]hoa, this statement evaluates as b]hoa, and the directives don't run. If
c]iaSkj is pnqa, the statement evaluates as pnqa, and the directives run.
You should be familiar with that way of checking for pnqa or b]hoa conditions from the
previous chapter. However, if the condition you're checking happens to be the value of
a >kkha]j variable, it's much more convenient and often makes your code easier to
read to use this shorthand:
Because the value of c]iaSkj can be only pnqa or b]hoa, it provides exactly the same
information as the first example.
You'll be using this as the preferred way for checking the value of >kkha]j variables in
eb statements throughout the topic. eb$c]iaSkj% is very close to the English phrase “If
the game is won.” Choose the names of your variables carefully to really help to make
your programs easier to read.
How does the program know whether the c]iaSkj variable is pnqa or b]hoa?
You initialized c]iaSkj to b]hoa at the very top of the program. That means it will always be b]hoa
unless you change it to pnqa somewhere in the game. You would change it to pnqa only if the player
actually meets the conditions for winning the game. In this game, there is only one way the player can
win: by guessing the correct number.
That makes things really easy because you know that if players still have enough guesses remaining,
and they've guessed the correct number, they must have won the game. So all you need to do is set
the c]iaSkj variable to pnqa and call the aj`C]ia method in the same eb+ahoa block that checks
whether the player's guess is correct. Easy!