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

Revealing Cells

Press SPACE to reveal a cell — see the number or hit a mine.

63% of Minefield

Movement alone doesn’t make a game. The player needs to act — reveal a cell to discover what’s underneath. Press SPACE on a hidden cell: if it’s safe, show the number. If it’s a mine, game over.

The Program

10 CLS
20 DIM m(8, 8)
30 DIM s(8, 8)
40 LET r = 1: LET c = 1
50 LET s(1, 1) = 1
60 REM place 10 mines
70 FOR i = 1 TO 10
80 LET y = INT (RND * 8) + 1
90 LET x = INT (RND * 8) + 1
100 IF m(y, x) = 9 THEN GO TO 80
110 IF y = 1 AND x = 1 THEN GO TO 80
120 LET m(y, x) = 9
130 NEXT i
140 GO SUB 500
145 LET r = 1: LET c = 1
150 GO SUB 600
160 PRINT AT 0, 0; "Q/A/O/P=move  SPACE=reveal"
170 REM game loop
180 PRINT AT r + 2, c * 2; INK 4; BRIGHT 1; "[": PRINT AT r + 2, c * 2 + 2; "]"
190 LET k$ = INKEY$
200 IF k$ = "" THEN GO TO 190
210 GO SUB 600
220 IF k$ = "q" AND r > 1 THEN LET r = r - 1
230 IF k$ = "a" AND r < 8 THEN LET r = r + 1
240 IF k$ = "o" AND c > 1 THEN LET c = c - 1
250 IF k$ = "p" AND c < 8 THEN LET c = c + 1
260 IF k$ = " " AND s(r, c) = 0 THEN LET s(r, c) = 1: BEEP 0.05, 15
270 IF k$ = " " AND m(r, c) = 9 THEN GO TO 400
280 GO TO 170
400 REM game over
410 REM reveal all
420 FOR y = 1 TO 8
430 FOR x = 1 TO 8
440 LET s(y, x) = 1
450 NEXT x
460 NEXT y
470 GO SUB 600
480 PRINT AT 12, 6; INK 2; "BOOM! Game Over"
490 BEEP 0.2, 0: BEEP 0.2, -3: BEEP 0.3, -7
495 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 FOR y = 1 TO 8
620 FOR x = 1 TO 8
630 IF s(y, x) = 0 THEN PRINT AT y + 2, x * 2 + 1; INK 5; "."
640 IF s(y, x) = 1 AND m(y, x) = 0 THEN PRINT AT y + 2, x * 2 + 1; " "
650 IF s(y, x) = 1 AND m(y, x) > 0 AND m(y, x) < 9 THEN PRINT AT y + 2, x * 2 + 1; INK 6; m(y, x)
660 IF s(y, x) = 1 AND m(y, x) = 9 THEN PRINT AT y + 2, x * 2 + 1; INK 2; "*"
670 NEXT x
680 NEXT y
690 RETURN

How It Works

Line 50 marks the starting cell as already revealed: LET s(1,1) = 1. The player can see cell (1,1) from the start.

Lines 260-270 handle the SPACE key:

260 IF k$ = " " AND s(r, c) = 0 THEN LET s(r, c) = 1: BEEP 0.05, 15
270 IF k$ = " " AND m(r, c) = 9 THEN GO TO 400

Line 260 checks two conditions: SPACE is pressed and the cell is hidden. If both are true, reveal it. The BEEP gives audio feedback — a short high tone for a safe reveal.

Line 270 checks if the revealed cell is a mine. If so, jump to the game-over sequence.

The order matters. Line 260 sets s(r,c) = 1 (revealed). Line 270 then checks if it’s a mine. If you press SPACE on an already-revealed cell, line 260 does nothing (because s(r,c) isn’t 0), and line 270 doesn’t trigger either.

Game Over

Lines 420-460 reveal every cell by setting s(y,x) = 1 in a nested loop. This shows the player where all the mines were.

Line 470 redraws the grid with everything visible.

Line 480 prints “BOOM! Game Over” in red.

Line 490 plays a descending three-note melody — the sound of failure.

Try This

Reveal sound. Change the BEEP 0.05, 15 pitch based on the cell’s value. Higher numbers get higher tones: BEEP 0.05, m(r,c) * 3 + 5.

Show position. Print the coordinates and value after each reveal: PRINT AT 0, 0; "(" ; r; ","; c; ") = "; m(r,c).

What You’ve Learnt

  • Action keys — SPACE reveals the current cell
  • State transitions — changing s(r,c) from 0 to 1
  • Two-step checking — reveal first, then check for mine
  • Game over sequence — reveal all, display message, play sound