Formal Mathematical & Algorithmic Specification

Web-native, deep-linkable edition of Phalanx Duel Classic v3.0 (Engine v1.6.0). Every formula below is bound to @phalanxduel/engine, verified across 2,355,388 finite combat transitions, and proved unbounded over all integers ℤ by Z3 SMT-LIB2.

1. State Space, Geometry & Deck Axioms

A Phalanx Duel match is a finite-state, zero-RNG-after-deal deterministic transition system ℳ = (𝒮, 𝒜, δ, s₀, ℰ) on a rectangular r × c battlefield (Classic: r = 2, c = 4, LP₀ = 20).

Axiom 1 — Global Geometry & Scarcity Invariants (PD-RULE-005..008)
r · c ≤ 48 ∧ h_max ≤ c ∧ D₀ = r·c + c ∧ 52 − D₀ ≥ 4

In Classic v3.0 (r = 2, c = 4): 8 battlefield slots, max hand 4, opening draw D₀ = 12, leaving a 40-card reserve (40 ≥ 4). Deck values: A = 1, 2–10 = face value, J = Q = K = 11.

2. Deterministic 8-Phase Turn Lifecycle

The authoritative transition function δ : 𝒮 × 𝒜 → 𝒮 × ℰ* advances through eight phases enforced by engine/src/state-machine.ts and verified deadlock-free:

StartTurn → DeploymentPhase → (AttackPhase → AttackResolution → CleanupPhase → ReinforcementPhase → DrawPhase → EndTurn)* → gameOver
View Single-Source Turn Lifecycle & Cascade Diagram (SVG) D2 · Build-Time SVG
Deterministic 8-phase turn lifecycle and v3.0 column cascade diagram

Open full-resolution SVG in new tab · Open live TypeScript STATE_MACHINE Graphviz SVG

3. Combat Cascade & Suit Boundary Algebra

For ordinary contact with incoming damage d ≥ 0 and target card HP h ≥ 1:

absorbed = min(d, h) | hp′ = h − absorbed | carryover = d − absorbed | destroyed ⇔ (hp′ = 0)
Card-to-Card Boundary — ℬ_card^(v3)

Fired between opposing Front and Back cards. Diamond shield (♦) resolves before Club weapon (♣):

ℬ_card^(v3)(c₁) = 2 · max(0, c₁ − δ_♦) if I_♣ = 1 and c₁ > δ_♦ = max(0, c₁ − δ_♦) otherwise
Player LP Boundary — ℬ_player^(v3)

Fired into Opponent LP. Last-destroyed Heart ward (♥) resolves before Spade finisher (♠):

ℬ_player^(v3)(c₂) = 2 · max(0, c₂ − δ_♥) if I_♠ = 1 and c₂ > δ_♥ = max(0, c₂ − δ_♥) otherwise

4. Theorem 1 — Non-Commutativity of Shield & Weapon Operators

Let S_δ(x) = max(0, x − δ) be a boundary shield and W(x) = 2x be a weapon multiplier. For all integers x ≥ 0, δ ≥ 0:

(S_δ ∘ W)(x) − (W ∘ S_δ)(x) = max(0, 2x − δ) − 2·max(0, x − δ) = min(δ, max(0, 2x − δ)) ≥ 0

Strict inequality (W ∘ S_δ)(x) < (S_δ ∘ W)(x) holds if and only if 0 < δ < 2x.

Design consequence: Classic v3.0 applies Shield-before-Weapon (W ∘ S_δ), strictly capping burst variance vs. legacy v1.0 (S_δ ∘ W) and guaranteeing that any shield with δ ≥ x zeroes out the weapon doubling trigger completely.

5. Theorem 2 — Guaranteed Finite Termination (Liveness)

At every v3.0 AttackPhase turn boundary, engine/src/liveness.ts evaluates a collision-free canonical position witness σ(s) and a four-coordinate progress vector:

Φ(s) = ( ∑ LP_p, ∑ hp_(p,k), ∑ |Draw_p|, −∑ |Discard_p| )
Precedence Lattice (Guaranteed Termination ≤ 200 Turns)
count(σ(s)) ≥ 3 ⇒ repetitionDraw ≻ noProgressTurns ≥ 50 ⇒ noProgressDraw ≻ t ≥ 200 ⇒ turnLimitDraw

No infinite loop is reachable in v3.0. Every match terminates in at most 200 completed turns.

6. Authority Ledger, Fog-of-War & Cryptographic Replay

Every match is an immutable fold from its initial seed over the server-authoritative action log, with per-turn canonical SHA-256 hashes:

s_n = δ*(s₀, ⟨a₁, …, a_n⟩) | h_t = SHA-256(canonical(s_t))

Each player receives only the redacted projection π_p(s_t) stripping opponent hidden hands and drawpile order; spectators receive delayed frames via replayGameAtOrBeforeTurn.

