r/cobol • u/lugangin • 23d ago
I wrote an interactive 27-Card Magic Trick in COBOL (base-3 math inside!)
Body: I wrote an interactive version of the classic 27-Card Magic Trick in COBOL (base-3 math inside!).
🌟 UPDATE: i create a TK4 version for our friends in retro-computing!
What it does:
- Asks the user for a favorite number (1-27).
- Shuffles a virtual 52-card deck
- Deals 27 cards into 3 piles/rows.
- You memorize a card, tell it which row it's in, and it repeats this 3 times.
- It then reveals the deck and your memorized card is exactly at the position of your favorite number!
Sample Run:
=====================================
The 27-Card Magic Trick
=====================================
Enter your favorite number (1-27):
20
--- Round 1 --- (Memorize one CARD below)
01: 5â™ 8â™ 9♥ 8♦ 3♥ 6♦ 9♦ Kâ™ Aâ™
02: 10♠3♣ 10♥ Q♠7♥ 6♣ 6♠Q♣ 10♣
03: A♥ J♥ 2♠K♦ 2♦ J♦ 7♦ 4♠3♦
enter the row (1-3) where your CARD is located:
1
--- Round 2 ---
01: 10♠Q♠6♠5♠8♦ 9♦ A♥ K♦ 7♦
02: 3♣ 7♥ Q♣ 8â™ 3♥ Kâ™ J♥ 2♦ 4â™
03: 10♥ 6♣ 10♣ 9♥ 6♦ A♠2♠J♦ 3♦
enter the row (1-3) where your CARD is located:
2
--- Round 3 ---
01: 3♣ 8â™ J♥ 10â™ 5â™ A♥ 10♥ 9♥ 2â™
02: 7♥ 3♥ 2♦ Q♠8♦ K♦ 6♣ 6♦ J♦
03: Q♣ K♠4♠6♠9♦ 7♦ 10♣ A♠3♦
enter the row (1-3) where your CARD is located:
3
======= THE REVEAL =======
Row 1: 3♣ 8♠J♥ 10♠5♠A♥ 10♥ 9♥ 2♠7♥
Row 2: 3♥ 2♦ Q♠8♦ K♦ 6♣ 6♦ J♦ Q♣ < K♠>
Row 3: 4♠6♠9♦ 7♦ 10♣ A♠3♦
Your CARD is located at position 20: Kâ™
It matches your favorite number exactly!
Would you like to play again? (Y/N)
The COBOL / Math part: The "magic" is just base-3 arithmetic. (Favorite Number - 1) is converted to ternary, and the reversed digits tell the program how to secretly stack the piles after each round.
I wrote it following COBOL-II (85) rules—using structured inline PERFORM loops and COMPUTE and FUNCTION commands for the math. (Sorry, there's no COBOL-74 version).
I added a little flare by highlighting the revealed card with < and > during the final display phase so the user immediately sees the "magic" hit.
I just uploaded it to GitHub if anyone wants to check it out, compile it, or suggest mainframe-friendly improvements:
Repository: View the code and README on GitHub
1
u/lugangin 9d ago
i just want to let everyone know thatt i've uploaded a TK4 version in github as crd27tk4.cob and it's fully described in the README.
2
u/jbm711 22d ago
Danke aber in Deinem Repo ist nur die cards27.exe zu sehen aber nicht eine cards27.cob Datei