Skip to content

Measure the distance

Use two coordinate differences to make a useful clue.

  • Sinclair ZX Spectrum
  • BASIC

A hit-or-miss answer says very little about where to try next. We can give a distance even when the probe misses. At the same time, hide the X: the player should learn about the target through the clues.

Delete lines 40, 50, 150, 4010, 4020. Enter each line number on its own, then press Enter.

Replace lines 10, 170, 180, 190, 250.

step-01-edits.bas
  10 LET stage=3
 170 LET d=ABS (r-tr)+ABS (c-tc)
 180 LET b$=STR$ d
 190 IF d<10 THEN LET b$=" "+b$
 250 IF d<>0 THEN PRINT AT 21,0;"Distance ";d;": rows + columns";
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;"Row then column. Q quits.";
 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)
 180 LET b$=STR$ d
 190 IF d<10 THEN LET b$=" "+b$
 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<>0 THEN PRINT AT 21,0;"Distance ";d;": rows + columns";
 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,9;"SONAR - STAGE ";stage
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

Count rows and columns

ABS returns the magnitude of a number without its sign. For a probe at row 1 and target at row 3, the difference is -2, but the separation is two rows. ABS (r-tr) gives that separation whichever position is larger. Do the same for the columns, then add them.

Probe Target Row steps Column steps Distance
(1,2) (3,6) 2 4 6
(1,6) (3,6) 2 0 2
(3,4) (3,6) 0 2 2
(3,6) (3,6) 0 0 0

This game counts horizontal and vertical steps, not diagonal shortcuts. It is a chosen rule for a deduction game, not a model of how sound travels through water. A clue supplies no direction: several cells can be the same distance from a probe.

The main drawing routine no longer paints the target. The long-input recovery routine also loses its target drawing, otherwise a typing mistake would reveal the answer. Both ordinary drawing and recovery must follow the same rule.

Fit the number in its cell

STR$ d turns the distance into text for b$. A distance below ten gets a leading space. A two-digit result already fills both positions. Zero replaces the number with *. The separate > still occupies the first of the cell’s three character columns.

Opposite corners of an 8×8 board are seven rows and seven columns apart, giving the maximum distance 14. The display has room for it even though our ordinary fixed target does not produce that maximum.

Does a clue of 2 mean that the target is two columns to the right?

Show the explanation

No. It could be two columns in either direction, two rows away, or one row and one column away. The clue is the sum of the separations, without a direction.

Try the four probes in the table and compare their results. To check the maximum, temporarily replace line 20 with target row 1, column 1 and probe (8,8). Restore (3,6) afterwards.

Exact clues can make a very efficient search. From the two ends of the top row, one clue gives the sum of the target’s offsets and the other helps separate them. You can investigate that shortcut on paper. Our next version will give broader clues, leaving more possibilities to compare after each probe.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapters 8–9 (ABS and STR$), and 15 (printing).

Run it here

This unit's program, built for you.

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

Download step-01.tap