Toss the coin, learn your fate ๐ช๐ฎ
A medieval-themed, AI-powered coin toss application that dynamically generates poetic prophecies based on your gameplay using the Gemini API.
Screen.Recording.2026-07-24.at.2.39.49.PM.mp4
Sometimes you need to make a tough decisionโlike whether to "Journey to the East" or "Defend the Castle". A standard coin flip gives you a binary answer, but it lacks flair. SpellFlip turns a simple coin toss into an immersive, atmospheric experience. By feeding the outcome of your tosses into a generative AI model, it acts as a digital seer, providing customized, narrative-driven prophecies that make your decisions feel legendary.
User Input (Labels & Tosses)
โ
Frontend (Vanilla JS / CSS Animations)
(Simulates the coin toss and tracks scores)
โ
Backend API โ Python Serverless Function
(Builds the prompt based on the final score)
โ
Gemini AI
(Generates a medieval, Instagram-worthy prophecy)
โ
The Magician's Note
Using Vanilla HTML/CSS/JS for the frontend keeps the application incredibly lightweight and blazing fast, without the overhead of heavy frameworks like React. The coin flip parallax animations (snowfall, layered backgrounds) run smoothly purely through CSS.
For the backend, Python was chosen for its unparalleled simplicity and native support on Vercel Serverless Functions. It securely hides the Gemini API key from the browser while efficiently formatting dynamic prompts using only the Python standard library.
| Feature | Description |
|---|---|
| Custom Stakes | You define what "Heads" and "Tails" represent (e.g., Buy Pizza vs Cook at Home). |
| Best-of-N | Choose how many times you want to toss (must be an odd number, as favored by the fates). |
| Immersive UI | Realistic coin flip animations, layered snowfall, and dark fantasy aesthetics. |
| AI Prophecies | The outcome is interpreted by a "medieval magician", giving you a highly contextual, poetic reading of your fate. |
| Role | Model | Provider | Why |
|---|---|---|---|
| The Magician | gemini-3.1-flash-lite |
Google AI | Incredibly fast, reliable, and capable of generating rich, poetic prose with very low latency. |
- Frontend: Vanilla JS, HTML, CSS
- Backend: Python 3 (Standard Library only)
- Deployment: Vercel (Static hosting + Python Serverless Functions)
- AI API: Google Gemini
SpellFlip/
โโโ api/
โ โโโ generate.py # Vercel Serverless Function endpoint
โ โโโ _utils.py # Python helper logic and API wrapper
โ โโโ prompt.txt # The AI prompt template
โโโ public/
โ โโโ index.html # Game UI
โ โโโ script.js # Frontend game logic
โ โโโ style.css # Animations and styling
โ โโโ assets/ # Media files (coin, dragon, castle)
โโโ .env # Secret API keys (gitignored)
โโโ requirements.txt # Empty (Signals Python runtime to Vercel)
โโโ server.py # Script for local development hosting
โโโ vercel.json # Vercel routing rules
- Python 3.10+
- A free Google Gemini API key โ Google AI Studio
git clone https://github.com/puja-ui/SpellFlip.git
cd SpellFlipCreate a .env file in the root directory and add your Gemini key:
GEMINI_API_KEY=your_gemini_key_hereStart the built-in Python web server:
python3 server.pyOpen your browser and navigate to http://localhost:8000.
The personality and rules of the Magician are defined in api/prompt.txt. You can easily tweak this file to change the tone of the output (e.g., from a medieval seer to a futuristic AI, or a sarcastic goblin) without touching any code.
This project is pre-configured to be deployed on Vercel with zero configuration.
- Push the code to a GitHub repository.
- Import the repository into Vercel.
- Add
GEMINI_API_KEYto the Environment Variables in the Vercel dashboard. - Deploy!
Vercel will automatically serve the /public folder as static files and convert api/generate.py into a Serverless Function.
Why no external dependencies?
The Python backend intentionally uses urllib.request instead of requests or the official Google SDK. This keeps the project extremely lightweight, ensures instant cold-starts on Vercel, and requires zero pip install steps for new developers.
Why is the backend separated? The game logic runs entirely in the browser (JS) for zero-latency coin flipping. The backend is only called at the very end. This protects the secret API key while ensuring the game feels incredibly responsive.
- Sound Effects: Add medieval atmospheric music and coin-clinking sound effects.
- Save History: Use
localStorageto keep a grimoire of past prophecies. - Multiple Coins: Introduce different coin types (gold, silver, cursed) that subtly alter the Magician's prompt.