Skip to content

Connect the rooms

Store twelve named rooms and four exits for each.

  • Sinclair ZX Spectrum
  • BASIC

A room’s exits can be stored as numbers. Room 1 leads south to room 5 and east to room 2. Following a link will change which room we display; first, build the records that make those links possible.

Replace lines 1040, 1050.

Add lines 200, 210, 220, 250, 260, 270, 280, 1060, 1070, 1080, 1090, 1100, 1110, 1160, 9500, 9510, 9520, 9530, 9540, 9550, 9560, 9570, 9580, 9590, 9600, 9610.

Delete lines 20, 30, 40, 50.

To delete a BASIC line, enter its number on its own.

changes.bas
 200 DIM m(12,4): DIM a$(12,24): DIM b$(12,28)
 210 RESTORE 9500
 220 FOR i=1 TO 12: READ a$(i),b$(i): FOR j=1 TO 4: READ m(i,j): NEXT j: NEXT i
 250 LET rm=1
 260 LET d$="NSEW"
 270 GO SUB 1000
 280 STOP
1040 PRINT AT 5,2; INK 5;a$(rm)
1050 PRINT AT 7,2; INK 7;b$(rm)
1060 PRINT AT 9,2; INK 6;"TUNNELS"
1070 FOR j=1 TO 4
1080 LET v=m(rm,j): LET y=10 +j
1090 PRINT AT y,2; INK 7;d$(j);"  --"
1100 IF v=0 THEN GO TO 1160
1110 PRINT AT y,2; INK 7;d$(j);"  Open"
1160 NEXT j
9500 DATA "The entrance","Daylight rims the stone.",0,5,2,0
9510 DATA "Split passage","Two worn paths meet here.",0,6,3,1
9520 DATA "Echo gallery","Every step returns twice.",0,7,4,2
9530 DATA "High ledge","A narrow shelf bends south.",0,8,0,3
9540 DATA "Root chamber","Roots grip the damp ceiling.",1,9,6,0
9550 DATA "Old camp","A torn pack lies in dust.",2,10,7,5
9560 DATA "The black shaft","The floor vanishes below.",3,11,8,6
9570 DATA "Dripping stair","Water counts the seconds.",4,12,0,7
9580 DATA "Still pool","Your lamp trembles in water.",5,0,10,0
9590 DATA "Buried shrine","Carved faces watch the path.",6,0,11,9
9600 DATA "Low arch","You stoop beneath the rock.",7,0,12,10
9610 DATA "Crystal hollow","Crystals catch the lamplight.",8,0,0,11

RUN the changed program.

Complete source at this checkpoint
caverns.bas
  10 BORDER 0: PAPER 0: INK 7: BRIGHT 1: CLS
 200 DIM m(12,4): DIM a$(12,24): DIM b$(12,28)
 210 RESTORE 9500
 220 FOR i=1 TO 12: READ a$(i),b$(i): FOR j=1 TO 4: READ m(i,j): NEXT j: NEXT i
 250 LET rm=1
 260 LET d$="NSEW"
 270 GO SUB 1000
 280 STOP
1000 CLS
1010 PRINT AT 0,8; INK 5;"THE CAVERNS"
1030 INK 1: PLOT 16,148: DRAW 223,0
1040 PRINT AT 5,2; INK 5;a$(rm)
1050 PRINT AT 7,2; INK 7;b$(rm)
1060 PRINT AT 9,2; INK 6;"TUNNELS"
1070 FOR j=1 TO 4
1080 LET v=m(rm,j): LET y=10 +j
1090 PRINT AT y,2; INK 7;d$(j);"  --"
1100 IF v=0 THEN GO TO 1160
1110 PRINT AT y,2; INK 7;d$(j);"  Open"
1160 NEXT j
1220 RETURN
9500 DATA "The entrance","Daylight rims the stone.",0,5,2,0
9510 DATA "Split passage","Two worn paths meet here.",0,6,3,1
9520 DATA "Echo gallery","Every step returns twice.",0,7,4,2
9530 DATA "High ledge","A narrow shelf bends south.",0,8,0,3
9540 DATA "Root chamber","Roots grip the damp ceiling.",1,9,6,0
9550 DATA "Old camp","A torn pack lies in dust.",2,10,7,5
9560 DATA "The black shaft","The floor vanishes below.",3,11,8,6
9570 DATA "Dripping stair","Water counts the seconds.",4,12,0,7
9580 DATA "Still pool","Your lamp trembles in water.",5,0,10,0
9590 DATA "Buried shrine","Carved faces watch the path.",6,0,11,9
9600 DATA "Low arch","You stoop beneath the rock.",7,0,12,10
9610 DATA "Crystal hollow","Crystals catch the lamplight.",8,0,0,11

RUN the program. The entrance still has its name and description, with south and east marked Open. North and west show --. The program then stops; movement comes in the next lesson.

One row per room, one column per direction

DIM m(12,4) reserves twelve groups of four numbers. The first subscript selects a room; the second selects an exit. We use 1 for north, 2 for south, 3 for east and 4 for west. m(1,2) is therefore the entrance’s southern destination: 5. Zero means there is no tunnel.

This is a graph: places connected by links. The array’s rows and columns organise records; they are not positions on the cave screen. The names make places recognisable, while numeric room identifiers let the program follow their connections.

DIM a$(12,24) stores twelve names of 24 characters each. DIM b$(12,28) stores twelve descriptions of 28 characters each. Shorter strings are padded with spaces; longer ones are cut to fit. a$(rm) selects a whole name. Unlike the numeric map, the final dimension of each string array is its text length.

Read one complete record

Each DATA line has a name, a description and four destinations in N/S/E/W order. RESTORE 9500 points READ to the start. The outer FOR loop visits rooms 1–12. READ takes the two strings, then the inner loop reads four links before the outer loop advances.

The drawing routine now looks up a$(rm) and b$(rm). rm starts at 1. The string d$ = "NSEW" supplies direction labels: d$(1) is N and d$(4) is W. For each direction, we read the link into v, print --, and replace it with Open when v is nonzero.

The entrance's east link is 2. Which link in room 2 should lead back to the entrance?

Show the explanation

Its west link, m(2,4), should be 1. Check the last value on DATA line 9510. The program does not invent the return tunnel; we author both directions.

Keep the DATA records together. A missing value shifts later reads into the wrong fields. Count two strings and four numbers on every room line if you see a type mismatch or an unexpected exit.

Keyboard notes

D enters DIM in keyword mode. Enter extended mode with Caps Shift and Symbol Shift together, release them, then use D for DATA, A for READ or S for RESTORE.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 4, chapter 6, chapter 12.

Run it here

This unit's program, built for you.

Program
caverns.tap
Machine
ZX Spectrum 48K (PAL)

Download caverns.tap