This program implements a classic “psychic card trick” where the computer appears to guess a letter chosen by the user from a displayed grid. A 9×3 grid of characters is initialised using CHR$ codes starting at offset 37, then shuffled 30 times by swapping random elements with the centre cell (row 5, column 2). Over three rounds the user identifies which column contains their chosen letter, and the program rearranges the grid columns so the target always migrates to the centre position R$(5,2). The column-swap and linearisation technique (lines 360–460) is the mathematical heart of the trick: after each selection the chosen column is swapped to the middle, then the grid is serialised and rebuilt, keeping the target converging on cell (5,2). The game loops on request, reinitialising the grid each time via the outer FOR I loop at line 180.
Program Analysis
Program Structure
The program divides into four logical phases:
- Initialisation (lines 40–100): Declares a 9×3 string array
R$and fills each cell with a unique character derived fromCHR$(34 + A*3 + B), giving characters in the range CHR$(38)–CHR$(63). The last cellR$(9,3)is set to a space, keeping the usable symbol count at 26 (one per letter of the alphabet). - Shuffle (lines 110–170): Performs 30 random swaps between a randomly chosen cell and the fixed centre cell
R$(5,2), ensuring the grid is well mixed before play. - Game loop (lines 180–480): Iterates three times (
FOR I=1 TO 3). Each iteration displays the grid, prompts for a column number, then rearranges the grid. - Reveal and replay (lines 490–600): Announces the chosen letter from
R$(5,C)and offers a replay.
The Mathematical Trick
The core illusion is a well-known combinatorial card trick applied to a 27-element grid. After the user names the column containing their letter, the program swaps that entire column into column 2 (the middle), guaranteeing the target letter lies somewhere in column 2. The grid is then linearised and rebuilt (lines 360–460), effectively re-striping the rows so that after three such operations the target converges on exactly row 5 of the middle column — cell R$(5,2).
Column Swap and Linearisation (Lines 310–460)
After reading the user’s column choice C, lines 310–350 swap every element of column C with the corresponding element of column 2. Lines 360–400 then concatenate all three columns in order into the temporary string T$ (column 1 first, then 2, then 3), producing a 27-character string. Lines 420–460 rebuild R$ from this string using the index formula T$(A*3+B-3), which reads nine characters per column back into each row. This redistribution ensures the middle column’s contents are spread across the middle third of the linear sequence, placing the target at the physical midpoint after the third round.
Key BASIC Idioms and Techniques
- Array-based string grid: Using
DIM R$(9,3)as a two-dimensional string array of single characters is an efficient way to represent a matrix without needing a two-dimensional numeric array plus a separate lookup table. - CHR$ initialisation: Filling cells with
CHR$(34+A*3+B)ensures all 26 active cells receive distinct, printable characters in one compact nested loop. - Swap idiom: The classic three-step swap via a temporary variable
T$appears both in the shuffle phase and in the column-swap routine. - Input validation (line 290):
IF C<1 OR C>3 THEN GOTO 280loops until a valid column (1–3) is entered. - Third-round shortcut (line 300): On the final iteration
IF I=3 THEN GOTO 470skips the rearrangement entirely; by this point the maths has already guaranteedR$(5,C)holds the target, so no further shuffling is needed. - Safe replay test (line 530):
(T$+" ")(1)="Y"padsT$with a space before taking the first character, preventing a crash if the user presses ENTER without typing anything (which would leaveT$as a null string).
Display Layout
Lines 190–260 print column headers “ROW 1”, “ROW 2”, “ROW 3” using TAB stops at 4, 13, and 22, then print each of the nine rows of the grid with eight spaces between characters (R$(A,B);" ";). The TAB 6 on line 210 indents each data row slightly. Two trailing commas on line 190 add blank lines for visual spacing.
Bugs and Anomalies
- Column labelling mismatch: The display headers read “ROW 1”, “ROW 2”, “ROW 3” but the prompt on line 270 asks for “THE ROW CONTAINING YOUR LETTER” — the grid is actually organised so the user is selecting a column, not a row. This is intentionally confusing terminology common to this type of trick, but may puzzle players.
- Final reveal uses
Cnot2: Line 490 printsR$(5,C)whereCis the last column entered. After the third round’s column swap (which is skipped by line 300), the target is inR$(5,C)because the swap from the previous iteration placed it at(5,2)and the user’s final choiceCmust equal 2 for the trick to work — or the trick relies on the linearisation ensuring row 5 of whichever columnCis contains the target. In practice the algorithm guarantees correctness only when all three rounds function as designed. - Shuffle biased toward centre: The shuffle always swaps with
R$(5,2), so the centre cell is involved in every swap. While this mixes the grid, it means the initial distribution is not uniformly random across all cells, though this does not affect correctness of the trick.
Content
Source Code
10 REM PSYCHIC
20 REM SYNC JAN/FEB 84 PP 19-20
30 REM GAME
40 DIM R$(9,3)
50 FOR A=1 TO 9
60 FOR B=1 TO 3
70 LET R$(A,B)=CHR$ (34+A*3+B)
80 NEXT B
90 NEXT A
100 LET R$(9,3)=" "
110 FOR I=1 TO 30
120 LET A=INT (RND*9)+1
130 LET B=INT (RND*3)+1
140 LET T$=R$(A,B)
150 LET R$(A,B)=R$(5,2)
160 LET R$(5,2)=T$
170 NEXT I
180 FOR I=1 TO 3
190 PRINT TAB 4;"ROW 1";TAB 13;"ROW 2";TAB 22;"ROW 3",,
200 FOR A=1 TO 9
210 PRINT TAB 6;
220 FOR B=1 TO 3
230 PRINT R$(A,B);" ";
240 NEXT B
250 PRINT
260 NEXT A
270 PRINT "ENTER THE ROW CONTAINING YOUR LETTER."
280 INPUT C
290 IF C<1 OR C>3 THEN GOTO 280
300 IF I=3 THEN GOTO 470
310 FOR A=1 TO 9
320 LET T$=R$(A,2)
330 LET R$(A,2)=R$(A,C)
340 LET R$(A,C)=T$
350 NEXT A
360 LET T$=""
370 FOR A=1 TO 3
380 FOR B=1 TO 9
390 LET T$=T$+R$(B,A)
400 NEXT B
410 NEXT A
420 FOR A=1 TO 9
430 FOR B=1 TO 3
440 LET R$(A,B)=T$(A*3+B-3)
450 NEXT B
460 NEXT A
470 CLS
480 NEXT I
490 PRINT ,,"I MYSTICALLY PERCEIVE YOUR","LETTER TO BE ";R$(5,C);"."
500 PRINT "WOULD YOU LIKE TO TEST MY MAGIC ABILITIES AGAIN?(Y/N)"
510 INPUT T$
520 CLS
530 IF (T$+" ")(1)="Y" THEN GOTO 180
600 STOP
700 SAVE "1014%4"
710 RUN
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
