Skip to content

Game-end result and status text are decided in two places in game.js #140

Closed

Description

@TMHSDigital

Summary

The decision "is the game over, and what is the result and the status text" is written twice in game.js: once after a move, and again for a position loaded from FEN. A third derivation is in saved-game.js. The copies have already started to differ.

Where

  • js/game.js:637-656 (playMove: checkmate and stalemate after a move)
  • js/game.js:664-671, js/game.js:1178-1181 (endIfAutomaticDraw, findDrawReason)
  • js/game.js:1536-1557 (endIfAlreadyDecided: the same outcomes for a FEN start)
  • js/saved-game.js:120 (the result derived again from last.isCheckmate)

Details

  • "Checkmate! X wins." is built at game.js:645 and again at :1541. "Stalemate! Game is a draw." appears at :651 and :1544.
  • Insufficient material and the fifty-move rule are tested in findDrawReason and again by hand at :1546-1554. That copy has a literal halfmoveClock >= 100 and a hand-written status instead of Draw by ${reason}.
  • ${color === WHITE ? 'White' : 'Black'} is repeated about 10 times in game.js, next to clock.js's own name().

Impact

A new draw rule, a wording change, or localisation has to be made in two or three places. If one is missed, a game loaded from FEN ends differently from the same position reached by play.

Suggested fix

Each step leaves the code working:

  1. Tests first: pin the result and status text for mate, stalemate, insufficient material and the fifty-move rule, both after a move and when starting from FEN. Include Black-to-move FEN starts, which are untested today: R5k1/5ppp/8/8/8/8/8/6K1 b - - 0 1 should give 1-0 and "Checkmate! White wins.", and a Black stalemate should give 1/2-1/2.
  2. Add a pure gameOutcome(position, player, halfmoveClock) to rules.js that returns { result, reason } or null, with unit tests and no callers yet.
  3. Switch endIfAlreadyDecided to it, with one statusForOutcome in game.js.
  4. Switch the end-of-move block in playMove and endIfAutomaticDraw to it.
  5. Have saved-game.js use the stored gameResult, or gameOutcome, instead of re-deriving the result.
  6. Add sideName(color) to utils.js and replace the repeated ternaries.

Acceptance criteria

  • One place decides game-end result and status text
  • Black-to-move FEN cases above are tested
  • Full suite passes with unchanged coverage thresholds

Getting started

  • Relevant files: js/game.js, js/rules.js, js/saved-game.js, tests/unit/draws-promotion.test.js, tests/unit/fen.test.js
  • How to run tests: npm ci && npm test

Filed by repo-audit/full-audit at 73cd279. Severity: medium. Effort: M. audit-key: tech-debt|js/game.js|endIfAlreadyDecided

Activity

  1. added
    tech-debt
    severity: medium
    on Nov 17, 2026
  2. TMHSDigital commented on Nov 17, 2026

    @TMHSDigital
    OwnerAuthor

    Fixed in fd6dc32: gameOutcome(position, player, draws) in rules.js now decides mate, stalemate and the draws; game.js turns it into the result and status in one statusForOutcome/endWith, used by playMove, endIfAutomaticDraw and endIfAlreadyDecided (findDrawReason removed). sideName() in utils.js replaces the White/Black ternaries in game.js, game-ui.js and clock.js. New tests pin FEN-vs-move results and wording (including Black-to-move mate and stalemate) and cover gameOutcome directly. Full suite (565), lint, format and coverage thresholds pass.

    One step left as is on purpose: saved-game.js still derives a result for old saves that predate stored results from the last move's isCheckmate flag. That path reads stored flags, not a position, and is pinned by the #141 tests; every save written now carries gameResult.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    severity: mediumtech-debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions