BBC BASIC 2.31 for the Tatung Einstein, showing DRIFT, the text adventure the course builds, in the middle of a game
← Back to Courses
module
20

The Map

Introduction

Part three is the game. Each section from here adds a piece to DRIFT, and at the end of each one you save the game so far, ADV1, ADV2 and on, and load it again at the start of the next.

A text adventure starts with a map: which rooms there are, and which way you can go from each. DRIFT has four.

            BRIDGE
              |
 CRYO BAY -- CORRIDOR -- ENGINEERING

Rooms And Exits As Numbers

Each room has a number - the cryo bay is 1, the corridor 2, engineering 3, the bridge 4 - and each direction has a number too: north 1, east 2, south 3, west 4. Then the whole map fits in a two-dimensional array, E%(room,direction): the room you reach by going that way, or 0 if you cannot.

E%(2,1) is 4, because north from the corridor is the bridge. E%(1,1) is 0: there is no way north from the cryo bay.

The map is written as DATA, one line per room: its name, then its exits north, east, south and west.

3010 DATA CORRIDOR,4,3,0,1

reads "the corridor: north to 4, east to 3, no way south, west to 1".

Where Everything Goes

DRIFT is going to grow to about two hundred lines, so it is laid out in blocks from the start, with room between:

LinesWhat
10-130the main program - a handful of lines that call everything else
1000 upprocedures: setting up, looking, moving, carrying
2000 upthe tools: wrapping text, making capitals, finding words
3000 upthe DATA - the map, and later the descriptions, objects and words

Every procedure is defined after the END in line 130, as S17 said they must be.

The Code

This is the first stage. Type it all:

10 REM DRIFT - A TEXT ADVENTURE
20 MODE 0:PROCSETUP
40 PROCLOOK
130 END
1000 DEF PROCSETUP
1010 DIM ROOM$(4),E%(4,4),DIR$(4)
1020 RESTORE 3000:FOR R%=1 TO 4:READ ROOM$(R%),E%(R%,1),E%(R%,2),E%(R%,3),E%(R%,4):NEXT
1025 FOR R%=1 TO 4:READ DIR$(R%):NEXT
1060 R%=1
1090 ENDPROC
1100 DEF PROCLOOK
1110 COLOUR 11:PRINT ROOM$(R%):COLOUR 15
1130 PROCEXITS
1150 ENDPROC
1200 DEF PROCEXITS
1210 LOCAL D%,X$
1220 FOR D%=1 TO 4
1230 IF E%(R%,D%) THEN X$=X$+" "+DIR$(D%)
1240 NEXT
1250 PRINT "EXITS:";X$
1260 ENDPROC
3000 DATA CRYO BAY,0,2,0,0
3010 DATA CORRIDOR,4,3,0,1
3020 DATA ENGINEERING,0,0,0,2
3030 DATA BRIDGE,0,0,2,0
3040 DATA NORTH,EAST,SOUTH,WEST
  • PROCSETUP makes the arrays and reads the map into them. RESTORE 3000 makes sure the reading starts at the map, whatever else is added later. The arrays are made inside a procedure, but they belong to the whole program.
  • R% is the room you are in. You start in room 1.
  • PROCLOOK shows where you are: the room's name in yellow, then its exits.
  • PROCEXITS goes through the four directions and adds each one that leads somewhere to a string, X$. IF E%(R%,D%) THEN is true whenever the exit is not 0. X$ is LOCAL, so it starts empty each time.

Starting from

A fresh boot, or NEW. When it runs, SAVE "ADV1".

What you should see

CRYO BAY
EXITS: EAST
>

CRYO BAY in light yellow. The screen was cleared by MODE 0 first.

The first room and its one exit

Change One Thing

  • Change line 1060 to R%=2, and run it. Then R%=4. Does each room show the exits the map says it should?
  • Put R%=1 back and change line 3000 to DATA CRYO BAY,0,2,0,2. What does the cryo bay say now - and where would going west take you?
  • After running it, type PRINT ROOM$(E%(2,1)) at the >. Which room is that, and why?

Exercises

20.1 Add a line 50 that shows all four rooms and their exits, one after another, using a loop and PROCLOOK.

20.2 After the program has run, print BRIDGE IS NORTH OF THE CORRIDOR, working both room names out from the arrays.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
Out of DATA in line 1020 or 1025A DATA line is missing a number, or has a comma too few. Each room needs a name and four exits.
A room shows the wrong exitsIts four numbers are in the wrong order: north, east, south, west.
No PROCLine 130's END is missing, and the program ran into the definitions.

Summary

The map is four rooms in DATA: names, and for each direction the number of the room it leads to. PROCSETUP reads it into arrays; PROCLOOK shows the room you are in. Save it as ADV1.

Next

S21, Moving - typing a direction, and going there.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.