View Authority Ledger & Fog-of-War Diagram (SVG) D2 · Build-Time SVG
Server-authoritative action ledger, Fog-of-War projection functor, MCP surface, and replay verification chain

7. Unbounded Mathematical Proofs (Z3 SMT-LIB2 · 8/8 Q.E.D.)

Each theorem below is proved over all unbounded mathematical integers ℤ in docs/formal/phalanx_duel_proofs.smt2 by asserting its negation and verifying Z3 returns unsat (re-verify locally with pnpm verify:proofs):

Proof 1 — Shield-Zeroes-Weapon Theorem Z3 · Q.E.D. (unsat)
∀ c ≥ 0, δ ≥ 0 : (δ ≥ c) ⇒ ℬ^(v3)(c, δ) = 0

Proves that whenever a Diamond or Heart shield meets or exceeds incoming carryover, the Club or Spade ×2 multiplier can never fire.

Proof 2 — v3.0 Burst-Dominance Theorem Z3 · Q.E.D. (unsat)
∀ c ≥ 0, δ ≥ 0 : ℬ^(v3)(c, δ) ≤ ℬ^(v1)(c, δ)

Proves Shield-before-Weapon never produces higher burst damage than legacy Weapon-before-Shield.

Proof 3 — Exact Non-Commutativity Closed-Form Identity Z3 · Q.E.D. (unsat)
∀ c ≥ 0, δ ≥ 0 : ℬ^(v1)(c, δ) − ℬ^(v3)(c, δ) = min(δ, max(0, 2c − δ))

Proves the exact integer delta between v1.0 and v3.0 across all non-negative integers.

Proof 4 — Strict Non-Commutativity Biconditional (iff) Z3 · Q.E.D. (unsat)
∀ c ≥ 0, δ ≥ 0 : (ℬ^(v3)(c, δ) < ℬ^(v1)(c, δ)) ⇔ (0 < δ < 2c)

Establishes the exact necessary and sufficient boundary where operator order changes combat output.

Proof 5 — Damage Conservation & Surviving-Card Stop Z3 · Q.E.D. (unsat)
∀ d ≥ 0, h ≥ 1 : (abs + carry = d) ∧ (hp′ + abs = h) ∧ (hp′ > 0 ⇒ carry = 0)

Proves zero damage is created or lost during standard contact, and any surviving card halts carryover.

Proof 6 — Ace Invulnerability Conservation Z3 · Q.E.D. (unsat)
∀ d ≥ 1 : (abs_ace = 1) ∧ (abs_ace + carry_ace = d) ∧ (carry_ace ≥ 0)

Proves non-Ace strikes against an Ace conserve total damage while keeping the Ace at 1 HP.

Proof 7 — Liveness Bounded Termination (≤ 200 Turns) Z3 · Q.E.D. (unsat)
∀ t ≥ 0, n ≥ 0, r ≥ 1 : (¬draw ⇒ t < 200 ∧ n < 50 ∧ r < 3) ∧ (t ≥ 200 ⇒ draw)

Proves no active v3.0 match can ever exceed the 200-turn hard termination bound.

Proof 8 — Fog-of-War Projection Non-Interference Z3 · Q.E.D. (unsat)
∀ h₀, h₁ₐ, h₁_b, d₁ₐ, d₁_b : π₀(h₀, h₁ₐ, d₁ₐ, …) = π₀(h₀, h₁_b, d₁_b, …)

Proves perturbing opponent hidden hands or drawpile order cannot alter Player 0's projected view.

8. Machine-Checked Worked Cascade Examples

Every example below is executed against the live TypeScript engine on every pnpm verify:formal run:

Attacker Defending Column Step-by-Step Resolution (v3.0) LP Loss
9♣ Front 4♦, Back 5♠ 9 hits 4♦ → destroyed, carryover 5. ♦ Shield: 5 − 4 = 1. ♣ Weapon: 1 × 2 = 2. Hits 5♠ → survives at 3 HP. 0 LP
10♠ Front 6♣, Back 2♥ 10 hits 6♣ → destroyed, carryover 4. Hits 2♥ → destroyed, carryover 2. ♥ Ward: 2 − 2 = 0. ♠ Weapon: 0 × 2 = 0. 0 LP
10♠ Front 5♣, Back 2♦ 10 hits 5♣ → destroyed, carryover 5. Hits 2♦ → destroyed, carryover 3. No Heart at LP boundary. ♠ Weapon: 3 × 2 = 6. 6 LP
8♥ Front A♦, Back 3♣ Non-Ace vs Ace: A♦ absorbs 1, survives (1 HP, no Diamond shield), carryover 7 destroys 3♣ → carryover 4 hits LP. 4 LP

9. Companion Print Editions & Source Artifacts

Prefer a printable sheet for tabletop play or offline archival review? Every artifact below compiles from the same canonical source:

Canonical sources: Z3 SMT-LIB2 Proofs · TLA+ Specification · Executable Engine Verifier