Skip to content

Read the sonar

Turn exact distances into near, medium and far clues.

  • Sinclair ZX Spectrum
  • BASIC

We will keep the distance calculation but change what the player learns from it. Sonar will report a band rather than an exact number.

Distance Stored code for this probe Display
0 0 * — found
1–2 1 N — near
3–4 2 M — medium
5 or more 3 F — far

Replace lines 80, 180, 190, 250, 1010.

Add lines 171, 172, 173, 195, 251, 252.

step-01-edits.bas
  80 PRINT AT 21,0;"N=1-2, M=3-4, F=5+ steps.";
 171 IF d>4 THEN LET d=3: GO TO 180
 172 IF d>2 THEN LET d=2: GO TO 180
 173 IF d>0 THEN LET d=1
 180 LET b$=" F"
 190 IF d=2 THEN LET b$=" M"
 195 IF d=1 THEN LET b$=" N"
 250 IF d=3 THEN PRINT AT 21,0;"Far: at least 5 steps away.";
 251 IF d=2 THEN PRINT AT 21,0;"Medium: 3 or 4 steps away.";
 252 IF d=1 THEN PRINT AT 21,0;"Near: 1 or 2 steps away.";
1010 PRINT AT 0,6;"SONAR N1-2 M3-4 F5+"
Complete program for this step
step-01.bas
  10 LET stage=3
  20 LET tr=3: LET tc=6
  30 GO SUB 1000
  60 LET lr=0: LET lc=0
  70 PAPER 0: INK 7
  80 PRINT AT 21,0;"N=1-2, M=3-4, F=5+ steps.";
 100 LET p$="Row": GO SUB 3000: LET pr=v
 110 LET p$="Column": GO SUB 3000: LET pc=v
 120 IF lr=0 THEN GO TO 160
 130 LET r=lr: LET c=lc: GO SUB 2000
 140 PRINT AT y,x;" . "
 160 LET r=pr: LET c=pc: GO SUB 2000
 170 LET d=ABS (r-tr)+ABS (c-tc)
 171 IF d>4 THEN LET d=3: GO TO 180
 172 IF d>2 THEN LET d=2: GO TO 180
 173 IF d>0 THEN LET d=1
 180 LET b$=" F"
 190 IF d=2 THEN LET b$=" M"
 195 IF d=1 THEN LET b$=" N"
 200 IF d=0 THEN LET b$=" *"
 210 PRINT AT y,x;">";b$
 220 LET lr=r: LET lc=c
 230 PAPER 0: INK 7
 240 PRINT AT 21,0;"                               ";
 250 IF d=3 THEN PRINT AT 21,0;"Far: at least 5 steps away.";
 251 IF d=2 THEN PRINT AT 21,0;"Medium: 3 or 4 steps away.";
 252 IF d=1 THEN PRINT AT 21,0;"Near: 1 or 2 steps away.";
 260 IF d=0 THEN PRINT AT 21,0;"Found! Q quits; RUN tries again";
 270 GO TO 100
1000 BORDER 1: PAPER 0: INK 7: CLS
1010 PRINT AT 0,6;"SONAR N1-2 M3-4 F5+"
1020 FOR c=1 TO 8
1030 PRINT AT 2,6 +3 *(c-1);c
1040 NEXT c
1050 PRINT AT 3,4;"+------------------------+"
1060 PRINT AT 20,4;"+------------------------+"
1070 FOR r=1 TO 8
1080 PRINT AT 4 +2 *(r-1),2;r
1090 FOR k=0 TO 1
1100 PRINT AT 4 +2 *(r-1)+k,4;":";AT 4 +2 *(r-1)+k,29;":"
1110 NEXT k
1120 FOR c=1 TO 8
1130 GO SUB 2000
1140 PRINT AT y,x;" . ";AT y+1,x;"   "
1150 NEXT c
1160 PAPER 0: INK 7
1170 NEXT r
1180 RETURN
2000 LET y=4 +2 *(r-1): LET x=5 +3 *(c-1)
2010 PAPER 1: INK 7
2020 IF r+c=2 *INT ((r+c)/2) THEN PAPER 5: INK 0
2030 RETURN
3000 PAPER 0: INK 7
3010 INPUT (p$+" (1-8, Q): "); LINE a$
3015 IF LEN a$>16 THEN GO SUB 4000
3020 IF a$="q" OR a$="Q" THEN GO TO 9000
3030 IF LEN a$<>1 THEN GO TO 3100
3040 IF a$<"1" OR a$>"8" THEN GO TO 3100
3050 LET v=VAL a$
3060 RETURN
3100 PRINT AT 21,0;"Use one digit from 1 to 8.      ";
3110 GO TO 3010
4000 GO SUB 1000
4030 IF lr=0 THEN GO TO 4070
4040 LET r=lr: LET c=lc: GO SUB 2000
4050 PRINT AT y,x;">";b$
4070 PAPER 0: INK 7
4080 RETURN
9000 PRINT AT 21,0;"Finished. RUN to try again.     ";
9010 STOP
Original code

Opens in your Spectrum.

Edit this listing, then run it. Changes are marked against the original code.

Listing options

