Electric Brain

Date: 198x
Type: Program
Platform(s): TS 2068
Tags: Game

Electric Brain is a code-breaking game in which the computer attempts to guess a three-digit code (using digits 1–9, no repeats) that the player holds in mind, similar to the “Bulls and Cows” or Mastermind family of games. The player responds to each guess with a count of “blacks” (correct digit in correct position) and “whites” (correct digit in wrong position). The program uses a candidate array C(9) that encodes elimination data directly into the numeric values of remaining candidates, appending penalty scores via arithmetic (multiplying by powers of 10) to bias future selections. A separate “best match” tracking variable AID and array E(3) ensure the computer preferentially selects guesses that match previously successful partial hits. A duplicate-guess prevention mechanism stores each guess as a packed three-digit integer in array K(100) and checks new candidates against all prior guesses before accepting them.


Program Structure

The program is organized into a main loop and three subroutines:

  1. Lines 5–270: Main game loop — initializes, then iterates guess cycles, collecting player feedback and updating the candidate pool.
  2. Lines 280–500: Subroutine to pick three non-repeating candidate digits using the scored candidate array.
  3. Lines 510–590: Initialization subroutine — dimensions arrays, seeds the random number generator, and populates the candidate pool.
  4. Lines 610–630: Win/termination block (jumped to from line 150 when b=3).

Candidate Scoring Mechanism

The heart of the program is the array C(9), initially holding values 1–9. When a digit is confirmed absent (0 blacks + 0 whites), its slot in C is zeroed (lines 200). When a digit appears in a guess with some hits, its candidate value is inflated by encoding the feedback score directly into its numeric representation (line 240):

  • C(Z) = C(Z) + (B+W)*100 + W*10

This means a candidate’s value grows larger the more often it has appeared in successful partial guesses. Later, when picking new guesses (subroutine at line 280), two candidate values are drawn randomly and compared by their tens digits (lines 340–360) using INT(D/10). The one with the higher tens digit (i.e., more white-hit evidence) is preferred.

After selection, the raw digit is extracted by repeatedly subtracting 100× and 10× multiples (lines 370–380), effectively computing the value modulo 10.

Best-Match Enforcement

The variable AID tracks the highest number of blacks scored in any previous guess, and E(3) stores that best guess. Lines 410–420 enforce that any newly proposed guess must match at least AID digits in the same positions as the best historical guess, otherwise it is rejected and a new one is drawn. This heuristic ensures the computer does not regress to worse guesses.

Duplicate Guess Prevention

Each accepted guess is packed as a three-digit integer (M = 100*B(1) + 10*B(2) + B(3)) and stored in K(GUESS). Before any guess is accepted after the third turn, lines 460–490 scan all prior entries in K and reject the candidate if it matches a previous guess. Note that K is dimensioned to 100 elements, giving ample room for extended play.

Early Guesses

For the first three guesses (GUESS < 3), the computer simply uses fixed triads — digits 1,2,3 then 4,5,6 then 7,8,9 — rather than invoking the subroutine (line 40). This seeds the candidate pool with known-position data before the probabilistic selection begins. Note that GUESS starts at 0 and is incremented to 1 before the first guess is shown, so the condition GUESS < 3 at line 40 covers guesses 1 and 2 (GUESS values 0 and 1 before increment — actually at the point of the check, GUESS has not yet been incremented, so values 0, 1, and 2 map to displayed guesses 1, 2, and 3).

Bugs and Anomalies

  • Mixed case variable b vs B: Line 150 tests b=3 and line 160 tests b=2, while B (the blacks count) was assigned at line 140 via INPUT ... B. In Sinclair BASIC, variable names are case-insensitive for numeric variables, so this works correctly, but the inconsistency is a readability concern. Similarly, c(2) at line 190 mixes with C() elsewhere.
  • Win branch skips white input: Line 150 jumps directly to line 600 (which does not exist in the listing — likely a typo for line 610) when b=3, correctly skipping the whites prompt since three blacks means the code is cracked.
  • Line 600 missing: The target of GO TO 600 (line 150) is absent; the program falls through to line 610. In Sinclair BASIC, GO TO to a non-existent line number jumps to the next higher line, so execution continues at line 610 as intended.
  • Unused DEF FN X(): Line 520 defines a function FN X() = INT(A-Z) referencing global variables A and Z, but this function is never called anywhere in the program. It appears to be a leftover from development.
  • Line 420 structure: The NEXT Z at line 420 closes a FOR loop opened at line 410, both guarded by IF AID>0 THEN. This is an unusual multi-statement IF continuation across two lines, which works in Sinclair BASIC because each line is treated as a single logical statement chain.
  • No input validation: The program does not validate that entered blacks/whites values are consistent or within range, so invalid input could corrupt the candidate pool.

