Beginner's Guide:
"Statements
and Control of Flow commands" |
There is something lacking in our example
quizz-program, in that you are only allowed to guess once. That's rather unfair, especially considering the odds on getting it right the first time (1:10). So, what we could perhaps do to make the program a little more fun, is to allow the user to guess more than once. How do we do that? We could, of course, type the lines a few times more, so that the question is asked a few times more, like so
A = ran(10)
Input "Guess a number between 0 and 10" B
If A = B then
print right$
else
print wrong$
endif
Input "Guess a number between 0 and 10" B
If A = B then
print right$
else
print wrong$
endif
...
Etc. |
But that is rather inefficient, especially considering that we do not know in advance how soon the user will guess the number, so we don't know how often we need to repeat it. What we need is something which automatically repeats the question until the answer is correct. In other words, we need a conditional loop. A conditional loop repeats itself until a certain expression becomes true - in our example this would be
A = B. There are usually two main types of conditional loops. One where the condition is tested at the beginning of the loop, and one where the condition is tested at the end of the loop. The most unsurprising loop is the
repeat ... until (expression = true) loop. This does just as you would expect (I hope at least that you expect
what it does) - it repeats the lines of code inbetween the repeat and until
statements until the expression between the brackets becomes true.
Here's it applied to our example program:
A = ran(10)
repeat
input "Guess a number between 0 and 10" B
wrong$ = "Sorry, but " + str$(B) + " is completely wrong. Try again."
right$ = "The number " + str$(B) + " is the correct number! Congratulations!"
if A = B then
print right$
else
print wrong$
endif
until (A = B)
print "The game is now over. Thank you for playing ..." |
This does exactly what we need. It repeats everything between
repeat and until, until the expression A = B is true -
in other words, that the guessed number is the same as the randomly picked
number; then it continues with the rest of the program, which prints that the
game is over, and then ends. Also notice that it is much clearer to see how the structure of your program is when you right-indent everything that is within a
control of flow statement. We can enhance our little program a little by also counting the number of turns it took the user to guess the correct number, by changing the last lines to so:
endif
turns = turns + 1
until (A = B)
print "It took you" + str$(turns) + " turns to complete the game."
print "The game is now over. Thank you for playing ..." |
Each time the program runs through the repeat ...
until loop, e.g. each time the user attempts another guess, the variable turns is assigned the value of itself + 1. So if turns was 2 before, when the computer performs the line
turns = turns + 1, then it assigns 2 + 1 (=3) to turns. So from then on,
turns is 3. When the loop is over and the number is guessed, this value is then printed in the penultimate line. To give another example, we could also add a hint option, so that the user will not have to guess blindly, by letting the computer tell the user whether he or she is close to the correct number or not ('warm' or 'not even close...'), by changing the lines between 'else' and 'endif' like so:
else
difference = abs(a - b)
if difference < 3 then
hint$ = "(You're warm though!)"
else
hint$ = "(You weren't even close this time ... )"
endif
print wrong$ + hint$
endif |
The absolute number of the difference between a and b, or abs(a
- b) for short, will give us the difference between a and b, whether a is bigger
than b or b is bigger than a. We can then check if the
difference is smaller than 3, in which case we can add a hint indicating that
the user is not far off with his guess; or, if difference isn't smaller than 3,
indicate that the user should radically alter his guess.
By now, you've learned about some of the most
important concepts of programming. "Really?", I can imagine you
thinking right now. But yes, really. You've learned the basics which allow a
program to react to input from the user in a dynamic way, by using the input as
a parameter in boolean logic and using the boolean logic to steer the computer
through different parts of the program in response.
This means that of any given interactive
situation, if you understand which input is needed to create a desired output,
and which logic is needed in combination with which actions to achieve this,
then you can write a program for that situation that interacts according to the
way you want it to. That sounds very impressive, doesn't it? In less impressive
words, basically you can now write a program that does something you want it to
do, as long as you understand exactly what you want it to do. Again, this is
important. There is a favourite bit from Douglas Adams' Hitchhiker's Guide to
the Galaxy series where humans build a computer that can build a computer which
can answer the question "What is the meaning of life?". After
generations of work, the computer is finally complete, and it comes up with the
answer "42". The humans are puzzled and question the computer, which
dryly answers "Well, are you certain you understood the question?". If
computers reply with something you don't understand, this usually means that you
have not understood the question, and need to rethink your program.
Here is the complete
source so far for the quizz-program, which you can type in and play around with
on your own Playstation 2:
A = int(ran(9)) + 1
count = 0
repeat
count = count +1
input "Guess a number between 0-10: "
B
if A = B then
? "Yep, that's it.
It took you " + str$(count) + " turn(s). Game Over."
else
if abs(A - B) < 3
then
?
"Wrong, but you're close!"
else
?
"No, that's way off ... "
if
A > B then
?
"(Try a little higher)"
else
?
"(Try a little lower)"
fi
fi
fi
until(A = B)
|
Notice a few differences to the code you've seen
so far. I've left out the string variables which I used to store text that I was
going to print for the user, because this way it's a little clearer to follow
the program in the source and makes the source shorter - which is nice for those
of you who use the controller to type this all in (it's doable, but you wouldn't
want to write "War and Peace" with it, now would you?). Also, instead
of print I used the short version ?, which means the same thing,
and instead of using endif, I used fi, which also means the same
thing, for the same reasons. The structure of the program has changed
slightly too - the position of telling the user that the game has ended has, among
others, changed. Study the program a little and see if you can understand if and
why it still works.
Finally, notice that I added the int() function -
the ran function returns a random number, but this is a real number, i.e.
a number that has a fractional part. But when I ask someone to guess a number
between 0 and 10, they'd be rather upset if I told them they were wrong all the
time, and then when they finally give up I tell them the number was 6.81742.
That'd be as hard as guessing a number between 0 and 100.000! To make it even
more precise, I added 1 to the number (now you can also have 0) and made it a
number between 0 and 9, so that with the added 1 you can now only get numbers
that are really between 0 and 10, not including either 0 or 10.
So, here we stand, a little dazzled, but also a
little wizer. Where to now? Well, from here onward, its case study time!
We'll look at some real-life programming problems and work our way through until
we've created a bunch of routines that will provide a good foundation for any
program you may wish to write yourself later on. The first case we'll be
studying is writing routines that read complex combinations of commands from
your controller, such as games like Tekken do, and we'll work our way towards
something resembling the "Tekken Practice Mode" in Tekken Tag (without
the graphics, naturally). After that, or perhaps even at the same time as a
paralel thread, we'll do some graphics' stuff to make you're user interface
(that which the user of your program sees and uses to interact with your
program) a little flashier.
(c) 2001 Arwin van Arum
Sony and Playstation 2 are registered trademarks owned by Sony Inc.
|