Regardless of how complicated it may seem, all programs have an underlying framework. Once this framework is understood, the program suddenly becomes clear. It's up to you, when you are designing the game, to choose the one most fitting for that game, and stick to it. There are two main formats you could choose:
Some programs are constructed with the classic "goto-label" format, in which each condition causes the program to jump to a new label, which marks a piece of code. For simple programs, this is the easiest to use, but the way it jumps around the program makes it highly impractical to use as the core structure of a complex program.
A simple guessing game using a goto-label framework might look as follows:
:ClrHome :randInt(0,100)->A :Lbl 1 :Input "GUESS",B :If A<B :Goto 2 :If A>B :Goto 3 :If A=B :Goto 4 :Lbl 2 :Disp "TOO HIGH" :Pause :ClrHome :Goto 1 :Lbl 3 :Disp "TOO LOW" :Pause :ClrHome :Goto 1 :Lbl 4 :Disp "YOU GOT IT" :Pause :ClrHome :Return
You should be able to see how these commands work. The Goto
command jumps to a part of the code marked with a Lbl
(no matter if it comes before or after), skipping the code after the Goto
. Valid labels can be given a name up two numbers or characters long. If you name two labels with the same name, only the first one counts.
The problem with this structure is that Goto
jumps are inherently slow. To find a label, the calculator searches through the entire program to find it, and when it does, it leaves a "trace" of the label. If you jump too many times from inside conditional statements such as If
, While
, and Repeat
, you'll start noticing the program slowing down, eventually ending in a memory error. None of these effects are permanent, but they do get annoying when playing a game.
In this format, the program is controlled by conditional statements in conjunction with commands such as If
and While
. It's pretty clear what these commands do: If
by itself (without a then) tests a condition and executes the next line if it is true. You saw it in action in the previous example. If you follow an If
statement with a Then
on the next line, it still tests a condition, but it executes the entire block of code until an End
is reached if it is true (the if-then-end structure). While
simply repeats the code block between itself and an End
while a condition is true; the condition is checked first, and if it is not true, it skips the entire block. Repeat
does the opposite, executing the block and then checking if the condition is not true, repeating if it is false. Be sure to note the differences between these commands.
Here's our guessing game using a conditional structure instead of labels:
:ClrHome :randInt(0,100)->A :While A≠B :Input "GUESS",B :If A<B :Then :Disp "TOO HIGH" :Pause :ClrHome :End :If A>B :Then :Disp "TOO LOW" :Pause :ClrHome :End :End :Disp "YOU GOT IT" :Pause :ClrHome :Return
As you can see, this program does the same thing as the first one, but using a While
loop and some if-then-end statements for control.