The Map
Four parallel arrays, loaded from a DATA table of twenty rooms, hold the cave as a graph — and the current room shows only which exits it has.
A cave is not a grid. Rooms connect in irregular ways — this tunnel goes north, that one east, some lead nowhere. You cannot store that in a 2D array. The shape that fits is a graph: each room records which room lies in each direction. That is the first new idea of The Caverns, and the whole world is built on it.
10 BORDER 0: PAPER 1: INK 7: CLS
110 DIM n(20): DIM s(20): DIM e(20): DIM w(20)
120 RESTORE 1010
130 FOR i = 1 TO 20
140 READ n(i), s(i), e(i), w(i)
150 NEXT i
160 LET rm = 1
270 CLS
280 LET a$ = "*** THE CAVERNS ***": LET y = 0: GO SUB 9000
300 PRINT AT 3, 2; "You are in room "; rm; "."
310 PRINT AT 5, 2; "Exits: ";
320 IF n(rm) > 0 THEN PRINT "N ";
330 IF s(rm) > 0 THEN PRINT "S ";
340 IF e(rm) > 0 THEN PRINT "E ";
350 IF w(rm) > 0 THEN PRINT "W ";
360 STOP
1000 REM --- Room map: N,S,E,W ---
1010 DATA 0,5,2,0
1020 DATA 0,0,3,1
1030 DATA 0,7,4,2
1040 DATA 0,8,0,3
1050 DATA 1,0,6,0
1060 DATA 0,10,7,5
1070 DATA 3,11,0,6
1080 DATA 4,12,0,0
1090 DATA 0,13,10,0
1100 DATA 6,0,11,9
1110 DATA 7,15,12,10
1120 DATA 8,0,0,11
1130 DATA 9,17,14,0
1140 DATA 0,18,0,13
1150 DATA 11,19,16,0
1160 DATA 0,20,0,15
1170 DATA 13,0,18,0
1180 DATA 14,0,19,17
1190 DATA 15,0,20,18
1200 DATA 16,0,0,19
9000 PRINT AT y, (32 - LEN a$) / 2; BRIGHT 1; a$
9010 RETURN
Four arrays, one per direction
Lines 110–150 build the map. DIM n(20), s(20), e(20), w(20) are four parallel arrays —
one for each compass direction — and the DATA table at line 1010 holds twenty rows of four
numbers: where north, south, east and west lead from each room. RESTORE 1010 then a READ loop
fills all four arrays at once. So n(7) is the room north of room 7, e(7) the room east of it, and
a 0 means there is no tunnel that way. Parallel arrays indexed by the same number are how BASIC
stores a record with several fields — here, a room and its four exits.
Showing only the exits
The display (lines 300–350) prints the room number and then walks the four arrays for the current
room rm, printing N, S, E or W only where the link is non-zero. The player sees where they
are and where they can go — nothing else. Sonar showed the whole grid at once; a cave shows one room,
and the rest lives in the data and in the player's memory. A 20-room cave is tiny on paper; in the
dark, with no map, it feels vast.
Next: follow those exits from room to room.