Building a Fun Bazi & Ziwei Doushu Fortune-Telling Quiz
Inspiration
I've long been drawn to traditional Chinese metaphysics — the I Ching, Ziwei Doushu (紫微斗数), and Bazi (八字, "Four Pillars of Destiny"). After going through a few personal Bazi and Ziwei Doushu consultations myself, tying my birth chart to real reflections about career and life direction, I kept thinking the same thing: this knowledge is fascinating, but it's locked behind specialists and dense reference books. Most people never get to interact with it directly.
I wanted to turn that into something playful and approachable — a lightweight web quiz where anyone could enter their birth information and instantly get a fun, personalized "fortune" read, without needing to understand the underlying system at all.
What I Learned
Turning a centuries-old divination system into working code taught me more than I expected:
- The calendar problem is the real problem. Bazi and Ziwei Doushu are built on the traditional Chinese lunisolar calendar, not the Gregorian one. Before any "fortune" logic can run, a birth date has to be correctly converted — accounting for leap months, which don't fall on a fixed schedule.
- The Sexagenary cycle is elegant math in disguise. The Heavenly Stems (天干, 10 of them) and Earthly Branches (地支, 12 of them) combine into a repeating 60-term cycle (甲子, 干支纪年). It's essentially two clocks of different lengths ticking together, and the combined state only repeats every $\text{lcm}(10, 12) = 60$ years.
- Small errors compound fast. Because every later step (Five Elements analysis, the twelve houses in a Ziwei chart, star placements) depends on the initial Stem/Branch calculation, a one-off error near the start silently corrupts everything downstream — this is what made debugging so tricky.
How I Built It
Given the scope (a single, self-contained fun quiz rather than a full professional tool), I kept the stack deliberately simple: a plain HTML/JS web page, no framework overhead, so it would load instantly and be easy to share as a link.
The build had three layers:
- Input & calendar conversion — a form collects birth date, time, and gender, then converts the Gregorian date to its lunisolar equivalent.
- Core calculation engine — this derives the Four Pillars (year, month, day, hour) as Stem-Branch pairs, then maps them into a simplified Five Elements (五行: Wood, Fire, Earth, Metal, Water) balance and a lightweight Ziwei Doushu-style chart.
- "Fun" presentation layer — rather than a dry technical printout, results are phrased as playful, shareable takeaways (a dominant element, a tongue-in-cheek personality read, a "luck" tagline), so it reads more like a quiz result than an astrology report.
Challenges
The biggest challenge, hands down, was getting the Bazi/Ziwei calculation logic right.
The Year Stem and Year Branch, for instance, follow a modular pattern relative to a fixed reference year:
$$ \text{Stem}{\text{index}} = (Y - 4) \bmod 10, \qquad \text{Branch}{\text{index}} = (Y - 4) \bmod 12 $$
where $Y$ is the Gregorian year. That part is straightforward. The hard part was the Hour Pillar, since the Hour Stem depends on the Day Stem, not the year:
$$ \text{HourStem}{\text{index}} = \left(2 \times \text{DayStem}{\text{index}} + \text{HourBranch}_{\text{index}}\right) \bmod 10 $$
Small mismatches here (off-by-one errors in indexing, or an hour boundary landing on the wrong two-hour "shichen" 时辰 block) would cascade into a completely wrong chart. I ended up building a small internal test suite — cross-checking calculated Four Pillars against known reference charts for specific dates — before trusting the engine's output.
The other underestimated challenge was striking the right tone: making sure the output felt fun and inviting rather than either (a) so vague it felt meaningless, or (b) so authoritative it read like a real fortune-telling claim. Balancing "playful quiz" against "respecting a real tradition" took more iteration than the code itself.
Built With
- bazi
- chinese-astrology
- css
- html
- javascript
- single-page-application
- vanilla-js
- web-app
- ziwei-doushu
Log in or sign up for Devpost to join the conversation.