Mind vs Machine

Date: 1983
Type: Cassette
Platform(s): TS 1000
Tags: Game

This compilation contains two separate BASIC games. The first, “The Chocolate Truffle Game,” is a Nim-style strategy game where the player and computer alternately take 1 to M−1 truffles from a pile of N truffles (N randomly chosen between 15 and 29), with the last player to take winning; the computer uses the classic modular arithmetic strategy (K = N mod M) to play optimally. The second program, “SuperSpy,” is a Caesar-cipher decoding game that encodes a randomly selected 20-character substring from a hardcoded string of phrases, shifting each character by a random offset O, and challenges the player to interactively map encoded letters back to their originals until the decoded message matches the plaintext. Both programs use PRINT AT for screen positioning and simple INKEY$/INPUT loops for user interaction. The Truffle game draws the remaining truffles graphically on screen row 10 using a block graphic character.


Program Analysis

Program 1: The Chocolate Truffle Game

This is a two-player Nim variant. A pile of N truffles (randomly 15–29) is shared between the human and the computer. On each turn, a player may take between 1 and M−1 truffles, where M is a random modulus (3–8). The player who takes the last truffle wins.

Program 1 Structure

LinesPurpose
1–19Setup: titles, RAND, CLEAR
20–30Initialize N (pile size) and M (max take + 1)
40–70Display move options for the human player
80–175Human input loop using INKEY$
180–200Update pile, check win condition for human
210–290Computer’s turn, update pile, check win condition
300–380Subroutine: draw remaining truffles graphically
400–410Invalid-input handler (unreachable — see anomalies)
500–620Win/loss messages and replay prompt

Nim Strategy (Computer AI)

The computer’s optimal move is calculated at line 230: K = N - M * INT(N/M), which is equivalent to K = N MOD M. This is the textbook winning strategy for this type of Nim game — by always leaving the opponent facing a multiple of M truffles, the computer forces a win whenever possible. If the result is zero (line 240), the computer is forced to take 1, meaning it is in a losing position.

Program 1: Notable Techniques

  • INKEY$ is used at line 150 for single-keypress input without requiring ENTER; the key code is converted to a numeric value via K = CODE K$ - 28 at line 160, exploiting the character code layout for digits.
  • The subroutine at line 300 redraws the truffle display each turn: it erases positions N+1 through 31 (line 310–330) then draws block graphic characters for positions 1 through N (lines 340–360), achieving simple animation by overwriting.
  • Inverse-video bracketed characters (e.g., [T][H][E]) are used for stylized title display at line 15.

Program 1: Bugs and Anomalies

  • Lines 400–410 (the “TRY AGAIN” handler) are never reached. The input validation at line 170 loops directly back to line 150 on invalid input, bypassing lines 400–410 entirely.
  • The loop at line 310 iterates J = N+1 TO 31, printing spaces at screen column J-1 (0–30). Since the display row is only 32 columns wide, this correctly erases up to column 30, leaving column 31 untouched — a minor cosmetic edge case for large N.

Program 2: SuperSpy (Caesar Cipher Decoder)

SuperSpy encodes a randomly selected 20-character substring from a hardcoded phrase bank and challenges the player to reconstruct the original message by interactively supplying letter substitutions. The encoding is a rotational (Caesar-style) cipher with a random shift.

Program 2 Structure

LinesPurpose
1–35Setup and intro text
50Define hardcoded source string S$ (100 chars of phrases)
54–65Pick random start offset T and cipher shift O; extract 20-char plaintext into M$
70–100Encode M$ into first 20 chars of S$; clear chars 21–40 of S$ to solid blocks
115–130Display encoded message (rows 10–11)
140–260Player input loop: ask for letter to translate, apply substitution to S$(21 TO 40)
270Check if decoded buffer matches plaintext
300–310Success message and reveal

Program 2: Encoding Mechanism

The cipher is implemented at lines 90–95. For each character of the plaintext M$, its code is shifted by O: T = CODE M$(J) + O - 64*(CODE M$(J) > 63 - O). The conditional subtraction of 64 implements a wrap-around within a 64-character window (codes 32–95, covering uppercase letters and spaces), keeping the encoded characters in the printable ASCII range. Line 95 then applies a further correction: CHR$(T + 37*(T < 38+O AND T > 0)), which handles edge cases for the space character and very low-code results.

Program 2: Decoding Interaction

The decode buffer occupies S$(21 TO 40), initialized to solid block characters at line 80. When the player maps an encoded letter C$ to a plaintext letter V$, the loop at lines 210–240 scans all 20 positions of the encoded message and writes V$ into the corresponding decode buffer position wherever a match is found. The game ends when M$ = S$(21 TO 40) (line 270), i.e., every decoded position matches the original plaintext exactly.

Program 2: Notable Techniques

  • The phrase bank at line 50 is a single long string; substrings are extracted using the slice notation S$(T+1 TO T+20), avoiding any array overhead.
  • The same string variable S$ serves three purposes simultaneously: the source phrase bank (indices 1–100 initially), the encoded ciphertext (indices 1–20 after encoding), and the player’s working decode buffer (indices 21–40). This is a compact memory reuse technique.
  • Input validation at lines 170 and 200 enforces single-character responses by checking LEN C$ <> 1.
  • The start offset T is calculated as 20 * INT(3.9 * RND), yielding multiples of 20 in {0, 20, 40, 60}, selecting one of four non-overlapping 20-character phrases from the bank.