Original code

  10 LET stage=3
  20 LET tr=3: LET tc=6
  30 GO SUB 1000
  60 LET lr=0: LET lc=0
  70 PAPER 0: INK 7
  80 PRINT AT 21,0;"N=1-2, M=3-4, F=5+ steps.";
 100 LET p$="Row": GO SUB 3000: LET pr=v
 110 LET p$="Column": GO SUB 3000: LET pc=v
 120 IF lr=0 THEN GO TO 160
 130 LET r=lr: LET c=lc: GO SUB 2000
 140 PRINT AT y,x;" . "
 160 LET r=pr: LET c=pc: GO SUB 2000
 170 LET d=ABS (r-tr)+ABS (c-tc)
 171 IF d>4 THEN LET d=3: GO TO 180
 172 IF d>2 THEN LET d=2: GO TO 180
 173 IF d>0 THEN LET d=1
 180 LET b$=" F"
 190 IF d=2 THEN LET b$=" M"
 195 IF d=1 THEN LET b$=" N"
 200 IF d=0 THEN LET b$=" *"
 210 PRINT AT y,x;">";b$
 220 LET lr=r: LET lc=c
 230 PAPER 0: INK 7
 240 PRINT AT 21,0;"                               ";
 250 IF d=3 THEN PRINT AT 21,0;"Far: at least 5 steps away.";
 251 IF d=2 THEN PRINT AT 21,0;"Medium: 3 or 4 steps away.";
 252 IF d=1 THEN PRINT AT 21,0;"Near: 1 or 2 steps away.";
 260 IF d=0 THEN PRINT AT 21,0;"Found! Q quits; RUN tries again";
 270 GO TO 100
1000 BORDER 1: PAPER 0: INK 7: CLS
1010 PRINT AT 0,6;"SONAR N1-2 M3-4 F5+"
1020 FOR c=1 TO 8
1030 PRINT AT 2,6 +3 *(c-1);c
1040 NEXT c
1050 PRINT AT 3,4;"+------------------------+"
1060 PRINT AT 20,4;"+------------------------+"
1070 FOR r=1 TO 8
1080 PRINT AT 4 +2 *(r-1),2;r
1090 FOR k=0 TO 1
1100 PRINT AT 4 +2 *(r-1)+k,4;":";AT 4 +2 *(r-1)+k,29;":"
1110 NEXT k
1120 FOR c=1 TO 8
1130 GO SUB 2000
1140 PRINT AT y,x;" . ";AT y+1,x;"   "
1150 NEXT c
1160 PAPER 0: INK 7
1170 NEXT r
1180 RETURN
2000 LET y=4 +2 *(r-1): LET x=5 +3 *(c-1)
2010 PAPER 1: INK 7
2020 IF r+c=2 *INT ((r+c)/2) THEN PAPER 5: INK 0
2030 RETURN
3000 PAPER 0: INK 7
3010 INPUT (p$+" (1-8, Q): "); LINE a$
3015 IF LEN a$>16 THEN GO SUB 4000
3020 IF a$="q" OR a$="Q" THEN GO TO 9000
3030 IF LEN a$<>1 THEN GO TO 3100
3040 IF a$<"1" OR a$>"8" THEN GO TO 3100
3050 LET v=VAL a$
3060 RETURN
3100 PRINT AT 21,0;"Use one digit from 1 to 8.      ";
3110 GO TO 3010
4000 GO SUB 1000
4030 IF lr=0 THEN GO TO 4070
4040 LET r=lr: LET c=lc: GO SUB 2000
4050 PRINT AT y,x;">";b$
4070 PAPER 0: INK 7
4080 RETURN
9000 PRINT AT 21,0;"Finished. RUN to try again.     ";
9010 STOP

Your Spectrum

48K · PAL

Click the screen to send it keys. Tab returns to the controls.

Not started.

On-screen keys
Keyboard and Session

Shift is CAPS SHIFT. Alt or Option is SYMBOL SHIFT. Comma and full stop use their Spectrum key combinations automatically. Escape sends BREAK while the screen has focus. Enter submits a line. Restart clears this session. Returning to the lesson pauses it.

To type into this screen, use a physical keyboard. On a touchscreen, edit the listing in the lesson and choose Run this code. Open On-screen keys here to answer numeric INPUT prompts or send BREAK.

Runs the Sinclair 48K ROM. Amstrad have kindly given their permission for the redistribution of their copyrighted material but retain that copyright.

Download Emu198x for desktop
Screen text
The Spectrum has not started yet.

Classify once

Line 170 still calculates the exact distance. Line 171 checks the far band first, replaces d with code 3 and jumps straight to the display preparation at 180. Line 172 does the same for medium. If neither branch matched, line 173 changes a positive distance to code 1. Zero passes through unchanged.

That jump matters because we reuse d. After converting a far distance to 3, running the medium test would mistakenly convert that code again. Each probe must be classified once. Until the conversion, d is a distance; afterwards it is a code with the meanings in the table.

The letters and status message now explain the code. The legend stays above the board, so the player need not remember the band boundaries. Colour still separates cells, but the clue is readable without relying on colour.

A probe is six steps from the target. Why must line 171 jump past the next two tests?

Show the explanation

It changes 6 to the far code 3. Without the jump, the next test would see a value greater than 2 and treat that code as a distance, changing it to medium. The jump preserves the first correct classification.

Ask a useful question

A far clue means at least five steps, not exactly five. Before probing again, imagine which cells still agree with the clue. A probe among them might find the target; a probe elsewhere might separate possibilities more clearly. Neither approach requires guessing a hidden direction.

For a boundary check, temporarily put the target at (1,1). Probe (1,1) through (1,6): the results should be *, N, N, M, M, F. Probe (8,8) and expect F again. Restore the ordinary target (3,6).

We still erase the previous probe. Try keeping two or three clues on paper and using them together. That small inconvenience is the reason for our next change: a board that remembers what we have discovered.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapters 3 and 13 (conditions and comparisons).

Run it here

This unit's program, built for you.

Program
step-01.tap
Machine
ZX Spectrum 48K (PAL)

Download step-01.tap