NCL 308: Moving Around a Board
In the last lesson, we used one value in #selection to represent one of three choices.
This time, we'll expand the same idea to nine positions on a game board.
We're going to start building Noughts and Crosses.
The board has nine playable squares:
| 0 | 1 | 2 |
| 3 | 4 | 5 |
| 6 | 7 | 8 |
Just like the three choices in the last lesson, we can represent the current selection with one number.
This time:
0means the upper-left square;4means the centre square;8means the lower-right square.
By the end of this lesson, you'll be able to move around the board with the arrow keys and place crosses with Enter.
We won't have another player yet, and the program won't know how to win.
First, we need a board.
Drawing the board
Our game will look something like this:
╳│╳│╳
─┼─┼─
╳│╳│╳
─┼─┼─
╳│╳│╳
ARROWS MOVE
ENTER PLACE
ESC TO EXIT
Of course, we won't begin with nine crosses. The playable squares will start empty.
Each position on the board occupies one complete physical Display tile.
NCS/e provides fullwidth versions of the characters in the Unicode 25XX range in its private E5XX range. The last two digits remain the same.
For example, the ordinary │ is U+2502. Its NCS/e fullwidth counterpart is U+E502.
We'll use fullwidth versions of ─, │, and ┼ to build the board.
We also need a fullwidth empty space:
#empty "\u3000"
U+3000 is the ideographic space. Unlike an ordinary space, it occupies an entire Display tile.
Our board begins at (11, 2).
The playable squares begin at these positions:
| Square | X | Y |
|---|---|---|
0 |
11 |
2 |
1 |
15 |
2 |
2 |
19 |
2 |
3 |
11 |
4 |
4 |
15 |
4 |
5 |
19 |
4 |
6 |
11 |
6 |
7 |
15 |
6 |
8 |
19 |
6 |
The board itself is five physical tiles wide and five physical tiles tall.
Three rows and columns contain playable squares. The others contain the walls between them.
Drawing the board
#empty "\u3000"
D.FIL " "
D.CUR 11 2
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.CUR 11 3
D.TXT "\uE500\uE53C\uE500\uE53C\uE500"
D.CUR 11 4
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.CUR 11 5
D.TXT "\uE500\uE53C\uE500\uE53C\uE500"
D.CUR 11 6
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.CUR 9 8
D.TXT "ARROWS MOVE"
D.CUR 9 9
D.TXT "ENTER PLACE"
D.CUR 9 10
D.TXT "ESC TO EXIT"
D.BLT
Now we need to turn a selection number from 0 through 8 into one of those Display positions.
From one number to two coordinates
We'll begin with these working locations:
#selection $000
#column $001
#row $002
#x $003
#y $004
Start with the upper-left square selected:
MOVE #selection 0
The board is three squares wide.
That means we can find the column with MOD:
MOD #column #selection 3
and the row with integer division:
DIV #row #selection 3
Suppose:
selection = 7
Then:
7 MOD 3 = 1
7 DIV 3 = 2
So square 7 is in column 1, row 2.
The same pattern works across the entire board:
| Selection | Column | Row |
|---|---|---|
0 |
0 |
0 |
1 |
1 |
0 |
2 |
2 |
0 |
3 |
0 |
1 |
4 |
1 |
1 |
5 |
2 |
1 |
6 |
0 |
2 |
7 |
1 |
2 |
8 |
2 |
2 |
Now we have coordinates on the game board.
We still need coordinates on the Display.
Horizontally, each playable square begins four logical character positions after the previous one:
MUL #x #column 4
ADD #x #x 11
Vertically, each playable row begins two Display rows after the previous one:
MUL #y #row 2
ADD #y #y 2
Together:
MOD #column #selection 3
DIV #row #selection 3
MUL #x #column 4
ADD #x #x 11
MUL #y #row 2
ADD #y #y 2
Now any selection from 0 through 8 can be turned into a Display position.
| Selection | Column | Row | X | Y |
|---|---|---|---|---|
0 |
0 |
0 |
11 |
2 |
2 |
2 |
0 |
19 |
2 |
4 |
1 |
1 |
15 |
4 |
7 |
1 |
2 |
15 |
6 |
8 |
2 |
2 |
19 |
6 |
One number has given us a position in two dimensions.
Showing the selection
An empty square is normally drawn as:
#empty "\u3000"
To show that a square is selected, we'll draw the same character inverted:
D.COL #D.COL.WHITE #D.TXT.INVERT
D.CUR #x #y
D.CHR #empty
D.COL #D.COL.WHITE #D.TXT.NORMAL
The square itself hasn't changed.
It is still empty.
We're only drawing it differently to show the player which position is selected.
This is the same distinction we used in the previous lesson:
#selectionis what the program remembers.
The inverted square is how the Display shows it.
Moving around the board
We'll read the Keyboard just as before:
#key $005
:input
SYS.AKEY #key
S.BEQ :left #key "LEFT"
S.BEQ :right #key "RIGHT"
S.BEQ :up #key "UP"
S.BEQ :down #key "DOWN"
S.BEQ :place #key "ENTER"
S.BEQ :exit #key "ESC"
JUMP :input
Each arrow key changes #selection by one square.
The useful pattern is:
LEFT selection - 1
RIGHT selection + 1
UP selection - 3
DOWN selection + 3
But first we need to make sure the requested square exists.
Left and right
For horizontal movement, the current column tells us whether we're at an edge:
:left
MOD #column #selection 3
BEQ :input #column 0
DEC #selection
If the column is 0, there is nowhere farther left.
Moving right is the opposite:
:right
MOD #column #selection 3
BEQ :input #column 2
INC #selection
Up and down
The top row contains selections 0, 1, and 2.
So:
:up
BLT :input #selection 3
SUB #selection #selection 3
The bottom row begins at selection 6:
:down
BGE :input #selection 6
ADD #selection #selection 3
These checks keep #selection between 0 and 8 without allowing horizontal movement to wrap around the sides of the board.
Changing the number is easy.
The next problem is drawing the board correctly after it changes.
Remembering the board
So far, every square has been empty.
Now let's let the player put something in one.
We'll use the fullwidth NCS/e version of ╳:
#cross "\uE573"
Suppose the player puts a cross in square 4, then moves to square 5.
The Display still needs to show that square 4 contains a cross.
That means the program needs to remember the contents of every square independently.
We'll use nine locations:
#cell0 $020
#cell1 $021
#cell2 $022
#cell3 $023
#cell4 $024
#cell5 $025
#cell6 $026
#cell7 $027
#cell8 $028
For now, each cell can contain one of two values:
| Value | Meaning |
|---|---|
0 |
Empty |
1 |
Cross |
The cells begin at 0, so the board begins empty.
If the player puts a cross in square 4:
MOVE #cell4 1
The program remembers:
cell4 = 1
The Display represents that value as:
╳
The board itself is made from stored values.
The Display turns those values into something the player can see.
Board state and Display state
The Display is not the board.
The nine cell values are the board state the program remembers.
What appears on the Display is a representation of that state.
This is why a cross can disappear from the selected position without being forgotten: the program can always draw it again from the stored cell value.
Drawing what belongs in a square
Our old movement code could restore the previous square with:
D.CHR #empty
That won't work anymore.
If the player moves away from a square containing a cross, we need to redraw the cross normally.
If the player moves onto a square containing a cross, we need to redraw the cross inverted.
Several different parts of the program now need to perform the same job:
Draw a particular board square using its current contents and selection state.
We don't want to write that complete drawing logic every time.
So we'll make one section of the program responsible for drawing a square.
There's just one awkward problem.
We know how to get there:
JUMP :draw_cell
But after it finishes drawing, where should it go?
Remembering where to return
Normally, we've given JUMP its destination directly:
JUMP :input
A label represents a location in the program.
That location can also be stored as a value:
#return $006
MOVE #return :after_draw
Then JUMP can use that stored destination:
JUMP #return
That gives us a way to temporarily leave one part of the program, use some shared code, and then come back.
For example:
MOVE #return :after_draw
JUMP :draw_cell
:after_draw
The shared drawing code eventually finishes with:
JUMP #return
Follow the execution:
MOVEstores the location of:after_drawin#return.JUMP :draw_cellgoes to the shared drawing code.:draw_celldoes its work.JUMP #returnreturns execution to:after_draw.
Another part of the program can use the same drawing code by storing a different destination first.
We'll improve this soon
We're manually remembering where shared code should return.
It works, but every use needs a return destination, and the caller has to store that destination before jumping.
Keep that inconvenience in mind.
NCL has a better way to solve this problem, and we'll meet it soon.
Drawing any cell
The drawing code needs to know which square we want.
We'll give it two more working locations:
#draw_cell $007
#value $008
Before using the drawing code, put the desired square number in #draw_cell:
MOVE #draw_cell #selection
Then the drawing code has to find the corresponding board value.
For example:
:draw_cell
BEQ :draw0 #draw_cell 0
BEQ :draw1 #draw_cell 1
BEQ :draw2 #draw_cell 2
BEQ :draw3 #draw_cell 3
BEQ :draw4 #draw_cell 4
BEQ :draw5 #draw_cell 5
BEQ :draw6 #draw_cell 6
BEQ :draw7 #draw_cell 7
JUMP :draw8
Each destination copies the appropriate cell into #value:
:draw0
MOVE #value #cell0
JUMP :paint
:draw1
MOVE #value #cell1
JUMP :paint
:draw2
MOVE #value #cell2
JUMP :paint
and so on through #cell8.
It's repetitive, but the purpose is simple:
Given a square number, put that square's stored value in
#value.
The complete program at the end contains all nine cases.
Once we have #value, we can choose what character belongs there:
#glyph $009
:paint
S.MOVE #glyph #empty
BEQ :have_glyph #value 0
S.MOVE #glyph #cross
:have_glyph
If the cell contains 0, #glyph becomes the empty square.
If it contains 1, #glyph becomes the cross.
Then calculate the Display position from #draw_cell:
MOD #column #draw_cell 3
DIV #row #draw_cell 3
MUL #x #column 4
ADD #x #x 11
MUL #y #row 2
ADD #y #y 2
We now know:
- what belongs in the square;
- where the square appears.
One thing remains: whether it should appear selected.
Drawing the selection
The same cell may need to be drawn normally or inverted.
We'll give the drawing code one more input:
#selected $00A
Before drawing a normal square:
MOVE #selected 0
Before drawing the selected square:
MOVE #selected 1
The drawing code can use that value to choose the style:
BEQ :normal #selected 0
D.COL #D.COL.WHITE #D.TXT.INVERT
JUMP :draw_glyph
:normal
D.COL #D.COL.WHITE #D.TXT.NORMAL
:draw_glyph
D.CUR #x #y
D.CHR #glyph
D.COL #D.COL.WHITE #D.TXT.NORMAL
JUMP #return
Now the drawing code has everything it needs.
State determines what we draw
Three pieces of state come together when we draw a square:
- the board decides what is in the square;
#draw_celldecides which square we're drawing;#selecteddecides how that square is shown.
The drawing code combines those values to produce what the player sees.
Moving with the drawing code
Now we can finish our movement handlers.
Every successful move has the same shape:
- draw the old square normally;
- change
#selection; - draw the new square selected;
- return to the input loop.
For example, here's the complete Left handler:
:left
MOD #column #selection 3
BEQ :input #column 0
MOVE #draw_cell #selection
MOVE #selected 0
MOVE #return :after_left_old
JUMP :draw_cell
:after_left_old
DEC #selection
MOVE #draw_cell #selection
MOVE #selected 1
MOVE #return :after_left_new
JUMP :draw_cell
:after_left_new
JUMP :input
Notice what the movement code doesn't need to know.
It doesn't care whether either square is empty or contains a cross.
It tells the shared drawing code which square to draw and whether that square is selected.
The drawing code handles the rest.
Right, Up, and Down use the same pattern with the movement checks we already wrote.
You'll see all four in the complete program.
Placing a cross
Enter should place a cross in the selected square.
Just as when drawing, we need to find which board location corresponds to #selection.
The dispatch begins:
:place
BEQ :place0 #selection 0
BEQ :place1 #selection 1
BEQ :place2 #selection 2
BEQ :place3 #selection 3
BEQ :place4 #selection 4
BEQ :place5 #selection 5
BEQ :place6 #selection 6
BEQ :place7 #selection 7
JUMP :place8
Each case checks whether that cell is empty before changing it.
For example:
:place4
BNEQ :input #cell4 0
MOVE #cell4 1
JUMP :placed
If #cell4 isn't 0, it already contains something.
Nothing changes.
Otherwise:
MOVE #cell4 1
changes the board state.
The other eight cases do exactly the same thing to their corresponding cells.
After a successful placement, #selection hasn't moved.
We only need to redraw that square:
:placed
MOVE #draw_cell #selection
MOVE #selected 1
MOVE #return :after_place
JUMP :draw_cell
:after_place
JUMP :input
The drawing code reads the new board value and displays the cross.
Because #selected is 1, the cross remains inverted.
The program changed its state first.
Then it redrew the part of the Display that represents that state.
The complete program
We now have a board that remembers its contents independently of what happens to be visible on the Display.
We also have one shared piece of drawing code that can display any square normally or selected.
Complete program
#selection $000
#column $001
#row $002
#x $003
#y $004
#key $005
#return $006
#draw_cell $007
#value $008
#glyph $009
#selected $00A
#empty "\u3000"
#cross "\uE573"
#cell0 $020
#cell1 $021
#cell2 $022
#cell3 $023
#cell4 $024
#cell5 $025
#cell6 $026
#cell7 $027
#cell8 $028
-- Start in the upper-left square.
MOVE #selection 0
-- Draw the board.
D.FIL " "
D.CUR 11 2
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.CUR 11 3
D.TXT "\uE500\uE53C\uE500\uE53C\uE500"
D.CUR 11 4
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.CUR 11 5
D.TXT "\uE500\uE53C\uE500\uE53C\uE500"
D.CUR 11 6
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.TXT "\uE502"
D.TXT #empty
D.CUR 9 8
D.TXT "ARROWS MOVE"
D.CUR 9 9
D.TXT "ENTER PLACE"
D.CUR 9 10
D.TXT "ESC TO EXIT"
D.BLT
-- Draw the initial selection.
MOVE #draw_cell #selection
MOVE #selected 1
MOVE #return :input
JUMP :draw_cell
-- Wait for input.
:input
SYS.AKEY #key
S.BEQ :left #key "LEFT"
S.BEQ :right #key "RIGHT"
S.BEQ :up #key "UP"
S.BEQ :down #key "DOWN"
S.BEQ :place #key "ENTER"
S.BEQ :exit #key "ESC"
JUMP :input
-- Move left.
:left
MOD #column #selection 3
BEQ :input #column 0
MOVE #draw_cell #selection
MOVE #selected 0
MOVE #return :after_left_old
JUMP :draw_cell
:after_left_old
DEC #selection
MOVE #draw_cell #selection
MOVE #selected 1
MOVE #return :after_left_new
JUMP :draw_cell
:after_left_new
JUMP :input
-- Move right.
:right
MOD #column #selection 3
BEQ :input #column 2
MOVE #draw_cell #selection
MOVE #selected 0
MOVE #return :after_right_old
JUMP :draw_cell
:after_right_old
INC #selection
MOVE #draw_cell #selection
MOVE #selected 1
MOVE #return :after_right_new
JUMP :draw_cell
:after_right_new
JUMP :input
-- Move up.
:up
BLT :input #selection 3
MOVE #draw_cell #selection
MOVE #selected 0
MOVE #return :after_up_old
JUMP :draw_cell
:after_up_old
SUB #selection #selection 3
MOVE #draw_cell #selection
MOVE #selected 1
MOVE #return :after_up_new
JUMP :draw_cell
:after_up_new
JUMP :input
-- Move down.
:down
BGE :input #selection 6
MOVE #draw_cell #selection
MOVE #selected 0
MOVE #return :after_down_old
JUMP :draw_cell
:after_down_old
ADD #selection #selection 3
MOVE #draw_cell #selection
MOVE #selected 1
MOVE #return :after_down_new
JUMP :draw_cell
:after_down_new
JUMP :input
-- Place a cross in the selected square.
:place
BEQ :place0 #selection 0
BEQ :place1 #selection 1
BEQ :place2 #selection 2
BEQ :place3 #selection 3
BEQ :place4 #selection 4
BEQ :place5 #selection 5
BEQ :place6 #selection 6
BEQ :place7 #selection 7
JUMP :place8
:place0
BNEQ :input #cell0 0
MOVE #cell0 1
JUMP :placed
:place1
BNEQ :input #cell1 0
MOVE #cell1 1
JUMP :placed
:place2
BNEQ :input #cell2 0
MOVE #cell2 1
JUMP :placed
:place3
BNEQ :input #cell3 0
MOVE #cell3 1
JUMP :placed
:place4
BNEQ :input #cell4 0
MOVE #cell4 1
JUMP :placed
:place5
BNEQ :input #cell5 0
MOVE #cell5 1
JUMP :placed
:place6
BNEQ :input #cell6 0
MOVE #cell6 1
JUMP :placed
:place7
BNEQ :input #cell7 0
MOVE #cell7 1
JUMP :placed
:place8
BNEQ :input #cell8 0
MOVE #cell8 1
-- Redraw the placed cross as selected.
:placed
MOVE #draw_cell #selection
MOVE #selected 1
MOVE #return :after_place
JUMP :draw_cell
:after_place
JUMP :input
-- Look up the requested board cell.
:draw_cell
BEQ :draw0 #draw_cell 0
BEQ :draw1 #draw_cell 1
BEQ :draw2 #draw_cell 2
BEQ :draw3 #draw_cell 3
BEQ :draw4 #draw_cell 4
BEQ :draw5 #draw_cell 5
BEQ :draw6 #draw_cell 6
BEQ :draw7 #draw_cell 7
JUMP :draw8
:draw0
MOVE #value #cell0
JUMP :paint
:draw1
MOVE #value #cell1
JUMP :paint
:draw2
MOVE #value #cell2
JUMP :paint
:draw3
MOVE #value #cell3
JUMP :paint
:draw4
MOVE #value #cell4
JUMP :paint
:draw5
MOVE #value #cell5
JUMP :paint
:draw6
MOVE #value #cell6
JUMP :paint
:draw7
MOVE #value #cell7
JUMP :paint
:draw8
MOVE #value #cell8
-- Choose the visible glyph.
:paint
S.MOVE #glyph #empty
BEQ :have_glyph #value 0
S.MOVE #glyph #cross
:have_glyph
-- Find the Display position.
MOD #column #draw_cell 3
DIV #row #draw_cell 3
MUL #x #column 4
ADD #x #x 11
MUL #y #row 2
ADD #y #y 2
-- Choose whether the square is selected.
BEQ :normal #selected 0
D.COL #D.COL.WHITE #D.TXT.INVERT
JUMP :draw_glyph
:normal
D.COL #D.COL.WHITE #D.TXT.NORMAL
-- Draw the square and return.
:draw_glyph
D.CUR #x #y
D.CHR #glyph
D.COL #D.COL.WHITE #D.TXT.NORMAL
JUMP #return
-- Leave the program.
:exit
JUMP 0
Try it
Move the selection to each edge of the board.
Try pressing the arrow key that would move beyond the edge. The selection should stay where it is.
Place several crosses and move away from them. Each cross should remain on the board.
Move back onto one. The cross should become inverted when selected and return to normal when you leave.
Press Enter on a square that already contains a cross. Nothing should change.
Then place three crosses in a row.
Nothing happens.
The program remembers that those three squares contain crosses, but as far as it currently knows, they are simply three cells containing 1.
It doesn't yet know that this combination means something.
Follow the state
Pick one square and follow what happens to it.
For example, place a cross in square 4.
The important change is:
cell4 = 1
Now move away from square 4 and back again.
The program isn't remembering the cross because it remains visible on the Display.
It's able to redraw the cross because the value 1 is still stored in #cell4.
We've turned one selection into a position on a two-dimensional board, given every square its own stored state, and built shared code that can draw that state on the Display.
Our manual return destinations work, but they're already making the program considerably more awkward.
In the next lesson, NCL 309: Winning the Game, we'll teach the program how to recognize the eight winning lines—and we'll give our shared code a much better way to return.