The Mathematics of Taikyoku Shogi
A formal model, exact counts from the engine, and a roadmap to a machine-checked formalization in Lean 4.
Abstract · 1 Model · 2 Counts · 3 State space · 4 Game tree · 5 Mobility · 6 Rank algebra · 7 Symmetry · 8 Complexity · 9 Lean roadmap · 10 AI and solving · 11 Open problems · References
Working draft. Every number marked V was computed by the game's own rules engine and is reproducible
with godot --headless --path . -s tests/math_stats.gd. P marks short proofs given here,
C results cited from the literature, O open or conjectural statements and
L statements planned for a machine-checked proof in Lean 4. The Lean development has only just begun. Nine files (and 31 generated chunk files) compile today (lean/Taikyoku/; see Section 9, M1 to M3 and M6): the piece data generated from the game's own data file together with kernel-checked facts about it, a Lean port of the move generator that reproduces the perft numbers of Section 2 (checked with native_decide, which trusts the compiler), the sums behind Propositions 4 and 4b for every board size, kernel-checked rider totals on the 36×36 board, and the symmetry facts of Section 7 (Symmetry.lean). Every other code
block below is a statement we intend to prove, not a compiled theorem.
Abstract
Taikyoku shogi is the largest known shogi variant: two armies of 402 pieces on a \(36\times 36\) board, drawn from 301 piece kinds with move rules that include unbounded riders, jumpers, two-step “area” moves, 90° turning “hook” moves and multi-capture “range” moves. We give a formal model of positions and legal moves, exact initial-position counts (\(97\) legal first moves; \(1{,}426{,}099\) positions after three plies), upper bounds on the size of the state space (\(<10^{3604}\)) and a measured branching factor (\(\approx 320.6\) in random play), closed forms for the mobility of riders on an \(N\times N\) board, and a rank algebra for range capture. We then state the theorems we plan to formalize in Lean 4, describe how we are working to make the AI stronger and to solve as much of the game as can be solved, and list open problems.
1. A formal model
1.1 Board, sides, pieces
1.2 Moves as set-valued functions
Write \(\mathrm{Own}_t(\pi)\) and \(\mathrm{Opp}_t(\pi)\) for the squares occupied by the side to move and by its opponent, and orient direction vectors by the mover (\(d\mapsto d\) for Black, \(d\mapsto -d\) for White). A move is a finite path \(p=q_0\to q_1\to\cdots\to q_m\) together with the set \(C\) of captured squares. A kind's move rule is a finite union of the following components.
The set \(\mathrm{Mv}(s)\) of legal moves of a position is the union over the mover's pieces of their components' moves. There is no check rule: a move is legal exactly when it is generated. A player wins by capturing every enemy royal piece. Promotion is a rule parameter: on first capture (default), on entering the far 11 ranks, or never; a promoted piece promotes only once unless chaining is switched on.
In the implementation a move is the triple \((\text{from},\text{to},\text{via})\). Distinct values of \(\text{via}\) are counted as distinct moves, which is why counts below exceed the number of (from,to) pairs.
2. Exact counts from the initial position
| \(n\) | \(\mathrm{perft}(s_0,n)\) | ratio to previous | distinct positions |
|---|---|---|---|
| 1 | 97 | - | 97 |
| 2 | 9,393 | 96.8 | 9,393 |
| 3 | 1,426,099 | 151.8 | 559,384 |
| 4 | 216,564,265 | 151.9 | 33,820,557 |
| 5 | 40,927,447,886 | 189.0 | – |
| 6 | 7,752,459,989,182 | 189.4 | – |
V Depths 4 to 6 come from a faster, independent program. The GDScript engine needs about 20 s for the \(1.4\times10^6\) leaves of depth 3, so we wrote a C port of the Python reference (tools/perft_c/); it reads the same piece data file the game reads. It reproduces \(97\), \(9393\), \(1426099\) and the Python reference's \(216564265\), and its 97 per-first-move counts at depth 4 are identical to the Python reference's. Depth 4 takes 8 s on one core (the Python reference needs about 40 CPU-minutes). \(\mathrm{perft}(s_0,5)=40{,}927{,}447{,}886\) took 311 s on 8 cores and, as far as we know, has not been published for this game. \(\mathrm{perft}(s_0,6)=7{,}752{,}459{,}989{,}182\) (about \(7.8\times10^{12}\); also not published for this game, as far as we know) was computed on 8 rented CPU pods (128 vCPUs, about 182 vCPU-hours, 1.6 hours) as 9,393 independent units whose results were saved one by one. The ratios (97, 97, 152, 152, 189, 189) are the mean numbers of legal moves at plies 1 to 6: they are equal in pairs at first because the two armies are mirror images, and the jump at ply 5 is the position opening up. Depths 5 and 6 have one implementation behind them (the same code that matches the Python reference at depth 4), so they are checked only by that code; the units of depth 6 are in a ledger that any machine can extend and cross-check (tools/perft_c/run.py, results/perft6.jsonl).
V Distinct positions. Perft counts move sequences; the last column counts the positions they reach, each once: \(97,\ 9393,\ 559{,}384\) and \(33{,}820{,}557\) after 1 to 4 plies. After two plies no two sequences meet. By ply 3 they do (Black's two moves can be played in either order), and at ply 4 each reachable position is reached by \(6.4\) sequences on average, because independent moves commute and a piece can often reach the same square by several routes. So the game graph is far smaller than the game tree even this early, which is why a transposition table pays. Depth 3 was recounted by the Python reference with different code and agrees; depth 4 used 128-bit position hashes in 16 partitions that account for all \(216{,}564{,}265\) sequences (./perft 4 distinct in tools/perft_c/).
V The opening has \(97\) legal moves for Black. Note \(\mathrm{perft}(s_0,2)=9393\) is \(16\) below \(97^2=9409\): White's number of replies depends slightly on which move Black played (the mean is \(9393/97\approx96.8\)).
L In Lean (lean/Taikyoku/Rules.lean, Perft.lean): perft_one : perft initialBoard false 1 = 97, perft_two \(=9393\) and perft_three \(=1426099\), proved by native_decide, so they trust Lean's compiler as well as its kernel. A pure kernel proof ran out of memory (about 14 GB) on this board representation, so a kernel-checked perft needs a leaner one. The Lean port, the GDScript engine and the independent Python reference tools/perft_ref.py agree on all three numbers; agreement shows the three implementations match each other, not that any of them is a faithful reading of the written rules.
3. Size of the state space
Both numbers dwarf the figures usually quoted for other games (as tabulated by Allis, 1994): chess \(\approx10^{46}\), shogi \(\approx10^{71}\), Go \(\approx10^{172}\) positions C. A far better bound should exist because reachable positions have at most 804 pieces and captures are irreversible, but any such bound remains astronomically beyond exhaustive enumeration.
3.1 Endgame tablebases are within reach
For a King and one other piece against a bare King the number of placements is \(1296\cdot1295\cdot1294\cdot 2=4,343,492,160\) P (ordered placements of three distinct pieces, times side to move). That is about \(4.3\times10^{9}\) states: small enough that exhaustive retrograde analysis of such endgames is a realistic computation, and would give exact distance-to-win tables for every piece kind. O We have not computed them.
4. Branching factor and game-tree size
We measured the number of legal moves along random playouts (3,600 positions over 30 playouts of up to 120 plies, fixed seed) V:
| plies | 0–19 | 20–39 | 40–59 | 60–79 | 80–99 | 100–119 |
|---|---|---|---|---|---|---|
| mean legal moves | 203.0 | 267.8 | 307.4 | 349.8 | 382.9 | 412.9 |
The overall mean is \(\bar b\approx 320.6\) with a maximum of 608. The growth with the ply number reflects the board opening up as pieces are captured and the riders gain open lines. (Random play is not representative of strong play, so \(\bar b\) is a rough scale, not an estimate for good players.)
Following Allis, a game-tree complexity estimate is \(\bar b^{\,d}\) for an average game length of \(d\) plies. The length of a real game is unknown, so we tabulate:
| \(d\) (plies) | \(\log_{10}\bar b^{\,d}\) |
|---|---|
| 100 | 251 |
| 200 | 501 |
| 400 | 1002 |
| 800 | 2005 |
For comparison Allis gives \(\approx10^{123}\) (chess), \(10^{226}\) (shogi) and \(10^{360}\) (Go) C. Even a short game of 200 plies exceeds all three.
V Length of a random game. We played 12 games of uniformly random moves (fixed seed), stopping a game at 10,000 plies. 10 of them ended with a side losing all its royals (4 wins for Black in the 10 decided games), after between 1,986 and 8,792 plies (median 7,230, mean 6,699); the others were still undecided at the cap, so these are lower bounds on the true length distribution. With \(\bar b\approx 320.6\) this puts random play at roughly \(10^{16787}\) nodes for a game of average length, but random play is not strong play and \(10\) games are a small sample, so we use it only as a reference scale (tests/math_lengths.gd). The length of a game between strong players remains open (Section 11). O
5. Mobility and piece value
V Over the 301 kinds: minimum \(\mu=1\), median \(43\), mean \(70.0\), maximum \(2,520\). The maximum is attained by the Hook mover, whose turning paths multiply the count: \(\mu\) counts moves \((\text{from},\text{to},\text{via})\), so one destination reached by several turning squares is counted several times. (The number of distinct destination squares is smaller; see Section 9, M6.)
V The engine agrees. Summed over all \(1296\) squares of the empty board, the plain rook (Flying chariot) has 90,720 moves \(=70\cdot1296\), the bishop (Angle mover) 59,640 \(=46.0185\ldots\cdot1296\), the queen (Free king) 150,360 (their sum) and the forward-only rider (Incense chariot) 22,680 \(=17.5\cdot1296\); see tests/math_squares.gd.
The mean mobility of a queen (rook + bishop) is therefore \(\approx 70+46.02=116.02\) on this board. The ratio of mean mobilities rook : bishop is \(70/46.02\approx1.52\), close to the classical chess convention \(5:3.25\approx1.54\); this is suggestive but not a derivation of piece values, because blockers are ignored. Proposition 4c below adds blockers.
This is a model, not a measurement: real positions have correlated blockers, friendly pieces block without being capturable, and the density changes during the game.
6. Range capture and the rank algebra
L Checked in Lean for the actual data (DataFacts.lean: rangecap_below_royal, royal_rank, rangecap_card): exactly six kinds range-capture, with capture ranks \(1,1,1,1,2,3\), all below the rank \(4\) of the King and Crown Prince. Note that the rules engine gives royals the finite rank \(4\), not \(\infty\) as in Definition 2; the immunity of royals holds because every capture rank is at most \(3\), which is a fact about the data and not a consequence of \(\rho=\infty\).
Planned Lean form.
-- sketch, not yet compiled
inductive Side | black | white
structure Pos where
board : Fin 36 → Fin 36 → Option (Kind × Side)
turn : Side
def rangeCapturePassed (π : Pos) (p : Fin 36 × Fin 36) (d : Int × Int) (r : Nat) : List (Fin 36 × Fin 36) := ...
theorem rangeCapture_rank_lt {π p d r c} (hc : c ∈ captured (rangeCapture π p d r)) :
rank (kindAt π c) < r := by ...
theorem royal_untouchable {π p d r c} (hc : c ∈ captured (rangeCapture π p d r)) :
¬ isRoyal (kindAt π c) := by ...
theorem rangeCapture_card_le {π p d r} : (captured (rangeCapture π p d r)).length ≤ 35 := by ...
7. Symmetries and invariants
initial_rotation_invariant, by native_decide), and so is its consequence that both sides have 97 moves at the start (initial_moves_by_side); the general bijection is still to be proved.The setup is not left–right symmetric (only 2 of its 12 ranks read the same in both directions) V L (setup_palindromic_ranks), so there is no reflection symmetry of the initial position to exploit.
promotion_chains_end_within_five, promotion_chain_lengths, promoted_kinds_may_promote_again, all kernel-checked); the others are still to prove. (i) the number of pieces of each side never increases except by the moving side's captures; (ii) promotion always ends: following \(\mathrm{pr}\) from any kind reaches a kind with \(\mathrm{pr}=\bot\) after at most five steps (a piece can promote more than once: 75 of the 301 kinds promote into a kind that promotes again; the longest chain is Pawn, Gold general, Flying chariot, Dragon king, Flying eagle, Great eagle; 106, 120, 55, 13, 6 and 1 kinds have chains of 0 to 5 steps); (iii) under the default rule a piece promotes only when its move enters or ends in the promotion zone; (iv) the game is over iff a side has no royal piece; (v) \(\mathrm{Mv}(s)=\varnothing\) is possible (a side may have no legal move) and its outcome must be specified.dead_squares, in 31 independent files built in parallel; native_decide, so it trusts the compiler).8. Complexity-theoretic context
For \(n\times n\) generalizations, chess is EXPTIME-complete (Fraenkel & Lichtenstein, 1981), shogi is EXPTIME-complete (Adachi, Kamekawa & Iwata, 1987) and Go under Japanese rules is EXPTIME-complete (Robson, 1983) C. Taikyoku shogi is a single finite instance, so asymptotic classes do not apply to it directly. We conjecture that the family of games obtained by letting \(N\) grow with the setup scaled accordingly (and a fixed repertoire of the component moves above) is also EXPTIME-complete: the natural route is a reduction from a generalized shogi position, since every shogi piece is a Taikyoku component. O We have no proof.
Infinite play. The rules as implemented contain no repetition or move-limit rule, so the game graph has cycles and the value of a position is defined by the usual retrograde fixpoint
(win/loss in a finite number of plies, otherwise draw by infinite play). Formalizing this in Lean needs an inductive predicate WinIn : Nat → Pos → Side → Prop and a coinductive notion of avoiding loss. L
9. Roadmap: a machine-checked formalization in Lean 4
We intend to develop the model above in Lean 4 (de Moura & Ullrich, 2021) so that every V and P statement on this page becomes a theorem checked by the kernel, and every counting claim is tied to the same data the game executes. Planned milestones:
- M1 – Data. Import the 301 kinds (Betza-style move descriptions) as a Lean structure; prove the generated component tables equal the ones the engine uses. Done:
tools/build_lean_data.pygenerateslean/Taikyoku/Data.leanfromdata/pieces.json(the file the engine reads), so the Lean data cannot drift from the game, andDataFacts.leanhas the Lean kernel check \(|K|=301\), the 92 promotion-only kinds, the \(12\times36\) setup with 402 pieces, the 209 kinds in it with multiplicities \(1,1,155,52\) of \(36,4,2,1\) (Definitions 2 and 3), and that the data's rook, bishop, queen and lance directions are the ones used by the rider model of M6. The Betza strings are carried as text; the component tables are the ones the engine uses. - M2 – Positions and moves. Define
Pos, the component move functions (slide, leap, jump-slide, hook, area, range capture) andmoves : Pos → Finset Move; prove soundness (every move is on the board, never captures an own piece except in range capture, never captures a royal by range capture). Mostly done:Rules.leandefines boards, moves and all six components (slide, jump-slide, leap, lion, hook, range capture), the zone promotion rule andapply, as a port ofgame.gd. Still to do: the soundness theorems and theFinset-level statements. - M3 – Perft.
perft init 1 = 97,perft init 2 = 9393bydecide;perft init 3 = 1426099by a verified efficient implementation (refinement proof from the naivemoves). A kernel-checked proof of \(n=3\) withoutnative_decideis a serious computation (about \(1.4\times10^{6}\) leaves) and may need a bespoke reflection proof. Done for \(n\le3\) withnative_decide(Perft.lean); the kernel-only proof and \(n=4\) (\(216{,}564{,}265\) in the Python reference and the C port) are still to do. - M4 – Invariants. Items (i)–(v) of Section 7.
- M5 – Rank algebra. Lemmas 1 and 2, then the statement that range capture is the unique move component that can capture more than 2 pieces.
- M6 – Closed forms. Mean mobility of riders (Propositions 3 and 4), generalized to arbitrary direction sets \(D\subseteq\{-1,0,1\}^2\), with
decidechecks for \(N=8\) and \(N=36\). Started:lean/Taikyoku/Mobility.leanproves, for every \(N\), the identities \(2\sum_{y<N}y+N=N^2\) and \(6\sum_{a,b\le N}\min(a,b)=N(N+1)(2N+1)\) (the sums in the proofs of Propositions 4 and 4b);lean/Taikyoku/Riders.leandefines rays on an empty board and has the Lean kernel check the engine totals \(90{,}720\), \(59{,}640\), \(150{,}360\) and \(22{,}680\) of Section 5, with no axioms beyond Lean's core. Both are core Lean 4 (no Mathlib). Still to do: link the general-\(N\) sums to the ray model, and arbitrary direction sets. - M7 – Symmetry. Proposition 5 and its consequences for outcome classes.
- M8 – Game theory. Outcome predicate by retrograde induction, correctness of a small endgame solver (King + one piece vs King), and the claim that for each kind the tablebase value is computable within the \(4.3\times10^{9}\) bound.
Beyond formulas we expect Lean to expose subtle points in the sources: where the surviving rule descriptions disagree (promotion, hook moves, area-move edge cases) the formal definitions will force a choice, and we will publish each choice as a labelled rule parameter.
10. Making the AI smarter, and solving the game
What the AI is today. The computer opponent is an iterative-deepening alpha-beta search with killer and history move ordering, quiescence search at the hardest level and a time budget (1.5 s at Normal, 3 s at Hard). Its evaluation is material weighted by the mobility value of Section 5, \(v(k)=10+2\mu(k,c)\), plus positional terms (advancement and pressure on the enemy royals). It is far from strong: with a branching factor of \(\approx 320.6\) (Section 4) a search of fixed time reaches only a few plies.
Our goals. We are working to make the AI substantially smarter and, as far as mathematics allows, to solve the game. Following Allis, a game is ultra-weakly solved when the value of the initial position is known, weakly solved when a strategy achieving that value from the initial position is known, and strongly solved when the value of every position is known. Strong solution is impossible here: Section 3 bounds the state space only by \(10^{3604}\), and even the placements of the two initial armies number \(\approx10^{2181}\). Weak or ultra-weak solution of the full game is also far beyond brute force at today's computing power, so our program is staged:
- Search engineering. Transposition tables with Zobrist hashing, better move ordering, null-move and late-move reductions, and a faster move generator (the current generator is written for clarity, not speed: it takes about 20 s to enumerate the \(1.4\times10^6\) leaves of \(\mathrm{perft}(s_0,3)\)). Every optimization will be checked against the reference perft numbers of Section 2.
- Learned evaluation. Replace hand-set mobility values by a value function learned from self-play, with the mobility model as the prior, and Monte-Carlo tree search or a policy network to focus the search on the few hundred plausible moves out of \(\approx 320.6\).
- Opening and middlegame knowledge. Opening books mined from self-play, and measured strength ratings of each version against the previous one (we already have an AI-versus-AI match harness for this).
- Endgame tablebases. Exact retrograde solution of small-material endgames, starting from King + one piece against King (\(4.3\times10^{9}\) states, Section 3.1), then positions with two or three pieces per side. These are strongly solved sub-games, and they give the search exact values at the leaves.
- Solved fragments of the rules. Complete solutions of reduced boards (for example \(N=8,\dots,12\) with scaled-down setups) to learn how first-player advantage and drawishness depend on size, which is evidence about the full game.
- Verified solvers. Where a computation claims a game-theoretic value (a tablebase, a reduced-board solution) we will certify it with a proof object checked in Lean (Section 9, M8), so a published result is not just “the program said so”.
We expect a full solution of Taikyoku shogi to remain out of reach for a long time, if it is possible at all; the point of the program is to make the AI measurably stronger, to establish exact results on every fragment that can be solved, and to say precisely where the boundary lies. O
11. Open problems
- Is there a first-player advantage? (Even for chess and shogi this is unknown; here it is far from decidable by search.) O
- What is the empirical distribution of game length under strong play? It fixes the real size of the game tree (Section 4).
- Exact reachable-state counts for small material (Section 3.1), and the longest forced win in King + piece vs King for each kind.
- A tight combinatorial bound on the number of reachable positions in terms of the multiplicities \(m_k\).
- Complexity of the scaled family (Section 8).
- Do range-capturing generals admit an elegant potential function proving that long sequences of captures must end (so that no cycle of pure captures exists)? Trivial for captures (the piece count decreases), but the stronger question about bounded numbers of non-capturing moves between captures is open.
References
- H. Adachi, H. Kamekawa and S. Iwata. Shogi on \(n\times n\) board is complete in exponential time. Trans. IEICE J70-D(10):1843–1852, 1987 (in Japanese).
- L. V. Allis. Searching for Solutions in Games and Artificial Intelligence. PhD thesis, University of Limburg, 1994.
- R. Betza. Chess with different armies and the “funny notation” for piece moves. Chess Variant Pages, 1996.
- L. de Moura and S. Ullrich. The Lean 4 theorem prover and programming language. CADE-28, 2021.
- A. S. Fraenkel and D. Lichtenstein. Computing a perfect strategy for \(n\times n\) chess requires time exponential in \(n\). J. Combin. Theory Ser. A 31(2):199–214, 1981.
- G. Hodges. Ten Shogi Variants. 2002.
- J. M. Robson. The complexity of Go. Proc. IFIP Congress, 1983.
- C. E. Shannon. Programming a computer for playing chess. Philosophical Magazine 41(314):256–275, 1950.
- J. Tromp. Counting legal Go positions (with G. Farnebäck), 2016, and his estimates of the number of chess positions.
- “Taikyoku shogi”, English Wikipedia (CC BY-SA 4.0): the source of the piece list and setup used here.
The engine, the data and the script that produced every V number are in the project repository; the numbers on this page are regenerated by python3 tools/build_math.py from data/math_stats.json.
Found an error or want to help with the Lean development? hello@taikyokushogi.com.