Skip to content
Game 14 Unit 2 of 6 1 hr learning time

Moving

Type a compass direction, look it up in the graph, and walk to the room it leads to — or be told there is no tunnel that way.

33% of The Caverns

The map exists; now you walk it. A move is a compass direction, and travelling is the simplest possible use of the graph: look up where this room's exit leads, and make that the room you are in. Following the links is what turns a table of numbers into a place you can move through.

  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 ";
 480 INPUT "Direction (N/S/E/W): "; d$
 490 LET dest = 0
 500 IF d$ = "N" OR d$ = "n" THEN LET dest = n(rm)
 510 IF d$ = "S" OR d$ = "s" THEN LET dest = s(rm)
 520 IF d$ = "E" OR d$ = "e" THEN LET dest = e(rm)
 530 IF d$ = "W" OR d$ = "w" THEN LET dest = w(rm)
 540 IF dest = 0 THEN PRINT AT 12, 2; "You can't go that way!": PAUSE 30: GO TO 270
 550 LET rm = dest
 730 GO TO 270
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
ZX Spectrum The Caverns: 'You are in room 5.' with 'Exits: N E' after moving south from room 1
Move south from room 1 and you are in room 5 — the display redraws for the new room.

A direction is an array lookup

Lines 480–530 read a letter and turn it into a destination. INPUT d$ takes the direction, and each line maps a letter to the matching array: IF d$ = "N" THEN LET dest = n(rm), and so on for south, east and west. (Both upper and lower case are accepted, the way Hangman handled letters.) The move itself is line 550 — LET rm = dest — and that single assignment is the whole act of walking: the new room becomes the current room, and the loop redraws everything for it.

Walls

Not every direction has a tunnel. If the chosen exit is 0, line 540 catches it — IF dest = 0 THEN ... "You can't go that way!" — and loops back without moving. The same 0-means-no-link rule that drew the exit list now blocks illegal moves; one value in the array answers both "which exits exist?" and "can I go this way?". GO TO 270 returns to the top, and the game is now a loop: show the room, read a direction, follow the link, repeat.

You can wander all twenty rooms. They are empty, though — next we put something in the dark.