# Rules engine and local practice opponent

The rules engine is the authority for every move, whether selected by a person, generated by search, imported from a record, or displayed in a lesson. It has no browser dependencies. Positions are plain data: variant, board, hands, side to move, and ply count. Each on-board piece records its owner, type, promotion, and facing.

## Rules, game history, and presentation

`engine.js` supplies topology-aware move and drop generation, attacks, king-safety filtering, position validation, and immutable application of canonical legal actions. `game.js` adds alternating turns and outcomes. A game record is replayed through these rules when loaded; its supplied move descriptions and results are not trusted as authoritative.

The genus-two graph is defined by inverse pairs of cardinal neighbor permutations. A diagonal exists only where its two cardinal orders agree. This keeps the cone-point convention in the same rule path for kings, bishops, and every other diagonal mover. The two charts are an atlas of one board, not separate games or discretionary portals.

`board-view.js` converts actual engine adjacencies into displayed joins. `presentation.js` renders the charts and pieces without generating moves. Black/White colors preserve ownership independently of the pointed end’s facing. The Klein display cut between ranks 9 and 10 has no game effect.

The position key used for repetition omits piece IDs and ply number. It keeps facing exactly when facing can change future legality. Both hands and side to move are included. Full game exports retain the move line so a reload does not reset repetition counts.

## Shogi tactical search

The shogi tactical engine uses iterative-deepening negamax with alpha–beta pruning. At each completed depth it retains a chosen legal action. An interrupted deeper iteration cannot overwrite that result; if no depth finishes, a generated legal move remains available.

The browser uses the shared [opponent integration](./opponents.md): nominal depth 3, 750 milliseconds and 2,000 search nodes, with a separate 15-second worker deadline. The cooperative search budget can overrun during a native rules operation. The worker preserves page responsiveness; cancellation, deadlines and stale replies cannot apply a move. The host revalidates and applies the selected action synchronously through its current native engine.

The provisional values, in units where a pawn is 100, are:

| Piece | Base | Added when promoted |
| --- | ---: | ---: |
| Pawn | 100 | 450 |
| Lance | 300 | 250 |
| Knight | 320 | 230 |
| Silver | 450 | 100 |
| Gold | 550 | — |
| Bishop | 800 | 200 |
| Rook | 1,000 | 200 |

These values are design inputs, not measured piece strengths on these surfaces. Pieces in hand count at base value. The king has no material value because game outcomes take precedence. Captures and promotions are searched first to make pruning more effective; this does not remove other legal moves.

A checked leaf searches all legal evasions until a quiet position or an outcome. There is no stand-pat material evaluation in check. An unusually long chain of checks aborts that iteration after 16 added plies. Quiet leaves recognize no-legal-action losses. Exact fourfold repetition and continuous-check penalties use the same conventions as Play. There is no capture-quiescence search, positional evaluation, opening book or transposition table within this native search. The shared opponent supplies a learned legal fallback before searching; a completed shogi search iteration replaces it. The browser inference safety limit is 4,096 recorded actions; exceeding it does not end the game.

The tests compare shallow search with exhaustive minimax, check a forced mate and check evasion, exercise repetition, and verify time/node interruption, immutable inputs, and stale-worker cancellation. They establish those behaviors, not playing strength.

## Bundled policies

Gameplay release 1.1 includes ten local policies trained to imitate a one-step teacher, one for each game. These policies provide legal fallbacks; chess and the Go families also use learned rankings to order search candidates. This is not a playing-strength evaluation. Go and CrystalGo search a selected candidate set, extended to include immediate captures and specified atari defenses, so good moves can still be omitted.

Rules and model provenance, file hashes, complete-history input contracts and deterministic verification fixtures are retained in `assets/agents/`. Run `npm run verify:agents` to check the unchanged package. App tests pin the native host modules to the original source or the two documented [input-validation patches](./opponents.md#maintenance), and check canonical game compatibility with the real bundled models and worker. Unexpected source changes fail verification; the original package metadata is preserved.
