“Idaho Squares” is a number puzzle game in which the player must reconstruct a 3×3 grid of integers by guessing missing values. The grid is generated using four random variables (A, B, C, D) constrained to be distinct, and each cell is an arithmetic combination of those variables such that every row sums to 27. The player sees blanked-out cells (stored as zeros in array B) and enters guesses one at a time; a correct guess for a cell fills it in permanently. The program tracks the number of guesses with variable J and reports how many cells are correctly placed after each guess using a simple match-count loop.
From Giant Book of Computer Games by Tim Hartnell.
Program Analysis
Program Structure
The program is divided into two logical phases: initialization (the subroutine beginning at line 230) and the main game loop (lines 30–200). Control flow is straightforward: the subroutine sets up the puzzle once, and the main loop repeatedly accepts guesses and redraws the board until all nine cells are solved.
- Lines 5–10: Screen setup and title display.
- Line 20: Call initialization subroutine at
230. - Lines 30–200: Main game loop — increment guess counter, clear screen, display grid, check for win, prompt for input, update board.
- Lines 230–470: Initialization subroutine — allocate arrays, generate random parameters, compute grid values, blank out four cells, reset guess counter.
- Line 9997–9998: Stop guard and SAVE statement.
Grid Generation
Four distinct random integers A, B, C, D are drawn from 1–9 (line 250–290). The nine grid values in array A() are arithmetic combinations of these variables:
| Cell | Formula |
|---|---|
| A(1) | A+B |
| A(2) | A−(B+C) |
| A(3) | A+C |
| A(4) | A−B+C |
| A(5) | A |
| A(6) | A+B−C |
| A(7) | A−C |
| A(8) | A+B+C |
| A(9) | A−B |
Each row sums to 3A (row 1: cells 1–3 = (A+B)+(A−B−C)+(A+C) = 3A; similarly for rows 2 and 3), so for the “rows sum to 27” hint to hold, A must equal 9. However, A is drawn randomly from 1–9, meaning the row-sum property only holds when A happens to be 9 — this is a latent bug. The hint on line 151 (“Make all rows=27”) is therefore only correct in approximately 1 in 9 games.
Variable Name Collision
A significant bug exists in the initialization subroutine: the program uses both the scalar variable A and the array A() (declared at line 230). In Sinclair BASIC, a scalar variable and an array of the same letter cannot coexist — declaring DIM A(9) while also using LET A=... causes the scalar and array to conflict. In practice, the DIM A(9) at line 230 is executed before LET A=... at line 250, which will overwrite the array entry, leading to unpredictable behavior. The same collision applies to scalar B and array B().
Display Logic
The 3×3 grid is printed in a single FOR loop (lines 70–110). A newline is inserted after every third cell using the condition 3*INT(Z/3)=Z at line 90, which is true when Z is a multiple of 3 (i.e., Z=3, 6, 9). Blank cells are stored as 0 in array B() and printed as such, so the player sees a zero rather than a blank space for missing values.
Guess Processing
When the player enters a guess X at line 160, the loop at lines 170–190 scans all nine cells. A cell is filled in only if it currently holds 0 (i.e., is still blank) AND the answer for that cell equals X (line 180). This means a single guess can fill multiple cells simultaneously if more than one hidden cell has the same value — a useful shortcut for the player.
Win Detection
After each display pass, the match counter M is accumulated at line 100. If M=9 (line 130), the player is congratulated, a beep is sounded via BEEP 1,15, and the program halts. The guess count displayed is J-1 because J is incremented at line 30 before the win check, overcounting by one.
Notable Idioms
- The distinctness check at line
290uses a chainedORcondition to ensure all four random values are unique before proceeding. RANDOMIZEat line240(with no argument) seeds the random number generator from the system clock, ensuring different puzzles each run.- The
SAVE "IDAHO SQUA" LINE 1at line9998saves the program with an auto-start at line1(which does not exist, so execution starts at the first available line, effectively line5).
Content
Source Code
5 PAPER 0: BORDER 0: INK 7
10 PRINT TAB 6;"IDAHO SQUARES"
20 GO SUB 230
30 LET J=J+1
40 CLS
50 PRINT : PRINT : PRINT TAB 3;
60 LET M=0
70 FOR Z=1 TO 9
80 PRINT B(Z);" ";
90 IF 3*INT (Z/3)=Z THEN PRINT : PRINT : PRINT TAB 3;
100 IF B(Z)=A(Z) THEN LET M=M+1
110 NEXT Z
120 PRINT
130 IF M=9 THEN PRINT "YOU'VE SOLVED IT": PRINT "IN JUST ";J-1;" GUESSES": BEEP 1,15: STOP
140 PRINT : PRINT "THIS IS GUESS NUMBER ";J
150 PRINT : PRINT "YOU HAVE ";M;" RIGHT": PRINT
151 PRINT : PRINT "Make all rows=27"
160 INPUT "ENTER YOUR GUESS ";X
170 FOR Z=1 TO 9
180 IF B(Z)=0 AND A(Z)=X THEN LET B(Z)=X
190 NEXT Z
200 GO TO 30
210 STOP
220 REM
230 DIM A(9): DIM B(9)
240 RANDOMIZE
250 LET A=INT (RND*9)+1
260 LET B=INT (RND*9)+1
270 LET C=INT (RND*9)+1
280 LET D=INT (RND*9)+1
290 IF A=B OR B=C OR A=C OR A=D OR B=D OR C=D THEN GO TO 260
300 LET A(1)=A+B
310 LET A(2)=A-(B+C)
320 LET A(3)=A+C
330 LET A(4)=A-B+C
340 LET A(5)=A
350 LET A(6)=A+B-C
360 LET A(7)=A-C
370 LET A(8)=A+B+C
380 LET A(9)=A-B
390 FOR Q=1 TO 9
400 LET B(Q)=A(Q)
410 NEXT Q
420 LET B(A)=0
430 LET B(B)=0
440 LET B(C)=0
450 LET B(D)=0
460 LET J=0
470 RETURN
9997 STOP
9998 SAVE "IDAHO SQUA" LINE 1
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
