NCL 307: Making a Selection
In the last lesson, Keyboard input changed a setting.
Pressing Up increased the temperature. Pressing Down decreased it. Each key directly caused an action.
This time, we'll give the user several choices and let them decide which action to perform.
And we'll use that to build our first game.
Pick a number
Think of a whole number from 1 to 1023.
Don't tell the computer.
Our program will try to find it by asking whether each guess is too high or too low.
The screen will look something like this:
PICK A NUMBER
Think of a number from 1 to 1023.
Is your number higher or lower
than 512?
> HIGHER
LOWER
I PICKED 512!
Up and Down will move the > between the three choices.
Enter will choose the one it points at.
This gives our input loop two different jobs:
- Up and Down change the selection.
- Enter performs the selected action.
Let's build it.
Keeping track of the game
The computer needs to remember which numbers could still be the one you picked.
We'll use three locations:
#low $000
#high $001
#guess $002
MOVE #low 1
MOVE #high 1023
#low contains the lowest number that could still be correct.
#high contains the highest.
#guess will contain the computer's current guess.
To make a guess, add the two ends of the range and divide the result by two:
ADD #guess #low #high
DIV #guess #guess 2
With a range from 1 to 1023, #guess becomes 512.
Binary search
The computer keeps track of the lowest and highest numbers that could still be correct.
Each guess is halfway between them:
guess = (low + high) รท 2
If the player says the answer is higher, everything up to the current guess can be eliminated.
If the player says it is lower, everything from the current guess upward can be eliminated.
This means each answer removes about half of the remaining possibilities.
This method is called a binary search.
Remembering the selection
The game has three choices:
| Selection | Choice |
|---|---|
0 |
HIGHER |
1 |
LOWER |
2 |
I PICKED |
We'll store the current selection in:
#selection $003
MOVE #selection 0
Selection 0 means HIGHER.
Selection 1 means LOWER.
Selection 2 means I PICKED.
The > on the Display will show the player which choice is currently selected.
But the marker itself is not the selection.
The selection is the value stored in #selection.
Turning a selection into a position
Our three choices occupy rows 8, 9, and 10.
That means we can calculate the marker's row directly from #selection:
#row $004
MOVE #row #selection
ADD #row #row 8
When #selection is 0, #row becomes 8.
When it is 1, #row becomes 9.
When it is 2, #row becomes 10.
Then we can draw the marker:
D.CUR 4 #row
D.CHR ">"
The program doesn't need to separately remember where every choice appears.
It remembers the choice, then calculates the Display position from it.
The same stored selection will later decide what happens when the player presses Enter.
The marker is not the selection
#selection is the program's stored choice.
The > is only how that choice is shown on the Display.
The same #selection also decides what happens when the player presses Enter.
Change the stored selection, and both the marker position and the selected action follow from it.
Drawing the game
Most of the Display stays the same while the game is running.
We can draw those parts once:
D.FIL " "
D.CUR 8 1
D.TXT "PICK A NUMBER"
D.CUR 1 3
D.TXT "Think of a number from 1 to 1023."
D.CUR 1 5
D.TXT "Is your number higher or lower"
D.CUR 1 6
D.TXT "than "
D.CUR 6 8
D.TXT "HIGHER"
D.CUR 6 9
D.TXT "LOWER"
D.CUR 6 10
D.TXT "I PICKED "
D.BLT
Only the current guess and the selection marker need to change during play.
The guess appears in two fields:
512?
512!
We'll reuse the field restoration technique from NCL 305:
#restore $005
S.MOVE #restore ""
S.PADR #restore #restore " " 5
S.PADR #restore #restore "\b" 10
Then we can draw the current guess:
D.CUR 6 6
D.CHR #restore
D.CHR #guess
D.CHR "?"
D.CUR 15 10
D.CHR #restore
D.CHR #guess
D.CHR "!"
If a later guess is shorter, the old field is cleared first.
Everything around it stays untouched.
Moving the selection
Now we need to let the player move the marker.
We'll store Keyboard input in:
#key $006
Then wait for a key:
:input
SYS.AKEY #key
S.BEQ :up #key "UP"
S.BEQ :down #key "DOWN"
S.BEQ :choose #key "ENTER"
JUMP :input
Any other key is ignored.
Unlike the temperature controls in the last lesson, Up and Down do not perform the selected action.
They only change which action is selected.
Moving up first checks that we're not already at the first choice:
:up
BLE :input #selection 0
D.CUR 4 #row
D.CHR " "
DEC #selection
MOVE #row #selection
ADD #row #row 8
D.CUR 4 #row
D.CHR ">"
JUMP :input
Moving down does the same thing in the other direction:
:down
BGE :input #selection 2
D.CUR 4 #row
D.CHR " "
INC #selection
MOVE #row #selection
ADD #row #row 8
D.CUR 4 #row
D.CHR ">"
JUMP :input
The bounds checks keep #selection between 0 and 2.
Nothing has been chosen yet.
We've only changed what Enter will do.
Choosing an action
Enter actually performs the selected action.
When it is pressed, #selection tells us which choice the player made:
:choose
BEQ :higher #selection 0
BEQ :lower #selection 1
JUMP :correct
If #selection is 0, execution goes to :higher.
If it is 1, execution goes to :lower.
The only remaining possibility is 2, so execution goes to :correct.
This is the other use of the same selection we used to position the marker.
The player sees:
> HIGHER
LOWER
I PICKED 512!
The program sees:
selection = 0
Both represent the same choice.
Narrowing the range
If the player chooses HIGHER, the current guess and everything below it can be eliminated.
The new lowest possible number is one greater than the current guess:
:higher
BGE :input #guess #high
MOVE #low #guess
INC #low
JUMP :next_guess
Suppose the guess was 512.
After:
MOVE #low #guess
INC #low
#low becomes 513.
The remaining range is now:
513 through 1023
LOWER does the same thing at the other end:
:lower
BLE :input #guess #low
MOVE #high #guess
DEC #high
JUMP :next_guess
If the guess was 512, #high becomes 511.
The remaining range is:
1 through 511
The first branch in each path protects the range.
If #guess is already equal to #high, there cannot be a valid answer above it.
Likewise, if #guess is already equal to #low, there cannot be a valid answer below it.
In either case, the impossible choice is ignored and the program returns to :input.
Making another guess
Once the range changes, the program needs to prepare the next question.
First, calculate another midpoint:
:next_guess
ADD #guess #low #high
DIV #guess #guess 2
Then reset the selection to HIGHER.
Erase the old marker:
D.CUR 4 #row
D.CHR " "
Reset the selection and calculate its row:
MOVE #selection 0
MOVE #row #selection
ADD #row #row 8
Then draw the marker again:
D.CUR 4 #row
D.CHR ">"
Finally, update the two guess fields:
D.CUR 6 6
D.CHR #restore
D.CHR #guess
D.CHR "?"
D.CUR 15 10
D.CHR #restore
D.CHR #guess
D.CHR "!"
JUMP :input
:next_guess prepares everything needed for the next question:
- calculate the new guess;
- reset the selection;
- move the marker;
- update the displayed guess;
- return to the input loop.
Nothing else on the screen changed, so nothing else needs to be redrawn.
Finding your number
Eventually, the computer will display the number you picked.
Move the marker down to:
I PICKED 731!
and press Enter.
That takes us to:
:correct
We can replace the game screen with a victory message:
:correct
D.FIL " "
D.CUR 8 4
D.TXT "I FOUND IT!"
D.CUR 10 6
D.TXT #guess
D.BLT
Execution then reaches the end of the program.
You've made a game.
The complete game
We've now built all of its pieces:
- a range of possible answers;
- a guess calculated from that range;
- a stored selection;
- a marker calculated from that selection;
- Up and Down controls that change the selection;
- Enter to perform the selected action;
- a range that becomes smaller after each answer.
Put them together and we have the complete game.
Complete program
#low $000
#high $001
#guess $002
#selection $003
#row $004
#restore $005
#key $006
MOVE #low 1
MOVE #high 1023
MOVE #selection 0
-- Build the field restoration string.
S.MOVE #restore ""
S.PADR #restore #restore " " 5
S.PADR #restore #restore "\b" 10
-- Make the first guess.
ADD #guess #low #high
DIV #guess #guess 2
-- Draw the parts of the screen that don't change.
D.FIL " "
D.CUR 8 1
D.TXT "PICK A NUMBER"
D.CUR 1 3
D.TXT "Think of a number from 1 to 1023."
D.CUR 1 5
D.TXT "Is your number higher or lower"
D.CUR 1 6
D.TXT "than "
D.CUR 6 8
D.TXT "HIGHER"
D.CUR 6 9
D.TXT "LOWER"
D.CUR 6 10
D.TXT "I PICKED "
D.BLT
-- Draw the current guess.
D.CUR 6 6
D.CHR #restore
D.CHR #guess
D.CHR "?"
D.CUR 15 10
D.CHR #restore
D.CHR #guess
D.CHR "!"
-- Draw the initial selection marker.
MOVE #row #selection
ADD #row #row 8
D.CUR 4 #row
D.CHR ">"
:input
SYS.AKEY #key
S.BEQ :up #key "UP"
S.BEQ :down #key "DOWN"
S.BEQ :choose #key "ENTER"
-- Ignore any other key.
JUMP :input
:up
BLE :input #selection 0
D.CUR 4 #row
D.CHR " "
DEC #selection
MOVE #row #selection
ADD #row #row 8
D.CUR 4 #row
D.CHR ">"
JUMP :input
:down
BGE :input #selection 2
D.CUR 4 #row
D.CHR " "
INC #selection
MOVE #row #selection
ADD #row #row 8
D.CUR 4 #row
D.CHR ">"
JUMP :input
:choose
BEQ :higher #selection 0
BEQ :lower #selection 1
JUMP :correct
:higher
BGE :input #guess #high
MOVE #low #guess
INC #low
JUMP :next_guess
:lower
BLE :input #guess #low
MOVE #high #guess
DEC #high
:next_guess
ADD #guess #low #high
DIV #guess #guess 2
-- Return the selection to HIGHER.
D.CUR 4 #row
D.CHR " "
MOVE #selection 0
MOVE #row #selection
ADD #row #row 8
D.CUR 4 #row
D.CHR ">"
-- Update the guess fields.
D.CUR 6 6
D.CHR #restore
D.CHR #guess
D.CHR "?"
D.CUR 15 10
D.CHR #restore
D.CHR #guess
D.CHR "!"
JUMP :input
:correct
D.FIL " "
D.CUR 8 4
D.TXT "I FOUND IT!"
D.CUR 10 6
D.TXT #guess
D.BLT
Try it
Pick several different numbers and see how quickly the computer finds them.
Try:
1
512
1023
When the computer has narrowed the range to a single number, try choosing HIGHER or LOWER anyway.
The program should refuse to move outside the remaining range.
Then play again and give the computer a wrong answer earlier in the game.
Watch what happens to its later guesses.
The computer doesn't know what number you're thinking of.
It only knows the range it has built from the answers you gave it.
Change the menu
Try changing the initial selection:
MOVE #selection 0
Make the game begin on LOWER or I PICKED instead.
Remember that the initial marker must agree with the value stored in #selection.
Then try changing the order of the three choices on the Display.
What else has to change so that:
- the marker appears on the correct row;
- Up and Down still move through the choices;
- Enter still performs the action shown by the marker?
In this lesson, one stored value represented a choice.
We used that value to decide both where something appeared and what an input would do.
In the next lesson, NCL 308: Moving Around a Board, we'll expand that idea to nine positions on a game board, and give each position something of its own to remember.