Skip to content

About

magic trick using some python and math

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

Repository files navigation

✧ pyHocusPocus

The classic 21-card trick — a little mathematics, a little magic.

Think of a card. Point to its pile three times. Watch it appear.

Play now

Deploy Python 3 HTML, CSS, JavaScript No dependencies

The Hocus Pocus browser game: three piles of seven cards on a green felt table

Contents

How the trick works

Deal 21 cards into three piles of seven. The player silently picks a card and says only which pile it is in. Pick the piles back up with the chosen pile in the middle, deal again, and repeat.

Each round squeezes the card into a smaller part of the deck:

Round Where the card can be Possibilities
Start 1 – 21 21
1 8 – 14 7
2 10 – 12 3
3 11 1

After the third round the card is always the 11th in the deck. It is deduction, not a guess.

📺 Numberphile has a great explanation of the maths: 21 Card Trick

🌐 Browser game

Play it here — nothing to install.

  1. Memorize any one of the 21 cards. Don't click it.
  2. Select I have my card.
  3. Choose the pile that contains your card, or press 1, 2, or 3.
  4. Do the same for the next two deals, then watch the reveal.

It works with mouse, touch, or keyboard, and has optional sound.

Run it locally

The game is plain HTML, CSS, and JavaScript with no build step. Open web/index.html directly, or serve the folder:

py -m http.server 8000 --directory web

Then open http://localhost:8000.

Deployment

Any change to web/ on main is published to GitHub Pages automatically by pages.yml.

🐍 Python version

The original terminal version. Requires Python 3.

py .\Pocus.py

Remember a card and enter its row number each time you are asked. The program suggests a card to follow, but any card works.

Screenshots
Round 1 Round 2
First round Second round
Round 3 Reveal
Third round Card reveal

Testing

Run one automated game, where the program picks a card itself and checks that the reveal matches:

py -c "from Pocus import test_Pocus; test_Pocus()"

The output is picked card, revealed card, Pass/Fail, final position:

9 of Clubs,9 of Clubs,Pass,8

Project layout

pyHocuspocus/
├── Pocus.py          # Terminal game and trick logic
├── Deck.py           # Card and deck helpers
├── test-pocus.ps1    # PowerShell test runner
├── media/            # Screenshots
└── web/              # Browser game
    ├── index.html
    ├── styles.css
    └── game.js

About

magic trick using some python and math

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages