Skip to content
Game 10 Unit 2 of 16 1 hr learning time

Map Data

Store a floor layout as strings in a DIM array, loaded from DATA statements.

13% of Night Patrol

Every floor of the building needs a layout — walls, corridors, rooms. You could hardcode dozens of PRINT AT statements, but that’s fragile and impossible to change. Instead, store the map as data and draw it programmatically.

The Program

10 CLS
20 PRINT "Loading map data..."
30 PRINT
40 DIM m$(8, 16)
50 FOR i = 1 TO 8
60 READ m$(i)
70 NEXT i
80 PRINT "Map loaded:"
90 PRINT
100 FOR r = 1 TO 8
110 PRINT m$(r)
120 NEXT r
130 PRINT
140 PRINT "Rows: 8  Cols: 16"
500 DATA "################"
510 DATA "#              #"
520 DATA "#  ## ####  #  #"
530 DATA "#     #     #  #"
540 DATA "#  ####  #     #"
550 DATA "#        #  #  #"
560 DATA "#   ##      #  #"
570 DATA "################"

How It Works

Line 20 creates the map array. DIM m$(8, 16) allocates 8 strings, each 16 characters long. Each string is one row of the map. Eight rows, sixteen columns — that’s the floor.

Lines 30-50 load the map from DATA. The FOR loop reads 8 strings into m$(1) through m$(8). Each READ m$(i) pulls the next DATA string.

Lines 60-100 print the raw data to verify it loaded correctly. The nested loops step through every character, printing it at the matching screen position.

Lines 900-970 hold the map data. # means wall, space means floor. Look at the pattern — you can see the corridors in the DATA statements. The border of # characters forms the outer walls. Internal # characters create rooms and passages.

Strings as Map Rows

This is the key insight: a string is a row of characters. m$(3) is the entire third row. m$(3)(5 TO 5) is the single character at column 5 of row 3. You can read any cell on the map with m$(row)(col TO col).

This is more compact than a 2D numeric array. Each character takes one byte. An 8x16 map is just 128 bytes of data.

Reading the Map

The DATA statements look like a picture. That’s deliberate — you design the map by editing the DATA. Change a # to a space and you’ve opened a wall. Change a space to # and you’ve blocked a corridor. The map is the code.

Try This

Edit the map. Change some # characters to spaces (or vice versa) in the DATA statements. Run the program and see how the layout changes.

Bigger corridors. Remove some internal walls to create wider open areas. Think about which layouts would make a stealth game interesting.