Content

Appears On

Related Products

Four opportunities to beat the computer. Tic Tac Toe I and II: Basic and advanced variations of the classic game...

Related Articles

Related Content

Image Gallery

Source Code

   1 REM TRUFFLES
   2 REM COPYRIGHT 1982:                    EMERSON AND STERN
  14 PRINT ""
  15 PRINT "   [T][H][E] [C][H][O][C][O][L][A][T][E] [T][R][U][F][F][L][E] [G][A][M][E]"
  16 PRINT "YOU TAKE SOME AND I TAKE SOME"
  17 RAND 
  18 PRINT "  THE LAST ONE TO TAKE SOME WINS "
  19 CLEAR
  20 LET N=INT (15*RND+15)
  30 LET M=INT (5.5*RND+3)
  40 GOSUB 300
  50 PRINT AT 19,0;" ";
  55 FOR J=1 TO M-2
  60 PRINT J;" ";
  65 NEXT J
  70 PRINT "OR ";M-1;" TRUFFLES"
  80 PRINT AT 18,0;"YOUR TURN..YOU MAY TAKE "
 140 PRINT AT 20,0;"HOW MANY?"
 150 LET K$=INKEY$ 
 160 LET K=CODE K$-28
 170 IF K<1 OR K>M-1 THEN GOTO 150
 175 PRINT AT 20,11;"OK ";K
 180 LET N=N-K
 190 GOSUB 300
 200 IF N=0 THEN GOTO 500
 210 PRINT AT 18,0;" MY TURN     I"
 220 PRINT AT 20,0;"I TAKE            "
 230 LET K=N-M*INT (N/M)
 240 IF K=0 THEN LET K=1
 250 PRINT AT 20,7;K
 260 LET N=N-K
 270 GOSUB 300
 280 IF N=0 THEN GOTO 550
 290 GOTO 80
 300 REM DRAW  AND REMOVE
 305 PRINT AT 13,0;"THERE ARE ";N;" TRUFFLES  "
 310 FOR J=N+1 TO 31
 320 PRINT AT 10,J-1;" "
 330 NEXT J
 340 FOR J=1 TO N
 350 PRINT AT 10,J-1;"▖"
 360 NEXT J
 380 RETURN
 400 PRINT AT 20,0;"TRY AGAIN"
 410 GOTO 150
 500 LET W$="[Y][O][U]█[W][I][N]█     "
 510 GOTO 600
 550 LET W$="[I]█[W][I][N]█       "
 600 PRINT AT 18,0;W$;"              "
 610 PRINT AT 19,0;"PRESS [R] (FOR RUN) THEN [E][N][T][E][R]"
 620 PRINT AT 20,0;"   TO  PLAY  AGAIN  "
 
   1 REM COPYRIGHT 1982 EMERSON AND STERN
  10 PRINT "████[*]█[S][U][P][E][R][S][P][Y]█[*]███"
  15 CLEAR
  20 PRINT "TO DECODE THE SECRET MESSAGE YOU" 
  25 PRINT "SAY HOW TO TRANSLATE EACH LETTER"
  30 PRINT AT 9,0;"I""M SMUGGLING A MESSAGE"
  35 PRINT AT 10,0;"PLEASE WAIT"
  50 LET S$="TIMEX SINCLAIR POWERTWO  BIT  SOFTWARE  CHILDREN ARE PEOPLE ALICE IN WONDERLAND THE SILICON CURTAIN "
  54 RAND 
  55 LET T=20*INT (3.9*RND)
  60 LET O=INT (24*RND)+1
  65 LET M$=S$(T+1 TO T+20)
  70 FOR J=1 TO 20
  80 LET S$(J+20)="█"
  90 LET T=CODE M$(J)+O-64*(CODE M$(J)>63-O)
  95 LET S$(J)=CHR$ (T+37*(T<38+O AND T>0))
 100 NEXT J
 115 PRINT AT 9,0;"    HERE""S YOUR"
 120 PRINT AT 10,0;S$(1 TO 20)
 130 PRINT AT 11,0;S$(21 TO 40)
 140 PRINT AT 20,0;"PRESS LETTER THEN ENTER"
 150 PRINT AT 19,0;"LETTER TO TRANSLATE?"
 160 INPUT C$
 170 IF LEN C$<>1 THEN GOTO 160
 180 PRINT AT 19,0;"TRANSLATE TO WHAT?  "
 190 INPUT V$
 200 IF LEN V$<>1 THEN GOTO 190
 205 PRINT AT 19,0;" I""M WORKING       "
 210 FOR J=1 TO 20
 220 IF S$(J)=C$ THEN LET S$(J+20)=V$
 240 NEXT J
 260 PRINT AT 11,0;S$(21 TO 40)
 270 IF M$<>S$(21 TO 40) THEN GOTO 150
 300 PRINT AT 20,0;"YOU GOT IT..GOOD WORK,SUPERSPY"
 310 PRINT AT 19,0;M$

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

People

No people associated with this content.

Scroll to Top