Subroutines
Organise the code into GO SUB routines for calculation and display.
The code is growing. Mine placement, neighbour counting, grid display — each is a distinct job. Subroutines separate these jobs so the main program reads like a plan: place mines, calculate counts, draw grid.
The Program
10 CLS
20 DIM m(8, 8)
30 DIM s(8, 8)
40 REM place 10 mines
50 FOR i = 1 TO 10
60 LET r = INT (RND * 8) + 1
70 LET c = INT (RND * 8) + 1
80 IF m(r, c) = 9 THEN GO TO 60
90 LET m(r, c) = 9
100 NEXT i
110 REM calculate counts
120 GO SUB 500
130 REM draw grid
140 GO SUB 600
150 STOP
500 REM === calculate counts ===
510 FOR r = 1 TO 8
520 FOR c = 1 TO 8
530 IF m(r, c) = 9 THEN GO TO 590
540 LET n = 0
550 FOR i = r - 1 TO r + 1
560 FOR j = c - 1 TO c + 1
570 IF i < 1 OR i > 8 OR j < 1 OR j > 8 THEN GO TO 582
580 IF m(i, j) = 9 THEN LET n = n + 1
582 NEXT j
584 NEXT i
586 LET m(r, c) = n
590 NEXT c
595 NEXT r
598 RETURN
600 REM === draw grid ===
610 PRINT AT 1, 2;
620 FOR c = 1 TO 8
630 PRINT " "; c;
640 NEXT c
650 FOR r = 1 TO 8
660 PRINT AT r + 2, 0; r; " ";
670 FOR c = 1 TO 8
680 IF s(r, c) = 0 THEN PRINT INK 5; ".";
690 IF s(r, c) = 0 THEN GO TO 720
700 IF m(r, c) = 9 THEN PRINT INK 2; "*";
710 IF m(r, c) <> 9 THEN PRINT m(r, c);
720 PRINT " ";
730 NEXT c
740 NEXT r
750 RETURN
How It Works
The main program is now just five lines:
110 REM calculate counts
120 GO SUB 500
130 REM draw grid
140 GO SUB 600
150 STOP
GO SUB 500 calculates neighbour counts for every cell. This is the same four-nested-loop algorithm from unit 6, now packaged as a subroutine that ends with RETURN.
GO SUB 600 draws the grid with column headers, row numbers, and visibility-aware display. Hidden cells show a cyan dot. Revealed cells show the number or a red star for mines.
Subroutine Layout
| Lines | Subroutine | Purpose |
|---|---|---|
| 500-598 | Calculate | Count neighbours for all cells |
| 600-750 | Draw grid | Display the grid with headers |
Each subroutine is self-contained. RETURN at the end sends control back to wherever GO SUB was called. The main program doesn’t need to know how counting or drawing works — just that it can call them.
PRINT AT for Grid Display
The draw subroutine uses PRINT AT for precise placement:
680 IF s(r, c) = 0 THEN PRINT INK 5; ".";
INK 5 (cyan) marks hidden cells. The semicolons keep output on the same line. Each cell gets its own screen position, so redrawing doesn’t disturb other parts of the display.
Try This
Reveal some cells. Before GO SUB 600, set a few cells to revealed: LET s(3,3) = 1. The grid should show numbers for revealed cells and dots for hidden ones.
Add a third subroutine. Move mine placement (lines 50-100) into GO SUB 400. The main program becomes: GO SUB 400: GO SUB 500: GO SUB 600: STOP.
What You’ve Learnt
- GO SUB architecture — each job in its own subroutine
- Main program as plan — high-level flow, details in subroutines
- RETURN — sends control back to the caller
- Consistent line numbering — subroutines at 500, 600, etc. for clarity