This is an implementation of the Javascript Card Trick video from Dr Tim Muller on Computerphile.
Spectator selects five cards at random from a deck, tells the computer about four of them, and the computer predicts the fifth.
Magician picks the order of the four cards given to the computer, thereby encoding the value of the fifth.
The goal is to host Tim's original implementation on a published GitHub Page along with my version that has an enhanced UI/UX (with help from Cursor).
My version with the enhanced UI/UX is available at magic.tinefamily.com
Tim's original version is available at magic.tinefamily.com/index_tim.html
The suit of the 5th card matches the suit of the first card
The rank of the 5th card is incremented up from the rank of the first card based on the order of cards 2-5 (with Ace being high) and the CHaSeD suit order breaking ties (e.g. Diamonds is higher than Clubs):
- Offset of 1 -> Cards 2-5 are ordered:
Low - Med - High - Offset of 2 -> Cards 2-5 are ordered:
Low - High - Med - Offset of 3 -> Cards 2-5 are ordered:
Med - Low - High - Offset of 4 -> Cards 2-5 are ordered:
Med - High - Low - Offset of 5 -> Cards 2-5 are ordered:
High - Low - Med - Offset of 6 -> Cards 2-5 are ordered:
High - Med - Low
Note: A is treated as low (i.e. value of 1) in determining the card order.
Open index.html in a browser
A voice-driven version powered by AWS Lambda + Amazon Bedrock (Claude 3 Haiku). The performer speaks four cards aloud; Zarcon the AI magician predicts the hidden fifth.
Phone mic → Web Speech API → voice.html
→ POST transcript → API Gateway → Lambda (Node.js 20)
→ Bedrock ConverseCommand (Claude 3 Haiku) with tool
→ Lambda runs card algorithm → returns prediction
→ voice.html speaks response via browser TTS
- AWS CLI configured (
aws configure) - Node.js 20+ via nvm (
nvm use 20) zip(sudo apt install zip)
./scripts/deploy.shThe script is safe to re-run. It will:
npm installthe Lambda dependencies and zip the package- Create an IAM role with Bedrock + Lambda execution permissions
- Create or update the Lambda function (Node.js 20, 256 MB, 30 s timeout)
- Create an API Gateway HTTP API with CORS configured for
magic.tinefamily.com - Patch
voice.htmlwith the live API endpoint URL
One manual step: Enable Bedrock model access in the AWS Console before invoking the function. The deploy script prints the direct link when it finishes.
Browsers block API calls from file:// URLs. Serve the page locally instead:
python3 -m http.server 8080
# then open http://localhost:8080/voice.htmlcurl -X POST <endpoint printed by deploy.sh> \
-H 'Content-Type: application/json' \
-d '{"transcript":"Ace of spades, three of hearts, jack of clubs, seven of diamonds"}'
# Expected: {"text":"...dramatic prediction...","audio":null}./scripts/teardown.shDeletes the Lambda function, IAM role, API Gateway, and local build artifacts. Restores the placeholder URL in voice.html.