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

Connecting Rooms

Carve L-shaped corridors between rooms so every part of the dungeon is reachable.

13% of Dungeons Of Dorin

Rooms alone are isolated chambers. Without corridors connecting them, the player would be trapped in whichever room they start in. This unit carves L-shaped corridors between adjacent rooms, guaranteeing that every room is reachable from every other.

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 === carve rooms ===
150 LET s = INT (RND * 3) + 3
160 DIM u(5,4)
170 FOR i = 1 TO s
180 LET u(i,1) = INT (RND * 10) + 2
190 LET u(i,2) = INT (RND * 10) + 2
200 LET u(i,3) = INT (RND * 4) + 3
210 LET u(i,4) = INT (RND * 4) + 3
220 FOR c = u(i,2) TO u(i,2) + u(i,4) - 1
230 FOR o = u(i,1) TO u(i,1) + u(i,3) - 1
240 IF c >= 1 AND c <= 16 AND o >= 1 AND o <= 16 THEN LET d(c,o) = 0
250 NEXT o: NEXT c
260 NEXT i
270 REM === connect rooms with corridors ===
280 FOR i = 1 TO s - 1
290 LET t = u(i,1) + INT (u(i,3) / 2)
300 LET t2 = u(i,2) + INT (u(i,4) / 2)
310 LET t3 = u(i+1,1) + INT (u(i+1,3) / 2)
320 LET t4 = u(i+1,2) + INT (u(i+1,4) / 2)
330 REM horizontal corridor
340 IF t <= t3 THEN FOR o = t TO t3: IF t2 >= 1 AND t2 <= 16 AND o >= 1 AND o <= 16 THEN LET d(t2,o) = 0
350 IF t <= t3 THEN NEXT o
360 IF t > t3 THEN FOR o = t3 TO t: IF t2 >= 1 AND t2 <= 16 AND o >= 1 AND o <= 16 THEN LET d(t2,o) = 0
370 IF t > t3 THEN NEXT o
380 REM vertical corridor
390 IF t2 <= t4 THEN FOR c = t2 TO t4: IF c >= 1 AND c <= 16 AND t3 >= 1 AND t3 <= 16 THEN LET d(c,t3) = 0
400 IF t2 <= t4 THEN NEXT c
410 IF t2 > t4 THEN FOR c = t4 TO t2: IF c >= 1 AND c <= 16 AND t3 >= 1 AND t3 <= 16 THEN LET d(c,t3) = 0
420 IF t2 > t4 THEN NEXT c
430 NEXT i
440 REM === display grid ===
450 FOR i = 1 TO 16: FOR o = 1 TO 16
460 IF d(i,o) = 1 THEN PRINT AT i + 2, o + 6; INK 4; CHR$ 143
470 IF d(i,o) = 0 THEN PRINT AT i + 2, o + 6; INK 7; "."
480 NEXT o: NEXT i
490 PRINT AT 20, 2; INK 7; s; " rooms connected"
500 STOP

How It Works

Lines 76-106 connect rooms with corridors. The loop runs from room 1 to room s-1, connecting each room to the next in sequence.

Lines 78-84 find the centre of two adjacent rooms. The centre is calculated as the room position plus half its width (or height). These centre points become the corridor endpoints.

Lines 86-94 carve the horizontal segment. Starting from the x-centre of the first room, the loop sets cells to 0 until it reaches the x-centre of the second room. The conditional check on lines 88 and 92 handles both directions — left-to-right and right-to-left — because the second room might be to either side of the first.

Lines 96-104 carve the vertical segment. From the y-centre of the second room, it carves up or down to connect the horizontal corridor to the second room.

The result is an L-shaped path: horizontal first, then vertical. Every room connects to its neighbour, and since rooms are connected in sequence, the entire dungeon forms a connected graph.

Why L-Shaped Corridors

An L-shaped corridor is the simplest way to connect two arbitrary points on a grid. It requires only two straight segments — one horizontal, one vertical — and avoids the complexity of diagonal paths or pathfinding algorithms. The approach guarantees connectivity with minimal code.

The corridors are not always interesting — sometimes they overlap with rooms or with each other. But they are always functional. Every room can be reached, which means the player can always find the stairs and descend to the next floor. Guaranteeing solvability is more important than visual elegance.

Bounds Checking

Each cell carved by the corridor loop is checked against the grid boundaries. Without these checks, a corridor extending past the edge of the 16x16 grid would cause a Subscript wrong error. The bounds check on every cell write is essential because corridors can start or end near the grid edges depending on where RND placed the rooms.

Try This

Reverse the corridor order. Carve the vertical segment first, then the horizontal one. The corridors still connect the same rooms, but the paths look different. Which feels more natural?

Add extra corridors. Connect the last room back to the first, creating a loop. Does this make the dungeon more interesting to explore?