This program implements a classic “psychic card trick” in which the computer correctly identifies a letter secretly chosen by the user from a 9×3 grid. The grid is initialized by populating a two-dimensional string array with sequential ASCII characters starting at CHR$ 65 (the letter “A”), with a space placed at position (9,3) to keep the count at 26. Before play begins, 30 random swaps shuffle the grid by exchanging arbitrary cells with the fixed center cell (5,2). The trick works by repeating a column-selection and matrix-transposition cycle three times: each round the user’s chosen column is swapped to become column 2, then the entire matrix is rebuilt by reading columns as rows, guaranteeing the chosen letter converges to position (5,2) after three passes. The program then directly reads and displays R$(5,C) as the “psychic” result.
Program Analysis
Program Structure
The program divides into four clear phases:
- Initialization (lines 5–100): Dimensions and populates the 9×3 string array
R$with 26 letters (A–Z) plus one trailing space, then places a space atR$(9,3)to fill the 27th cell. - Shuffle (lines 110–170): Performs 30 random swaps, each exchanging a randomly chosen cell with the fixed center cell
R$(5,2). - Game loop (lines 180–480): Repeats three times: displays the grid, asks the user which column their letter is in, swaps that column to column 2, then transposes the matrix by reading columns as rows.
- Reveal (lines 490–540): Prints the value at
R$(5,C)as the “psychic” answer and offers a replay.
Grid Initialization
Lines 50–90 fill R$(A,B) using the expression CHR$ (61+A*3+B). For A=1, B=1 this yields CHR$ 65 = “A”, and the sequence runs through CHR$ 90 = “Z” at A=9, B=2, with CHR$ 91 = “[” at R$(9,3). Line 100 then overwrites that cell with a space, correcting the 27th slot to a non-letter filler.
The Mathematical Trick
The algorithm is a well-known combinatorial card trick generalized to a letter grid. Each round, swapping the user’s chosen column into the middle column (column 2) aligns the target letter somewhere in column 2. The transposition step (lines 360–460) rebuilds the matrix by treating each original column as a new row: column 1 becomes row 1, column 2 becomes rows 4–6 (positions 4–6 of the new first column, etc.). After three repetitions the chosen letter is guaranteed to occupy position R$(5,2) — the exact center of the 9×3 grid — regardless of where it started.
Transposition Technique
The transposition is performed in two steps. First, a temporary string T$ is built by concatenating all three columns sequentially (lines 370–410), yielding a 27-character string ordered column-by-column. Then the array is refilled from T$ using the index T$(A*3+B-3) (lines 420–460), which maps back to the new row-major ordering.
Key BASIC Idioms
- Column-swap idiom: Lines 310–350 perform a three-way swap of columns
Cand 2 usingT$as a temporary variable, row by row. - Input validation: Line 290 uses
IF C<1 OR C>3 THEN GO TO 280to loop until a valid column (1–3) is entered. - Replay guard: Line 530 uses
(T$+" ")(1)to safely extract the first character of the input without risking an error on an empty string — a common defensive idiom. - Loop-controlled display: The outer
FOR I=1 TO 3loop (line 180) drives both display and column selection; line 300 skips the transposition on the third (final) pass since the answer is read immediately after.
Notable Anomalies
| Location | Observation |
|---|---|
| Line 100 | R$(9,3) is set to a space to correct the off-by-one in the CHR$ formula, which would otherwise place “[” (CHR$ 91) in the last cell. |
| Line 300 | When I=3 the program jumps to line 470 (CLS), skipping the column swap and transposition. This is intentional — the reveal uses R$(5,C) with the final column input, so no further rearrangement is needed. |
| Line 533 | GO TO 180: STOP — the STOP after the GO TO is unreachable dead code; execution always branches before reaching it. |
| Lines 110–170 | The shuffle always swaps through R$(5,2), meaning the center cell is statistically far more likely to move than other cells. This is a cosmetic shuffle whose primary purpose is visual randomization of the display, not true uniformity. |
Content
Source Code
5 CLS
10 REM "PSYCHIC"
20 REM "RICHARD W. MCDANIEL"
30 REM "JANUARY 1,1983"
40 DIM R$(9,3)
50 FOR A=1 TO 9
60 FOR B=1 TO 3
70 LET R$(A,B)=CHR$ (61+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;"COL.1";TAB 13;"COL.2";TAB 22;"COL.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 COLUMN CONTAINING YOUR LETTER."
280 INPUT C
290 IF C<1 OR C>3 THEN GO TO 280
300 IF I=3 THEN GO TO 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 OR N)"
510 INPUT T$
520 CLS
530 IF (T$+" ")(1)="Y" THEN GO TO 180: STOP
540 STOP
9998 SAVE "PSYCHIC" LINE 5
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
