Skip to content
Game 15 Unit 2 of 32 1 hr learning time

The Empty Grid

Create a 16x16 2D array filled with walls — the dungeon grid that underpins every floor.

6% of Dungeons Of Dorin

The dungeon is a 16x16 grid. Each cell is either a wall or open space. Before rooms and corridors can be carved, the grid must exist and be filled entirely with walls. This unit creates the 2D array that stores the dungeon and displays it on screen.

The Program

5 REM === DUNGEONS OF DORIN ===
10 BORDER 0: PAPER 0: INK 7: CLS
20 PRINT AT 3, 4; INK 7; BRIGHT 1; "DUNGEONS OF DORIN"
30 PRINT AT 6, 2; INK 6; "Descend ten floors of"
35 PRINT AT 7, 2; INK 6; "darkness. Find the Heart."
40 PRINT AT 10, 2; INK 7; "Press any key to begin"
50 PAUSE 0
60 CLS
70 REM === define stats ===
80 LET h = 20: LET z = 20: LET a = 3: LET f = 2
90 REM === create dungeon grid ===
100 DIM d(16,16)
110 FOR i = 1 TO 16: FOR o = 1 TO 16
120 LET d(i,o) = 1
130 NEXT o: NEXT i
140 REM === display grid ===
150 FOR i = 1 TO 16: FOR o = 1 TO 16
160 IF d(i,o) = 1 THEN PRINT AT i + 2, o + 6; INK 4; CHR$ 143
170 IF d(i,o) = 0 THEN PRINT AT i + 2, o + 6; INK 7; "."
180 NEXT o: NEXT i
190 PRINT AT 20, 4; INK 7; "16x16 dungeon grid"
200 STOP

How It Works

Line 20 dimensions the dungeon array d(16,16). This creates 256 cells, each capable of holding a number. The value stored in each cell determines what it contains: 1 means wall, 0 means floor, and later units will add stairs (2) and chests (3).

Lines 42-46 fill the entire grid with walls. Two nested FOR/NEXT loops visit every cell and set it to 1. After this runs, the dungeon is a solid block of stone — nothing can pass through it yet.

The display loop prints the grid using PRINT AT. Each wall cell shows as a solid block character (CHR$ 143), while floor cells would show as dots. Right now every cell is a wall, so the screen fills with blocks.

2D Arrays as Game Worlds

A 2D array is a natural fit for a grid-based game world. The row index maps to the y-coordinate, the column index maps to the x-coordinate, and the value at each position describes what occupies that cell. This is the same pattern used in board games, tile maps, and level editors — a grid of numbers that the rendering code interprets as visual elements.

The 16x16 size is a practical choice. It fits comfortably on the Spectrum display, leaves room for a status panel, and keeps memory usage modest. The array uses 256 entries, each holding a single number. Larger grids are possible but would make the viewport code more complex and slow down generation.

Cell Type Constants

The numbers stored in the grid follow a convention:

ValueMeaning
0Floor (passable)
1Wall (impassable)
2Stairs down
3Treasure chest

These values are just numbers — BASIC has no concept of named constants. Keeping a mental note of what each number means is essential as the program grows.

Try This

Change the grid size. Try d(8,8) for a smaller dungeon or d(20,20) for a larger one. How does the display change? What happens if the grid is too large to fit on screen?

Manually carve a room. After filling the grid with walls, add a loop that sets a 4x4 block of cells to 0. See the room appear in the wall grid.