Winning
Count the hits, end the game on the third, and guard against probing a cell twice — plus a guess counter and a status line so the board reads as a game.
The clues work; now the game needs an ending. Three targets means three hits to win, so the program counts them and stops on the third — and along the way it keeps a guess count and blocks the player from wasting a turn on a cell they have already probed.
10 BORDER 0: PAPER 0: INK 7: CLS
110 RANDOMIZE
120 DIM g(8,8)
130 FOR i = 1 TO 3
140 LET r = INT (RND * 8) + 1: LET c = INT (RND * 8) + 1
150 IF g(r,c) = 9 THEN GO TO 140
160 LET g(r,c) = 9
170 NEXT i
180 LET hits = 0: LET guesses = 0
190 CLS
200 INVERSE 1: PRINT AT 0, 0; " *** SONAR *** ": INVERSE 0
210 PRINT AT 1, 2; "Found: "; hits; "/3 Guesses: "; guesses; " "
240 PRINT AT 3, 11; "12345678"
250 FOR r = 1 TO 8
260 PRINT AT 3 + r, 9; r;
270 FOR c = 1 TO 8
280 LET v = g(r,c)
290 IF v = 9 OR v = 0 THEN INK 7: PRINT ".";
300 IF v = -1 THEN INK 4: PRINT "*";
310 IF v >= 1 AND v <= 2 THEN INK 2: PRINT v;
320 IF v >= 3 AND v <= 4 THEN INK 6: PRINT v;
330 IF v >= 5 AND v < 9 THEN INK 5: PRINT v;
340 NEXT c
350 INK 7
370 NEXT r
380 INPUT "Row (1-8): "; r
390 INPUT "Col (1-8): "; c
400 IF r < 1 OR r > 8 OR c < 1 OR c > 8 THEN GO TO 380
410 IF g(r,c) > 0 AND g(r,c) < 9 THEN PRINT AT 13, 2; "Already probed! ": PAUSE 50: GO TO 190
420 IF g(r,c) = -1 THEN PRINT AT 13, 2; "Already found! ": PAUSE 50: GO TO 190
430 LET guesses = guesses + 1
460 IF g(r,c) = 9 THEN GO TO 570
470 LET dist = 99
480 FOR a = 1 TO 8: FOR b = 1 TO 8
490 IF g(a,b) <> 9 THEN GO TO 520
500 LET d = ABS (r - a) + ABS (c - b)
510 IF d < dist THEN LET dist = d
520 NEXT b: NEXT a
530 IF dist > 8 THEN LET dist = 8
540 LET g(r,c) = dist
550 BEEP 0.1, -5
560 GO TO 190
570 LET g(r,c) = -1
580 LET hits = hits + 1
590 BEEP 0.1, 10: BEEP 0.1, 15
600 IF hits = 3 THEN GO TO 620
610 GO TO 190
620 CLS
630 LET a$ = "*** SONAR ***": LET y = 6: GO SUB 9000
640 PRINT AT 9, 6; INK 4; "All targets found!"
650 PRINT AT 11, 6; INK 7; "Guesses: "; guesses
660 BEEP 0.1, 10: BEEP 0.1, 15: BEEP 0.1, 20
670 STOP
9000 PRINT AT y, (32 - LEN a$) / 2; BRIGHT 1; a$
9010 RETURN
Counting toward the win
Two counters start at line 180: hits and guesses. Every valid probe bumps guesses (line
430); every hit bumps hits (line 580). Line 600 is the win check — IF hits = 3 THEN GO TO 620 — jumping to a win screen that reports the guess count. The status line at the top (line
210) shows Found: n/3 and the running guess total, so the player always knows where they
stand. This is the same score-and-check loop from earlier games, now driving a board game to its
conclusion.
Guarding the probe
A grid game has a new failure the earlier games did not: probing the same cell twice. Lines 410
and 420 catch it — if the cell already holds a distance (> 0 AND < 9) or a found target
(-1), the program says so and loops back without spending a guess. The three cell states the
grid has carried since the start are exactly what makes this check possible: the value already
records whether a cell has been touched, so the guard is a single comparison. State you stored
for one reason often answers a question you did not plan for.
The game is complete and playable. The last unit polishes it into the finished Sonar — a proper title, a graphical grid, and a play-again loop.