Variable Summary

VariablePurpose
GUESSCurrent guess number (counter)
B(3)Current guess digits (array of 3)
C(9)Candidate pool with encoded scores
E(3)Best-scoring previous guess digits
K(100)History of packed guess integers
AIDBest blacks score so far
QSize of active candidate pool (fixed at 9)
BBlacks count from player input
WWhites count from player input
COUNTTemporary counter for duplicate checking
MPacked integer representation of current guess
D1, D2Randomly drawn candidate values for comparison

Content

Appears On

Related Products

Related Articles

Related Content

Image Gallery

Source Code

   5 PAPER 0:BORDER 0:INK 7
  20 GO SUB 510:REM Initialise
  30 REM Make a guess
  40 IF GUESS <3 THEN FOR Z=1 TO 3:LET B(Z)=Z+3*GUESS:NEXT Z:GO TO 70
  60 GO SUB 280
  70 CLS 
  80 LET GUESS =GUESS +1
  90 PRINT :PRINT 
 100 PRINT "GUESS NUMBER";GUESS
 110 PRINT 
 120 PRINT "My gues is ";B(1);B(2);B(3)
 130 PRINT :PRINT 
 140 INPUT "How many blacks ";B
 150 IF b=3 THEN GO TO 600
 160 IF b=2 THEN GO TO 190
 170 PRINT :PRINT 
 180 INPUT "And how many whites ? ";W
 190 IF W+B=3 THEN LET Q=3:LET C(1)=B(1):LET c(2)=B(2):LET C(3)=B(3)
 200 IF B+W=0 THEN LET C(B(1))=0:LET C(B(2))=0:LET C(B(3))=0:GO TO 30
 210 IF B>AID THEN FOR Z=1 TO 3:LET E(Z)=B(Z):NEXT Z:LET AID=B
 220 FOR Z=1 TO 9
 230 FOR D=1 TO 3
 240 IF B(D)=C(Z) THEN LET C(Z)=C(Z)+(B+W)*100+W*10
 250 NEXT D
 260 NEXT Z
 270 GO TO 30
 280 REM Pick three numbers
 290 FOR Z=1 TO 3
 300 LET D1=C(INT (RND*Q)+1)
 310 IF D1=0 THEN GO TO 300
 320 LET D2=C(INT (RND*Q)+1)
 330 IF D2=0 THEN GO TO 320
 340 IF INT (D1/10)> INT (D2/10) THEN LET B(Z)=D1
 350 IF INT (D1/10)< INT (D2/10) THEN LET B(Z)=D2
 360 IF INT (D1/10)= INT (D2/10) THEN LET B(Z)=D1
 370 IF B(Z)>100 THEN LET B(Z)=B(Z)-100* INT (B(Z)/100):GO TO 370
 380 IF B(Z)>10 THEN LET B(Z)=B(Z)-10* INT (B(Z)/10):GO TO 380
 390 NEXT Z
 400 IF B(1)=B(2) OR B(1)=B(3) OR B(2)=B(3) THEN GO TO 290
 410 IF AID>0 THEN LET COUNT =0:FOR Z=1 TO 3:IF B(Z)=E(Z) THEN LET COUNT=COUNT+1
 420 IF AID>0 THEN NEXT Z:IF COUNT<AID THEN GO TO 290
 430 LET M=100*B(1)+10*B(2)+B(3)
 440 LET K(GUESS)=M
 450 IF GUESS<3 THEN GO TO 500
 460 LET COUNT=1
 470 LET COUNT=COUNT+1
 480 IF K(COUNT)=M THEN GO TO 290
 490 IF COUNT<GUESS-1 THEN GO TO 470
 500 RETURN 
 510 REM INITIALISE
 520 DEF FN X()= INT (A-Z)
 530 LET GUESS=0:LET Q=9:LET AID=0
 540 RANDOMIZE 
 550 DIM B(3):DIM C(9):DIM E(3):DIM K(100)
 560 FOR Z=1 TO 9
 570 LET C(Z)=Z
 580 NEXT Z
 590 RETURN 
 610 PRINT "I guessed your code of ";\{20}\{0}B(1);B(2);B(3)
 620 PRINT TAB 5;"in just ";GUESS;" guesses"
 630 STOP 

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top