Compare commits

..

27 Commits

Author SHA1 Message Date
LQ63 db955c08a5 style: fix CRLF line endings introduced by rebase
Build & Test (NowChessSystems) TeamCity build failed
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 21:44:29 +02:00
LQ63 2c4d96e373 feat(backcore): Quarkus tests
Added quarkus test compatability with sonar through jacoco reporter
2026-04-14 21:28:16 +02:00
LQ63 acddd58ad3 feat: NCS-37 import FEN/PGN, export endpoints, NDJSON stream snapshot 2026-04-14 21:28:16 +02:00
LQ63 33dd63a9b6 feat: NCS-37 resign, draw actions, and export/stream endpoints 2026-04-14 21:28:16 +02:00
LQ63 6fc3b3c3df feat: NCS-37 move-making, legal moves, undo/redo endpoints 2026-04-14 21:28:16 +02:00
LQ63 41fa674bf7 fix: use PgnExporter for SAN pgn field, fix status priority in GameMapper 2026-04-14 21:28:16 +02:00
LQ63 21eee717f1 feat: NCS-37 game lifecycle endpoints (createGame, getGame)
Add domain model (GameResult, GameSession, GameId, GameStore, GameMapper),
DTOs, JacksonConfig, and GameResource REST endpoints with QuarkusTest coverage.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 21:28:16 +02:00
LQ63 d59d692381 fix: use version catalog keys for jackson-scala and junit-bom in backcore 2026-04-14 21:28:16 +02:00
LQ63 c830b143dc feat: NCS-37 add backcore Quarkus module skeleton
Introduces the backcore Quarkus REST module with build configuration,
application.yml, and a smoke test confirming Quarkus boots successfully.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 21:28:16 +02:00
TeamCity b2e62dc60c ci: bump version with Build-40 2026-04-14 19:23:01 +00:00
Janis b0399a4e48 feat: NCS-14 implemented insufficient moves rule (#30)
Build & Test (NowChessSystems) TeamCity build finished
Reviewed-on: #30
Co-authored-by: Janis <janis.e.20@gmx.de>
Co-committed-by: Janis <janis.e.20@gmx.de>
2026-04-14 21:17:56 +02:00
TeamCity ec2ab2f365 ci: bump version with Build-39 2026-04-12 19:03:52 +00:00
Janis fd4e67d4f7 feat: NCS-25 Add linters to keep quality up (#27)
Build & Test (NowChessSystems) TeamCity build finished
Reviewed-on: #27
Reviewed-by: Leon Hermann <lq@blackhole.local>
Co-authored-by: Janis <janis.e.20@gmx.de>
Co-committed-by: Janis <janis.e.20@gmx.de>
2026-04-12 20:58:39 +02:00
TeamCity 3cb3160731 ci: bump version with Build-38 2026-04-12 17:41:12 +00:00
Janis dbcafd2869 feat: NCS-29 JSON - Cherry Picked (#28)
Build & Test (NowChessSystems) TeamCity build finished
Reviewed-on: #28
Reviewed-by: Shahd Lala <shosho996@blackhole.local>
Co-authored-by: Janis <janis.e.20@gmx.de>
Co-committed-by: Janis <janis.e.20@gmx.de>
2026-04-12 19:36:02 +02:00
TeamCity 3ecb2c9d66 ci: bump version with Build-37 2026-04-12 15:20:01 +00:00
lq64 9ad11fb97a docs: NCS-36 Added an API spec for Now Chess (#25)
Build & Test (NowChessSystems) TeamCity build finished
## Summary
  - Adds `docs/api-spec.yaml` — a full OpenAPI 3.0.3 specification for the NowChess REST API
  - Endpoint paths follow the lichess `board/bot` split convention (`/api/board/game/...`)
    to leave room for a future bot API under `/api/bot/game/...`
  - Covers game lifecycle, move-making, draw handling, undo/redo,
    legal move introspection, and FEN/PGN import/export

  ## Endpoints
  - Game: POST /api/board/game, GET /{gameId}, GET /{gameId}/stream, POST /{gameId}/resign
  - Move: POST /{gameId}/move/{uci}, GET /{gameId}/moves, POST /{gameId}/undo, POST /{gameId}/redo
  - Draw: POST /{gameId}/draw/{action}
  - Import: POST /import/fen, POST /import/pgn
  - Export: GET /{gameId}/export/fen, GET /{gameId}/export/pgn

  ## Test plan
  - [ ] Open docs/api-spec.yaml in Swagger Editor (https://editor.swagger.io) — zero validation errors expected
  - [ ] Verify every endpoint maps to an existing GameEngine or RuleSet method

Co-authored-by: LQ63 <lkhermann@web.de>
Reviewed-on: #25
Reviewed-by: Janis <janis-e@gmx.de>
Co-authored-by: Leon Hermann <lq@blackhole.local>
Co-committed-by: Leon Hermann <lq@blackhole.local>
2026-04-12 17:15:11 +02:00
TeamCity e158b0a7f0 ci: bump version with Build-36 2026-04-12 14:45:56 +00:00
Janis f1c9df16b6 feat: add initial project structure and documentation files (#24)
Build & Test (NowChessSystems) TeamCity build finished
Reviewed-on: #24
2026-04-12 16:39:17 +02:00
TeamCity 9d11d25b99 ci: bump version with Build-35 2026-04-08 07:37:40 +00:00
lq64 7a045d31d7 feat: NCS-31 FastParse FEN (#22)
Build & Test (NowChessSystems) TeamCity build finished
Summary

  - Added fastparse_3:3.0.2 dependency to modules/io
  - Implemented FenParserFastParse as a second alternative FEN parser using FastParse, with the same public API as
  FenParser and FenParserCombinators
  - Parsers are built bottom-up using (using P[Any]) Scala 3 syntax with NoWhitespace.* to prevent implicit whitespace
  skipping; rank sum validation uses Pass/Fail inside .flatMap
  - Added FenParserFastParseTest mirroring FenParserCombinatorsTest to prove behavioural equivalence across all three
  implementations

  Test plan

  - All existing tests pass — FenParser, FenParserCombinators, and all other modules untouched
  - FenParserFastParseTest covers all cases: valid FEN, invalid color, invalid castling, invalid board shapes, en
  passant, rank overflow, round-trip via FenExporter
  - All parser logic branches genuinely covered — known scoverage gap documented in docs/unresolved.md (FastParse inline
   macro generates synthetic proxy methods that scoverage instruments but that never execute at runtime)

Co-authored-by: LQ63 <lkhermann@web.de>
Reviewed-on: #22
Reviewed-by: Janis <janis-e@gmx.de>
Co-authored-by: Leon Hermann <lq@blackhole.local>
Co-committed-by: Leon Hermann <lq@blackhole.local>
2026-04-08 09:32:57 +02:00
TeamCity b518c704fa ci: bump version with Build-34 2026-04-07 19:42:37 +00:00
Janis fe8e3c0539 fix: NCS-32 Queenside Castle doesn't care about pieces in the way (#23)
Build & Test (NowChessSystems) TeamCity build finished
Reviewed-on: #23
Co-authored-by: Janis <janis.e.20@gmx.de>
Co-committed-by: Janis <janis.e.20@gmx.de>
2026-04-07 20:32:48 +02:00
TeamCity 1b16adcc72 ci: bump version with Build-33 2026-04-07 18:02:38 +00:00
lq64 b4bc72f7e4 feat: NCS-30 FEN Parser using ParserCombinators (#21)
Build & Test (NowChessSystems) TeamCity build finished
Summary

  - Added scala-parser-combinators_3:2.4.0 dependency to modules/io
  - Implemented FenParserCombinators as an alternative FEN parser using RegexParsers, with the same public API as the
  existing FenParser
  - Parsers are built bottom-up: piece characters → rank tokens → rank → board, composed with explicit field separators
  into a full FEN parser
  - Added FenParserCombinatorsTest mirroring the existing FenParserTest to prove behavioural equivalence

  Test plan

  - All existing tests pass — FenParser and all other modules untouched
  - FenParserCombinatorsTest covers all cases: valid FEN, invalid color, invalid castling, invalid board shapes, en
  passant, round-trip via FenExporter
  - 100% line/branch/method coverage on FenParserCombinators

Co-authored-by: LQ63 <lkhermann@web.de>
Reviewed-on: #21
Reviewed-by: Janis <janis-e@gmx.de>
Co-authored-by: Leon Hermann <lq@blackhole.local>
Co-committed-by: Leon Hermann <lq@blackhole.local>
2026-04-07 12:28:43 +02:00
Janis 8959c3a849 refactor: NCS-24 update CLAUDE.md for improved structure and clarity (#20)
Build & Test (NowChessSystems) TeamCity build finished
Reviewed-on: #20
Reviewed-by: Leon Hermann <lq@blackhole.local>
Co-authored-by: Janis <janis-e@gmx.de>
Co-committed-by: Janis <janis-e@gmx.de>
2026-04-07 12:25:33 +02:00
TeamCity 47032378e2 ci: bump version with Build-31 2026-04-07 06:25:56 +00:00
131 changed files with 6430 additions and 1499 deletions
-9
View File
@@ -1,9 +0,0 @@
# Memory Index
## Feedback
- [feedback_keep_structure_updated.md](feedback_keep_structure_updated.md) — Update structure memory files whenever source files are added, removed, or changed
## Project Structure
- [project_structure_root.md](project_structure_root.md) — Top-level layout, modules list, VERSIONS map, navigation rules (skip `build/`, `.gradle/`, `.idea/`)
- [project_structure_api.md](project_structure_api.md) — `modules/api`: all files and types (Board, Piece, Square, GameState, Move, ApiResponse, PlayerInfo)
- [project_structure_core.md](project_structure_core.md) — `modules/core`: all files and types (GameContext, GameRules, MoveValidator, GameController, Parser, Renderer)
@@ -1,16 +0,0 @@
---
name: keep-structure-memory-updated
description: Always update the project structure memory files when adding, removing, or changing source files
type: feedback
---
After any change that adds, removes, renames, or significantly alters a source file, update the relevant structure memory file:
- New/renamed/deleted file in `modules/api` → update `project_structure_api.md`
- New/renamed/deleted file in `modules/core` → update `project_structure_core.md`
- New module, dependency version change, or new top-level directory → update `project_structure_root.md`
- New module added → create a new `project_structure_<module>.md` and add it to `MEMORY.md`
**Why:** Structure memories are the primary navigation aid. Stale entries cause wasted exploration.
**How to apply:** Treat the structure memory update as part of completing any implementation task — do it in the same session, not as a follow-up.
-51
View File
@@ -1,51 +0,0 @@
---
name: module-api-structure
description: File and type overview for the modules/api module (shared domain types)
type: project
---
# Module: `modules/api`
**Purpose:** Shared domain model — pure data types with no game logic. Depended on by `modules/core`.
**Gradle:** `id("scala")`, no `application` plugin. No Quarkus. Uses scoverage plugin.
**Package root:** `de.nowchess.api`
## Source files (`src/main/scala/de/nowchess/api/`)
### `board/`
| File | Contents |
|------|----------|
| `Board.scala` | `opaque type Board = Map[Square, Piece]` — extensions: `pieceAt`, `withMove`, `pieces`; `Board.initial` sets up start position |
| `Color.scala` | `enum Color { White, Black }``.opposite`, `.label` |
| `Piece.scala` | `case class Piece(color, pieceType)` — convenience vals `WhitePawn``BlackKing` |
| `PieceType.scala` | `enum PieceType { Pawn, Knight, Bishop, Rook, Queen, King }``.label` |
| `Square.scala` | `enum File { AH }`, `enum Rank { R1R8 }`, `case class Square(file, rank)``.toString` algebraic, `Square.fromAlgebraic(s)` |
### `game/`
| File | Contents |
|------|----------|
| `GameState.scala` | `case class CastlingRights(kingSide, queenSide)` + `.None`/`.Both`; `enum GameResult { WhiteWins, BlackWins, Draw }`; `enum GameStatus { NotStarted, InProgress, Finished(result) }`; `case class GameState(piecePlacement, activeColor, castlingWhite, castlingBlack, enPassantTarget, halfMoveClock, fullMoveNumber, status)` — FEN-compatible snapshot |
### `move/`
| File | Contents |
|------|----------|
| `Move.scala` | `enum PromotionPiece { Knight, Bishop, Rook, Queen }`; `enum MoveType { Normal, CastleKingside, CastleQueenside, EnPassant, Promotion(piece) }`; `case class Move(from, to, moveType = Normal)` |
### `player/`
| File | Contents |
|------|----------|
| `PlayerInfo.scala` | `opaque type PlayerId = String`; `case class PlayerInfo(id: PlayerId, displayName: String)` |
### `response/`
| File | Contents |
|------|----------|
| `ApiResponse.scala` | `sealed trait ApiResponse[+A]``Success[A](data)` / `Failure(errors)`; `case class ApiError(code, message, field?)`; `case class Pagination(page, pageSize, totalItems)` + `.totalPages`; `case class PagedResponse[A](items, pagination)` |
## Test files (`src/test/scala/de/nowchess/api/`)
Mirror of main structure — one `*Test.scala` per source file using `AnyFunSuite with Matchers`.
## Notes
- `GameState` is FEN-style but `Board` (in `core`) is a `Map[Square,Piece]` — the two are separate representations
- `CastlingRights` is defined here in `api`; the castling logic lives in `core`
-48
View File
@@ -1,48 +0,0 @@
---
name: module-core-structure
description: File and type overview for the modules/core module (TUI chess engine)
type: project
---
# Module: `modules/core`
**Purpose:** Standalone TUI chess application. All game logic, move validation, rendering. Depends on `modules/api`.
**Gradle:** `id("scala")` + `application` plugin. Main class: `de.nowchess.chess.Main`. Uses scoverage plugin.
**Package root:** `de.nowchess.chess`
## Source files (`src/main/scala/de/nowchess/chess/`)
### Root
| File | Contents |
|------|----------|
| `Main.scala` | Entry point — prints welcome, starts `GameController.gameLoop(GameContext.initial, Color.White)` |
### `controller/`
| File | Contents |
|------|----------|
| `GameController.scala` | `sealed trait MoveResult` ADT: `Quit`, `InvalidFormat`, `NoPiece`, `WrongColor`, `IllegalMove`, `Moved`, `MovedInCheck`, `Checkmate`, `Stalemate`; `object GameController``processMove(ctx, turn, raw): MoveResult` (pure), `gameLoop(ctx, turn)` (I/O loop), `applyRightsRevocation(...)` (castling rights bookkeeping) |
| `Parser.scala` | `object Parser``parseMove(input): Option[(Square, Square)]` parses coordinate notation e.g. `"e2e4"` |
### `logic/`
| File | Contents |
|------|----------|
| `GameContext.scala` | `enum CastleSide { Kingside, Queenside }`; `case class GameContext(board, whiteCastling, blackCastling)``.castlingFor(color)`, `.withUpdatedRights(color, rights)`; `GameContext.initial`; `extension (Board).withCastle(color, side)` moves king+rook atomically |
| `GameRules.scala` | `enum PositionStatus { Normal, InCheck, Mated, Drawn }`; `object GameRules``isInCheck(board, color)`, `legalMoves(ctx, color): Set[(Square,Square)]`, `gameStatus(ctx, color): PositionStatus` |
| `MoveValidator.scala` | `object MoveValidator``isLegal(board, from, to)`, `legalTargets(board, from): Set[Square]` (board-only, no castling), `legalTargets(ctx, from)` (context-aware, includes castling), `isCastle`, `castleSide`, `castlingTargets(ctx, color)` — full castling legality (empty squares, no check through transit) |
### `view/`
| File | Contents |
|------|----------|
| `Renderer.scala` | `object Renderer``render(board): String` outputs ANSI-colored board with file/rank labels |
| `PieceUnicode.scala` | `extension (Piece).unicode: String` maps each piece to its Unicode chess symbol |
## Test files (`src/test/scala/de/nowchess/chess/`)
Mirror of main structure — one `*Test.scala` per source file using `AnyFunSuite with Matchers`.
## Key design notes
- `MoveValidator` has two overloaded `legalTargets`: one takes `Board` (geometry only), one takes `GameContext` (adds castling)
- `GameRules.legalMoves` filters by check — it calls `MoveValidator.legalTargets(ctx, from)` then simulates each move
- Castling rights revocation is in `GameController.applyRightsRevocation`, triggered after every move
- No `@QuarkusTest` — this module is a plain Scala application, not a Quarkus service
-55
View File
@@ -1,55 +0,0 @@
---
name: project-root-structure
description: Top-level project structure, modules list, and navigation notes for NowChessSystems
type: project
---
# NowChessSystems — Root Structure
## Directory layout (skip `build/`, `.gradle/`, `.idea/`)
```
NowChessSystems/
├── build.gradle.kts # Root: sonarqube plugin, VERSIONS map
├── settings.gradle.kts # include(":modules:core", ":modules:api")
├── gradlew / gradlew.bat
├── CLAUDE.md # Project instructions for Claude Code
├── .claude/
│ ├── CLAUDE.MD # Working agreement (plan/verify/unresolved)
│ ├── settings.json
│ └── agents/ # architect, code-reviewer, gradle-builder, scala-implementer, test-writer
├── docs/
│ ├── Claude-Skills.md
│ ├── Security.md
│ └── unresolved.md
├── jacoco-reporter/ # Python scripts for coverage gap reporting
└── modules/
├── api/ # Shared domain types (no logic)
└── core/ # TUI chess engine + game logic
```
## Modules
| Module | Gradle path | Purpose |
|--------|-------------|---------|
| `api` | `:modules:api` | Shared domain model: Board, Piece, Move, GameState, ApiResponse |
| `core` | `:modules:core` | TUI chess app: game logic, move validation, rendering |
`core` depends on `api` via `implementation(project(":modules:api"))`.
## VERSIONS (root `build.gradle.kts`)
| Key | Value |
|-----|-------|
| `QUARKUS_SCALA3` | 1.0.0 |
| `SCALA3` | 3.5.1 |
| `SCALA_LIBRARY` | 2.13.18 |
| `SCALATEST` | 3.2.19 |
| `SCALATEST_JUNIT` | 0.1.11 |
| `SCOVERAGE` | 2.1.1 |
## Navigation rules
- **Always skip** `build/`, `.gradle/`, `.idea/` when exploring — they are generated artifacts
- Tests use `AnyFunSuite with Matchers` (ScalaTest), not JUnit `@Test`
- No Quarkus in current modules — Quarkus is planned for future services
- Agent workflow: architect → scala-implementer → test-writer → gradle-builder → code-reviewer
+203
View File
@@ -0,0 +1,203 @@
# NowChessSystems — AI Context Map
> **Stack:** raw-http | none | unknown | scala
> 0 routes | 0 models | 0 components | 35 lib files | 0 env vars | 0 middleware
> **Token savings:** this file is ~3.700 tokens. Without it, AI exploration would cost ~18.200 tokens. **Saves ~14.500 tokens per conversation.**
---
# Libraries
- `jacoco-reporter/scoverage_coverage_gaps.py`
- function parse_scoverage_xml: (xml_path) -> tuple[dict, list[ClassGap]]
- function format_agent: (project_stats, classes) -> str
- function format_json: (project_stats, classes) -> str
- function format_markdown: (project_stats, classes) -> str
- function format_module_gaps: (module_name, classes, stmt_pct) -> str
- function run_scan_modules: (modules_dir, package_filter, min_coverage) -> None
- _...4 more_
- `jacoco-reporter/test_gaps.py`
- function parse_suite_xml: (xml_path) -> SuiteResult
- function load_module: (module_dir, results_subdir) -> Optional[ModuleResult]
- function format_module: (mod) -> str
- function run: (modules_dir, results_subdir, module_filter) -> None
- function main: () -> None
- class TestCase
- _...2 more_
- `modules/api/src/main/scala/de/nowchess/api/board/Board.scala`
- class Board
- function apply
- function pieceAt
- function updated
- function removed
- function withMove
- _...2 more_
- `modules/api/src/main/scala/de/nowchess/api/board/CastlingRights.scala`
- function hasAnyRights
- function hasRights
- function revokeColor
- function revokeKingSide
- function revokeQueenSide
- class CastlingRights
- `modules/api/src/main/scala/de/nowchess/api/board/Color.scala` — function opposite, function label
- `modules/api/src/main/scala/de/nowchess/api/board/Piece.scala` — class Piece
- `modules/api/src/main/scala/de/nowchess/api/board/PieceType.scala` — function label
- `modules/api/src/main/scala/de/nowchess/api/board/Square.scala`
- class Square
- function fromAlgebraic
- function offset
- `modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala`
- function withBoard
- function withTurn
- function withCastlingRights
- function withEnPassantSquare
- function withHalfMoveClock
- function withMove
- _...2 more_
- `modules/api/src/main/scala/de/nowchess/api/player/PlayerInfo.scala` — class PlayerId, function apply
- `modules/api/src/main/scala/de/nowchess/api/response/ApiResponse.scala`
- class ApiResponse
- function error
- function totalPages
- `modules/core/src/main/scala/de/nowchess/chess/command/Command.scala`
- class Command
- function execute
- function undo
- function description
- class MoveResult
- `modules/core/src/main/scala/de/nowchess/chess/command/CommandInvoker.scala`
- class CommandInvoker
- function execute
- function undo
- function redo
- function history
- function getCurrentIndex
- _...3 more_
- `modules/core/src/main/scala/de/nowchess/chess/controller/Parser.scala` — class Parser, function parseMove
- `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`
- class GameEngine
- function isPendingPromotion
- function board
- function turn
- function context
- function canUndo
- _...10 more_
- `modules/core/src/main/scala/de/nowchess/chess/observer/Observer.scala`
- function context
- class Observer
- function onGameEvent
- class Observable
- function subscribe
- function unsubscribe
- _...1 more_
- `modules/io/src/main/scala/de/nowchess/io/GameContextExport.scala` — class GameContextExport, function exportGameContext
- `modules/io/src/main/scala/de/nowchess/io/GameContextImport.scala` — class GameContextImport, function importGameContext
- `modules/io/src/main/scala/de/nowchess/io/fen/FenExporter.scala`
- class FenExporter
- function boardToFen
- function gameContextToFen
- function exportGameContext
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParser.scala`
- class FenParser
- function parseFen
- function importGameContext
- function parseBoard
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParserCombinators.scala`
- class FenParserCombinators
- function parseFen
- function parseBoard
- function importGameContext
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParserFastParse.scala`
- class FenParserFastParse
- function parseFen
- function parseBoard
- function importGameContext
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParserSupport.scala` — function buildSquares
- `modules/io/src/main/scala/de/nowchess/io/pgn/PgnExporter.scala`
- class PgnExporter
- function exportGameContext
- function exportGame
- `modules/io/src/main/scala/de/nowchess/io/pgn/PgnParser.scala`
- class PgnParser
- function validatePgn
- function importGameContext
- function parsePgn
- function parseAlgebraicMove
- `modules/rule/src/main/scala/de/nowchess/rules/RuleSet.scala`
- class RuleSet
- function candidateMoves
- function legalMoves
- function allLegalMoves
- function isCheck
- function isCheckmate
- _...4 more_
- `modules/rule/src/main/scala/de/nowchess/rules/sets/DefaultRules.scala`
- class DefaultRules
- function loop
- function toMoves
- function loop
- `modules/ui/src/main/scala/de/nowchess/ui/Main.scala` — class Main, function main
- `modules/ui/src/main/scala/de/nowchess/ui/gui/ChessBoardView.scala`
- class ChessBoardView
- function updateBoard
- function updateUndoRedoButtons
- function showMessage
- function showPromotionDialog
- `modules/ui/src/main/scala/de/nowchess/ui/gui/ChessGUI.scala`
- class ChessGUIApp
- class ChessGUILauncher
- function getEngine
- function launch
- `modules/ui/src/main/scala/de/nowchess/ui/gui/GUIObserver.scala` — class GUIObserver
- `modules/ui/src/main/scala/de/nowchess/ui/gui/PieceSprites.scala`
- class PieceSprites
- function loadPieceImage
- class SquareColors
- `modules/ui/src/main/scala/de/nowchess/ui/terminal/TerminalUI.scala` — class TerminalUI, function start
- `modules/ui/src/main/scala/de/nowchess/ui/utils/PieceUnicode.scala` — function unicode
- `modules/ui/src/main/scala/de/nowchess/ui/utils/Renderer.scala` — class Renderer, function render
---
# Dependency Graph
## Most Imported Files (change these carefully)
- `modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala` — imported by **28** files
- `modules/api/src/main/scala/de/nowchess/api/board/Square.scala` — imported by **21** files
- `modules/api/src/main/scala/de/nowchess/api/board/Color.scala` — imported by **19** files
- `modules/api/src/main/scala/de/nowchess/api/move/Move.scala` — imported by **14** files
- `modules/api/src/main/scala/de/nowchess/api/board/Board.scala` — imported by **13** files
- `modules/api/src/main/scala/de/nowchess/api/board/Piece.scala` — imported by **10** files
- `modules/api/src/main/scala/de/nowchess/api/board/PieceType.scala` — imported by **9** files
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParser.scala` — imported by **9** files
- `modules/rule/src/main/scala/de/nowchess/rules/sets/DefaultRules.scala` — imported by **8** files
- `modules/io/src/main/scala/de/nowchess/io/GameContextImport.scala` — imported by **7** files
- `modules/api/src/main/scala/de/nowchess/api/board/CastlingRights.scala` — imported by **4** files
- `modules/io/src/main/scala/de/nowchess/io/GameContextExport.scala` — imported by **4** files
- `modules/rule/src/main/scala/de/nowchess/rules/RuleSet.scala` — imported by **4** files
- `modules/core/src/main/scala/de/nowchess/chess/observer/Observer.scala` — imported by **4** files
- `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala` — imported by **4** files
- `modules/io/src/main/scala/de/nowchess/io/pgn/PgnParser.scala` — imported by **2** files
- `modules/io/src/main/scala/de/nowchess/io/pgn/PgnExporter.scala` — imported by **2** files
- `modules/io/src/main/scala/de/nowchess/io/fen/FenExporter.scala` — imported by **2** files
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParserSupport.scala` — imported by **2** files
- `modules/core/src/main/scala/de/nowchess/chess/controller/Parser.scala` — imported by **1** files
## Import Map (who imports what)
- `modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala``modules/core/src/main/scala/de/nowchess/chess/command/Command.scala`, `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/main/scala/de/nowchess/chess/observer/Observer.scala`, `modules/core/src/test/scala/de/nowchess/chess/command/CommandInvokerBranchTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/command/CommandInvokerTest.scala` +23 more
- `modules/api/src/main/scala/de/nowchess/api/board/Square.scala``modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala`, `modules/api/src/main/scala/de/nowchess/api/move/Move.scala`, `modules/api/src/test/scala/de/nowchess/api/game/GameContextTest.scala`, `modules/api/src/test/scala/de/nowchess/api/move/MoveTest.scala`, `modules/core/src/main/scala/de/nowchess/chess/command/Command.scala` +16 more
- `modules/api/src/main/scala/de/nowchess/api/board/Color.scala``modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala`, `modules/api/src/test/scala/de/nowchess/api/game/GameContextTest.scala`, `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/main/scala/de/nowchess/chess/observer/Observer.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/EngineTestHelpers.scala` +14 more
- `modules/api/src/main/scala/de/nowchess/api/move/Move.scala``modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala`, `modules/api/src/test/scala/de/nowchess/api/board/BoardTest.scala`, `modules/api/src/test/scala/de/nowchess/api/game/GameContextTest.scala`, `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineIntegrationTest.scala` +9 more
- `modules/api/src/main/scala/de/nowchess/api/board/Board.scala``modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala`, `modules/api/src/test/scala/de/nowchess/api/game/GameContextTest.scala`, `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/EngineTestHelpers.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineGameEndingTest.scala` +8 more
- `modules/api/src/main/scala/de/nowchess/api/board/Piece.scala``modules/core/src/main/scala/de/nowchess/chess/command/Command.scala`, `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEnginePromotionTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineScenarioTest.scala`, `modules/rule/src/test/scala/de/nowchess/rule/DefaultRulesStateTransitionsTest.scala` +5 more
- `modules/api/src/main/scala/de/nowchess/api/board/PieceType.scala``modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineIntegrationTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEnginePromotionTest.scala`, `modules/rule/src/test/scala/de/nowchess/rule/DefaultRulesStateTransitionsTest.scala`, `modules/rule/src/test/scala/de/nowchess/rule/DefaultRulesTest.scala` +4 more
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParser.scala``modules/core/src/test/scala/de/nowchess/chess/engine/EngineTestHelpers.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineLoadGameTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineNotationTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEnginePromotionTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineScenarioTest.scala` +4 more
- `modules/rule/src/main/scala/de/nowchess/rules/sets/DefaultRules.scala``modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/EngineTestHelpers.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineIntegrationTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEnginePromotionTest.scala`, `modules/io/src/main/scala/de/nowchess/io/pgn/PgnExporter.scala` +3 more
- `modules/io/src/main/scala/de/nowchess/io/GameContextImport.scala``modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineIntegrationTest.scala`, `modules/io/src/main/scala/de/nowchess/io/fen/FenParser.scala`, `modules/io/src/main/scala/de/nowchess/io/fen/FenParserCombinators.scala`, `modules/io/src/main/scala/de/nowchess/io/fen/FenParserFastParse.scala` +2 more
---
_Generated by [codesight](https://github.com/Houseofmvps/codesight) — see your codebase clearly_
+37
View File
@@ -0,0 +1,37 @@
# Dependency Graph
## Most Imported Files (change these carefully)
- `modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala` — imported by **28** files
- `modules/api/src/main/scala/de/nowchess/api/board/Square.scala` — imported by **21** files
- `modules/api/src/main/scala/de/nowchess/api/board/Color.scala` — imported by **19** files
- `modules/api/src/main/scala/de/nowchess/api/move/Move.scala` — imported by **14** files
- `modules/api/src/main/scala/de/nowchess/api/board/Board.scala` — imported by **13** files
- `modules/api/src/main/scala/de/nowchess/api/board/Piece.scala` — imported by **10** files
- `modules/api/src/main/scala/de/nowchess/api/board/PieceType.scala` — imported by **9** files
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParser.scala` — imported by **9** files
- `modules/rule/src/main/scala/de/nowchess/rules/sets/DefaultRules.scala` — imported by **8** files
- `modules/io/src/main/scala/de/nowchess/io/GameContextImport.scala` — imported by **7** files
- `modules/api/src/main/scala/de/nowchess/api/board/CastlingRights.scala` — imported by **4** files
- `modules/io/src/main/scala/de/nowchess/io/GameContextExport.scala` — imported by **4** files
- `modules/rule/src/main/scala/de/nowchess/rules/RuleSet.scala` — imported by **4** files
- `modules/core/src/main/scala/de/nowchess/chess/observer/Observer.scala` — imported by **4** files
- `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala` — imported by **4** files
- `modules/io/src/main/scala/de/nowchess/io/pgn/PgnParser.scala` — imported by **2** files
- `modules/io/src/main/scala/de/nowchess/io/pgn/PgnExporter.scala` — imported by **2** files
- `modules/io/src/main/scala/de/nowchess/io/fen/FenExporter.scala` — imported by **2** files
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParserSupport.scala` — imported by **2** files
- `modules/core/src/main/scala/de/nowchess/chess/controller/Parser.scala` — imported by **1** files
## Import Map (who imports what)
- `modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala``modules/core/src/main/scala/de/nowchess/chess/command/Command.scala`, `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/main/scala/de/nowchess/chess/observer/Observer.scala`, `modules/core/src/test/scala/de/nowchess/chess/command/CommandInvokerBranchTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/command/CommandInvokerTest.scala` +23 more
- `modules/api/src/main/scala/de/nowchess/api/board/Square.scala``modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala`, `modules/api/src/main/scala/de/nowchess/api/move/Move.scala`, `modules/api/src/test/scala/de/nowchess/api/game/GameContextTest.scala`, `modules/api/src/test/scala/de/nowchess/api/move/MoveTest.scala`, `modules/core/src/main/scala/de/nowchess/chess/command/Command.scala` +16 more
- `modules/api/src/main/scala/de/nowchess/api/board/Color.scala``modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala`, `modules/api/src/test/scala/de/nowchess/api/game/GameContextTest.scala`, `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/main/scala/de/nowchess/chess/observer/Observer.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/EngineTestHelpers.scala` +14 more
- `modules/api/src/main/scala/de/nowchess/api/move/Move.scala``modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala`, `modules/api/src/test/scala/de/nowchess/api/board/BoardTest.scala`, `modules/api/src/test/scala/de/nowchess/api/game/GameContextTest.scala`, `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineIntegrationTest.scala` +9 more
- `modules/api/src/main/scala/de/nowchess/api/board/Board.scala``modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala`, `modules/api/src/test/scala/de/nowchess/api/game/GameContextTest.scala`, `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/EngineTestHelpers.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineGameEndingTest.scala` +8 more
- `modules/api/src/main/scala/de/nowchess/api/board/Piece.scala``modules/core/src/main/scala/de/nowchess/chess/command/Command.scala`, `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEnginePromotionTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineScenarioTest.scala`, `modules/rule/src/test/scala/de/nowchess/rule/DefaultRulesStateTransitionsTest.scala` +5 more
- `modules/api/src/main/scala/de/nowchess/api/board/PieceType.scala``modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineIntegrationTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEnginePromotionTest.scala`, `modules/rule/src/test/scala/de/nowchess/rule/DefaultRulesStateTransitionsTest.scala`, `modules/rule/src/test/scala/de/nowchess/rule/DefaultRulesTest.scala` +4 more
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParser.scala``modules/core/src/test/scala/de/nowchess/chess/engine/EngineTestHelpers.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineLoadGameTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineNotationTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEnginePromotionTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineScenarioTest.scala` +4 more
- `modules/rule/src/main/scala/de/nowchess/rules/sets/DefaultRules.scala``modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/EngineTestHelpers.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineIntegrationTest.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEnginePromotionTest.scala`, `modules/io/src/main/scala/de/nowchess/io/pgn/PgnExporter.scala` +3 more
- `modules/io/src/main/scala/de/nowchess/io/GameContextImport.scala``modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`, `modules/core/src/test/scala/de/nowchess/chess/engine/GameEngineIntegrationTest.scala`, `modules/io/src/main/scala/de/nowchess/io/fen/FenParser.scala`, `modules/io/src/main/scala/de/nowchess/io/fen/FenParserCombinators.scala`, `modules/io/src/main/scala/de/nowchess/io/fen/FenParserFastParse.scala` +2 more
+150
View File
@@ -0,0 +1,150 @@
# Libraries
- `jacoco-reporter/scoverage_coverage_gaps.py`
- function parse_scoverage_xml: (xml_path) -> tuple[dict, list[ClassGap]]
- function format_agent: (project_stats, classes) -> str
- function format_json: (project_stats, classes) -> str
- function format_markdown: (project_stats, classes) -> str
- function format_module_gaps: (module_name, classes, stmt_pct) -> str
- function run_scan_modules: (modules_dir, package_filter, min_coverage) -> None
- _...4 more_
- `jacoco-reporter/test_gaps.py`
- function parse_suite_xml: (xml_path) -> SuiteResult
- function load_module: (module_dir, results_subdir) -> Optional[ModuleResult]
- function format_module: (mod) -> str
- function run: (modules_dir, results_subdir, module_filter) -> None
- function main: () -> None
- class TestCase
- _...2 more_
- `modules/api/src/main/scala/de/nowchess/api/board/Board.scala`
- class Board
- function apply
- function pieceAt
- function updated
- function removed
- function withMove
- _...2 more_
- `modules/api/src/main/scala/de/nowchess/api/board/CastlingRights.scala`
- function hasAnyRights
- function hasRights
- function revokeColor
- function revokeKingSide
- function revokeQueenSide
- class CastlingRights
- `modules/api/src/main/scala/de/nowchess/api/board/Color.scala` — function opposite, function label
- `modules/api/src/main/scala/de/nowchess/api/board/Piece.scala` — class Piece
- `modules/api/src/main/scala/de/nowchess/api/board/PieceType.scala` — function label
- `modules/api/src/main/scala/de/nowchess/api/board/Square.scala`
- class Square
- function fromAlgebraic
- function offset
- `modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala`
- function withBoard
- function withTurn
- function withCastlingRights
- function withEnPassantSquare
- function withHalfMoveClock
- function withMove
- _...2 more_
- `modules/api/src/main/scala/de/nowchess/api/player/PlayerInfo.scala` — class PlayerId, function apply
- `modules/api/src/main/scala/de/nowchess/api/response/ApiResponse.scala`
- class ApiResponse
- function error
- function totalPages
- `modules/core/src/main/scala/de/nowchess/chess/command/Command.scala`
- class Command
- function execute
- function undo
- function description
- class MoveResult
- `modules/core/src/main/scala/de/nowchess/chess/command/CommandInvoker.scala`
- class CommandInvoker
- function execute
- function undo
- function redo
- function history
- function getCurrentIndex
- _...3 more_
- `modules/core/src/main/scala/de/nowchess/chess/controller/Parser.scala` — class Parser, function parseMove
- `modules/core/src/main/scala/de/nowchess/chess/engine/GameEngine.scala`
- class GameEngine
- function isPendingPromotion
- function board
- function turn
- function context
- function canUndo
- _...10 more_
- `modules/core/src/main/scala/de/nowchess/chess/observer/Observer.scala`
- function context
- class Observer
- function onGameEvent
- class Observable
- function subscribe
- function unsubscribe
- _...1 more_
- `modules/io/src/main/scala/de/nowchess/io/GameContextExport.scala` — class GameContextExport, function exportGameContext
- `modules/io/src/main/scala/de/nowchess/io/GameContextImport.scala` — class GameContextImport, function importGameContext
- `modules/io/src/main/scala/de/nowchess/io/fen/FenExporter.scala`
- class FenExporter
- function boardToFen
- function gameContextToFen
- function exportGameContext
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParser.scala`
- class FenParser
- function parseFen
- function importGameContext
- function parseBoard
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParserCombinators.scala`
- class FenParserCombinators
- function parseFen
- function parseBoard
- function importGameContext
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParserFastParse.scala`
- class FenParserFastParse
- function parseFen
- function parseBoard
- function importGameContext
- `modules/io/src/main/scala/de/nowchess/io/fen/FenParserSupport.scala` — function buildSquares
- `modules/io/src/main/scala/de/nowchess/io/pgn/PgnExporter.scala`
- class PgnExporter
- function exportGameContext
- function exportGame
- `modules/io/src/main/scala/de/nowchess/io/pgn/PgnParser.scala`
- class PgnParser
- function validatePgn
- function importGameContext
- function parsePgn
- function parseAlgebraicMove
- `modules/rule/src/main/scala/de/nowchess/rules/RuleSet.scala`
- class RuleSet
- function candidateMoves
- function legalMoves
- function allLegalMoves
- function isCheck
- function isCheckmate
- _...4 more_
- `modules/rule/src/main/scala/de/nowchess/rules/sets/DefaultRules.scala`
- class DefaultRules
- function loop
- function toMoves
- function loop
- `modules/ui/src/main/scala/de/nowchess/ui/Main.scala` — class Main, function main
- `modules/ui/src/main/scala/de/nowchess/ui/gui/ChessBoardView.scala`
- class ChessBoardView
- function updateBoard
- function updateUndoRedoButtons
- function showMessage
- function showPromotionDialog
- `modules/ui/src/main/scala/de/nowchess/ui/gui/ChessGUI.scala`
- class ChessGUIApp
- class ChessGUILauncher
- function getEngine
- function launch
- `modules/ui/src/main/scala/de/nowchess/ui/gui/GUIObserver.scala` — class GUIObserver
- `modules/ui/src/main/scala/de/nowchess/ui/gui/PieceSprites.scala`
- class PieceSprites
- function loadPieceImage
- class SquareColors
- `modules/ui/src/main/scala/de/nowchess/ui/terminal/TerminalUI.scala` — class TerminalUI, function start
- `modules/ui/src/main/scala/de/nowchess/ui/utils/PieceUnicode.scala` — function unicode
- `modules/ui/src/main/scala/de/nowchess/ui/utils/Renderer.scala` — class Renderer, function render
+44
View File
@@ -0,0 +1,44 @@
# NowChessSystems — Wiki
_Generated 2026-04-12 — re-run `npx codesight --wiki` if the codebase has changed._
Structural map compiled from source code via AST. No LLM — deterministic, 200ms.
> **How to use safely:** These articles tell you WHERE things live and WHAT exists. They do not show full implementation logic. Always read the actual source files before implementing new features or making changes. Never infer how a function works from the wiki alone.
## Articles
- [Overview](./overview.md)
## Quick Stats
- Routes: **0**
- Models: **0**
- Components: **0**
- Env vars: **0** required, **0** with defaults
## How to Use
- **New session:** read `index.md` (this file) for orientation — WHERE things are
- **Architecture question:** read `overview.md` (~500 tokens)
- **Domain question:** read the relevant article, then **read those source files**
- **Database question:** read `database.md`, then read the actual schema files
- **Before implementing anything:** read the source files listed in the article
- **Full source context:** read `.codesight/CODESIGHT.md`
## What the Wiki Does Not Cover
These exist in your codebase but are **not** reflected in wiki articles:
- Routes registered dynamically at runtime (loops, plugin factories, `app.use(dynamicRouter)`)
- Internal routes from npm packages (e.g. Better Auth's built-in `/api/auth/*` endpoints)
- WebSocket and SSE handlers
- Raw SQL tables not declared through an ORM
- Computed or virtual fields absent from schema declarations
- TypeScript types that are not actual database columns
- Routes marked `[inferred]` were detected via regex and may have lower precision
- gRPC, tRPC, and GraphQL resolvers may be partially captured
When in doubt, search the source. The wiki is a starting point, not a complete inventory.
---
_Last compiled: 2026-04-12 · 2 articles · [codesight](https://github.com/Houseofmvps/codesight)_
+5
View File
@@ -0,0 +1,5 @@
# Wiki Log
History of `npx codesight --wiki` runs. Capped at 20 entries.
## [2026-04-12 14:34:19] scan | 0 routes, 0 models, 0 components → 2 articles
+19
View File
@@ -0,0 +1,19 @@
# NowChessSystems — Overview
> **Navigation aid.** This article shows WHERE things live (routes, models, files). Read actual source files before implementing new features or making changes.
**NowChessSystems** is a scala project built with raw-http.
## High-Impact Files
Changes to these files have the widest blast radius across the codebase:
- `modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala` — imported by **28** files
- `modules/api/src/main/scala/de/nowchess/api/board/Square.scala` — imported by **21** files
- `modules/api/src/main/scala/de/nowchess/api/board/Color.scala` — imported by **19** files
- `modules/api/src/main/scala/de/nowchess/api/move/Move.scala` — imported by **14** files
- `modules/api/src/main/scala/de/nowchess/api/board/Board.scala` — imported by **13** files
- `modules/api/src/main/scala/de/nowchess/api/board/Piece.scala` — imported by **10** files
---
_Back to [index.md](./index.md) · Generated 2026-04-12_
+41
View File
@@ -0,0 +1,41 @@
# Normalize text files in the repo
* text=auto eol=lf
# Keep Windows command scripts in CRLF
*.bat text eol=crlf
*.cmd text eol=crlf
# Keep Unix shell scripts in LF
*.sh text eol=lf
# Binary assets (no EOL normalization / textual diff)
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.webp binary
*.bmp binary
*.ico binary
# ML / model / numeric artifacts
*.bin binary
*.pt binary
*.pth binary
*.onnx binary
*.h5 binary
*.hdf5 binary
*.pb binary
*.tflite binary
*.npy binary
*.npz binary
*.safetensors binary
# Firmware / hex-like artifacts
*.hex binary
# Packaged binaries
*.jar binary
*.zip binary
*.7z binary
*.gz binary
+2
View File
@@ -38,6 +38,8 @@ bin/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
graphify-out/
.graphify_*.json
### Mac OS ### ### Mac OS ###
.DS_Store .DS_Store
+1
View File
@@ -11,6 +11,7 @@
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/modules" /> <option value="$PROJECT_DIR$/modules" />
<option value="$PROJECT_DIR$/modules/api" /> <option value="$PROJECT_DIR$/modules/api" />
<option value="$PROJECT_DIR$/modules/backcore" />
<option value="$PROJECT_DIR$/modules/core" /> <option value="$PROJECT_DIR$/modules/core" />
<option value="$PROJECT_DIR$/modules/io" /> <option value="$PROJECT_DIR$/modules/io" />
<option value="$PROJECT_DIR$/modules/rule" /> <option value="$PROJECT_DIR$/modules/rule" />
+1 -1
View File
@@ -5,7 +5,7 @@
<option name="deprecationWarnings" value="true" /> <option name="deprecationWarnings" value="true" />
<option name="uncheckedWarnings" value="true" /> <option name="uncheckedWarnings" value="true" />
</profile> </profile>
<profile name="Gradle 2" modules="NowChessSystems.modules.core.main,NowChessSystems.modules.core.scoverage,NowChessSystems.modules.core.test,NowChessSystems.modules.io.main,NowChessSystems.modules.io.scoverage,NowChessSystems.modules.io.test,NowChessSystems.modules.rule.main,NowChessSystems.modules.rule.scoverage,NowChessSystems.modules.rule.test,NowChessSystems.modules.ui.main,NowChessSystems.modules.ui.scoverage,NowChessSystems.modules.ui.test"> <profile name="Gradle 2" modules="NowChessSystems.modules.backcore.integrationTest,NowChessSystems.modules.backcore.main,NowChessSystems.modules.backcore.native-test,NowChessSystems.modules.backcore.quarkus-generated-sources,NowChessSystems.modules.backcore.quarkus-test-generated-sources,NowChessSystems.modules.backcore.scoverage,NowChessSystems.modules.backcore.test,NowChessSystems.modules.core.main,NowChessSystems.modules.core.scoverage,NowChessSystems.modules.core.test,NowChessSystems.modules.io.main,NowChessSystems.modules.io.scoverage,NowChessSystems.modules.io.test,NowChessSystems.modules.rule.main,NowChessSystems.modules.rule.scoverage,NowChessSystems.modules.rule.test,NowChessSystems.modules.ui.main,NowChessSystems.modules.ui.scoverage,NowChessSystems.modules.ui.test">
<option name="deprecationWarnings" value="true" /> <option name="deprecationWarnings" value="true" />
<option name="uncheckedWarnings" value="true" /> <option name="uncheckedWarnings" value="true" />
<parameters> <parameters>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ScalaProjectSettings">
<option name="scala3DisclaimerShown" value="true" />
</component>
</project>
+15
View File
@@ -0,0 +1,15 @@
rules = [
DisableSyntax,
LeakingImplicitClassVal,
NoValInForComprehension,
ProcedureSyntax,
]
DisableSyntax.noVars = true
DisableSyntax.noThrows = true
DisableSyntax.noNulls = true
DisableSyntax.noReturns = true
DisableSyntax.noAsInstanceOf = true
DisableSyntax.noIsInstanceOf = true
DisableSyntax.noXml = true
DisableSyntax.noFinalize = true
+8
View File
@@ -0,0 +1,8 @@
version = 3.8.1
runner.dialect = scala3
maxColumn = 120
indent.main = 2
align.preset = more
trailingCommas = always
rewrite.rules = [SortImports, RedundantBraces]
rewrite.scala3.convertToNewSyntax = true
+21
View File
@@ -0,0 +1,21 @@
# Project Context
This is a scala project using raw-http.
Middleware includes: custom.
High-impact files (most imported, changes here affect many other files):
- modules/api/src/main/scala/de/nowchess/api/game/GameContext.scala (imported by 50 files)
- modules/api/src/main/scala/de/nowchess/api/board/Square.scala (imported by 33 files)
- modules/api/src/main/scala/de/nowchess/api/board/Color.scala (imported by 30 files)
- modules/api/src/main/scala/de/nowchess/api/move/Move.scala (imported by 29 files)
- modules/api/src/main/scala/de/nowchess/api/board/Board.scala (imported by 19 files)
- modules/api/src/main/scala/de/nowchess/api/board/PieceType.scala (imported by 18 files)
- modules/rule/src/main/scala/de/nowchess/rules/sets/DefaultRules.scala (imported by 17 files)
- modules/api/src/main/scala/de/nowchess/api/board/Piece.scala (imported by 15 files)
Required environment variables (no defaults):
- STOCKFISH_PATH (modules/bot/python/nnue.py)
Read .codesight/wiki/index.md for orientation (WHERE things live). Then read actual source files before implementing. Wiki articles are navigation aids, not implementation guides.
Read .codesight/CODESIGHT.md for the complete AI context map including all routes, schema, components, libraries, config, middleware, and dependency graph.
+6 -6
View File
@@ -1,7 +1,7 @@
YOU CAN: YOU CAN:
- Edit and use the asset in any commercial or non commercial project - Edit and use the asset in any commercial or non commercial project
- Use the asset in any commercial or non commercial project - Use the asset in any commercial or non commercial project
YOU CAN'T: YOU CAN'T:
- Resell or distribute the asset to others - Resell or distribute the asset to others
- Edit and resell the asset to others - - Credits required using This link: https://fatman200.itch.io/ - Edit and resell the asset to others - - Credits required using This link: https://fatman200.itch.io/
+89 -52
View File
@@ -1,58 +1,95 @@
# CLAUDE.md — NowChessSystems # Now-Chess
## Stack Scala 3.5.1 · Gradle 9
Scala 3.5.x · Quarkus + quarkus-scala3 · Hibernate/Jakarta · Lanterna TUI · K8s + ArgoCD + Kargo · Frontend TBD (Vite/React/Angular/Vue)
### Memory
Your memory is saved under .claude/memory/MEMORY.md.
## Structure
```
build.gradle.kts / settings.gradle.kts # root; include(":modules:<svc>") per service
modules/<svc>/build.gradle.kts + src/
docs/adr/ docs/api/ docs/unresolved.md
```
Versions in root `extra["VERSIONS"]`; modules read via `rootProject.extra["VERSIONS"] as Map<String,String>`.
## Commands ## Commands
```bash
./gradlew build
./gradlew :modules:<svc>:build|test
./gradlew :modules:<svc>:test --tests "de.nowchess.<svc>.<Class>"
``` ```
./clean # Clear build dirs — only when necessary
## Workflow ./compile # Compile all modules — always run
1. **Plan** — restate requirement, list files, flag risks. Proceed unless genuine ambiguity. ./test # Run all tests
2. **Tests first** — cover only new behaviour. ./coverage # Check coverage
3. **Implement** — no scope creep.
4. **Verify** — check each requirement; confirm green build.
## Scala/Quarkus Rules
- `given`/`using` only (no `implicit`); `Option`/`Either`/`Try` (no `null`/`.get`)
- `jakarta.*` only; reactive I/O (`Uni`/`Multi`), no blocking on event loop
- Always exclude `org.scala-lang:scala-library` from Quarkus BOM
- Unit tests: `extends AnyFunSuite with Matchers` — no `@Test`, no `: Unit`
- Integration tests: `@QuarkusTest` + JUnit 5 — `@Test` methods need explicit `: Unit`
## Coverage
Line = 100% · Branch = 100% · Method = 100% · Regression tests · document exceptions
Check: `jacoco-reporter/scoverage_coverage_gaps.py modules/{svc}/build/reports/scoverageTest/scoverage.xml`
⚠️ Use `scoverageTest/`, NOT `scoverage/`.
## Bug Fixing
Fix failures immediately without asking. After 3 failed attempts → log in `docs/unresolved.md` + surface summary.
## Agents (new service)
Sequential: architect → scala-implementer → test-writer → gradle-builder → code-reviewer (review only, no self-fix)
Parallel: only when services are fully independent (no shared contracts/state).
## Unresolved (`docs/unresolved.md`)
Append only, never delete:
```
## [YYYY-MM-DD] <title>
**Requirement/Bug:** **Root Cause:** **Attempted Fixes:** **Next Step:**
``` ```
Try to stick to these commands for consistency.
## Done Checklist ## Modules
- [ ] Plan written · files created/modified · tests green · requirements verified · unresolved logged
| Module | Role | Depends on |
|--------|------|-----------|
| `api` | Model / shared types | (none) |
| `core` | Primary business logic | api, rule |
| `rule` | Game rules | api |
| `io` | Export formats | api, core |
| `ui` | Entrypoint & UI | core, io |
## Style
- Use immutable data and pure functions.
- Keep functions under 30 lines. If you need "and" to describe it, split it.
- Keep cyclomatic complexity under 15.
- Avoid comments. Let names carry intent; comment only non-obvious algorithms.
- Scan for duplicated logic before finishing. Extract it.
- Follow default Sonar style for Scala.
- Use `Option` or `Either` for fallible operations; avoid exceptions for control flow.
- Naming: types are PascalCase, functions/values are camelCase.
## Code Quality
- **Coverage:** 100% condition coverage required in `api`, `core`, `rule`, `io` (mandatory); `ui` exempt.
### Linters
- **scalafmt** — enforces formatting; run `./gradlew spotlessScalaCheck` to check and `./gradlew spotlessScalaApply` to refactor.
- **scalafix** — enforces style and detects unused imports/code; run `./gradlew scalafix` to apply rules.
## Architecture Decisions
- **Immutable state as primary model:** GameContext (api) holds board, history, player state — immutable, passed through the system. Each move creates a new GameContext, enabling undo/redo without side effects.
- **Observer pattern for UI decoupling:** GameEngine publishes move/state events; CommandInvoker queues moves; UI listens to events, not polling. GameEngine never imports UI code.
- **RuleSet trait encapsulates rules:** Move generation, check, castling, en passant all in RuleSet impl. GameEngine calls rules as a black box; rules don't know about the rest of core.
## Rules
- **Tests are the spec.** Never modify tests to pass; modify requirements or code. Update tests only if requirements change.
- Never read build folders. Ask permission if needed.
- Keep this file up to date with any important decisions or conventions.
---
## Instructions for Claude Code
### Two-Step Rule (mandatory)
**Step 1 — Orient:** Use wiki articles to find WHERE things live.
**Step 2 — Verify:** Read the actual source files listed in the wiki article BEFORE writing any code.
Wiki articles are structural summaries extracted by AST. They show routes, models, and file locations.
They do NOT show full function logic, middleware internals, or dynamic runtime behavior.
**Never write or modify code based solely on wiki content — always read source files first.**
Read in order at session start:
1. `.codesight/wiki/index.md` — orientation map (~200 tokens)
2. `.codesight/wiki/overview.md` — architecture overview (~500 tokens)
3. Domain article (e.g. `.codesight/wiki/auth.md`) → check "Source Files" section → read those files
4. `.codesight/CODESIGHT.md` — full context map for deep exploration
Routes marked `[inferred]` in wiki articles were detected via regex — verify against source before trusting.
If any source file shows ⚠ in the wiki, re-run `codesight --wiki` before proceeding.
Or use the codesight MCP server for on-demand queries:
- `codesight_get_wiki_article` — read a specific wiki article by name
- `codesight_get_wiki_index` — get the wiki index
- `codesight_get_summary` — quick project overview
- `codesight_get_routes --prefix /api/users` — filtered routes
- `codesight_get_blast_radius --file src/lib/db.ts` — impact analysis before changes
- `codesight_get_schema --model users` — specific model details
Only open specific files after consulting codesight context. This saves ~16.893 tokens per conversation.
## graphify
This project has a graphify knowledge graph at graphify-out/.
Rules:
- Before answering architecture or codebase questions, read graphify-out/GRAPH_REPORT.md for god nodes and community structure
- If graphify-out/wiki/index.md exists, navigate it instead of reading raw files
- After modifying code files in this session, run `python3 -c "from graphify.watch import _rebuild_code; from pathlib import Path; _rebuild_code(Path('.'))"` to keep the graph current
+39 -1
View File
@@ -1,6 +1,8 @@
plugins { plugins {
id("org.sonarqube") version "7.2.3.7755" id("org.sonarqube") version "7.2.3.7755"
id("org.scoverage") version "8.1" apply false id("org.scoverage") version "8.1" apply false
id("com.diffplug.spotless") version "8.4.0" apply false
id("io.github.cosmicsilence.scalafix") version "0.2.6" apply false
} }
group = "de.nowchess" group = "de.nowchess"
@@ -19,7 +21,15 @@ sonar {
if (report.exists()) report.absolutePath else null if (report.exists()) report.absolutePath else null
}.joinToString(",") }.joinToString(",")
val jacocoReports = subprojects.mapNotNull { subproject ->
val report = subproject.file("build/reports/jacoco/test/jacocoTestReport.xml")
if (report.exists()) report.absolutePath else null
}.joinToString(",")
property("sonar.scala.coverage.reportPaths", scoverageReports) property("sonar.scala.coverage.reportPaths", scoverageReports)
if (jacocoReports.isNotEmpty()) {
property("sonar.coverage.jacoco.xmlReportPaths", jacocoReports)
}
} }
} }
@@ -32,7 +42,35 @@ val versions = mapOf(
"SCOVERAGE" to "2.1.1", "SCOVERAGE" to "2.1.1",
"SCALAFX" to "21.0.0-R32", "SCALAFX" to "21.0.0-R32",
"JAVAFX" to "21.0.1", "JAVAFX" to "21.0.1",
"JUNIT_BOM" to "5.13.4" "JUNIT_BOM" to "5.13.4",
"SCALA_PARSER_COMBINATORS" to "2.4.0",
"FASTPARSE" to "3.0.2",
"JACKSON" to "2.17.2",
"JACKSON_SCALA" to "2.17.2"
) )
extra["VERSIONS"] = versions extra["VERSIONS"] = versions
subprojects {
apply(plugin = "com.diffplug.spotless")
pluginManager.withPlugin("scala") {
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
scala {
scalafmt().configFile(rootProject.file(".scalafmt.conf"))
}
}
apply(plugin = "io.github.cosmicsilence.scalafix")
configure<io.github.cosmicsilence.scalafix.ScalafixExtension> {
configFile.set(rootProject.file(".scalafix.conf"))
}
// Disable SemanticDB config for the scoverage source set — it sets -sourceroot to
// the root project dir, which conflicts with scoverage's own -sourceroot and causes
// reportTestScoverage to fail with "No source root found".
tasks.matching { it.name in setOf("configSemanticDBScoverage", "checkScalafixScoverage", "checkScalafixTest") }.configureEach {
enabled = false
}
}
}
+776
View File
@@ -0,0 +1,776 @@
openapi: 3.0.3
info:
title: NowChess API
description: |
REST API for the NowChess application. Designed to feel familiar to users
of the [lichess API](https://lichess.org/api).
## Authentication
Most endpoints require a Bearer token:
```
Authorization: Bearer <token>
```
Authentication is reserved for future implementation — endpoints are currently
open unless noted otherwise.
## Move notation
Moves are expressed in **UCI notation**: `{from}{to}[promotion]`
- Normal move: `e2e4`
- Capture: `d5e6`
- Promotion: `e7e8q` (q=queen, r=rook, b=bishop, n=knight)
- Castling: `e1g1` (kingside white), `e1c1` (queenside white)
## Streaming
Endpoints that support streaming return **NDJSON** (newline-delimited JSON).
Request them with:
```
Accept: application/x-ndjson
```
Each line of the response is a complete JSON object. Empty lines are
keep-alive heartbeats.
## Rate limiting
Requests that exceed the rate limit receive `429 Too Many Requests`.
Honour the `Retry-After` response header and wait before retrying.
version: 1.0.0
contact:
name: NowChess
license:
name: MIT
servers:
- url: http://localhost:8080
description: Local development server
tags:
- name: game
description: Create and manage chess games
- name: move
description: Make moves and navigate game history
- name: draw
description: Draw offers and claims
- name: import
description: Load a game from FEN or PGN
- name: export
description: Export a game as FEN or PGN
paths:
# ---------------------------------------------------------------------------
# Game lifecycle
# ---------------------------------------------------------------------------
/api/board/game:
post:
operationId: createGame
tags: [game]
summary: Create a new game
description: |
Creates a new chess game starting from the initial position.
Returns the full game state including the generated `gameId`.
security:
- bearerAuth: []
requestBody:
required: false
content:
application/json:
schema:
$ref: '#/components/schemas/CreateGameRequest'
responses:
'201':
description: Game created
content:
application/json:
schema:
$ref: '#/components/schemas/GameFull'
'400':
$ref: '#/components/responses/BadRequest'
'401':
$ref: '#/components/responses/Unauthorized'
'429':
$ref: '#/components/responses/TooManyRequests'
/api/board/game/{gameId}:
get:
operationId: getGame
tags: [game]
summary: Get game state
description: Returns the full current state of a game.
security:
- bearerAuth: []
parameters:
- $ref: '#/components/parameters/gameId'
responses:
'200':
description: Current game state
content:
application/json:
schema:
$ref: '#/components/schemas/GameFull'
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
/api/board/game/{gameId}/stream:
get:
operationId: streamGame
tags: [game]
summary: Stream game events
description: |
Opens a persistent NDJSON stream for a game. The first object sent is
a `gameFull` event containing the complete game state. Subsequent
objects are `gameState` events sent whenever the game changes (move
made, draw offered, game over, etc.).
Empty lines are heartbeats to keep the connection alive.
Connect with:
```
Accept: application/x-ndjson
```
security:
- bearerAuth: []
parameters:
- $ref: '#/components/parameters/gameId'
responses:
'200':
description: NDJSON event stream
content:
application/x-ndjson:
schema:
oneOf:
- $ref: '#/components/schemas/GameFullEvent'
- $ref: '#/components/schemas/GameStateEvent'
- $ref: '#/components/schemas/ErrorEvent'
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
/api/board/game/{gameId}/resign:
post:
operationId: resignGame
tags: [game]
summary: Resign the game
description: The active player resigns. The game ends immediately.
security:
- bearerAuth: []
parameters:
- $ref: '#/components/parameters/gameId'
responses:
'200':
description: Resignation accepted
content:
application/json:
schema:
$ref: '#/components/schemas/OkResponse'
'400':
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
# ---------------------------------------------------------------------------
# Move-making
# ---------------------------------------------------------------------------
/api/board/game/{gameId}/move/{uci}:
post:
operationId: makeMove
tags: [move]
summary: Make a move
description: |
Submit a move in UCI notation. The move must be legal for the side
currently to move.
For promotion moves include the target piece as the fifth character:
`e7e8q`, `a2a1r`, etc.
If the move results in a pawn reaching the back rank and no promotion
character is supplied, the game enters `promotionPending` status and
the move is not yet applied — resubmit with the promotion character.
security:
- bearerAuth: []
parameters:
- $ref: '#/components/parameters/gameId'
- name: uci
in: path
required: true
description: Move in UCI notation (e.g. `e2e4`, `e7e8q`)
schema:
type: string
pattern: '^[a-h][1-8][a-h][1-8][qrbn]?$'
example: e2e4
responses:
'200':
description: Move applied — returns updated game state
content:
application/json:
schema:
$ref: '#/components/schemas/GameState'
'400':
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
/api/board/game/{gameId}/moves:
get:
operationId: getLegalMoves
tags: [move]
summary: Get legal moves
description: |
Returns all legal moves for the side currently to move.
Optionally filter to moves originating from a single square.
security:
- bearerAuth: []
parameters:
- $ref: '#/components/parameters/gameId'
- name: square
in: query
required: false
description: Filter to moves from this square (e.g. `e2`)
schema:
type: string
pattern: '^[a-h][1-8]$'
example: e2
responses:
'200':
description: List of legal moves
content:
application/json:
schema:
$ref: '#/components/schemas/LegalMovesResponse'
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
/api/board/game/{gameId}/undo:
post:
operationId: undoMove
tags: [move]
summary: Undo the last move
description: Reverts the most recent move. Returns the updated game state.
security:
- bearerAuth: []
parameters:
- $ref: '#/components/parameters/gameId'
responses:
'200':
description: Move undone
content:
application/json:
schema:
$ref: '#/components/schemas/GameState'
'400':
description: No moves to undo
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
/api/board/game/{gameId}/redo:
post:
operationId: redoMove
tags: [move]
summary: Redo a previously undone move
description: Re-applies the next move in the undo stack. Returns the updated game state.
security:
- bearerAuth: []
parameters:
- $ref: '#/components/parameters/gameId'
responses:
'200':
description: Move redone
content:
application/json:
schema:
$ref: '#/components/schemas/GameState'
'400':
description: No moves to redo
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
# ---------------------------------------------------------------------------
# Draw handling
# ---------------------------------------------------------------------------
/api/board/game/{gameId}/draw/{action}:
post:
operationId: drawAction
tags: [draw]
summary: Offer, accept, decline, or claim a draw
description: |
Perform a draw-related action:
| Action | Description |
|-----------|-------------|
| `offer` | Offer a draw to the opponent |
| `accept` | Accept the opponent's draw offer |
| `decline` | Decline the opponent's draw offer |
| `claim` | Claim a draw under the fifty-move rule (only valid when `status` is `fiftyMoveAvailable`) |
security:
- bearerAuth: []
parameters:
- $ref: '#/components/parameters/gameId'
- name: action
in: path
required: true
schema:
type: string
enum: [offer, accept, decline, claim]
responses:
'200':
description: Action accepted
content:
application/json:
schema:
$ref: '#/components/schemas/OkResponse'
'400':
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
# ---------------------------------------------------------------------------
# Import
# ---------------------------------------------------------------------------
/api/board/game/import/fen:
post:
operationId: importFen
tags: [import]
summary: Load a position from FEN
description: |
Creates a new game from a FEN string. The game starts at the position
described by the FEN; move history prior to that position is not
available.
security:
- bearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ImportFenRequest'
responses:
'201':
description: Game created from FEN
content:
application/json:
schema:
$ref: '#/components/schemas/GameFull'
'400':
$ref: '#/components/responses/BadRequest'
'429':
$ref: '#/components/responses/TooManyRequests'
/api/board/game/import/pgn:
post:
operationId: importPgn
tags: [import]
summary: Load a game from PGN
description: |
Creates a new game by replaying all moves in a PGN string. The game
starts at the position after the final move in the PGN; undo is
available for every replayed move.
security:
- bearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ImportPgnRequest'
responses:
'201':
description: Game created from PGN
content:
application/json:
schema:
$ref: '#/components/schemas/GameFull'
'400':
$ref: '#/components/responses/BadRequest'
'429':
$ref: '#/components/responses/TooManyRequests'
# ---------------------------------------------------------------------------
# Export
# ---------------------------------------------------------------------------
/api/board/game/{gameId}/export/fen:
get:
operationId: exportFen
tags: [export]
summary: Export current position as FEN
description: Returns the FEN string representing the current board position.
security:
- bearerAuth: []
parameters:
- $ref: '#/components/parameters/gameId'
responses:
'200':
description: FEN string
content:
text/plain:
schema:
type: string
example: rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
/api/board/game/{gameId}/export/pgn:
get:
operationId: exportPgn
tags: [export]
summary: Export game as PGN
description: Returns the full PGN for the game including headers and move text.
security:
- bearerAuth: []
parameters:
- $ref: '#/components/parameters/gameId'
responses:
'200':
description: PGN text
content:
application/x-chess-pgn:
schema:
type: string
example: |
[Event "NowChess game"]
[White "Player1"]
[Black "Player2"]
[Result "*"]
1. e4 e5 2. Nf3 *
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
# =============================================================================
# Components
# =============================================================================
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
description: 'Personal access token — `Authorization: Bearer <token>`'
parameters:
gameId:
name: gameId
in: path
required: true
description: 8-character alphanumeric game ID (e.g. `Qa7FJNk2`)
schema:
type: string
pattern: '^[A-Za-z0-9]{8}$'
example: Qa7FJNk2
responses:
BadRequest:
description: Invalid input
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
Unauthorized:
description: Missing or invalid authentication token
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
NotFound:
description: Game not found
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
TooManyRequests:
description: Rate limit exceeded — see `Retry-After` header
headers:
Retry-After:
description: Seconds to wait before retrying
schema:
type: integer
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
schemas:
# -------------------------------------------------------------------------
# Requests
# -------------------------------------------------------------------------
CreateGameRequest:
type: object
description: Parameters for creating a new game. All fields are optional.
properties:
white:
$ref: '#/components/schemas/PlayerInfo'
black:
$ref: '#/components/schemas/PlayerInfo'
ImportFenRequest:
type: object
required: [fen]
properties:
fen:
type: string
description: Complete FEN string (6 fields)
example: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
white:
$ref: '#/components/schemas/PlayerInfo'
black:
$ref: '#/components/schemas/PlayerInfo'
ImportPgnRequest:
type: object
required: [pgn]
properties:
pgn:
type: string
description: PGN text (headers and move list)
example: "1. e4 e5 2. Nf3 Nc6 *"
# -------------------------------------------------------------------------
# Game state
# -------------------------------------------------------------------------
GameFull:
type: object
description: Complete game information including players and current state.
required: [gameId, white, black, state]
properties:
gameId:
type: string
description: Unique 8-character game identifier
example: Qa7FJNk2
white:
$ref: '#/components/schemas/PlayerInfo'
black:
$ref: '#/components/schemas/PlayerInfo'
state:
$ref: '#/components/schemas/GameState'
GameState:
type: object
description: |
The current game state. Included in `GameFull` and returned by move
endpoints and stream events.
required: [fen, pgn, turn, status, moves, undoAvailable, redoAvailable]
properties:
fen:
type: string
description: FEN string for the current position
example: rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
pgn:
type: string
description: PGN move text for the full game so far
example: "1. e4"
turn:
type: string
enum: [white, black]
description: The side to move
status:
$ref: '#/components/schemas/GameStatus'
winner:
type: string
enum: [white, black]
description: Set when `status` is `checkmate` or `resign`
nullable: true
moves:
type: array
description: All moves played so far, in UCI notation
items:
type: string
example: [e2e4, e7e5, g1f3]
undoAvailable:
type: boolean
description: Whether `POST /undo` is currently valid
redoAvailable:
type: boolean
description: Whether `POST /redo` is currently valid
GameStatus:
type: string
description: |
Current game status:
| Value | Meaning |
|-------|---------|
| `started` | Game in progress, no special condition |
| `check` | Side to move is in check |
| `checkmate` | Side to move is checkmated — game over |
| `stalemate` | Side to move has no legal moves, not in check — game over (draw) |
| `resign` | A player resigned — game over |
| `draw` | Draw agreed or claimed — game over |
| `drawOffered` | Waiting for the opponent to accept or decline a draw offer |
| `fiftyMoveAvailable` | Fifty-move rule threshold reached; active player may claim draw |
| `promotionPending` | A pawn reached the back rank; awaiting promotion piece selection |
| `insufficientMaterial` | Neither side has enough pieces to deliver checkmate — game over (draw) |
enum:
- started
- check
- checkmate
- stalemate
- resign
- draw
- drawOffered
- fiftyMoveAvailable
- promotionPending
- insufficientMaterial
# -------------------------------------------------------------------------
# Moves
# -------------------------------------------------------------------------
LegalMovesResponse:
type: object
required: [moves]
properties:
moves:
type: array
items:
$ref: '#/components/schemas/LegalMove'
LegalMove:
type: object
required: [from, to, uci, moveType]
properties:
from:
type: string
description: Origin square in algebraic notation
example: e2
to:
type: string
description: Destination square in algebraic notation
example: e4
uci:
type: string
description: Full move in UCI notation
example: e2e4
moveType:
$ref: '#/components/schemas/MoveType'
promotion:
type: string
enum: [queen, rook, bishop, knight]
description: Target piece for promotion moves
nullable: true
MoveType:
type: string
description: Classification of the move
enum:
- normal
- capture
- castleKingside
- castleQueenside
- enPassant
- promotion
# -------------------------------------------------------------------------
# Streaming events
# -------------------------------------------------------------------------
GameFullEvent:
type: object
description: |
First event on a game stream. Contains the complete game snapshot.
required: [type, game]
properties:
type:
type: string
enum: [gameFull]
game:
$ref: '#/components/schemas/GameFull'
GameStateEvent:
type: object
description: |
Emitted on a game stream whenever the game state changes (move played,
draw offered, game over, etc.).
required: [type, state]
properties:
type:
type: string
enum: [gameState]
state:
$ref: '#/components/schemas/GameState'
ErrorEvent:
type: object
description: Emitted on a game stream when an error occurs.
required: [type, error]
properties:
type:
type: string
enum: [error]
error:
$ref: '#/components/schemas/ApiError'
# -------------------------------------------------------------------------
# Shared types
# -------------------------------------------------------------------------
PlayerInfo:
type: object
required: [id, displayName]
properties:
id:
type: string
description: Unique player identifier
example: player1
displayName:
type: string
description: Human-readable display name
example: Alice
OkResponse:
type: object
required: [ok]
properties:
ok:
type: boolean
enum: [true]
ApiError:
type: object
required: [code, message]
properties:
code:
type: string
description: Machine-readable error code
example: INVALID_MOVE
message:
type: string
description: Human-readable error description
example: e2e5 is not a legal move
field:
type: string
description: Request field that caused the error, if applicable
example: uci
nullable: true
-20
View File
@@ -1,20 +0,0 @@
## [2026-03-31] Unreachable code blocking 100% statement coverage
**Requirement/Bug:** Reach 100% statement coverage in core module.
**Root Cause:** 4 remaining uncovered statements (99.6% coverage) are unreachable code:
1. **PgnParser.scala:160** (`case _ => None` in extractPromotion) - Regex `=([QRBN])` only matches those 4 characters; fallback case can never execute
2. **GameHistory.scala:29** (`addMove$default$4` compiler-generated method) - Method overload 3 without defaults shadows the 4-param version, making promotionPiece default accessor unreachable
3. **GameEngine.scala:201-202** (`case _` in completePromotion) - GameController.completePromotion always returns one of 4 expected MoveResult types; catch-all is defensive code
**Attempted Fixes:**
1. Added comprehensive PGN parsing tests (all 4 promotion types) - PgnParser improved from 95.8% to 99.4%
2. Added GameHistory tests using named parameters - hit `addMove$default$3` (castleSide) but not `$default$4` (promotionPiece)
3. Named parameter approach: `addMove(from=..., to=..., promotionPiece=...)` triggers 4-param with castleSide default ✓
4. Positional approach: `addMove(f, t, None, None)` requires all 4 args (explicit, no defaults used) - doesn't hit $default$4
5. Root issue: Scala's overload resolution prefers more-specific non-default overloads (2-param, 3-param) over the 4-param with defaults
**Recommendation:** 99.6% (1029/1033) is maximum achievable without refactoring method overloads. Unreachable code design patterns:
- **Pattern 1 (unreachable regex fallback):** Defensive pattern match against exhaustive regex
- **Pattern 2 (overshadowed defaults):** Method overloads shadow default parameters in parent signature
- **Pattern 3 (defensive catch-all):** Error handling for impossible external API returns
+5
View File
@@ -0,0 +1,5 @@
quarkusPluginId=io.quarkus
quarkusPluginVersion=3.32.4
quarkusPlatformGroupId=io.quarkus.platform
quarkusPlatformArtifactId=quarkus-bom
quarkusPlatformVersion=3.32.4
+18
View File
@@ -16,3 +16,21 @@
### Features ### Features
* NCS-21 Write Scripts to automate certain tasks ([#15](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/15)) ([8051871](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/80518719d536a087d339fe02530825dc07f8b388)) * NCS-21 Write Scripts to automate certain tasks ([#15](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/15)) ([8051871](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/80518719d536a087d339fe02530825dc07f8b388))
## (2026-04-07)
### Features
* NCS-21 Write Scripts to automate certain tasks ([#15](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/15)) ([8051871](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/80518719d536a087d339fe02530825dc07f8b388))
## (2026-04-12)
### Features
* NCS-21 Write Scripts to automate certain tasks ([#15](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/15)) ([8051871](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/80518719d536a087d339fe02530825dc07f8b388))
* NCS-25 Add linters to keep quality up ([#27](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/27)) ([fd4e67d](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/fd4e67d4f782a7e955822d90cb909d0a81676fb2))
## (2026-04-14)
### Features
* NCS-14 implemented insufficient moves rule ([#30](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/30)) ([b0399a4](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/b0399a4e489950083066c9538df9a84dcc7a4613))
* NCS-21 Write Scripts to automate certain tasks ([#15](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/15)) ([8051871](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/80518719d536a087d339fe02530825dc07f8b388))
* NCS-25 Add linters to keep quality up ([#27](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/27)) ([fd4e67d](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/fd4e67d4f782a7e955822d90cb909d0a81676fb2))
@@ -7,11 +7,11 @@ object Board:
def apply(pieces: Map[Square, Piece]): Board = pieces def apply(pieces: Map[Square, Piece]): Board = pieces
extension (b: Board) extension (b: Board)
def pieceAt(sq: Square): Option[Piece] = b.get(sq) def pieceAt(sq: Square): Option[Piece] = b.get(sq)
def updated(sq: Square, piece: Piece): Board = b.updated(sq, piece) def updated(sq: Square, piece: Piece): Board = b.updated(sq, piece)
def removed(sq: Square): Board = b.removed(sq) def removed(sq: Square): Board = b.removed(sq)
def withMove(from: Square, to: Square): (Board, Option[Piece]) = def withMove(from: Square, to: Square): (Board, Option[Piece]) =
val captured = b.get(to) val captured = b.get(to)
val updatedBoard = b.removed(from).updated(to, b(from)) val updatedBoard = b.removed(from).updated(to, b(from))
(updatedBoard, captured) (updatedBoard, captured)
def applyMove(move: de.nowchess.api.move.Move): Board = def applyMove(move: de.nowchess.api.move.Move): Board =
@@ -21,8 +21,14 @@ object Board:
val initial: Board = val initial: Board =
val backRank: Vector[PieceType] = Vector( val backRank: Vector[PieceType] = Vector(
PieceType.Rook, PieceType.Knight, PieceType.Bishop, PieceType.Queen, PieceType.Rook,
PieceType.King, PieceType.Bishop, PieceType.Knight, PieceType.Rook PieceType.Knight,
PieceType.Bishop,
PieceType.Queen,
PieceType.King,
PieceType.Bishop,
PieceType.Knight,
PieceType.Rook,
) )
val entries = for val entries = for
fileIdx <- 0 until 8 fileIdx <- 0 until 8
@@ -30,7 +36,7 @@ object Board:
(Color.White, Rank.R1, backRank(fileIdx)), (Color.White, Rank.R1, backRank(fileIdx)),
(Color.White, Rank.R2, PieceType.Pawn), (Color.White, Rank.R2, PieceType.Pawn),
(Color.Black, Rank.R8, backRank(fileIdx)), (Color.Black, Rank.R8, backRank(fileIdx)),
(Color.Black, Rank.R7, PieceType.Pawn) (Color.Black, Rank.R7, PieceType.Pawn),
) )
yield Square(File.values(fileIdx), rank) -> Piece(color, pieceType) yield Square(File.values(fileIdx), rank) -> Piece(color, pieceType)
Board(entries.toMap) Board(entries.toMap)
@@ -1,50 +1,48 @@
package de.nowchess.api.board package de.nowchess.api.board
/** /** Unified castling rights tracker for all four sides. Tracks whether castling is still available for each side and
* Unified castling rights tracker for all four sides. * direction.
* Tracks whether castling is still available for each side and direction. *
* * @param whiteKingSide
* @param whiteKingSide White's king-side castling (0-0) still legally available * White's king-side castling (0-0) still legally available
* @param whiteQueenSide White's queen-side castling (0-0-0) still legally available * @param whiteQueenSide
* @param blackKingSide Black's king-side castling (0-0) still legally available * White's queen-side castling (0-0-0) still legally available
* @param blackQueenSide Black's queen-side castling (0-0-0) still legally available * @param blackKingSide
*/ * Black's king-side castling (0-0) still legally available
* @param blackQueenSide
* Black's queen-side castling (0-0-0) still legally available
*/
final case class CastlingRights( final case class CastlingRights(
whiteKingSide: Boolean, whiteKingSide: Boolean,
whiteQueenSide: Boolean, whiteQueenSide: Boolean,
blackKingSide: Boolean, blackKingSide: Boolean,
blackQueenSide: Boolean blackQueenSide: Boolean,
): ):
/** /** Check if either side has any castling rights remaining.
* Check if either side has any castling rights remaining. */
*/
def hasAnyRights: Boolean = def hasAnyRights: Boolean =
whiteKingSide || whiteQueenSide || blackKingSide || blackQueenSide whiteKingSide || whiteQueenSide || blackKingSide || blackQueenSide
/** /** Check if a specific color has any castling rights remaining.
* Check if a specific color has any castling rights remaining. */
*/
def hasRights(color: Color): Boolean = color match def hasRights(color: Color): Boolean = color match
case Color.White => whiteKingSide || whiteQueenSide case Color.White => whiteKingSide || whiteQueenSide
case Color.Black => blackKingSide || blackQueenSide case Color.Black => blackKingSide || blackQueenSide
/** /** Revoke all castling rights for a specific color.
* Revoke all castling rights for a specific color. */
*/
def revokeColor(color: Color): CastlingRights = color match def revokeColor(color: Color): CastlingRights = color match
case Color.White => copy(whiteKingSide = false, whiteQueenSide = false) case Color.White => copy(whiteKingSide = false, whiteQueenSide = false)
case Color.Black => copy(blackKingSide = false, blackQueenSide = false) case Color.Black => copy(blackKingSide = false, blackQueenSide = false)
/** /** Revoke a specific castling right.
* Revoke a specific castling right. */
*/
def revokeKingSide(color: Color): CastlingRights = color match def revokeKingSide(color: Color): CastlingRights = color match
case Color.White => copy(whiteKingSide = false) case Color.White => copy(whiteKingSide = false)
case Color.Black => copy(blackKingSide = false) case Color.Black => copy(blackKingSide = false)
/** /** Revoke a specific castling right.
* Revoke a specific castling right. */
*/
def revokeQueenSide(color: Color): CastlingRights = color match def revokeQueenSide(color: Color): CastlingRights = color match
case Color.White => copy(whiteQueenSide = false) case Color.White => copy(whiteQueenSide = false)
case Color.Black => copy(blackQueenSide = false) case Color.Black => copy(blackQueenSide = false)
@@ -55,7 +53,7 @@ object CastlingRights:
whiteKingSide = false, whiteKingSide = false,
whiteQueenSide = false, whiteQueenSide = false,
blackKingSide = false, blackKingSide = false,
blackQueenSide = false blackQueenSide = false,
) )
/** All castling rights available. */ /** All castling rights available. */
@@ -63,7 +61,7 @@ object CastlingRights:
whiteKingSide = true, whiteKingSide = true,
whiteQueenSide = true, whiteQueenSide = true,
blackKingSide = true, blackKingSide = true,
blackQueenSide = true blackQueenSide = true,
) )
/** Standard starting position castling rights (both sides can castle both ways). */ /** Standard starting position castling rights (both sides can castle both ways). */
@@ -5,16 +5,16 @@ final case class Piece(color: Color, pieceType: PieceType)
object Piece: object Piece:
// Convenience constructors // Convenience constructors
val WhitePawn: Piece = Piece(Color.White, PieceType.Pawn) val WhitePawn: Piece = Piece(Color.White, PieceType.Pawn)
val WhiteKnight: Piece = Piece(Color.White, PieceType.Knight) val WhiteKnight: Piece = Piece(Color.White, PieceType.Knight)
val WhiteBishop: Piece = Piece(Color.White, PieceType.Bishop) val WhiteBishop: Piece = Piece(Color.White, PieceType.Bishop)
val WhiteRook: Piece = Piece(Color.White, PieceType.Rook) val WhiteRook: Piece = Piece(Color.White, PieceType.Rook)
val WhiteQueen: Piece = Piece(Color.White, PieceType.Queen) val WhiteQueen: Piece = Piece(Color.White, PieceType.Queen)
val WhiteKing: Piece = Piece(Color.White, PieceType.King) val WhiteKing: Piece = Piece(Color.White, PieceType.King)
val BlackPawn: Piece = Piece(Color.Black, PieceType.Pawn) val BlackPawn: Piece = Piece(Color.Black, PieceType.Pawn)
val BlackKnight: Piece = Piece(Color.Black, PieceType.Knight) val BlackKnight: Piece = Piece(Color.Black, PieceType.Knight)
val BlackBishop: Piece = Piece(Color.Black, PieceType.Bishop) val BlackBishop: Piece = Piece(Color.Black, PieceType.Bishop)
val BlackRook: Piece = Piece(Color.Black, PieceType.Rook) val BlackRook: Piece = Piece(Color.Black, PieceType.Rook)
val BlackQueen: Piece = Piece(Color.Black, PieceType.Queen) val BlackQueen: Piece = Piece(Color.Black, PieceType.Queen)
val BlackKing: Piece = Piece(Color.Black, PieceType.King) val BlackKing: Piece = Piece(Color.Black, PieceType.King)
@@ -1,43 +1,38 @@
package de.nowchess.api.board package de.nowchess.api.board
/** /** A file (column) on the chess board, ah. Ordinal values 07 correspond to ah.
* A file (column) on the chess board, ah. */
* Ordinal values 07 correspond to ah.
*/
enum File: enum File:
case A, B, C, D, E, F, G, H case A, B, C, D, E, F, G, H
/** /** A rank (row) on the chess board, 18. Ordinal values 07 correspond to ranks 18.
* A rank (row) on the chess board, 18. */
* Ordinal values 07 correspond to ranks 18.
*/
enum Rank: enum Rank:
case R1, R2, R3, R4, R5, R6, R7, R8 case R1, R2, R3, R4, R5, R6, R7, R8
/** /** A unique square on the board, identified by its file and rank.
* A unique square on the board, identified by its file and rank. *
* * @param file
* @param file the column, ah * the column, ah
* @param rank the row, 18 * @param rank
*/ * the row, 18
*/
final case class Square(file: File, rank: Rank): final case class Square(file: File, rank: Rank):
/** Algebraic notation string, e.g. "e4". */ /** Algebraic notation string, e.g. "e4". */
override def toString: String = override def toString: String =
s"${file.toString.toLowerCase}${rank.ordinal + 1}" s"${file.toString.toLowerCase}${rank.ordinal + 1}"
object Square: object Square:
/** Parse a square from algebraic notation (e.g. "e4"). /** Parse a square from algebraic notation (e.g. "e4"). Returns None if the input is not a valid square name.
* Returns None if the input is not a valid square name. */ */
def fromAlgebraic(s: String): Option[Square] = def fromAlgebraic(s: String): Option[Square] =
if s.length != 2 then None if s.length != 2 then None
else else
val fileChar = s.charAt(0) val fileChar = s.charAt(0)
val rankChar = s.charAt(1) val rankChar = s.charAt(1)
val fileOpt = File.values.find(_.toString.equalsIgnoreCase(fileChar.toString)) val fileOpt = File.values.find(_.toString.equalsIgnoreCase(fileChar.toString))
val rankOpt = val rankOpt =
rankChar.toString.toIntOption.flatMap(n => rankChar.toString.toIntOption.flatMap(n => if n >= 1 && n <= 8 then Some(Rank.values(n - 1)) else None)
if n >= 1 && n <= 8 then Some(Rank.values(n - 1)) else None
)
for f <- fileOpt; r <- rankOpt yield Square(f, r) for f <- fileOpt; r <- rankOpt yield Square(f, r)
val all: IndexedSeq[Square] = val all: IndexedSeq[Square] =
@@ -46,12 +41,13 @@ object Square:
f <- File.values.toIndexedSeq f <- File.values.toIndexedSeq
yield Square(f, r) yield Square(f, r)
/** Compute a target square by offsetting file and rank. /** Compute a target square by offsetting file and rank. Returns None if the resulting square is outside the board
* Returns None if the resulting square is outside the board (0-7 range). */ * (0-7 range).
*/
extension (sq: Square) extension (sq: Square)
def offset(fileDelta: Int, rankDelta: Int): Option[Square] = def offset(fileDelta: Int, rankDelta: Int): Option[Square] =
val newFileOrd = sq.file.ordinal + fileDelta val newFileOrd = sq.file.ordinal + fileDelta
val newRankOrd = sq.rank.ordinal + rankDelta val newRankOrd = sq.rank.ordinal + rankDelta
if newFileOrd >= 0 && newFileOrd < 8 && newRankOrd >= 0 && newRankOrd < 8 then if newFileOrd >= 0 && newFileOrd < 8 && newRankOrd >= 0 && newRankOrd < 8 then
Some(Square(File.values(newFileOrd), Rank.values(newRankOrd))) Some(Square(File.values(newFileOrd), Rank.values(newRankOrd)))
else None else None
@@ -0,0 +1,8 @@
package de.nowchess.api.game
/** Reason why a game ended in a draw. */
enum DrawReason:
case Stalemate
case InsufficientMaterial
case FiftyMoveRule
case Agreement
@@ -1,18 +1,18 @@
package de.nowchess.api.game package de.nowchess.api.game
import de.nowchess.api.board.{Board, Color, Square, CastlingRights} import de.nowchess.api.board.{Board, CastlingRights, Color, Square}
import de.nowchess.api.move.Move import de.nowchess.api.move.Move
/** Immutable bundle of complete game state. /** Immutable bundle of complete game state. All state changes produce new GameContext instances.
* All state changes produce new GameContext instances. */
*/
case class GameContext( case class GameContext(
board: Board, board: Board,
turn: Color, turn: Color,
castlingRights: CastlingRights, castlingRights: CastlingRights,
enPassantSquare: Option[Square], enPassantSquare: Option[Square],
halfMoveClock: Int, halfMoveClock: Int,
moves: List[Move] moves: List[Move],
result: Option[GameResult] = None,
): ):
/** Create new context with updated board. */ /** Create new context with updated board. */
def withBoard(newBoard: Board): GameContext = copy(board = newBoard) def withBoard(newBoard: Board): GameContext = copy(board = newBoard)
@@ -32,6 +32,9 @@ case class GameContext(
/** Create new context with move appended to history. */ /** Create new context with move appended to history. */
def withMove(move: Move): GameContext = copy(moves = moves :+ move) def withMove(move: Move): GameContext = copy(moves = moves :+ move)
/** Create new context with updated result. */
def withResult(newResult: Option[GameResult]): GameContext = copy(result = newResult)
object GameContext: object GameContext:
/** Initial position: white to move, all castling rights, no en passant. */ /** Initial position: white to move, all castling rights, no en passant. */
def initial: GameContext = GameContext( def initial: GameContext = GameContext(
@@ -40,5 +43,5 @@ object GameContext:
castlingRights = CastlingRights.Initial, castlingRights = CastlingRights.Initial,
enPassantSquare = None, enPassantSquare = None,
halfMoveClock = 0, halfMoveClock = 0,
moves = List.empty moves = List.empty,
) )
@@ -0,0 +1,8 @@
package de.nowchess.api.game
import de.nowchess.api.board.Color
/** Outcome of a finished game. */
enum GameResult:
case Win(color: Color)
case Draw(reason: DrawReason)
@@ -10,24 +10,30 @@ enum PromotionPiece:
enum MoveType: enum MoveType:
/** A normal move or capture with no special rule. */ /** A normal move or capture with no special rule. */
case Normal(isCapture: Boolean = false) case Normal(isCapture: Boolean = false)
/** Kingside castling (O-O). */ /** Kingside castling (O-O). */
case CastleKingside case CastleKingside
/** Queenside castling (O-O-O). */ /** Queenside castling (O-O-O). */
case CastleQueenside case CastleQueenside
/** En-passant pawn capture. */ /** En-passant pawn capture. */
case EnPassant case EnPassant
/** Pawn promotion; carries the chosen promotion piece. */ /** Pawn promotion; carries the chosen promotion piece. */
case Promotion(piece: PromotionPiece) case Promotion(piece: PromotionPiece)
/** /** A half-move (ply) in a chess game.
* A half-move (ply) in a chess game. *
* * @param from
* @param from origin square * origin square
* @param to destination square * @param to
* @param moveType special semantics; defaults to Normal * destination square
*/ * @param moveType
* special semantics; defaults to Normal
*/
final case class Move( final case class Move(
from: Square, from: Square,
to: Square, to: Square,
moveType: MoveType = MoveType.Normal() moveType: MoveType = MoveType.Normal(),
) )
@@ -1,27 +1,26 @@
package de.nowchess.api.player package de.nowchess.api.player
/** /** An opaque player identifier.
* An opaque player identifier. *
* * Wraps a plain String so that IDs are not accidentally interchanged with other String values at compile time.
* Wraps a plain String so that IDs are not accidentally interchanged with */
* other String values at compile time.
*/
opaque type PlayerId = String opaque type PlayerId = String
object PlayerId: object PlayerId:
def apply(value: String): PlayerId = value def apply(value: String): PlayerId = value
extension (id: PlayerId) def value: String = id extension (id: PlayerId) def value: String = id
/** /** The minimal cross-service identity stub for a player.
* The minimal cross-service identity stub for a player. *
* * Full profile data (email, rating history, etc.) lives in the user-management service. Only what every service needs
* Full profile data (email, rating history, etc.) lives in the user-management * is held here.
* service. Only what every service needs is held here. *
* * @param id
* @param id unique identifier * unique identifier
* @param displayName human-readable name shown in the UI * @param displayName
*/ * human-readable name shown in the UI
*/
final case class PlayerInfo( final case class PlayerInfo(
id: PlayerId, id: PlayerId,
displayName: String displayName: String,
) )
@@ -1,13 +1,12 @@
package de.nowchess.api.response package de.nowchess.api.response
/** /** A standardised envelope for every API response.
* A standardised envelope for every API response. *
* * Success and failure are modelled as subtypes so that callers can pattern-match exhaustively.
* Success and failure are modelled as subtypes so that callers *
* can pattern-match exhaustively. * @tparam A
* * the payload type for a successful response
* @tparam A the payload type for a successful response */
*/
sealed trait ApiResponse[+A] sealed trait ApiResponse[+A]
object ApiResponse: object ApiResponse:
@@ -20,43 +19,49 @@ object ApiResponse:
/** Convenience constructor for a single-error failure. */ /** Convenience constructor for a single-error failure. */
def error(err: ApiError): Failure = Failure(List(err)) def error(err: ApiError): Failure = Failure(List(err))
/** /** A structured error descriptor.
* A structured error descriptor. *
* * @param code
* @param code machine-readable error code (e.g. "INVALID_MOVE", "NOT_FOUND") * machine-readable error code (e.g. "INVALID_MOVE", "NOT_FOUND")
* @param message human-readable explanation * @param message
* @param field optional field name when the error relates to a specific input * human-readable explanation
*/ * @param field
* optional field name when the error relates to a specific input
*/
final case class ApiError( final case class ApiError(
code: String, code: String,
message: String, message: String,
field: Option[String] = None field: Option[String] = None,
) )
/** /** Pagination metadata for list responses.
* Pagination metadata for list responses. *
* * @param page
* @param page current 0-based page index * current 0-based page index
* @param pageSize number of items per page * @param pageSize
* @param totalItems total number of items across all pages * number of items per page
*/ * @param totalItems
* total number of items across all pages
*/
final case class Pagination( final case class Pagination(
page: Int, page: Int,
pageSize: Int, pageSize: Int,
totalItems: Long totalItems: Long,
): ):
def totalPages: Int = def totalPages: Int =
if pageSize <= 0 then 0 if pageSize <= 0 then 0
else Math.ceil(totalItems.toDouble / pageSize).toInt else Math.ceil(totalItems.toDouble / pageSize).toInt
/** /** A paginated list response envelope.
* A paginated list response envelope. *
* * @param items
* @param items the items on the current page * the items on the current page
* @param pagination pagination metadata * @param pagination
* @tparam A the item type * pagination metadata
*/ * @tparam A
* the item type
*/
final case class PagedResponse[A]( final case class PagedResponse[A](
items: List[A], items: List[A],
pagination: Pagination pagination: Pagination,
) )
@@ -22,9 +22,9 @@ class BoardTest extends AnyFunSuite with Matchers:
} }
test("withMove returns captured piece when destination is occupied") { test("withMove returns captured piece when destination is occupied") {
val from = Square(File.A, Rank.R1) val from = Square(File.A, Rank.R1)
val to = Square(File.A, Rank.R8) val to = Square(File.A, Rank.R8)
val b = Board(Map(from -> Piece.WhiteRook, to -> Piece.BlackRook)) val b = Board(Map(from -> Piece.WhiteRook, to -> Piece.BlackRook))
val (board, captured) = b.withMove(from, to) val (board, captured) = b.withMove(from, to)
captured shouldBe Some(Piece.BlackRook) captured shouldBe Some(Piece.BlackRook)
board.pieceAt(to) shouldBe Some(Piece.WhiteRook) board.pieceAt(to) shouldBe Some(Piece.WhiteRook)
@@ -51,8 +51,14 @@ class BoardTest extends AnyFunSuite with Matchers:
test("initial board white back rank") { test("initial board white back rank") {
val expectedBackRank = Vector( val expectedBackRank = Vector(
PieceType.Rook, PieceType.Knight, PieceType.Bishop, PieceType.Queen, PieceType.Rook,
PieceType.King, PieceType.Bishop, PieceType.Knight, PieceType.Rook PieceType.Knight,
PieceType.Bishop,
PieceType.Queen,
PieceType.King,
PieceType.Bishop,
PieceType.Knight,
PieceType.Rook,
) )
File.values.zipWithIndex.foreach { (file, i) => File.values.zipWithIndex.foreach { (file, i) =>
Board.initial.pieceAt(Square(file, Rank.R1)) shouldBe Board.initial.pieceAt(Square(file, Rank.R1)) shouldBe
@@ -62,8 +68,14 @@ class BoardTest extends AnyFunSuite with Matchers:
test("initial board black back rank") { test("initial board black back rank") {
val expectedBackRank = Vector( val expectedBackRank = Vector(
PieceType.Rook, PieceType.Knight, PieceType.Bishop, PieceType.Queen, PieceType.Rook,
PieceType.King, PieceType.Bishop, PieceType.Knight, PieceType.Rook PieceType.Knight,
PieceType.Bishop,
PieceType.Queen,
PieceType.King,
PieceType.Bishop,
PieceType.Knight,
PieceType.Rook,
) )
File.values.zipWithIndex.foreach { (file, i) => File.values.zipWithIndex.foreach { (file, i) =>
Board.initial.pieceAt(Square(file, Rank.R8)) shouldBe Board.initial.pieceAt(Square(file, Rank.R8)) shouldBe
@@ -76,12 +88,11 @@ class BoardTest extends AnyFunSuite with Matchers:
for for
rank <- emptyRanks rank <- emptyRanks
file <- File.values file <- File.values
do do Board.initial.pieceAt(Square(file, rank)) shouldBe None
Board.initial.pieceAt(Square(file, rank)) shouldBe None
} }
test("updated adds and replaces piece at squares") { test("updated adds and replaces piece at squares") {
val b = Board(Map(e2 -> Piece.WhitePawn)) val b = Board(Map(e2 -> Piece.WhitePawn))
val added = b.updated(e4, Piece.WhiteKnight) val added = b.updated(e4, Piece.WhiteKnight)
added.pieceAt(e2) shouldBe Some(Piece.WhitePawn) added.pieceAt(e2) shouldBe Some(Piece.WhitePawn)
added.pieceAt(e4) shouldBe Some(Piece.WhiteKnight) added.pieceAt(e4) shouldBe Some(Piece.WhiteKnight)
@@ -91,7 +102,7 @@ class BoardTest extends AnyFunSuite with Matchers:
} }
test("removed deletes piece from board") { test("removed deletes piece from board") {
val b = Board(Map(e2 -> Piece.WhitePawn, e4 -> Piece.WhiteKnight)) val b = Board(Map(e2 -> Piece.WhitePawn, e4 -> Piece.WhiteKnight))
val removed = b.removed(e2) val removed = b.removed(e2)
removed.pieceAt(e2) shouldBe None removed.pieceAt(e2) shouldBe None
removed.pieceAt(e4) shouldBe Some(Piece.WhiteKnight) removed.pieceAt(e4) shouldBe Some(Piece.WhiteKnight)
@@ -105,4 +116,3 @@ class BoardTest extends AnyFunSuite with Matchers:
moved.pieceAt(e4) shouldBe Some(Piece.WhitePawn) moved.pieceAt(e4) shouldBe Some(Piece.WhitePawn)
moved.pieceAt(e2) shouldBe None moved.pieceAt(e2) shouldBe None
} }
@@ -10,7 +10,7 @@ class CastlingRightsTest extends AnyFunSuite with Matchers:
whiteKingSide = true, whiteKingSide = true,
whiteQueenSide = false, whiteQueenSide = false,
blackKingSide = false, blackKingSide = false,
blackQueenSide = true blackQueenSide = true,
) )
rights.hasAnyRights shouldBe true rights.hasAnyRights shouldBe true
@@ -54,4 +54,3 @@ class CastlingRightsTest extends AnyFunSuite with Matchers:
val blackQueenSideRevoked = all.revokeQueenSide(Color.Black) val blackQueenSideRevoked = all.revokeQueenSide(Color.Black)
blackQueenSideRevoked.blackKingSide shouldBe true blackQueenSideRevoked.blackKingSide shouldBe true
blackQueenSideRevoked.blackQueenSide shouldBe false blackQueenSideRevoked.blackQueenSide shouldBe false
@@ -8,7 +8,7 @@ class ColorTest extends AnyFunSuite with Matchers:
test("Color values expose opposite and label consistently"): test("Color values expose opposite and label consistently"):
val cases = List( val cases = List(
(Color.White, Color.Black, "White"), (Color.White, Color.Black, "White"),
(Color.Black, Color.White, "Black") (Color.Black, Color.White, "Black"),
) )
cases.foreach { (color, opposite, label) => cases.foreach { (color, opposite, label) =>
@@ -7,24 +7,24 @@ class PieceTest extends AnyFunSuite with Matchers:
test("Piece holds color and pieceType") { test("Piece holds color and pieceType") {
val p = Piece(Color.White, PieceType.Queen) val p = Piece(Color.White, PieceType.Queen)
p.color shouldBe Color.White p.color shouldBe Color.White
p.pieceType shouldBe PieceType.Queen p.pieceType shouldBe PieceType.Queen
} }
test("all convenience constants map to expected color and piece type") { test("all convenience constants map to expected color and piece type") {
val expected = List( val expected = List(
Piece.WhitePawn -> Piece(Color.White, PieceType.Pawn), Piece.WhitePawn -> Piece(Color.White, PieceType.Pawn),
Piece.WhiteKnight -> Piece(Color.White, PieceType.Knight), Piece.WhiteKnight -> Piece(Color.White, PieceType.Knight),
Piece.WhiteBishop -> Piece(Color.White, PieceType.Bishop), Piece.WhiteBishop -> Piece(Color.White, PieceType.Bishop),
Piece.WhiteRook -> Piece(Color.White, PieceType.Rook), Piece.WhiteRook -> Piece(Color.White, PieceType.Rook),
Piece.WhiteQueen -> Piece(Color.White, PieceType.Queen), Piece.WhiteQueen -> Piece(Color.White, PieceType.Queen),
Piece.WhiteKing -> Piece(Color.White, PieceType.King), Piece.WhiteKing -> Piece(Color.White, PieceType.King),
Piece.BlackPawn -> Piece(Color.Black, PieceType.Pawn), Piece.BlackPawn -> Piece(Color.Black, PieceType.Pawn),
Piece.BlackKnight -> Piece(Color.Black, PieceType.Knight), Piece.BlackKnight -> Piece(Color.Black, PieceType.Knight),
Piece.BlackBishop -> Piece(Color.Black, PieceType.Bishop), Piece.BlackBishop -> Piece(Color.Black, PieceType.Bishop),
Piece.BlackRook -> Piece(Color.Black, PieceType.Rook), Piece.BlackRook -> Piece(Color.Black, PieceType.Rook),
Piece.BlackQueen -> Piece(Color.Black, PieceType.Queen), Piece.BlackQueen -> Piece(Color.Black, PieceType.Queen),
Piece.BlackKing -> Piece(Color.Black, PieceType.King) Piece.BlackKing -> Piece(Color.Black, PieceType.King),
) )
expected.foreach { case (actual, wanted) => expected.foreach { case (actual, wanted) =>
@@ -7,12 +7,12 @@ class PieceTypeTest extends AnyFunSuite with Matchers:
test("PieceType values expose the expected labels"): test("PieceType values expose the expected labels"):
val expectedLabels = List( val expectedLabels = List(
PieceType.Pawn -> "Pawn", PieceType.Pawn -> "Pawn",
PieceType.Knight -> "Knight", PieceType.Knight -> "Knight",
PieceType.Bishop -> "Bishop", PieceType.Bishop -> "Bishop",
PieceType.Rook -> "Rook", PieceType.Rook -> "Rook",
PieceType.Queen -> "Queen", PieceType.Queen -> "Queen",
PieceType.King -> "King" PieceType.King -> "King",
) )
expectedLabels.foreach { (pieceType, expectedLabel) => expectedLabels.foreach { (pieceType, expectedLabel) =>
@@ -16,7 +16,7 @@ class SquareTest extends AnyFunSuite with Matchers:
"a1" -> Square(File.A, Rank.R1), "a1" -> Square(File.A, Rank.R1),
"e4" -> Square(File.E, Rank.R4), "e4" -> Square(File.E, Rank.R4),
"h8" -> Square(File.H, Rank.R8), "h8" -> Square(File.H, Rank.R8),
"E4" -> Square(File.E, Rank.R4) "E4" -> Square(File.E, Rank.R4),
) )
expected.foreach { case (raw, sq) => expected.foreach { case (raw, sq) =>
Square.fromAlgebraic(raw) shouldBe Some(sq) Square.fromAlgebraic(raw) shouldBe Some(sq)
@@ -34,4 +34,3 @@ class SquareTest extends AnyFunSuite with Matchers:
Square(File.A, Rank.R1).offset(-1, 0) shouldBe None Square(File.A, Rank.R1).offset(-1, 0) shouldBe None
Square(File.H, Rank.R8).offset(0, 1) shouldBe None Square(File.H, Rank.R8).offset(0, 1) shouldBe None
} }
@@ -2,6 +2,7 @@ package de.nowchess.api.game
import de.nowchess.api.board.{Board, CastlingRights, Color, File, Rank, Square} import de.nowchess.api.board.{Board, CastlingRights, Color, File, Rank, Square}
import de.nowchess.api.move.Move import de.nowchess.api.move.Move
import de.nowchess.api.game.{DrawReason, GameResult}
import org.scalatest.funsuite.AnyFunSuite import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers import org.scalatest.matchers.should.Matchers
@@ -16,11 +17,12 @@ class GameContextTest extends AnyFunSuite with Matchers:
initial.enPassantSquare shouldBe None initial.enPassantSquare shouldBe None
initial.halfMoveClock shouldBe 0 initial.halfMoveClock shouldBe 0
initial.moves shouldBe List.empty initial.moves shouldBe List.empty
initial.result shouldBe None
test("withBoard updates only board"): test("withBoard updates only board"):
val square = Square(File.E, Rank.R4) val square = Square(File.E, Rank.R4)
val updatedBoard = Board.initial.updated(square, de.nowchess.api.board.Piece.WhiteQueen) val updatedBoard = Board.initial.updated(square, de.nowchess.api.board.Piece.WhiteQueen)
val updated = GameContext.initial.withBoard(updatedBoard) val updated = GameContext.initial.withBoard(updatedBoard)
updated.board shouldBe updatedBoard updated.board shouldBe updatedBoard
updated.turn shouldBe GameContext.initial.turn updated.turn shouldBe GameContext.initial.turn
updated.castlingRights shouldBe GameContext.initial.castlingRights updated.castlingRights shouldBe GameContext.initial.castlingRights
@@ -34,13 +36,13 @@ class GameContextTest extends AnyFunSuite with Matchers:
whiteKingSide = true, whiteKingSide = true,
whiteQueenSide = false, whiteQueenSide = false,
blackKingSide = false, blackKingSide = false,
blackQueenSide = true blackQueenSide = true,
) )
val square = Some(Square(File.E, Rank.R3)) val square = Some(Square(File.E, Rank.R3))
val updatedTurn = initial.withTurn(Color.Black) val updatedTurn = initial.withTurn(Color.Black)
val updatedRights = initial.withCastlingRights(rights) val updatedRights = initial.withCastlingRights(rights)
val updatedEp = initial.withEnPassantSquare(square) val updatedEp = initial.withEnPassantSquare(square)
val updatedClock = initial.withHalfMoveClock(17) val updatedClock = initial.withHalfMoveClock(17)
updatedTurn.turn shouldBe Color.Black updatedTurn.turn shouldBe Color.Black
updatedTurn.board shouldBe initial.board updatedTurn.board shouldBe initial.board
@@ -58,3 +60,14 @@ class GameContextTest extends AnyFunSuite with Matchers:
val move = Move(Square(File.E, Rank.R2), Square(File.E, Rank.R4)) val move = Move(Square(File.E, Rank.R2), Square(File.E, Rank.R4))
GameContext.initial.withMove(move).moves shouldBe List(move) GameContext.initial.withMove(move).moves shouldBe List(move)
test("withResult sets Win result"):
val win = Some(GameResult.Win(Color.White))
GameContext.initial.withResult(win).result shouldBe win
test("withResult sets Draw result"):
val draw = Some(GameResult.Draw(DrawReason.Stalemate))
GameContext.initial.withResult(draw).result shouldBe draw
test("withResult clears result"):
val ctx = GameContext.initial.withResult(Some(GameResult.Win(Color.Black)))
ctx.withResult(None).result shouldBe None
@@ -25,7 +25,7 @@ class MoveTest extends AnyFunSuite with Matchers:
MoveType.Promotion(PromotionPiece.Queen), MoveType.Promotion(PromotionPiece.Queen),
MoveType.Promotion(PromotionPiece.Rook), MoveType.Promotion(PromotionPiece.Rook),
MoveType.Promotion(PromotionPiece.Bishop), MoveType.Promotion(PromotionPiece.Bishop),
MoveType.Promotion(PromotionPiece.Knight) MoveType.Promotion(PromotionPiece.Knight),
) )
moveTypes.foreach { moveType => moveTypes.foreach { moveType =>
@@ -7,12 +7,12 @@ class PlayerInfoTest extends AnyFunSuite with Matchers:
test("PlayerId and PlayerInfo preserve constructor values") { test("PlayerId and PlayerInfo preserve constructor values") {
val raw = "player-123" val raw = "player-123"
val id = PlayerId(raw) val id = PlayerId(raw)
id.value shouldBe raw id.value shouldBe raw
val playerId = PlayerId("p1") val playerId = PlayerId("p1")
val info = PlayerInfo(playerId, "Magnus") val info = PlayerInfo(playerId, "Magnus")
info.id.value shouldBe "p1" info.id.value shouldBe "p1"
info.displayName shouldBe "Magnus" info.displayName shouldBe "Magnus"
} }
@@ -14,9 +14,9 @@ class ApiResponseTest extends AnyFunSuite with Matchers:
ApiResponse.error(err) shouldBe ApiResponse.Failure(List(err)) ApiResponse.error(err) shouldBe ApiResponse.Failure(List(err))
val e = ApiError("CODE", "message") val e = ApiError("CODE", "message")
e.code shouldBe "CODE" e.code shouldBe "CODE"
e.message shouldBe "message" e.message shouldBe "message"
e.field shouldBe None e.field shouldBe None
ApiError("INVALID", "bad value", Some("email")).field shouldBe Some("email") ApiError("INVALID", "bad value", Some("email")).field shouldBe Some("email")
} }
@@ -31,6 +31,6 @@ class ApiResponseTest extends AnyFunSuite with Matchers:
test("PagedResponse holds items and pagination") { test("PagedResponse holds items and pagination") {
val pagination = Pagination(page = 1, pageSize = 5, totalItems = 20) val pagination = Pagination(page = 1, pageSize = 5, totalItems = 20)
val pr = PagedResponse(List("a", "b"), pagination) val pr = PagedResponse(List("a", "b"), pagination)
pr.items shouldBe List("a", "b") pr.items shouldBe List("a", "b")
pr.pagination shouldBe pagination pr.pagination shouldBe pagination
} }
+1 -1
View File
@@ -1,3 +1,3 @@
MAJOR=0 MAJOR=0
MINOR=2 MINOR=5
PATCH=0 PATCH=0
+101
View File
@@ -0,0 +1,101 @@
plugins {
id("scala")
id("org.scoverage") version "8.1"
id("io.quarkus")
id("jacoco")
}
@Suppress("UNCHECKED_CAST")
val versions = rootProject.extra["VERSIONS"] as Map<String, String>
repositories {
mavenCentral()
mavenLocal()
}
scala {
scalaVersion = versions["SCALA3"]!!
}
scoverage {
scoverageVersion.set(versions["SCOVERAGE"]!!)
}
tasks.withType<ScalaCompile> {
scalaCompileOptions.additionalParameters = listOf("-encoding", "UTF-8")
}
val quarkusPlatformGroupId: String by project
val quarkusPlatformArtifactId: String by project
val quarkusPlatformVersion: String by project
dependencies {
implementation(project(":modules:api"))
implementation(project(":modules:core"))
implementation(project(":modules:io"))
implementation(project(":modules:rule"))
implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))
implementation("io.quarkus:quarkus-rest")
implementation("io.quarkus:quarkus-rest-jackson")
implementation("io.quarkus:quarkus-config-yaml")
implementation("io.quarkus:quarkus-arc")
implementation("com.fasterxml.jackson.module:jackson-module-scala_3:${versions["JACKSON_SCALA"]!!}")
testImplementation(platform("org.junit:junit-bom:${versions["JUNIT_BOM"]!!}"))
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.scalatest:scalatest_3:${versions["SCALATEST"]!!}")
testImplementation("co.helmethair:scalatest-junit-runner:${versions["SCALATEST_JUNIT"]!!}")
testImplementation("io.quarkus:quarkus-junit5")
testImplementation("io.quarkus:quarkus-jacoco")
testImplementation("io.rest-assured:rest-assured")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
configurations.matching { !it.name.startsWith("scoverage") }.configureEach {
resolutionStrategy.force("org.scala-lang:scala-library:${versions["SCALA_LIBRARY"]!!}")
}
configurations.scoverage {
resolutionStrategy.eachDependency {
if (requested.group == "org.scoverage" && requested.name.startsWith("scalac-scoverage-plugin_")) {
useTarget("${requested.group}:scalac-scoverage-plugin_2.13.16:2.3.0")
}
}
}
group = "de.nowchess"
version = "1.0-SNAPSHOT"
tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
options.compilerArgs.add("-parameters")
}
tasks.withType<Jar>().configureEach {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
tasks.test {
useJUnitPlatform {
includeEngines("scalatest", "junit-jupiter")
}
testLogging {
events("passed", "skipped", "failed")
}
finalizedBy(tasks.named("jacocoTestReport"))
}
tasks.jacocoTestReport {
dependsOn(tasks.test)
executionData.setFrom(layout.buildDirectory.file("jacoco-quarkus.exec"))
sourceDirectories.setFrom(files("src/main/scala"))
classDirectories.setFrom(files(layout.buildDirectory.dir("classes/scala/main")))
reports {
xml.required.set(true)
xml.outputLocation.set(
layout.buildDirectory.file("reports/jacoco/test/jacocoTestReport.xml")
)
html.required.set(false)
}
}
@@ -0,0 +1,6 @@
quarkus:
http:
port: 8080
jacoco:
data-file: ${user.dir}/build/jacoco-quarkus.exec
report: false
@@ -0,0 +1,11 @@
package de.nowchess.backcore.config
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import io.quarkus.jackson.ObjectMapperCustomizer
import jakarta.inject.Singleton
@Singleton
class JacksonConfig extends ObjectMapperCustomizer:
def customize(mapper: ObjectMapper): Unit =
mapper.registerModule(DefaultScalaModule)
@@ -0,0 +1,57 @@
package de.nowchess.backcore.dto
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonInclude.Include
case class PlayerInfoDto(id: String, displayName: String)
case class GameStateResponse(
fen: String,
pgn: String,
turn: String,
status: String,
@JsonInclude(Include.NON_ABSENT) winner: Option[String],
moves: List[String],
undoAvailable: Boolean,
redoAvailable: Boolean,
)
case class GameFullResponse(
gameId: String,
white: PlayerInfoDto,
black: PlayerInfoDto,
state: GameStateResponse,
)
case class OkResponse(ok: Boolean = true)
@JsonInclude(Include.NON_ABSENT)
case class ApiErrorResponse(
code: String,
message: String,
field: Option[String] = None,
)
// Requests
case class CreateGameRequest(
white: Option[PlayerInfoDto] = None,
black: Option[PlayerInfoDto] = None,
)
case class ImportFenRequest(
fen: String = "",
white: Option[PlayerInfoDto] = None,
black: Option[PlayerInfoDto] = None,
)
case class ImportPgnRequest(pgn: String = "")
case class LegalMoveDto(
from: String,
to: String,
uci: String,
moveType: String,
@JsonInclude(Include.NON_ABSENT) promotion: Option[String] = None,
)
case class LegalMovesResponse(moves: List[LegalMoveDto])
@@ -0,0 +1,10 @@
package de.nowchess.backcore.game
import java.security.SecureRandom
object GameId:
private val chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
private val random = SecureRandom()
def generate(): String =
(1 to 8).map(_ => chars(random.nextInt(chars.length))).mkString
@@ -0,0 +1,80 @@
package de.nowchess.backcore.game
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import de.nowchess.api.board.Color
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
import de.nowchess.backcore.dto.*
import de.nowchess.io.fen.FenExporter
import de.nowchess.io.pgn.PgnExporter
import de.nowchess.rules.sets.DefaultRules
object GameMapper:
private val mapper = new ObjectMapper().registerModule(DefaultScalaModule)
def toGameFullJson(session: GameSession): String =
mapper.writeValueAsString(toGameFull(session))
def toGameFull(session: GameSession): GameFullResponse =
GameFullResponse(
gameId = session.gameId,
white = toPlayerInfo(session.white),
black = toPlayerInfo(session.black),
state = toGameState(session),
)
def toGameState(session: GameSession): GameStateResponse =
val (status, winner) = computeStatus(session)
GameStateResponse(
fen = FenExporter.exportGameContext(session.context),
pgn = buildPgn(session.context.moves),
turn = if session.context.turn == Color.White then "white" else "black",
status = status,
winner = winner,
moves = session.context.moves.map(moveToUci),
undoAvailable = session.invoker.canUndo,
redoAvailable = session.invoker.canRedo,
)
private def toPlayerInfo(p: de.nowchess.api.player.PlayerInfo): PlayerInfoDto =
PlayerInfoDto(id = p.id.value, displayName = p.displayName)
private def computeStatus(session: GameSession): (String, Option[String]) =
session.result match
case Some(GameResult.Checkmate(winner)) =>
val w = if winner == Color.White then "white" else "black"
("checkmate", Some(w))
case Some(GameResult.Stalemate) =>
("stalemate", None)
case Some(GameResult.Resign(winner)) =>
val w = if winner == Color.White then "white" else "black"
("resign", Some(w))
case Some(GameResult.AgreedDraw) | Some(GameResult.FiftyMoveDraw) =>
("draw", None)
case Some(GameResult.InsufficientMaterial) =>
("insufficientMaterial", None)
case None =>
computeLiveStatus(session)
private def computeLiveStatus(session: GameSession): (String, Option[String]) =
val ctx = session.context
if DefaultRules.isCheck(ctx) then ("check", None)
else if session.drawOfferedBy.isDefined then ("drawOffered", None)
else if DefaultRules.isFiftyMoveRule(ctx) then ("fiftyMoveAvailable", None)
else ("started", None)
def moveToUci(move: Move): String =
val base = s"${move.from}${move.to}"
move.moveType match
case MoveType.Promotion(piece) =>
val suffix = piece match
case PromotionPiece.Queen => "q"
case PromotionPiece.Rook => "r"
case PromotionPiece.Bishop => "b"
case PromotionPiece.Knight => "n"
base + suffix
case _ => base
private def buildPgn(moves: List[Move]): String =
// Use PgnExporter with no headers to get move-text only (SAN notation)
PgnExporter.exportGame(Map.empty, moves)
@@ -0,0 +1,12 @@
package de.nowchess.backcore.game
import de.nowchess.api.board.Color
sealed trait GameResult
object GameResult:
case class Checkmate(winner: Color) extends GameResult
case object Stalemate extends GameResult
case class Resign(winner: Color) extends GameResult
case object AgreedDraw extends GameResult
case object FiftyMoveDraw extends GameResult
case object InsufficientMaterial extends GameResult
@@ -0,0 +1,16 @@
package de.nowchess.backcore.game
import de.nowchess.api.board.Color
import de.nowchess.api.game.GameContext
import de.nowchess.api.player.PlayerInfo
import de.nowchess.chess.command.CommandInvoker
case class GameSession(
gameId: String,
white: PlayerInfo,
black: PlayerInfo,
context: GameContext,
invoker: CommandInvoker,
drawOfferedBy: Option[Color] = None,
result: Option[GameResult] = None,
)
@@ -0,0 +1,260 @@
package de.nowchess.backcore.game
import de.nowchess.api.board.{Color, Square}
import de.nowchess.api.game.GameContext
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
import de.nowchess.api.player.{PlayerId, PlayerInfo}
import de.nowchess.backcore.dto.{CreateGameRequest, ImportFenRequest, PlayerInfoDto}
import de.nowchess.chess.command.{CommandInvoker, MoveCommand, MoveResult}
import de.nowchess.io.fen.FenParser
import de.nowchess.io.pgn.PgnParser
import de.nowchess.rules.sets.DefaultRules
import jakarta.enterprise.context.ApplicationScoped
import scala.collection.mutable
@ApplicationScoped
class GameStore:
private val games: mutable.Map[String, GameSession] = mutable.Map.empty
// ─── Create / Get ────────────────────────────────────────────────
def create(req: CreateGameRequest): GameSession = synchronized:
val id = generateId()
val session = newSession(id, req.white, req.black, GameContext.initial)
games(id) = session
session
def get(id: String): Option[GameSession] = synchronized:
games.get(id)
// ─── Move-making ─────────────────────────────────────────────────
def applyMove(id: String, uci: String): Either[String, GameSession] = synchronized:
withSession(id): session =>
if session.result.isDefined then Left("Game is already over")
else
parseUci(uci) match
case None => Left(s"Invalid UCI notation: $uci")
case Some((from, to, promotion)) =>
val legalCandidates = DefaultRules.legalMoves(session.context)(from)
findMatchingMove(legalCandidates, to, promotion) match
case None => Left(s"$uci is not a legal move")
case Some(move) =>
val nextCtx = DefaultRules.applyMove(session.context)(move)
val prevCtx = session.context
val cmd = MoveCommand(
from = move.from,
to = move.to,
moveResult = Some(MoveResult.Successful(nextCtx, prevCtx.board.pieceAt(move.to))),
previousContext = Some(prevCtx),
)
session.invoker.execute(cmd)
val result = detectGameOver(nextCtx)
val updated = session.copy(context = nextCtx, result = result)
games(id) = updated
Right(updated)
def legalMoves(id: String, square: Option[Square]): Either[String, List[Move]] = synchronized:
withSession(id): session =>
val moves = square match
case Some(sq) => DefaultRules.legalMoves(session.context)(sq)
case None => DefaultRules.allLegalMoves(session.context)
Right(moves)
// ─── Undo / Redo ─────────────────────────────────────────────────
def undo(id: String): Either[String, GameSession] = synchronized:
withSession(id): session =>
if !session.invoker.canUndo then Left("No moves to undo")
else
val idx = session.invoker.getCurrentIndex
session.invoker.history(idx) match
case cmd: MoveCommand =>
cmd.previousContext match
case None => Left("Cannot undo: no previous context stored")
case Some(prevCtx) =>
session.invoker.undo()
val updated = session.copy(context = prevCtx, result = None, drawOfferedBy = None)
games(id) = updated
Right(updated)
case _ => Left("Cannot undo this command type")
def redo(id: String): Either[String, GameSession] = synchronized:
withSession(id): session =>
if !session.invoker.canRedo then Left("No moves to redo")
else
val idx = session.invoker.getCurrentIndex + 1
session.invoker.history(idx) match
case cmd: MoveCommand =>
cmd.moveResult match
case Some(MoveResult.Successful(nextCtx, _)) =>
session.invoker.redo()
val result = detectGameOver(nextCtx)
val updated = session.copy(context = nextCtx, result = result)
games(id) = updated
Right(updated)
case _ => Left("Cannot redo: move result not available")
case _ => Left("Cannot redo this command type")
// ─── Resign ──────────────────────────────────────────────────────
def resign(id: String): Either[String, GameSession] = synchronized:
withSession(id): session =>
if session.result.isDefined then Left("Game is already over")
else
val winner = session.context.turn.opposite
val updated = session.copy(result = Some(GameResult.Resign(winner)))
games(id) = updated
Right(updated)
// ─── Draw actions ────────────────────────────────────────────────
def drawAction(id: String, action: String): Either[String, GameSession] = synchronized:
withSession(id): session =>
if session.result.isDefined then Left("Game is already over")
else
action match
case "offer" =>
val updated = session.copy(drawOfferedBy = Some(session.context.turn))
games(id) = updated
Right(updated)
case "accept" =>
session.drawOfferedBy match
case None => Left("No draw offer to accept")
case Some(offerer) if offerer == session.context.turn =>
Left("Cannot accept your own draw offer")
case Some(_) =>
val updated = session.copy(result = Some(GameResult.AgreedDraw), drawOfferedBy = None)
games(id) = updated
Right(updated)
case "decline" =>
session.drawOfferedBy match
case None => Left("No draw offer to decline")
case Some(_) =>
val updated = session.copy(drawOfferedBy = None)
games(id) = updated
Right(updated)
case "claim" =>
if DefaultRules.isFiftyMoveRule(session.context) then
val updated = session.copy(result = Some(GameResult.FiftyMoveDraw))
games(id) = updated
Right(updated)
else Left("Fifty-move rule has not been triggered")
case other => Left(s"Unknown draw action: $other")
// ─── Import ──────────────────────────────────────────────────────
def importFen(req: ImportFenRequest): Either[String, GameSession] = synchronized:
FenParser.parseFen(req.fen) match
case Left(err) => Left(err)
case Right(ctx) =>
val id = generateId()
val session = newSession(id, req.white, req.black, ctx)
games(id) = session
Right(session)
def importPgn(pgn: String, white: Option[PlayerInfoDto], black: Option[PlayerInfoDto]): Either[String, GameSession] =
synchronized:
PgnParser.validatePgn(pgn) match
case Left(err) => Left(err)
case Right(game) =>
val id = generateId()
val session = newSession(id, white, black, GameContext.initial)
replayIntoSession(session, game.moves, GameContext.initial) match
case Left(err) => Left(err)
case Right(s) =>
games(id) = s
Right(s)
// ─── Private helpers ─────────────────────────────────────────────
private def withSession[A](id: String)(f: GameSession => Either[String, A]): Either[String, A] =
games.get(id) match
case None => Left(s"Game $id not found")
case Some(session) => f(session)
private def generateId(): String =
var id = GameId.generate()
while games.contains(id) do id = GameId.generate()
id
private def newSession(
id: String,
white: Option[PlayerInfoDto],
black: Option[PlayerInfoDto],
ctx: GameContext,
): GameSession =
GameSession(
gameId = id,
white = toPlayerInfo(white, "white", "White"),
black = toPlayerInfo(black, "black", "Black"),
context = ctx,
invoker = new CommandInvoker(),
)
private def toPlayerInfo(dto: Option[PlayerInfoDto], defaultId: String, defaultName: String): PlayerInfo =
dto.fold(PlayerInfo(PlayerId(defaultId), defaultName))(d => PlayerInfo(PlayerId(d.id), d.displayName))
private def parseUci(uci: String): Option[(Square, Square, Option[PromotionPiece])] =
if uci.length < 4 || uci.length > 5 then None
else
for
from <- Square.fromAlgebraic(uci.substring(0, 2))
to <- Square.fromAlgebraic(uci.substring(2, 4))
yield
val promotion = if uci.length == 5 then parsePromotionChar(uci.charAt(4)) else None
(from, to, promotion)
private def parsePromotionChar(c: Char): Option[PromotionPiece] =
c match
case 'q' => Some(PromotionPiece.Queen)
case 'r' => Some(PromotionPiece.Rook)
case 'b' => Some(PromotionPiece.Bishop)
case 'n' => Some(PromotionPiece.Knight)
case _ => None
private def findMatchingMove(
candidates: List[Move],
to: Square,
promotion: Option[PromotionPiece],
): Option[Move] =
candidates.filter(_.to == to) match
case Nil => None
case moves =>
promotion match
case Some(pp) => moves.find(_.moveType == MoveType.Promotion(pp))
case None =>
moves
.find(m => !m.moveType.isInstanceOf[MoveType.Promotion])
.orElse(moves.headOption)
private def detectGameOver(ctx: GameContext): Option[GameResult] =
if DefaultRules.isCheckmate(ctx) then Some(GameResult.Checkmate(ctx.turn.opposite))
else if DefaultRules.isStalemate(ctx) then Some(GameResult.Stalemate)
else if DefaultRules.isInsufficientMaterial(ctx) then Some(GameResult.InsufficientMaterial)
else None
private def replayIntoSession(
session: GameSession,
moves: List[Move],
startCtx: GameContext,
): Either[String, GameSession] =
moves.foldLeft[Either[String, GameSession]](Right(session)):
case (Left(err), _) => Left(err)
case (Right(s), move) =>
val legal = DefaultRules.legalMoves(s.context)(move.from)
legal
.find(m => m.from == move.from && m.to == move.to && m.moveType == move.moveType)
.orElse(legal.find(m => m.from == move.from && m.to == move.to)) match
case None => Left(s"Illegal move in PGN: $move")
case Some(legalMove) =>
val nextCtx = DefaultRules.applyMove(s.context)(legalMove)
val cmd = MoveCommand(
from = legalMove.from,
to = legalMove.to,
moveResult = Some(MoveResult.Successful(nextCtx, s.context.board.pieceAt(legalMove.to))),
previousContext = Some(s.context),
)
s.invoker.execute(cmd)
Right(s.copy(context = nextCtx))
@@ -0,0 +1,99 @@
package de.nowchess.backcore.resource
import de.nowchess.backcore.dto.*
import de.nowchess.backcore.game.{GameMapper, GameStore}
import jakarta.enterprise.context.ApplicationScoped
import jakarta.inject.Inject
import jakarta.ws.rs.*
import jakarta.ws.rs.core.{MediaType, Response}
@Path("/api/board/game")
@Produces(Array(MediaType.APPLICATION_JSON))
@ApplicationScoped
class GameResource @Inject() (store: GameStore):
@POST
@Consumes(Array(MediaType.APPLICATION_JSON))
def createGame(req: CreateGameRequest): Response =
val session = store.create(Option(req).getOrElse(CreateGameRequest()))
Response.status(201).entity(GameMapper.toGameFull(session)).build()
@GET
@Path("/{gameId}")
def getGame(@PathParam("gameId") gameId: String): Response =
store.get(gameId) match
case Some(session) => Response.ok(GameMapper.toGameFull(session)).build()
case None =>
Response
.status(404)
.entity(ApiErrorResponse("GAME_NOT_FOUND", s"Game $gameId not found"))
.build()
@GET
@Path("/{gameId}/stream")
@Produces(Array("application/x-ndjson"))
def streamGame(@PathParam("gameId") gameId: String): Response =
store.get(gameId) match
case None =>
Response
.status(404)
.`type`(MediaType.APPLICATION_JSON)
.entity(ApiErrorResponse("GAME_NOT_FOUND", s"Game $gameId not found"))
.build()
case Some(session) =>
// Simplified: return a single-line NDJSON snapshot of the current game state
val event = s"""{"type":"gameFull","game":${GameMapper.toGameFullJson(session)}}"""
Response.ok(event + "\n").build()
@POST
@Path("/{gameId}/resign")
def resignGame(@PathParam("gameId") gameId: String): Response =
store.resign(gameId) match
case Right(_) => Response.ok(OkResponse()).build()
case Left(err) if err.contains("not found") =>
Response.status(404).entity(ApiErrorResponse("GAME_NOT_FOUND", err)).build()
case Left(err) =>
Response.status(400).entity(ApiErrorResponse("RESIGN_ERROR", err)).build()
@POST
@Path("/{gameId}/draw/{action}")
def drawAction(
@PathParam("gameId") gameId: String,
@PathParam("action") action: String,
): Response =
store.drawAction(gameId, action) match
case Right(_) => Response.ok(OkResponse()).build()
case Left(err) if err.contains("not found") =>
Response.status(404).entity(ApiErrorResponse("GAME_NOT_FOUND", err)).build()
case Left(err) =>
Response.status(400).entity(ApiErrorResponse("DRAW_ERROR", err)).build()
@GET
@Path("/{gameId}/export/fen")
@Produces(Array(MediaType.TEXT_PLAIN))
def exportFen(@PathParam("gameId") gameId: String): Response =
store.get(gameId) match
case None =>
Response
.status(404)
.`type`(MediaType.APPLICATION_JSON)
.entity(ApiErrorResponse("GAME_NOT_FOUND", s"Game $gameId not found"))
.build()
case Some(session) =>
import de.nowchess.io.fen.FenExporter
Response.ok(FenExporter.exportGameContext(session.context)).build()
@GET
@Path("/{gameId}/export/pgn")
@Produces(Array("application/x-chess-pgn"))
def exportPgn(@PathParam("gameId") gameId: String): Response =
store.get(gameId) match
case None =>
Response
.status(404)
.`type`(MediaType.APPLICATION_JSON)
.entity(ApiErrorResponse("GAME_NOT_FOUND", s"Game $gameId not found"))
.build()
case Some(session) =>
import de.nowchess.io.pgn.PgnExporter
Response.ok(PgnExporter.exportGameContext(session.context)).build()
@@ -0,0 +1,29 @@
package de.nowchess.backcore.resource
import de.nowchess.backcore.dto.{ApiErrorResponse, ImportFenRequest, ImportPgnRequest}
import de.nowchess.backcore.game.{GameMapper, GameStore}
import jakarta.enterprise.context.ApplicationScoped
import jakarta.inject.Inject
import jakarta.ws.rs.*
import jakarta.ws.rs.core.{MediaType, Response}
@Path("/api/board/game/import")
@Produces(Array(MediaType.APPLICATION_JSON))
@Consumes(Array(MediaType.APPLICATION_JSON))
@ApplicationScoped
class ImportResource @Inject() (store: GameStore):
@POST
@Path("/fen")
def importFen(req: ImportFenRequest): Response =
store.importFen(Option(req).getOrElse(ImportFenRequest())) match
case Right(session) => Response.status(201).entity(GameMapper.toGameFull(session)).build()
case Left(err) => Response.status(400).entity(ApiErrorResponse("INVALID_FEN", err)).build()
@POST
@Path("/pgn")
def importPgn(req: ImportPgnRequest): Response =
val body = Option(req).getOrElse(ImportPgnRequest())
store.importPgn(body.pgn, None, None) match
case Right(session) => Response.status(201).entity(GameMapper.toGameFull(session)).build()
case Left(err) => Response.status(400).entity(ApiErrorResponse("INVALID_PGN", err)).build()
@@ -0,0 +1,87 @@
package de.nowchess.backcore.resource
import de.nowchess.api.board.Square
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
import de.nowchess.backcore.dto.*
import de.nowchess.backcore.game.{GameMapper, GameStore}
import jakarta.enterprise.context.ApplicationScoped
import jakarta.inject.Inject
import jakarta.ws.rs.*
import jakarta.ws.rs.core.{MediaType, Response}
@Path("/api/board/game")
@Produces(Array(MediaType.APPLICATION_JSON))
@ApplicationScoped
class MoveResource @Inject() (store: GameStore):
@POST
@Path("/{gameId}/move/{uci}")
def makeMove(
@PathParam("gameId") gameId: String,
@PathParam("uci") uci: String,
): Response =
store.applyMove(gameId, uci) match
case Right(session) => Response.ok(GameMapper.toGameState(session)).build()
case Left(err) if err.contains("not found") =>
Response.status(404).entity(ApiErrorResponse("GAME_NOT_FOUND", err)).build()
case Left(err) =>
Response.status(400).entity(ApiErrorResponse("INVALID_MOVE", err)).build()
@GET
@Path("/{gameId}/moves")
def getLegalMoves(
@PathParam("gameId") gameId: String,
@QueryParam("square") squareParam: String,
): Response =
val square = Option(squareParam).flatMap(Square.fromAlgebraic)
store.legalMoves(gameId, square) match
case Right(moves) =>
val dtos = moves.map(toLegalMoveDto)
Response.ok(LegalMovesResponse(dtos)).build()
case Left(err) if err.contains("not found") =>
Response.status(404).entity(ApiErrorResponse("GAME_NOT_FOUND", err)).build()
case Left(err) =>
Response.status(400).entity(ApiErrorResponse("ERROR", err)).build()
@POST
@Path("/{gameId}/undo")
def undoMove(@PathParam("gameId") gameId: String): Response =
store.undo(gameId) match
case Right(session) => Response.ok(GameMapper.toGameState(session)).build()
case Left(err) if err.contains("not found") =>
Response.status(404).entity(ApiErrorResponse("GAME_NOT_FOUND", err)).build()
case Left(err) =>
Response.status(400).entity(ApiErrorResponse("UNDO_NOT_AVAILABLE", err)).build()
@POST
@Path("/{gameId}/redo")
def redoMove(@PathParam("gameId") gameId: String): Response =
store.redo(gameId) match
case Right(session) => Response.ok(GameMapper.toGameState(session)).build()
case Left(err) if err.contains("not found") =>
Response.status(404).entity(ApiErrorResponse("GAME_NOT_FOUND", err)).build()
case Left(err) =>
Response.status(400).entity(ApiErrorResponse("REDO_NOT_AVAILABLE", err)).build()
private def toLegalMoveDto(move: Move): LegalMoveDto =
val uci = GameMapper.moveToUci(move)
val (moveType, promotion) = move.moveType match
case MoveType.Normal(true) => ("capture", None)
case MoveType.Normal(false) => ("normal", None)
case MoveType.CastleKingside => ("castleKingside", None)
case MoveType.CastleQueenside => ("castleQueenside", None)
case MoveType.EnPassant => ("enPassant", None)
case MoveType.Promotion(pp) =>
val pName = pp match
case PromotionPiece.Queen => "queen"
case PromotionPiece.Rook => "rook"
case PromotionPiece.Bishop => "bishop"
case PromotionPiece.Knight => "knight"
("promotion", Some(pName))
LegalMoveDto(
from = move.from.toString,
to = move.to.toString,
uci = uci,
moveType = moveType,
promotion = promotion,
)
@@ -0,0 +1,11 @@
package de.nowchess.backcore
import io.quarkus.test.junit.QuarkusTest
import org.junit.jupiter.api.Test
@QuarkusTest
class BackcoreStartupTest:
@Test
def applicationStarts(): Unit =
// If we get here the Quarkus container started successfully
()
@@ -0,0 +1,67 @@
package de.nowchess.backcore.resource
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import org.hamcrest.Matchers.{equalTo, matchesPattern, notNullValue}
import org.junit.jupiter.api.Test
@QuarkusTest
class GameResourceTest:
@Test
def createGameReturns201WithGameId(): Unit =
RestAssured
.`given`()
.contentType("application/json")
.body("{}")
.when()
.post("/api/board/game")
.`then`()
.statusCode(201)
.body("gameId", matchesPattern("[A-Za-z0-9]{8}"))
.body("state.fen", notNullValue())
.body("state.turn", equalTo("white"))
.body("state.status", equalTo("started"))
@Test
def createGameWithPlayersReturns201(): Unit =
RestAssured
.`given`()
.contentType("application/json")
.body("""{"white":{"id":"p1","displayName":"Alice"},"black":{"id":"p2","displayName":"Bob"}}""")
.when()
.post("/api/board/game")
.`then`()
.statusCode(201)
.body("white.id", equalTo("p1"))
.body("black.displayName", equalTo("Bob"))
@Test
def getGameReturns200ForExistingGame(): Unit =
val gameId = RestAssured
.`given`()
.contentType("application/json")
.body("{}")
.when()
.post("/api/board/game")
.`then`()
.statusCode(201)
.extract()
.path[String]("gameId")
RestAssured
.`given`()
.when()
.get(s"/api/board/game/$gameId")
.`then`()
.statusCode(200)
.body("gameId", equalTo(gameId))
@Test
def getGameReturns404ForUnknownId(): Unit =
RestAssured
.`given`()
.when()
.get("/api/board/game/XXXXXXXX")
.`then`()
.statusCode(404)
@@ -0,0 +1,177 @@
package de.nowchess.backcore.resource
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import org.hamcrest.Matchers.{containsString, equalTo, matchesPattern, notNullValue}
import org.junit.jupiter.api.Test
@QuarkusTest
class ImportExportTest:
private val startFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
// ─── Import FEN ────────────────────────────────────────────────
@Test
def importFenReturns201WithCorrectPosition(): Unit =
RestAssured
.`given`()
.contentType("application/json")
.body(s"""{"fen":"$startFen"}""")
.when()
.post("/api/board/game/import/fen")
.`then`()
.statusCode(201)
.body("gameId", matchesPattern("[A-Za-z0-9]{8}"))
.body("state.fen", equalTo(startFen))
.body("state.turn", equalTo("white"))
@Test
def importFenWithCustomPositionWorks(): Unit =
val fen = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"
RestAssured
.`given`()
.contentType("application/json")
.body(s"""{"fen":"$fen"}""")
.when()
.post("/api/board/game/import/fen")
.`then`()
.statusCode(201)
.body("state.fen", equalTo(fen))
.body("state.turn", equalTo("black"))
@Test
def importFenWithInvalidFenReturns400(): Unit =
RestAssured
.`given`()
.contentType("application/json")
.body("""{"fen":"not-a-fen"}""")
.when()
.post("/api/board/game/import/fen")
.`then`()
.statusCode(400)
// ─── Import PGN ────────────────────────────────────────────────
@Test
def importPgnReturns201(): Unit =
RestAssured
.`given`()
.contentType("application/json")
.body("""{"pgn":"1. e4 e5 2. Nf3 Nc6 *"}""")
.when()
.post("/api/board/game/import/pgn")
.`then`()
.statusCode(201)
.body("gameId", matchesPattern("[A-Za-z0-9]{8}"))
.body("state.turn", equalTo("white"))
@Test
def importPgnWithInvalidPgnReturns400(): Unit =
RestAssured
.`given`()
.contentType("application/json")
.body("""{"pgn":"1. z9 *"}""")
.when()
.post("/api/board/game/import/pgn")
.`then`()
.statusCode(400)
// ─── Export FEN ────────────────────────────────────────────────
@Test
def exportFenReturnsStartingFen(): Unit =
val gameId = RestAssured
.`given`()
.contentType("application/json")
.body("{}")
.when()
.post("/api/board/game")
.`then`()
.statusCode(201)
.extract()
.path[String]("gameId")
RestAssured
.`given`()
.when()
.get(s"/api/board/game/$gameId/export/fen")
.`then`()
.statusCode(200)
.body(equalTo(startFen))
@Test
def exportFenOnUnknownGameReturns404(): Unit =
RestAssured
.`given`()
.when()
.get("/api/board/game/XXXXXXXX/export/fen")
.`then`()
.statusCode(404)
// ─── Export PGN ────────────────────────────────────────────────
@Test
def exportPgnReturnsText(): Unit =
val gameId = RestAssured
.`given`()
.contentType("application/json")
.body("{}")
.when()
.post("/api/board/game")
.`then`()
.statusCode(201)
.extract()
.path[String]("gameId")
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/move/e2e4")
.`then`()
.statusCode(200)
RestAssured
.`given`()
.when()
.get(s"/api/board/game/$gameId/export/pgn")
.`then`()
.statusCode(200)
.body(containsString("e4"))
// ─── Stream ────────────────────────────────────────────────────
@Test
def streamReturnsNdjsonSnapshot(): Unit =
val gameId = RestAssured
.`given`()
.contentType("application/json")
.body("{}")
.when()
.post("/api/board/game")
.`then`()
.statusCode(201)
.extract()
.path[String]("gameId")
val body = RestAssured
.`given`()
.when()
.get(s"/api/board/game/$gameId/stream")
.`then`()
.statusCode(200)
.contentType("application/x-ndjson")
.extract()
.body()
.asString()
assert(body.trim.startsWith("""{"type":"gameFull""""), s"Expected gameFull event, got: $body")
@Test
def streamOnUnknownGameReturns404(): Unit =
RestAssured
.`given`()
.when()
.get("/api/board/game/XXXXXXXX/stream")
.`then`()
.statusCode(404)
@@ -0,0 +1,84 @@
package de.nowchess.backcore.resource
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import org.hamcrest.Matchers.{containsString, empty, equalTo, hasItem, hasItems, not, notNullValue}
import org.junit.jupiter.api.Test
@QuarkusTest
class MoveResourceTest:
private def createGame(): String =
RestAssured
.`given`()
.contentType("application/json")
.body("{}")
.when()
.post("/api/board/game")
.`then`()
.statusCode(201)
.extract()
.path[String]("gameId")
@Test
def makeMoveReturns200WithUpdatedFen(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/move/e2e4")
.`then`()
.statusCode(200)
.body("fen", containsString("4P3")) // e4 pawn present in FEN
.body("turn", equalTo("black"))
.body("moves", hasItem("e2e4"))
@Test
def makeMoveOnUnknownGameReturns404(): Unit =
RestAssured
.`given`()
.when()
.post("/api/board/game/XXXXXXXX/move/e2e4")
.`then`()
.statusCode(404)
@Test
def illegalMoveReturns400(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/move/e2e5") // illegal — pawns can't jump 3 squares
.`then`()
.statusCode(400)
@Test
def getLegalMovesReturnsNonEmptyList(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.get(s"/api/board/game/$gameId/moves")
.`then`()
.statusCode(200)
.body("moves", not(empty()))
@Test
def getLegalMovesFilteredBySquareReturnsCorrectMoves(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.get(s"/api/board/game/$gameId/moves?square=e2")
.`then`()
.statusCode(200)
.body("moves.uci", hasItems("e2e3", "e2e4"))
@Test
def getLegalMovesOnUnknownGameReturns404(): Unit =
RestAssured
.`given`()
.when()
.get("/api/board/game/XXXXXXXX/moves")
.`then`()
.statusCode(404)
@@ -0,0 +1,168 @@
package de.nowchess.backcore.resource
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import org.hamcrest.Matchers.{equalTo, notNullValue}
import org.junit.jupiter.api.Test
@QuarkusTest
class ResignDrawTest:
private def createGame(): String =
RestAssured
.`given`()
.contentType("application/json")
.body("{}")
.when()
.post("/api/board/game")
.`then`()
.statusCode(201)
.extract()
.path[String]("gameId")
// ─── Resign ────────────────────────────────────────────────────
@Test
def resignReturns200(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/resign")
.`then`()
.statusCode(200)
.body("ok", equalTo(true))
@Test
def afterResignGameShowsResignStatusAndWinner(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/resign")
.`then`()
.statusCode(200)
RestAssured
.`given`()
.when()
.get(s"/api/board/game/$gameId")
.`then`()
.statusCode(200)
.body("state.status", equalTo("resign"))
.body("state.winner", notNullValue())
@Test
def resignOnUnknownGameReturns404(): Unit =
RestAssured
.`given`()
.when()
.post("/api/board/game/XXXXXXXX/resign")
.`then`()
.statusCode(404)
// ─── Draw ──────────────────────────────────────────────────────
@Test
def offerDrawSetsDrawOfferedStatus(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/draw/offer")
.`then`()
.statusCode(200)
.body("ok", equalTo(true))
RestAssured
.`given`()
.when()
.get(s"/api/board/game/$gameId")
.`then`()
.statusCode(200)
.body("state.status", equalTo("drawOffered"))
@Test
def acceptDrawAfterOfferSetsDrawStatus(): Unit =
val gameId = createGame()
// White offers
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/draw/offer")
.`then`()
.statusCode(200)
// Black moves so it's black's turn... actually the API doesn't enforce turn-based draw accept.
// White offered, so black (opponent) accepts — but since there's no auth, we just call accept.
// The GameStore checks drawOfferedBy != turn to allow accept.
// White offered on white's turn, so black needs to accept — but current turn is still white.
// We need to make a move first to switch turns.
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/move/e2e4")
.`then`()
.statusCode(200)
// Now it's black's turn and white offered the draw — black accepts
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/draw/accept")
.`then`()
.statusCode(200)
.body("ok", equalTo(true))
RestAssured
.`given`()
.when()
.get(s"/api/board/game/$gameId")
.`then`()
.statusCode(200)
.body("state.status", equalTo("draw"))
@Test
def declineDrawClearsOffer(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/draw/offer")
.`then`()
.statusCode(200)
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/draw/decline")
.`then`()
.statusCode(200)
.body("ok", equalTo(true))
RestAssured
.`given`()
.when()
.get(s"/api/board/game/$gameId")
.`then`()
.statusCode(200)
.body("state.status", equalTo("started"))
@Test
def acceptWithoutOfferReturns400(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/draw/accept")
.`then`()
.statusCode(400)
@Test
def drawOnUnknownGameReturns404(): Unit =
RestAssured
.`given`()
.when()
.post("/api/board/game/XXXXXXXX/draw/offer")
.`then`()
.statusCode(404)
@@ -0,0 +1,88 @@
package de.nowchess.backcore.resource
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import org.hamcrest.Matchers.{containsString, equalTo}
import org.junit.jupiter.api.Test
@QuarkusTest
class UndoRedoTest:
private val initialFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
private def createGame(): String =
RestAssured
.`given`()
.contentType("application/json")
.body("{}")
.when()
.post("/api/board/game")
.`then`()
.statusCode(201)
.extract()
.path[String]("gameId")
@Test
def undoAfterMoveRestoresOriginalPosition(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/move/e2e4")
.`then`()
.statusCode(200)
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/undo")
.`then`()
.statusCode(200)
.body("fen", equalTo(initialFen))
.body("undoAvailable", equalTo(false))
@Test
def redoAfterUndoRestoresMovedPosition(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/move/e2e4")
.`then`()
.statusCode(200)
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/undo")
.`then`()
.statusCode(200)
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/redo")
.`then`()
.statusCode(200)
.body("fen", containsString("4P3"))
.body("turn", equalTo("black"))
@Test
def undoWithNoHistoryReturns400(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/undo")
.`then`()
.statusCode(400)
@Test
def redoWithNoRedoStackReturns400(): Unit =
val gameId = createGame()
RestAssured
.`given`()
.when()
.post(s"/api/board/game/$gameId/redo")
.`then`()
.statusCode(400)
+72
View File
@@ -188,3 +188,75 @@
* correct test board positions and captureOutput/withInput interaction ([f0481e2](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/f0481e2561b779df00925b46ee281dc36a795150)) * correct test board positions and captureOutput/withInput interaction ([f0481e2](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/f0481e2561b779df00925b46ee281dc36a795150))
* update main class path in build configuration and adjust VCS directory mapping ([7b1f8b1](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7b1f8b117623d327232a1a92a8a44d18582e0189)) * update main class path in build configuration and adjust VCS directory mapping ([7b1f8b1](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7b1f8b117623d327232a1a92a8a44d18582e0189))
* update move validation to check for king safety ([#13](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/13)) ([e5e20c5](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/e5e20c566e368b12ca1dc59680c34e9112bf6762)) * update move validation to check for king safety ([#13](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/13)) ([e5e20c5](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/e5e20c566e368b12ca1dc59680c34e9112bf6762))
## (2026-04-07)
### Features
* add GameRules stub with PositionStatus enum ([76d4168](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/76d4168038de23e5d6083d4e8f0504fbf31d15a3))
* add MovedInCheck/Checkmate/Stalemate MoveResult variants (stub dispatch) ([8b7ec57](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/8b7ec57e5ea6ee1615a1883848a426dc07d26364))
* implement GameRules with isInCheck, legalMoves, gameStatus ([94a02ff](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/94a02ff6849436d9496c70a0f16c21666dae8e4e))
* implement legal castling ([#1](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/1)) ([00d326c](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/00d326c1ba67711fbe180f04e1100c3f01dd0254))
* NCS-10 Implement Pawn Promotion ([#12](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/12)) ([13bfc16](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/13bfc16cfe25db78ec607db523ca6d993c13430c))
* NCS-11 50-move rule ([#9](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/9)) ([412ed98](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/412ed986a95703a3b282276540153480ceed229d))
* NCS-16 Core Separation via Patterns ([#10](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/10)) ([1361dfc](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/1361dfc89553b146864fb8ff3526cf12cf3f293a))
* NCS-17 Implement basic ScalaFX UI ([#14](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/14)) ([3ff8031](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/3ff80318b4f16c59733a46498581a5c27f048287))
* NCS-21 Write Scripts to automate certain tasks ([#15](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/15)) ([8051871](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/80518719d536a087d339fe02530825dc07f8b388))
* NCS-6 Implementing FEN & PGN ([#7](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/7)) ([f28e69d](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/f28e69dc181416aa2f221fdc4b45c2cda5efbf07))
* NCS-9 En passant implementation ([#8](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/8)) ([919beb3](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/919beb3b4bfa8caf2f90976a415fe9b19b7e9747))
* wire check/checkmate/stalemate into processMove and gameLoop ([5264a22](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/5264a225418b885c5e6ea6411b96f85e38837f6c))
### Bug Fixes
* add missing kings to gameLoop capture test board ([aedd787](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/aedd787b77203c2af934751dba7b784eaf165032))
* correct test board positions and captureOutput/withInput interaction ([f0481e2](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/f0481e2561b779df00925b46ee281dc36a795150))
* update main class path in build configuration and adjust VCS directory mapping ([7b1f8b1](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7b1f8b117623d327232a1a92a8a44d18582e0189))
* update move validation to check for king safety ([#13](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/13)) ([e5e20c5](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/e5e20c566e368b12ca1dc59680c34e9112bf6762))
## (2026-04-12)
### Features
* add GameRules stub with PositionStatus enum ([76d4168](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/76d4168038de23e5d6083d4e8f0504fbf31d15a3))
* add MovedInCheck/Checkmate/Stalemate MoveResult variants (stub dispatch) ([8b7ec57](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/8b7ec57e5ea6ee1615a1883848a426dc07d26364))
* implement GameRules with isInCheck, legalMoves, gameStatus ([94a02ff](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/94a02ff6849436d9496c70a0f16c21666dae8e4e))
* implement legal castling ([#1](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/1)) ([00d326c](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/00d326c1ba67711fbe180f04e1100c3f01dd0254))
* NCS-10 Implement Pawn Promotion ([#12](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/12)) ([13bfc16](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/13bfc16cfe25db78ec607db523ca6d993c13430c))
* NCS-11 50-move rule ([#9](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/9)) ([412ed98](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/412ed986a95703a3b282276540153480ceed229d))
* NCS-16 Core Separation via Patterns ([#10](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/10)) ([1361dfc](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/1361dfc89553b146864fb8ff3526cf12cf3f293a))
* NCS-17 Implement basic ScalaFX UI ([#14](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/14)) ([3ff8031](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/3ff80318b4f16c59733a46498581a5c27f048287))
* NCS-21 Write Scripts to automate certain tasks ([#15](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/15)) ([8051871](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/80518719d536a087d339fe02530825dc07f8b388))
* NCS-25 Add linters to keep quality up ([#27](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/27)) ([fd4e67d](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/fd4e67d4f782a7e955822d90cb909d0a81676fb2))
* NCS-6 Implementing FEN & PGN ([#7](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/7)) ([f28e69d](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/f28e69dc181416aa2f221fdc4b45c2cda5efbf07))
* NCS-9 En passant implementation ([#8](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/8)) ([919beb3](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/919beb3b4bfa8caf2f90976a415fe9b19b7e9747))
* wire check/checkmate/stalemate into processMove and gameLoop ([5264a22](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/5264a225418b885c5e6ea6411b96f85e38837f6c))
### Bug Fixes
* add missing kings to gameLoop capture test board ([aedd787](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/aedd787b77203c2af934751dba7b784eaf165032))
* correct test board positions and captureOutput/withInput interaction ([f0481e2](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/f0481e2561b779df00925b46ee281dc36a795150))
* update main class path in build configuration and adjust VCS directory mapping ([7b1f8b1](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7b1f8b117623d327232a1a92a8a44d18582e0189))
* update move validation to check for king safety ([#13](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/13)) ([e5e20c5](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/e5e20c566e368b12ca1dc59680c34e9112bf6762))
## (2026-04-14)
### Features
* add GameRules stub with PositionStatus enum ([76d4168](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/76d4168038de23e5d6083d4e8f0504fbf31d15a3))
* add MovedInCheck/Checkmate/Stalemate MoveResult variants (stub dispatch) ([8b7ec57](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/8b7ec57e5ea6ee1615a1883848a426dc07d26364))
* implement GameRules with isInCheck, legalMoves, gameStatus ([94a02ff](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/94a02ff6849436d9496c70a0f16c21666dae8e4e))
* implement legal castling ([#1](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/1)) ([00d326c](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/00d326c1ba67711fbe180f04e1100c3f01dd0254))
* NCS-10 Implement Pawn Promotion ([#12](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/12)) ([13bfc16](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/13bfc16cfe25db78ec607db523ca6d993c13430c))
* NCS-11 50-move rule ([#9](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/9)) ([412ed98](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/412ed986a95703a3b282276540153480ceed229d))
* NCS-14 implemented insufficient moves rule ([#30](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/30)) ([b0399a4](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/b0399a4e489950083066c9538df9a84dcc7a4613))
* NCS-16 Core Separation via Patterns ([#10](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/10)) ([1361dfc](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/1361dfc89553b146864fb8ff3526cf12cf3f293a))
* NCS-17 Implement basic ScalaFX UI ([#14](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/14)) ([3ff8031](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/3ff80318b4f16c59733a46498581a5c27f048287))
* NCS-21 Write Scripts to automate certain tasks ([#15](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/15)) ([8051871](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/80518719d536a087d339fe02530825dc07f8b388))
* NCS-25 Add linters to keep quality up ([#27](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/27)) ([fd4e67d](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/fd4e67d4f782a7e955822d90cb909d0a81676fb2))
* NCS-6 Implementing FEN & PGN ([#7](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/7)) ([f28e69d](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/f28e69dc181416aa2f221fdc4b45c2cda5efbf07))
* NCS-9 En passant implementation ([#8](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/8)) ([919beb3](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/919beb3b4bfa8caf2f90976a415fe9b19b7e9747))
* wire check/checkmate/stalemate into processMove and gameLoop ([5264a22](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/5264a225418b885c5e6ea6411b96f85e38837f6c))
### Bug Fixes
* add missing kings to gameLoop capture test board ([aedd787](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/aedd787b77203c2af934751dba7b784eaf165032))
* correct test board positions and captureOutput/withInput interaction ([f0481e2](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/f0481e2561b779df00925b46ee281dc36a795150))
* update main class path in build configuration and adjust VCS directory mapping ([7b1f8b1](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7b1f8b117623d327232a1a92a8a44d18582e0189))
* update move validation to check for king safety ([#13](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/13)) ([e5e20c5](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/e5e20c566e368b12ca1dc59680c34e9112bf6762))
@@ -1,11 +1,11 @@
package de.nowchess.chess.command package de.nowchess.chess.command
import de.nowchess.api.board.{Square, Piece} import de.nowchess.api.board.{Piece, Square}
import de.nowchess.api.game.GameContext import de.nowchess.api.game.GameContext
/** Marker trait for all commands that can be executed and undone. /** Marker trait for all commands that can be executed and undone. Commands encapsulate user actions and game state
* Commands encapsulate user actions and game state transitions. * transitions.
*/ */
trait Command: trait Command:
/** Execute the command and return true if successful, false otherwise. */ /** Execute the command and return true if successful, false otherwise. */
def execute(): Boolean def execute(): Boolean
@@ -16,15 +16,14 @@ trait Command:
/** A human-readable description of this command. */ /** A human-readable description of this command. */
def description: String def description: String
/** Command to move a piece from one square to another. /** Command to move a piece from one square to another. Stores the move result so undo can restore previous state.
* Stores the move result so undo can restore previous state. */
*/
case class MoveCommand( case class MoveCommand(
from: Square, from: Square,
to: Square, to: Square,
moveResult: Option[MoveResult] = None, moveResult: Option[MoveResult] = None,
previousContext: Option[GameContext] = None, previousContext: Option[GameContext] = None,
notation: String = "" notation: String = "",
) extends Command: ) extends Command:
override def execute(): Boolean = override def execute(): Boolean =
@@ -39,18 +38,18 @@ case class MoveCommand(
sealed trait MoveResult sealed trait MoveResult
object MoveResult: object MoveResult:
case class Successful(newContext: GameContext, captured: Option[Piece]) extends MoveResult case class Successful(newContext: GameContext, captured: Option[Piece]) extends MoveResult
case object InvalidFormat extends MoveResult case object InvalidFormat extends MoveResult
case object InvalidMove extends MoveResult case object InvalidMove extends MoveResult
/** Command to quit the game. */ /** Command to quit the game. */
case class QuitCommand() extends Command: case class QuitCommand() extends Command:
override def execute(): Boolean = true override def execute(): Boolean = true
override def undo(): Boolean = false override def undo(): Boolean = false
override def description: String = "Quit game" override def description: String = "Quit game"
/** Command to reset the board to initial position. */ /** Command to reset the board to initial position. */
case class ResetCommand( case class ResetCommand(
previousContext: Option[GameContext] = None previousContext: Option[GameContext] = None,
) extends Command: ) extends Command:
override def execute(): Boolean = true override def execute(): Boolean = true
@@ -3,21 +3,19 @@ package de.nowchess.chess.command
/** Manages command execution and history for undo/redo support. */ /** Manages command execution and history for undo/redo support. */
class CommandInvoker: class CommandInvoker:
private val executedCommands = scala.collection.mutable.ListBuffer[Command]() private val executedCommands = scala.collection.mutable.ListBuffer[Command]()
@SuppressWarnings(Array("DisableSyntax.var"))
private var currentIndex = -1 private var currentIndex = -1
/** Execute a command and add it to history. /** Execute a command and add it to history. Discards any redo history if not at the end of the stack.
* Discards any redo history if not at the end of the stack. */
*/
def execute(command: Command): Boolean = synchronized { def execute(command: Command): Boolean = synchronized {
if command.execute() then if command.execute() then
// Remove any commands after current index (redo stack is discarded) // Remove any commands after current index (redo stack is discarded)
while currentIndex < executedCommands.size - 1 do while currentIndex < executedCommands.size - 1 do executedCommands.remove(executedCommands.size - 1)
executedCommands.remove(executedCommands.size - 1)
executedCommands += command executedCommands += command
currentIndex += 1 currentIndex += 1
true true
else else false
false
} }
/** Undo the last executed command if possible. */ /** Undo the last executed command if possible. */
@@ -27,10 +25,8 @@ class CommandInvoker:
if command.undo() then if command.undo() then
currentIndex -= 1 currentIndex -= 1
true true
else else false
false else false
else
false
} }
/** Redo the next command in history if available. */ /** Redo the next command in history if available. */
@@ -40,10 +36,8 @@ class CommandInvoker:
if command.execute() then if command.execute() then
currentIndex += 1 currentIndex += 1
true true
else else false
false else false
else
false
} }
/** Get the history of all executed commands. */ /** Get the history of all executed commands. */
@@ -4,21 +4,25 @@ import de.nowchess.api.board.{File, Rank, Square}
object Parser: object Parser:
/** Parses coordinate notation such as "e2e4" or "g1f3". /** Parses coordinate notation such as "e2e4" or "g1f3". Returns None for any input that does not match the expected
* Returns None for any input that does not match the expected format. * format.
*/ */
def parseMove(input: String): Option[(Square, Square)] = def parseMove(input: String): Option[(Square, Square)] =
val trimmed = input.trim.toLowerCase val trimmed = input.trim.toLowerCase
Option.when(trimmed.length == 4)(trimmed).flatMap: s => Option
for .when(trimmed.length == 4)(trimmed)
from <- parseSquare(s.substring(0, 2)) .flatMap: s =>
to <- parseSquare(s.substring(2, 4)) for
yield (from, to) from <- parseSquare(s.substring(0, 2))
to <- parseSquare(s.substring(2, 4))
yield (from, to)
private def parseSquare(s: String): Option[Square] = private def parseSquare(s: String): Option[Square] =
Option.when(s.length == 2)(s).flatMap: sq => Option
val fileIdx = sq(0) - 'a' .when(s.length == 2)(s)
val rankIdx = sq(1) - '1' .flatMap: sq =>
Option.when(fileIdx >= 0 && fileIdx <= 7 && rankIdx >= 0 && rankIdx <= 7)( val fileIdx = sq(0) - 'a'
Square(File.values(fileIdx), Rank.values(rankIdx)) val rankIdx = sq(1) - '1'
) Option.when(fileIdx >= 0 && fileIdx <= 7 && rankIdx >= 0 && rankIdx <= 7)(
Square(File.values(fileIdx), Rank.values(rankIdx)),
)
@@ -2,49 +2,50 @@ package de.nowchess.chess.engine
import de.nowchess.api.board.{Board, Color, Piece, PieceType, Square} import de.nowchess.api.board.{Board, Color, Piece, PieceType, Square}
import de.nowchess.api.move.{Move, MoveType, PromotionPiece} import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
import de.nowchess.api.game.GameContext import de.nowchess.api.game.{DrawReason, GameContext, GameResult}
import de.nowchess.chess.controller.Parser import de.nowchess.chess.controller.Parser
import de.nowchess.chess.observer.* import de.nowchess.chess.observer.*
import de.nowchess.chess.command.{CommandInvoker, MoveCommand, MoveResult} import de.nowchess.chess.command.{CommandInvoker, MoveCommand, MoveResult}
import de.nowchess.io.{GameContextImport, GameContextExport} import de.nowchess.io.{GameContextExport, GameContextImport}
import de.nowchess.rules.RuleSet import de.nowchess.rules.RuleSet
import de.nowchess.rules.sets.DefaultRules import de.nowchess.rules.sets.DefaultRules
/** Pure game engine that manages game state and notifies observers of state changes. /** Pure game engine that manages game state and notifies observers of state changes. All rule queries delegate to the
* All rule queries delegate to the injected RuleSet. * injected RuleSet. All user interactions go through Commands; state changes are broadcast via GameEvents.
* All user interactions go through Commands; state changes are broadcast via GameEvents. */
*/
class GameEngine( class GameEngine(
val initialContext: GameContext = GameContext.initial, val initialContext: GameContext = GameContext.initial,
val ruleSet: RuleSet = DefaultRules val ruleSet: RuleSet = DefaultRules,
) extends Observable: ) extends Observable:
@SuppressWarnings(Array("DisableSyntax.var"))
private var currentContext: GameContext = initialContext private var currentContext: GameContext = initialContext
private val invoker = new CommandInvoker() private val invoker = new CommandInvoker()
/** Pending promotion: the Move that triggered it (from/to only, moveType filled in later). */ /** Pending promotion: the Move that triggered it (from/to only, moveType filled in later). */
private case class PendingPromotion(from: Square, to: Square, contextBefore: GameContext) private case class PendingPromotion(from: Square, to: Square, contextBefore: GameContext)
@SuppressWarnings(Array("DisableSyntax.var"))
private var pendingPromotion: Option[PendingPromotion] = None private var pendingPromotion: Option[PendingPromotion] = None
/** True if a pawn promotion move is pending and needs a piece choice. */ /** True if a pawn promotion move is pending and needs a piece choice. */
def isPendingPromotion: Boolean = synchronized { pendingPromotion.isDefined } def isPendingPromotion: Boolean = synchronized(pendingPromotion.isDefined)
// Synchronized accessors for current state // Synchronized accessors for current state
def board: Board = synchronized { currentContext.board } def board: Board = synchronized(currentContext.board)
def turn: Color = synchronized { currentContext.turn } def turn: Color = synchronized(currentContext.turn)
def context: GameContext = synchronized { currentContext } def context: GameContext = synchronized(currentContext)
/** Check if undo is available. */ /** Check if undo is available. */
def canUndo: Boolean = synchronized { invoker.canUndo } def canUndo: Boolean = synchronized(invoker.canUndo)
/** Check if redo is available. */ /** Check if redo is available. */
def canRedo: Boolean = synchronized { invoker.canRedo } def canRedo: Boolean = synchronized(invoker.canRedo)
/** Get the command history for inspection (testing/debugging). */ /** Get the command history for inspection (testing/debugging). */
def commandHistory: List[de.nowchess.chess.command.Command] = synchronized { invoker.history } def commandHistory: List[de.nowchess.chess.command.Command] = synchronized(invoker.history)
/** Process a raw move input string and update game state if valid. /** Process a raw move input string and update game state if valid. Notifies all observers of the outcome via
* Notifies all observers of the outcome via GameEvent. * GameEvent.
*/ */
def processUserInput(rawInput: String): Unit = synchronized { def processUserInput(rawInput: String): Unit = synchronized {
val trimmed = rawInput.trim.toLowerCase val trimmed = rawInput.trim.toLowerCase
trimmed match trimmed match
@@ -59,13 +60,16 @@ class GameEngine(
case "draw" => case "draw" =>
if currentContext.halfMoveClock >= 100 then if currentContext.halfMoveClock >= 100 then
currentContext = currentContext.withResult(Some(GameResult.Draw(DrawReason.FiftyMoveRule)))
invoker.clear() invoker.clear()
notifyObservers(DrawClaimedEvent(currentContext)) notifyObservers(DrawEvent(currentContext, DrawReason.FiftyMoveRule))
else else
notifyObservers(InvalidMoveEvent( notifyObservers(
currentContext, InvalidMoveEvent(
"Draw cannot be claimed: the 50-move rule has not been triggered." currentContext,
)) "Draw cannot be claimed: the 50-move rule has not been triggered.",
),
)
case "" => case "" =>
notifyObservers(InvalidMoveEvent(currentContext, "Please enter a valid move or command.")) notifyObservers(InvalidMoveEvent(currentContext, "Please enter a valid move or command."))
@@ -73,10 +77,12 @@ class GameEngine(
case moveInput => case moveInput =>
Parser.parseMove(moveInput) match Parser.parseMove(moveInput) match
case None => case None =>
notifyObservers(InvalidMoveEvent( notifyObservers(
currentContext, InvalidMoveEvent(
s"Invalid move format '$moveInput'. Use coordinate notation, e.g. e2e4." currentContext,
)) s"Invalid move format '$moveInput'. Use coordinate notation, e.g. e2e4.",
),
)
case Some((from, to)) => case Some((from, to)) =>
handleParsedMove(from, to) handleParsedMove(from, to)
} }
@@ -108,9 +114,8 @@ class GameEngine(
to.rank.ordinal == promoRank to.rank.ordinal == promoRank
} }
/** Apply a player's promotion piece choice. /** Apply a player's promotion piece choice. Must only be called when isPendingPromotion is true.
* Must only be called when isPendingPromotion is true. */
*/
def completePromotion(piece: PromotionPiece): Unit = synchronized { def completePromotion(piece: PromotionPiece): Unit = synchronized {
pendingPromotion match pendingPromotion match
case None => case None =>
@@ -120,23 +125,19 @@ class GameEngine(
val move = Move(pending.from, pending.to, MoveType.Promotion(piece)) val move = Move(pending.from, pending.to, MoveType.Promotion(piece))
// Verify it's actually legal // Verify it's actually legal
val legal = ruleSet.legalMoves(currentContext)(pending.from) val legal = ruleSet.legalMoves(currentContext)(pending.from)
if legal.contains(move) then if legal.contains(move) then executeMove(move)
executeMove(move) else notifyObservers(InvalidMoveEvent(currentContext, "Error completing promotion."))
else
notifyObservers(InvalidMoveEvent(currentContext, "Error completing promotion."))
} }
/** Undo the last move. */ /** Undo the last move. */
def undo(): Unit = synchronized { performUndo() } def undo(): Unit = synchronized(performUndo())
/** Redo the last undone move. */ /** Redo the last undone move. */
def redo(): Unit = synchronized { performRedo() } def redo(): Unit = synchronized(performRedo())
/** Load a game using the provided importer. /** Load a game using the provided importer. If the imported context has moves, they are replayed through the command
* If the imported context has moves, they are replayed through the command system. * system. Otherwise, the position is set directly. Notifies observers with PgnLoadedEvent on success.
* Otherwise, the position is set directly. */
* Notifies observers with PgnLoadedEvent on success.
*/
def loadGame(importer: GameContextImport, input: String): Either[String, Unit] = synchronized { def loadGame(importer: GameContextImport, input: String): Either[String, Unit] = synchronized {
importer.importGameContext(input) match importer.importGameContext(input) match
case Left(err) => Left(err) case Left(err) => Left(err)
@@ -155,29 +156,24 @@ class GameEngine(
if ctx.moves.isEmpty then if ctx.moves.isEmpty then
currentContext = ctx currentContext = ctx
Right(()) Right(())
else else replayMoves(ctx.moves, savedContext)
replayMoves(ctx.moves, savedContext)
private[engine] def replayMoves(moves: List[Move], savedContext: GameContext): Either[String, Unit] = private[engine] def replayMoves(moves: List[Move], savedContext: GameContext): Either[String, Unit] =
var error: Option[String] = None val result = moves.foldLeft[Either[String, Unit]](Right(())) { (acc, move) =>
moves.foreach: move => acc.flatMap(_ => applyReplayMove(move))
if error.isEmpty then }
handleParsedMove(move.from, move.to) result.left.foreach(_ => currentContext = savedContext)
result
move.moveType match { private def applyReplayMove(move: Move): Either[String, Unit] =
case MoveType.Promotion(pp) => handleParsedMove(move.from, move.to)
if pendingPromotion.isDefined then move.moveType match
completePromotion(pp) case MoveType.Promotion(pp) if pendingPromotion.isDefined =>
else completePromotion(pp)
error = Some(s"Promotion required for move ${move.from}${move.to}")
case _ => ()
}
error match
case Some(err) =>
currentContext = savedContext
Left(err)
case None =>
Right(()) Right(())
case MoveType.Promotion(_) =>
Left(s"Promotion required for move ${move.from}${move.to}")
case _ => Right(())
/** Export the current game context using the provided exporter. */ /** Export the current game context using the provided exporter. */
def exportGame(exporter: GameContextExport): String = synchronized { def exportGame(exporter: GameContextExport): String = synchronized {
@@ -203,47 +199,51 @@ class GameEngine(
private def executeMove(move: Move): Unit = private def executeMove(move: Move): Unit =
val contextBefore = currentContext val contextBefore = currentContext
val nextContext = ruleSet.applyMove(currentContext)(move) val nextContext = ruleSet.applyMove(currentContext)(move)
val captured = computeCaptured(currentContext, move) val captured = computeCaptured(currentContext, move)
val cmd = MoveCommand( val cmd = MoveCommand(
from = move.from, from = move.from,
to = move.to, to = move.to,
moveResult = Some(MoveResult.Successful(nextContext, captured)), moveResult = Some(MoveResult.Successful(nextContext, captured)),
previousContext = Some(contextBefore), previousContext = Some(contextBefore),
notation = translateMoveToNotation(move, contextBefore.board) notation = translateMoveToNotation(move, contextBefore.board),
) )
invoker.execute(cmd) invoker.execute(cmd)
currentContext = nextContext currentContext = nextContext
notifyObservers(MoveExecutedEvent( notifyObservers(
currentContext, MoveExecutedEvent(
move.from.toString, currentContext,
move.to.toString, move.from.toString,
captured.map(c => s"${c.color.label} ${c.pieceType.label}") move.to.toString,
)) captured.map(c => s"${c.color.label} ${c.pieceType.label}"),
),
)
if ruleSet.isCheckmate(currentContext) then if ruleSet.isCheckmate(currentContext) then
val winner = currentContext.turn.opposite val winner = currentContext.turn.opposite
currentContext = currentContext.withResult(Some(GameResult.Win(winner)))
notifyObservers(CheckmateEvent(currentContext, winner)) notifyObservers(CheckmateEvent(currentContext, winner))
invoker.clear() invoker.clear()
currentContext = GameContext.initial
else if ruleSet.isStalemate(currentContext) then else if ruleSet.isStalemate(currentContext) then
notifyObservers(StalemateEvent(currentContext)) currentContext = currentContext.withResult(Some(GameResult.Draw(DrawReason.Stalemate)))
notifyObservers(DrawEvent(currentContext, DrawReason.Stalemate))
invoker.clear() invoker.clear()
currentContext = GameContext.initial else if ruleSet.isInsufficientMaterial(currentContext) then
else if ruleSet.isCheck(currentContext) then currentContext = currentContext.withResult(Some(GameResult.Draw(DrawReason.InsufficientMaterial)))
notifyObservers(CheckDetectedEvent(currentContext)) notifyObservers(DrawEvent(currentContext, DrawReason.InsufficientMaterial))
invoker.clear()
else if ruleSet.isCheck(currentContext) then notifyObservers(CheckDetectedEvent(currentContext))
if currentContext.halfMoveClock >= 100 then if currentContext.halfMoveClock >= 100 then notifyObservers(FiftyMoveRuleAvailableEvent(currentContext))
notifyObservers(FiftyMoveRuleAvailableEvent(currentContext))
private def translateMoveToNotation(move: Move, boardBefore: Board): String = private def translateMoveToNotation(move: Move, boardBefore: Board): String =
move.moveType match move.moveType match
case MoveType.CastleKingside => "O-O" case MoveType.CastleKingside => "O-O"
case MoveType.CastleQueenside => "O-O-O" case MoveType.CastleQueenside => "O-O-O"
case MoveType.EnPassant => enPassantNotation(move) case MoveType.EnPassant => enPassantNotation(move)
case MoveType.Promotion(pp) => promotionNotation(move, pp) case MoveType.Promotion(pp) => promotionNotation(move, pp)
case MoveType.Normal(isCapture) => normalMoveNotation(move, boardBefore, isCapture) case MoveType.Normal(isCapture) => normalMoveNotation(move, boardBefore, isCapture)
private def enPassantNotation(move: Move): String = private def enPassantNotation(move: Move): String =
@@ -295,8 +295,7 @@ class GameEngine(
moveCmd.previousContext.foreach(currentContext = _) moveCmd.previousContext.foreach(currentContext = _)
invoker.undo() invoker.undo()
notifyObservers(MoveUndoneEvent(currentContext, moveCmd.notation)) notifyObservers(MoveUndoneEvent(currentContext, moveCmd.notation))
else else notifyObservers(InvalidMoveEvent(currentContext, "Nothing to undo."))
notifyObservers(InvalidMoveEvent(currentContext, "Nothing to undo."))
private def performRedo(): Unit = private def performRedo(): Unit =
if invoker.canRedo then if invoker.canRedo then
@@ -307,12 +306,13 @@ class GameEngine(
currentContext = nextCtx currentContext = nextCtx
invoker.redo() invoker.redo()
val capturedDesc = cap.map(c => s"${c.color.label} ${c.pieceType.label}") val capturedDesc = cap.map(c => s"${c.color.label} ${c.pieceType.label}")
notifyObservers(MoveRedoneEvent( notifyObservers(
currentContext, MoveRedoneEvent(
moveCmd.notation, currentContext,
moveCmd.from.toString, moveCmd.notation,
moveCmd.to.toString, moveCmd.from.toString,
capturedDesc moveCmd.to.toString,
)) capturedDesc,
else ),
notifyObservers(InvalidMoveEvent(currentContext, "Nothing to redo.")) )
else notifyObservers(InvalidMoveEvent(currentContext, "Nothing to redo."))
@@ -1,84 +1,79 @@
package de.nowchess.chess.observer package de.nowchess.chess.observer
import de.nowchess.api.board.{Color, Square} import de.nowchess.api.board.{Color, Square}
import de.nowchess.api.game.GameContext import de.nowchess.api.game.{DrawReason, GameContext}
/** Base trait for all game state events. /** Base trait for all game state events. Events are immutable snapshots of game state changes.
* Events are immutable snapshots of game state changes. */
*/
sealed trait GameEvent: sealed trait GameEvent:
def context: GameContext def context: GameContext
/** Fired when a move is successfully executed. */ /** Fired when a move is successfully executed. */
case class MoveExecutedEvent( case class MoveExecutedEvent(
context: GameContext, context: GameContext,
fromSquare: String, fromSquare: String,
toSquare: String, toSquare: String,
capturedPiece: Option[String] capturedPiece: Option[String],
) extends GameEvent ) extends GameEvent
/** Fired when the current player is in check. */ /** Fired when the current player is in check. */
case class CheckDetectedEvent( case class CheckDetectedEvent(
context: GameContext context: GameContext,
) extends GameEvent ) extends GameEvent
/** Fired when the game reaches checkmate. */ /** Fired when the game reaches checkmate. */
case class CheckmateEvent( case class CheckmateEvent(
context: GameContext, context: GameContext,
winner: Color winner: Color,
) extends GameEvent ) extends GameEvent
/** Fired when the game reaches stalemate. */ /** Fired when the game ends in a draw. */
case class StalemateEvent( case class DrawEvent(
context: GameContext context: GameContext,
reason: DrawReason,
) extends GameEvent ) extends GameEvent
/** Fired when a move is invalid. */ /** Fired when a move is invalid. */
case class InvalidMoveEvent( case class InvalidMoveEvent(
context: GameContext, context: GameContext,
reason: String reason: String,
) extends GameEvent ) extends GameEvent
/** Fired when a pawn reaches the back rank and the player must choose a promotion piece. */ /** Fired when a pawn reaches the back rank and the player must choose a promotion piece. */
case class PromotionRequiredEvent( case class PromotionRequiredEvent(
context: GameContext, context: GameContext,
from: Square, from: Square,
to: Square to: Square,
) extends GameEvent ) extends GameEvent
/** Fired when the board is reset. */ /** Fired when the board is reset. */
case class BoardResetEvent( case class BoardResetEvent(
context: GameContext context: GameContext,
) extends GameEvent ) extends GameEvent
/** Fired after any move where the half-move clock reaches 100 — the 50-move rule is now claimable. */ /** Fired after any move where the half-move clock reaches 100 — the 50-move rule is now claimable. */
case class FiftyMoveRuleAvailableEvent( case class FiftyMoveRuleAvailableEvent(
context: GameContext context: GameContext,
) extends GameEvent
/** Fired when a player successfully claims a draw under the 50-move rule. */
case class DrawClaimedEvent(
context: GameContext
) extends GameEvent ) extends GameEvent
/** Fired when a move is undone, carrying PGN notation of the reversed move. */ /** Fired when a move is undone, carrying PGN notation of the reversed move. */
case class MoveUndoneEvent( case class MoveUndoneEvent(
context: GameContext, context: GameContext,
pgnNotation: String pgnNotation: String,
) extends GameEvent ) extends GameEvent
/** Fired when a previously undone move is redone, carrying PGN notation of the replayed move. */ /** Fired when a previously undone move is redone, carrying PGN notation of the replayed move. */
case class MoveRedoneEvent( case class MoveRedoneEvent(
context: GameContext, context: GameContext,
pgnNotation: String, pgnNotation: String,
fromSquare: String, fromSquare: String,
toSquare: String, toSquare: String,
capturedPiece: Option[String] capturedPiece: Option[String],
) extends GameEvent ) extends GameEvent
/** Fired after a PGN string is successfully loaded and all moves are replayed into history. */ /** Fired after a PGN string is successfully loaded and all moves are replayed into history. */
case class PgnLoadedEvent( case class PgnLoadedEvent(
context: GameContext context: GameContext,
) extends GameEvent ) extends GameEvent
/** Observer trait: implement to receive game state updates. */ /** Observer trait: implement to receive game state updates. */
@@ -1,6 +1,6 @@
package de.nowchess.chess.command package de.nowchess.chess.command
import de.nowchess.api.board.{Square, File, Rank} import de.nowchess.api.board.{File, Rank, Square}
import de.nowchess.api.game.GameContext import de.nowchess.api.game.GameContext
import org.scalatest.funsuite.AnyFunSuite import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers import org.scalatest.matchers.should.Matchers
@@ -10,13 +10,18 @@ class CommandInvokerBranchTest extends AnyFunSuite with Matchers:
private def sq(f: File, r: Rank): Square = Square(f, r) private def sq(f: File, r: Rank): Square = Square(f, r)
private case class FailingCommand() extends Command: private case class FailingCommand() extends Command:
override def execute(): Boolean = false override def execute(): Boolean = false
override def undo(): Boolean = false override def undo(): Boolean = false
override def description: String = "Failing command" override def description: String = "Failing command"
private case class ConditionalFailCommand(var shouldFailOnUndo: Boolean = false, var shouldFailOnExecute: Boolean = false) extends Command: private class ConditionalFailCommand(
override def execute(): Boolean = !shouldFailOnExecute initialShouldFailOnUndo: Boolean = false,
override def undo(): Boolean = !shouldFailOnUndo initialShouldFailOnExecute: Boolean = false,
) extends Command:
val shouldFailOnUndo = new java.util.concurrent.atomic.AtomicBoolean(initialShouldFailOnUndo)
val shouldFailOnExecute = new java.util.concurrent.atomic.AtomicBoolean(initialShouldFailOnExecute)
override def execute(): Boolean = !shouldFailOnExecute.get()
override def undo(): Boolean = !shouldFailOnUndo.get()
override def description: String = "Conditional fail" override def description: String = "Conditional fail"
private def createMoveCommand(from: Square, to: Square, executeSucceeds: Boolean = true): MoveCommand = private def createMoveCommand(from: Square, to: Square, executeSucceeds: Boolean = true): MoveCommand =
@@ -24,12 +29,12 @@ class CommandInvokerBranchTest extends AnyFunSuite with Matchers:
from = from, from = from,
to = to, to = to,
moveResult = if executeSucceeds then Some(MoveResult.Successful(GameContext.initial, None)) else None, moveResult = if executeSucceeds then Some(MoveResult.Successful(GameContext.initial, None)) else None,
previousContext = Some(GameContext.initial) previousContext = Some(GameContext.initial),
) )
test("execute rejects failing commands and keeps history unchanged"): test("execute rejects failing commands and keeps history unchanged"):
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd = FailingCommand() val cmd = FailingCommand()
invoker.execute(cmd) shouldBe false invoker.execute(cmd) shouldBe false
invoker.history.size shouldBe 0 invoker.history.size shouldBe 0
invoker.getCurrentIndex shouldBe -1 invoker.getCurrentIndex shouldBe -1
@@ -52,8 +57,8 @@ class CommandInvokerBranchTest extends AnyFunSuite with Matchers:
{ {
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd1 = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) val cmd1 = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4))
val cmd2 = createMoveCommand(sq(File.E, Rank.R7), sq(File.E, Rank.R5)) val cmd2 = createMoveCommand(sq(File.E, Rank.R7), sq(File.E, Rank.R5))
invoker.execute(cmd1) invoker.execute(cmd1)
invoker.execute(cmd2) invoker.execute(cmd2)
invoker.undo() invoker.undo()
@@ -62,8 +67,8 @@ class CommandInvokerBranchTest extends AnyFunSuite with Matchers:
} }
{ {
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val failingUndoCmd = ConditionalFailCommand(shouldFailOnUndo = true) val failingUndoCmd = ConditionalFailCommand(initialShouldFailOnUndo = true)
invoker.execute(failingUndoCmd) shouldBe true invoker.execute(failingUndoCmd) shouldBe true
invoker.canUndo shouldBe true invoker.canUndo shouldBe true
invoker.undo() shouldBe false invoker.undo() shouldBe false
@@ -71,7 +76,7 @@ class CommandInvokerBranchTest extends AnyFunSuite with Matchers:
} }
{ {
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val successUndoCmd = ConditionalFailCommand() val successUndoCmd = ConditionalFailCommand()
invoker.execute(successUndoCmd) shouldBe true invoker.execute(successUndoCmd) shouldBe true
invoker.undo() shouldBe true invoker.undo() shouldBe true
@@ -85,28 +90,28 @@ class CommandInvokerBranchTest extends AnyFunSuite with Matchers:
{ {
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) val cmd = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4))
invoker.execute(cmd) invoker.execute(cmd)
invoker.canRedo shouldBe false invoker.canRedo shouldBe false
invoker.redo() shouldBe false invoker.redo() shouldBe false
} }
{ {
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd1 = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) val cmd1 = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4))
val redoFailCmd = ConditionalFailCommand() val redoFailCmd = ConditionalFailCommand()
invoker.execute(cmd1) invoker.execute(cmd1)
invoker.execute(redoFailCmd) invoker.execute(redoFailCmd)
invoker.undo() invoker.undo()
invoker.canRedo shouldBe true invoker.canRedo shouldBe true
redoFailCmd.shouldFailOnExecute = true redoFailCmd.shouldFailOnExecute.set(true)
invoker.redo() shouldBe false invoker.redo() shouldBe false
invoker.getCurrentIndex shouldBe 0 invoker.getCurrentIndex shouldBe 0
} }
{ {
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) val cmd = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4))
invoker.execute(cmd) shouldBe true invoker.execute(cmd) shouldBe true
invoker.undo() shouldBe true invoker.undo() shouldBe true
invoker.redo() shouldBe true invoker.redo() shouldBe true
@@ -115,9 +120,9 @@ class CommandInvokerBranchTest extends AnyFunSuite with Matchers:
{ {
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd1 = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) val cmd1 = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4))
val cmd2 = createMoveCommand(sq(File.E, Rank.R7), sq(File.E, Rank.R5)) val cmd2 = createMoveCommand(sq(File.E, Rank.R7), sq(File.E, Rank.R5))
val cmd3 = createMoveCommand(sq(File.D, Rank.R2), sq(File.D, Rank.R4)) val cmd3 = createMoveCommand(sq(File.D, Rank.R2), sq(File.D, Rank.R4))
invoker.execute(cmd1) invoker.execute(cmd1)
invoker.execute(cmd2) invoker.execute(cmd2)
invoker.undo() invoker.undo()
@@ -130,10 +135,10 @@ class CommandInvokerBranchTest extends AnyFunSuite with Matchers:
{ {
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd1 = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) val cmd1 = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4))
val cmd2 = createMoveCommand(sq(File.E, Rank.R7), sq(File.E, Rank.R5)) val cmd2 = createMoveCommand(sq(File.E, Rank.R7), sq(File.E, Rank.R5))
val cmd3 = createMoveCommand(sq(File.G, Rank.R1), sq(File.F, Rank.R3)) val cmd3 = createMoveCommand(sq(File.G, Rank.R1), sq(File.F, Rank.R3))
val cmd4 = createMoveCommand(sq(File.D, Rank.R2), sq(File.D, Rank.R4)) val cmd4 = createMoveCommand(sq(File.D, Rank.R2), sq(File.D, Rank.R4))
invoker.execute(cmd1) invoker.execute(cmd1)
invoker.execute(cmd2) invoker.execute(cmd2)
invoker.execute(cmd3) invoker.execute(cmd3)
@@ -1,6 +1,6 @@
package de.nowchess.chess.command package de.nowchess.chess.command
import de.nowchess.api.board.{Square, File, Rank} import de.nowchess.api.board.{File, Rank, Square}
import de.nowchess.api.game.GameContext import de.nowchess.api.game.GameContext
import org.scalatest.funsuite.AnyFunSuite import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers import org.scalatest.matchers.should.Matchers
@@ -14,12 +14,12 @@ class CommandInvokerTest extends AnyFunSuite with Matchers:
from = from, from = from,
to = to, to = to,
moveResult = Some(MoveResult.Successful(GameContext.initial, None)), moveResult = Some(MoveResult.Successful(GameContext.initial, None)),
previousContext = Some(GameContext.initial) previousContext = Some(GameContext.initial),
) )
test("execute appends commands and updates index"): test("execute appends commands and updates index"):
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) val cmd = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4))
invoker.execute(cmd) shouldBe true invoker.execute(cmd) shouldBe true
invoker.history.size shouldBe 1 invoker.history.size shouldBe 1
invoker.getCurrentIndex shouldBe 0 invoker.getCurrentIndex shouldBe 0
@@ -31,7 +31,7 @@ class CommandInvokerTest extends AnyFunSuite with Matchers:
test("undo and redo update index and availability flags"): test("undo and redo update index and availability flags"):
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) val cmd = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4))
invoker.canUndo shouldBe false invoker.canUndo shouldBe false
invoker.execute(cmd) invoker.execute(cmd)
invoker.canUndo shouldBe true invoker.canUndo shouldBe true
@@ -43,7 +43,7 @@ class CommandInvokerTest extends AnyFunSuite with Matchers:
test("clear removes full history and resets index"): test("clear removes full history and resets index"):
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) val cmd = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4))
invoker.execute(cmd) invoker.execute(cmd)
invoker.clear() invoker.clear()
invoker.history.size shouldBe 0 invoker.history.size shouldBe 0
@@ -51,9 +51,9 @@ class CommandInvokerTest extends AnyFunSuite with Matchers:
test("execute after undo discards redo history"): test("execute after undo discards redo history"):
val invoker = new CommandInvoker() val invoker = new CommandInvoker()
val cmd1 = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) val cmd1 = createMoveCommand(sq(File.E, Rank.R2), sq(File.E, Rank.R4))
val cmd2 = createMoveCommand(sq(File.E, Rank.R7), sq(File.E, Rank.R5)) val cmd2 = createMoveCommand(sq(File.E, Rank.R7), sq(File.E, Rank.R5))
val cmd3 = createMoveCommand(sq(File.D, Rank.R2), sq(File.D, Rank.R4)) val cmd3 = createMoveCommand(sq(File.D, Rank.R2), sq(File.D, Rank.R4))
invoker.execute(cmd1) invoker.execute(cmd1)
invoker.execute(cmd2) invoker.execute(cmd2)
invoker.undo() invoker.undo()
@@ -8,7 +8,6 @@ class CommandTest extends AnyFunSuite with Matchers:
test("QuitCommand properties and behavior"): test("QuitCommand properties and behavior"):
val cmd = QuitCommand() val cmd = QuitCommand()
cmd shouldNot be(null)
cmd.execute() shouldBe true cmd.execute() shouldBe true
cmd.undo() shouldBe false cmd.undo() shouldBe false
cmd.description shouldBe "Quit game" cmd.description shouldBe "Quit game"
@@ -1,6 +1,6 @@
package de.nowchess.chess.command package de.nowchess.chess.command
import de.nowchess.api.board.{Square, File, Rank} import de.nowchess.api.board.{File, Rank, Square}
import de.nowchess.api.game.GameContext import de.nowchess.api.game.GameContext
import org.scalatest.funsuite.AnyFunSuite import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers import org.scalatest.matchers.should.Matchers
@@ -21,7 +21,7 @@ class MoveCommandTest extends AnyFunSuite with Matchers:
val executable = MoveCommand( val executable = MoveCommand(
from = sq(File.E, Rank.R2), from = sq(File.E, Rank.R2),
to = sq(File.E, Rank.R4), to = sq(File.E, Rank.R4),
moveResult = Some(MoveResult.Successful(GameContext.initial, None)) moveResult = Some(MoveResult.Successful(GameContext.initial, None)),
) )
executable.execute() shouldBe true executable.execute() shouldBe true
@@ -29,7 +29,7 @@ class MoveCommandTest extends AnyFunSuite with Matchers:
from = sq(File.E, Rank.R2), from = sq(File.E, Rank.R2),
to = sq(File.E, Rank.R4), to = sq(File.E, Rank.R4),
moveResult = Some(MoveResult.Successful(GameContext.initial, None)), moveResult = Some(MoveResult.Successful(GameContext.initial, None)),
previousContext = Some(GameContext.initial) previousContext = Some(GameContext.initial),
) )
undoable.undo() shouldBe true undoable.undo() shouldBe true
@@ -39,7 +39,7 @@ class MoveCommandTest extends AnyFunSuite with Matchers:
val result = MoveResult.Successful(GameContext.initial, None) val result = MoveResult.Successful(GameContext.initial, None)
val cmd2 = cmd1.copy( val cmd2 = cmd1.copy(
moveResult = Some(result), moveResult = Some(result),
previousContext = Some(GameContext.initial) previousContext = Some(GameContext.initial),
) )
cmd1.moveResult shouldBe None cmd1.moveResult shouldBe None
@@ -52,14 +52,14 @@ class MoveCommandTest extends AnyFunSuite with Matchers:
from = sq(File.E, Rank.R2), from = sq(File.E, Rank.R2),
to = sq(File.E, Rank.R4), to = sq(File.E, Rank.R4),
moveResult = None, moveResult = None,
previousContext = None previousContext = None,
) )
val eq2 = MoveCommand( val eq2 = MoveCommand(
from = sq(File.E, Rank.R2), from = sq(File.E, Rank.R2),
to = sq(File.E, Rank.R4), to = sq(File.E, Rank.R4),
moveResult = None, moveResult = None,
previousContext = None previousContext = None,
) )
eq1 shouldBe eq2 eq1 shouldBe eq2
@@ -27,11 +27,11 @@ object EngineTestHelpers:
private val _events = mutable.ListBuffer[GameEvent]() private val _events = mutable.ListBuffer[GameEvent]()
def events: mutable.ListBuffer[GameEvent] = _events def events: mutable.ListBuffer[GameEvent] = _events
def eventCount: Int = _events.length def eventCount: Int = _events.length
def hasEvent[T <: GameEvent](implicit ct: scala.reflect.ClassTag[T]): Boolean = def hasEvent[T <: GameEvent](implicit ct: scala.reflect.ClassTag[T]): Boolean =
_events.exists(ct.runtimeClass.isInstance(_)) _events.exists(ct.runtimeClass.isInstance(_))
def getEvent[T <: GameEvent](implicit ct: scala.reflect.ClassTag[T]): Option[T] = def getEvent[T <: GameEvent](implicit ct: scala.reflect.ClassTag[T]): Option[T] =
_events.collectFirst { case e if ct.runtimeClass.isInstance(e) => e.asInstanceOf[T] } _events.collectFirst { case e: T => e }
override def onGameEvent(event: GameEvent): Unit = override def onGameEvent(event: GameEvent): Unit =
_events += event _events += event
@@ -1,8 +1,9 @@
package de.nowchess.chess.engine package de.nowchess.chess.engine
import scala.collection.mutable import scala.collection.mutable
import de.nowchess.api.board.{Board, Color} import de.nowchess.api.board.Color
import de.nowchess.chess.observer.{Observer, GameEvent, CheckDetectedEvent, CheckmateEvent, StalemateEvent} import de.nowchess.api.game.DrawReason
import de.nowchess.chess.observer.{CheckDetectedEvent, CheckmateEvent, DrawEvent, GameEvent, Observer}
import org.scalatest.funsuite.AnyFunSuite import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers import org.scalatest.matchers.should.Matchers
@@ -10,82 +11,84 @@ import org.scalatest.matchers.should.Matchers
class GameEngineGameEndingTest extends AnyFunSuite with Matchers: class GameEngineGameEndingTest extends AnyFunSuite with Matchers:
test("GameEngine handles Checkmate (Fool's Mate)"): test("GameEngine handles Checkmate (Fool's Mate)"):
val engine = new GameEngine() val engine = new GameEngine()
val observer = new EndingMockObserver() val observer = new EndingMockObserver()
engine.subscribe(observer) engine.subscribe(observer)
// Play Fool's mate // Play Fool's mate
engine.processUserInput("f2f3") engine.processUserInput("f2f3")
engine.processUserInput("e7e5") engine.processUserInput("e7e5")
engine.processUserInput("g2g4") engine.processUserInput("g2g4")
observer.events.clear() observer.events.clear()
engine.processUserInput("d8h4") engine.processUserInput("d8h4")
// Verify CheckmateEvent (engine also fires MoveExecutedEvent before CheckmateEvent) // Verify CheckmateEvent (engine also fires MoveExecutedEvent before CheckmateEvent)
observer.events.last shouldBe a[CheckmateEvent] observer.events.last shouldBe a[CheckmateEvent]
val event = observer.events.last.asInstanceOf[CheckmateEvent] val event = observer.events.collectFirst { case e: CheckmateEvent => e }.get
event.winner shouldBe Color.Black event.winner shouldBe Color.Black
// Board should be reset after checkmate
engine.board shouldBe Board.initial
engine.turn shouldBe Color.White
test("GameEngine handles check detection"): test("GameEngine handles check detection"):
val engine = new GameEngine() val engine = new GameEngine()
val observer = new EndingMockObserver() val observer = new EndingMockObserver()
engine.subscribe(observer) engine.subscribe(observer)
// Play a simple check // Play a simple check
engine.processUserInput("e2e4") engine.processUserInput("e2e4")
engine.processUserInput("e7e5") engine.processUserInput("e7e5")
engine.processUserInput("f1c4") engine.processUserInput("f1c4")
engine.processUserInput("g8f6") engine.processUserInput("g8f6")
observer.events.clear() observer.events.clear()
engine.processUserInput("c4f7") // Check! engine.processUserInput("c4f7") // Check!
val checkEvents = observer.events.collect { case e: CheckDetectedEvent => e } val checkEvents = observer.events.collect { case e: CheckDetectedEvent => e }
checkEvents.size shouldBe 1 checkEvents.size shouldBe 1
checkEvents.head.context.turn shouldBe Color.Black // Black is now in check checkEvents.head.context.turn shouldBe Color.Black // Black is now in check
// Shortest known stalemate is 19 moves. Here is a faster one: // Shortest known stalemate is 19 moves. Here is a faster one:
// e3 a5 Qh5 Ra6 Qxa5 h5 h4 Rah6 Qxc7 f6 Qxd7+ Kf7 Qxb7 Qd3 Qxb8 Qh7 Qxc8 Kg6 Qe6 // e3 a5 Qh5 Ra6 Qxa5 h5 h4 Rah6 Qxc7 f6 Qxd7+ Kf7 Qxb7 Qd3 Qxb8 Qh7 Qxc8 Kg6 Qe6
// Wait, let's just use Sam Loyd's 10-move stalemate: // Wait, let's just use Sam Loyd's 10-move stalemate:
// 1. e3 a5 2. Qh5 Ra6 3. Qxa5 h5 4. h4 Rah6 5. Qxc7 f6 6. Qxd7+ Kf7 7. Qxb7 Qd3 8. Qxb8 Qh7 9. Qxc8 Kg6 10. Qe6 // 1. e3 a5 2. Qh5 Ra6 3. Qxa5 h5 4. h4 Rah6 5. Qxc7 f6 6. Qxd7+ Kf7 7. Qxb7 Qd3 8. Qxb8 Qh7 9. Qxc8 Kg6 10. Qe6
test("GameEngine handles Stalemate via 10-move known sequence"): test("GameEngine handles Stalemate via 10-move known sequence"):
val engine = new GameEngine() val engine = new GameEngine()
val observer = new EndingMockObserver() val observer = new EndingMockObserver()
engine.subscribe(observer) engine.subscribe(observer)
val moves = List( val moves = List(
"e2e3", "a7a5", "e2e3",
"d1h5", "a8a6", "a7a5",
"h5a5", "h7h5", "d1h5",
"h2h4", "a6h6", "a8a6",
"a5c7", "f7f6", "h5a5",
"c7d7", "e8f7", "h7h5",
"d7b7", "d8d3", "h2h4",
"b7b8", "d3h7", "a6h6",
"b8c8", "f7g6", "a5c7",
"c8e6" "f7f6",
"c7d7",
"e8f7",
"d7b7",
"d8d3",
"b7b8",
"d3h7",
"b8c8",
"f7g6",
"c8e6",
) )
moves.dropRight(1).foreach(engine.processUserInput) moves.dropRight(1).foreach(engine.processUserInput)
observer.events.clear() observer.events.clear()
engine.processUserInput(moves.last) engine.processUserInput(moves.last)
val stalemateEvents = observer.events.collect { case e: StalemateEvent => e } val drawEvents = observer.events.collect { case e: DrawEvent => e }
stalemateEvents.size shouldBe 1 drawEvents.size shouldBe 1
drawEvents.head.reason shouldBe DrawReason.Stalemate
// Board should be reset after stalemate
engine.board shouldBe Board.initial
engine.turn shouldBe Color.White
private class EndingMockObserver extends Observer: private class EndingMockObserver extends Observer:
val events = mutable.ListBuffer[GameEvent]() val events = mutable.ListBuffer[GameEvent]()
override def onGameEvent(event: GameEvent): Unit = override def onGameEvent(event: GameEvent): Unit =
events += event events += event
@@ -38,7 +38,7 @@ class GameEngineIntegrationTest extends AnyFunSuite with Matchers:
engine.processUserInput("undo") engine.processUserInput("undo")
engine.processUserInput("redo") engine.processUserInput("redo")
events.count(_.isInstanceOf[InvalidMoveEvent]) should be >= 3 events.count { case _: InvalidMoveEvent => true; case _ => false } should be >= 3
test("processUserInput emits Illegal move for syntactically valid but illegal target"): test("processUserInput emits Illegal move for syntactically valid but illegal target"):
val engine = new GameEngine() val engine = new GameEngine()
@@ -69,7 +69,9 @@ class GameEngineIntegrationTest extends AnyFunSuite with Matchers:
engine.context shouldBe target engine.context shouldBe target
engine.commandHistory shouldBe empty engine.commandHistory shouldBe empty
events.lastOption.exists(_.isInstanceOf[de.nowchess.chess.observer.BoardResetEvent]) shouldBe true events.lastOption.exists {
case _: de.nowchess.chess.observer.BoardResetEvent => true; case _ => false
} shouldBe true
test("redo event includes captured piece description when replaying a capture"): test("redo event includes captured piece description when replaying a capture"):
val engine = new GameEngine() val engine = new GameEngine()
@@ -92,12 +94,12 @@ class GameEngineIntegrationTest extends AnyFunSuite with Matchers:
def candidateMoves(context: GameContext)(square: Square): List[Move] = legalMoves(context)(square) def candidateMoves(context: GameContext)(square: Square): List[Move] = legalMoves(context)(square)
def legalMoves(context: GameContext)(square: Square): List[Move] = def legalMoves(context: GameContext)(square: Square): List[Move] =
if square == sq("e2") then List(promotionMove) else List.empty if square == sq("e2") then List(promotionMove) else List.empty
def allLegalMoves(context: GameContext): List[Move] = List(promotionMove) def allLegalMoves(context: GameContext): List[Move] = List(promotionMove)
def isCheck(context: GameContext): Boolean = false def isCheck(context: GameContext): Boolean = false
def isCheckmate(context: GameContext): Boolean = false def isCheckmate(context: GameContext): Boolean = false
def isStalemate(context: GameContext): Boolean = false def isStalemate(context: GameContext): Boolean = false
def isInsufficientMaterial(context: GameContext): Boolean = false def isInsufficientMaterial(context: GameContext): Boolean = false
def isFiftyMoveRule(context: GameContext): Boolean = false def isFiftyMoveRule(context: GameContext): Boolean = false
def applyMove(context: GameContext)(move: Move): GameContext = DefaultRules.applyMove(context)(move) def applyMove(context: GameContext)(move: Move): GameContext = DefaultRules.applyMove(context)(move)
val engine = new GameEngine(ruleSet = permissiveRules) val engine = new GameEngine(ruleSet = permissiveRules)
@@ -112,14 +114,14 @@ class GameEngineIntegrationTest extends AnyFunSuite with Matchers:
val promotionMove = Move(sq("e2"), sq("e8"), MoveType.Promotion(PromotionPiece.Queen)) val promotionMove = Move(sq("e2"), sq("e8"), MoveType.Promotion(PromotionPiece.Queen))
val noLegalMoves = new RuleSet: val noLegalMoves = new RuleSet:
def candidateMoves(context: GameContext)(square: Square): List[Move] = List.empty def candidateMoves(context: GameContext)(square: Square): List[Move] = List.empty
def legalMoves(context: GameContext)(square: Square): List[Move] = List.empty def legalMoves(context: GameContext)(square: Square): List[Move] = List.empty
def allLegalMoves(context: GameContext): List[Move] = List.empty def allLegalMoves(context: GameContext): List[Move] = List.empty
def isCheck(context: GameContext): Boolean = false def isCheck(context: GameContext): Boolean = false
def isCheckmate(context: GameContext): Boolean = false def isCheckmate(context: GameContext): Boolean = false
def isStalemate(context: GameContext): Boolean = false def isStalemate(context: GameContext): Boolean = false
def isInsufficientMaterial(context: GameContext): Boolean = false def isInsufficientMaterial(context: GameContext): Boolean = false
def isFiftyMoveRule(context: GameContext): Boolean = false def isFiftyMoveRule(context: GameContext): Boolean = false
def applyMove(context: GameContext)(move: Move): GameContext = context def applyMove(context: GameContext)(move: Move): GameContext = context
val engine = new GameEngine(ruleSet = noLegalMoves) val engine = new GameEngine(ruleSet = noLegalMoves)
engine.processUserInput("e2e4") engine.processUserInput("e2e4")
@@ -137,21 +139,20 @@ class GameEngineIntegrationTest extends AnyFunSuite with Matchers:
test("loadGame replay executes non-promotion moves through default replay branch"): test("loadGame replay executes non-promotion moves through default replay branch"):
val normalMove = Move(sq("e2"), sq("e4"), MoveType.Normal()) val normalMove = Move(sq("e2"), sq("e4"), MoveType.Normal())
val engine = new GameEngine() val engine = new GameEngine()
engine.replayMoves(List(normalMove), engine.context) shouldBe Right(()) engine.replayMoves(List(normalMove), engine.context) shouldBe Right(())
engine.context.moves.lastOption shouldBe Some(normalMove) engine.context.moves.lastOption shouldBe Some(normalMove)
test("replayMoves skips later moves after the first move triggers an error"): test("replayMoves skips later moves after the first move triggers an error"):
val engine = new GameEngine() val engine = new GameEngine()
val saved = engine.context val saved = engine.context
val illegalPromotion = Move(sq("e2"), sq("e1"), MoveType.Promotion(PromotionPiece.Queen)) val illegalPromotion = Move(sq("e2"), sq("e1"), MoveType.Promotion(PromotionPiece.Queen))
val trailingMove = Move(sq("e2"), sq("e4")) val trailingMove = Move(sq("e2"), sq("e4"))
engine.replayMoves(List(illegalPromotion, trailingMove), saved) shouldBe Left("Promotion required for move e2e1") engine.replayMoves(List(illegalPromotion, trailingMove), saved) shouldBe Left("Promotion required for move e2e1")
engine.context shouldBe saved engine.context shouldBe saved
test("normalMoveNotation handles missing source piece"): test("normalMoveNotation handles missing source piece"):
val engine = new GameEngine() val engine = new GameEngine()
val result = engine.normalMoveNotation(Move(sq("e3"), sq("e4")), Board.initial, isCapture = false) val result = engine.normalMoveNotation(Move(sq("e3"), sq("e4")), Board.initial, isCapture = false)
@@ -174,5 +175,3 @@ class GameEngineIntegrationTest extends AnyFunSuite with Matchers:
engine.observerCount shouldBe 1 engine.observerCount shouldBe 1
engine.unsubscribe(observer) engine.unsubscribe(observer)
engine.observerCount shouldBe 0 engine.observerCount shouldBe 0
@@ -3,7 +3,7 @@ package de.nowchess.chess.engine
import scala.collection.mutable import scala.collection.mutable
import de.nowchess.api.board.{Board, Color} import de.nowchess.api.board.{Board, Color}
import de.nowchess.api.game.GameContext import de.nowchess.api.game.GameContext
import de.nowchess.chess.observer.{Observer, GameEvent, PgnLoadedEvent} import de.nowchess.chess.observer.{GameEvent, Observer, PgnLoadedEvent}
import de.nowchess.io.pgn.PgnParser import de.nowchess.io.pgn.PgnParser
import de.nowchess.io.fen.FenParser import de.nowchess.io.fen.FenParser
import de.nowchess.io.pgn.PgnExporter import de.nowchess.io.pgn.PgnExporter
@@ -15,7 +15,7 @@ class GameEngineLoadGameTest extends AnyFunSuite with Matchers:
test("loadGame with PgnParser: loads valid PGN and enables undo/redo"): test("loadGame with PgnParser: loads valid PGN and enables undo/redo"):
val engine = new GameEngine() val engine = new GameEngine()
val pgn = "[Event \"Test\"]\n\n1. e4 e5\n" val pgn = "[Event \"Test\"]\n\n1. e4 e5\n"
val result = engine.loadGame(PgnParser, pgn) val result = engine.loadGame(PgnParser, pgn)
result shouldBe Right(()) result shouldBe Right(())
engine.context.moves.size shouldBe 2 engine.context.moves.size shouldBe 2
@@ -23,7 +23,7 @@ class GameEngineLoadGameTest extends AnyFunSuite with Matchers:
test("loadGame with FenParser: loads position without replaying moves"): test("loadGame with FenParser: loads position without replaying moves"):
val engine = new GameEngine() val engine = new GameEngine()
val fen = "8/4P3/4k3/8/8/8/8/8 w - - 0 1" val fen = "8/4P3/4k3/8/8/8/8/8 w - - 0 1"
val result = engine.loadGame(FenParser, fen) val result = engine.loadGame(FenParser, fen)
result shouldBe Right(()) result shouldBe Right(())
engine.context.moves.isEmpty shouldBe true engine.context.moves.isEmpty shouldBe true
@@ -9,11 +9,11 @@ import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers import org.scalatest.matchers.should.Matchers
/** Tests that exercise moveToPgn branches not covered by other test files: /** Tests that exercise moveToPgn branches not covered by other test files:
* - CastleQueenside (line 223) * - CastleQueenside (line 223)
* - EnPassant notation (lines 224-225) and computeCaptured EnPassant (lines 254-255) * - EnPassant notation (lines 224-225) and computeCaptured EnPassant (lines 254-255)
* - Promotion(Bishop) notation (line 230) * - Promotion(Bishop) notation (line 230)
* - King normal move notation (line 246) * - King normal move notation (line 246)
*/ */
class GameEngineNotationTest extends AnyFunSuite with Matchers: class GameEngineNotationTest extends AnyFunSuite with Matchers:
private def captureEvents(engine: GameEngine): collection.mutable.ListBuffer[GameEvent] = private def captureEvents(engine: GameEngine): collection.mutable.ListBuffer[GameEvent] =
@@ -28,10 +28,10 @@ class GameEngineNotationTest extends AnyFunSuite with Matchers:
val board = FenParser.parseBoard("k7/8/8/8/8/8/8/R3K3").get val board = FenParser.parseBoard("k7/8/8/8/8/8/8/R3K3").get
// Castling rights: white queen-side only (no king-side rook present) // Castling rights: white queen-side only (no king-side rook present)
val castlingRights = de.nowchess.api.board.CastlingRights( val castlingRights = de.nowchess.api.board.CastlingRights(
whiteKingSide = false, whiteKingSide = false,
whiteQueenSide = true, whiteQueenSide = true,
blackKingSide = false, blackKingSide = false,
blackQueenSide = false blackQueenSide = false,
) )
val ctx = GameContext.initial val ctx = GameContext.initial
.withBoard(board) .withBoard(board)
@@ -43,7 +43,7 @@ class GameEngineNotationTest extends AnyFunSuite with Matchers:
// White castles queenside: e1c1 // White castles queenside: e1c1
engine.processUserInput("e1c1") engine.processUserInput("e1c1")
events.exists(_.isInstanceOf[MoveExecutedEvent]) should be (true) events.exists { case _: MoveExecutedEvent => true; case _ => false } should be(true)
events.clear() events.clear()
engine.undo() engine.undo()
@@ -55,7 +55,7 @@ class GameEngineNotationTest extends AnyFunSuite with Matchers:
test("undo after en passant emits MoveUndoneEvent with file-x-destination notation"): test("undo after en passant emits MoveUndoneEvent with file-x-destination notation"):
// White pawn on e5, black pawn on d5 (just double-pushed), en passant square d6 // White pawn on e5, black pawn on d5 (just double-pushed), en passant square d6
val board = FenParser.parseBoard("k7/8/8/3pP3/8/8/8/7K").get val board = FenParser.parseBoard("k7/8/8/3pP3/8/8/8/7K").get
val epSquare = Square.fromAlgebraic("d6") val epSquare = Square.fromAlgebraic("d6")
val ctx = GameContext.initial val ctx = GameContext.initial
.withBoard(board) .withBoard(board)
@@ -68,12 +68,12 @@ class GameEngineNotationTest extends AnyFunSuite with Matchers:
// White pawn on e5 captures en passant to d6 // White pawn on e5 captures en passant to d6
engine.processUserInput("e5d6") engine.processUserInput("e5d6")
events.exists(_.isInstanceOf[MoveExecutedEvent]) should be (true) events.exists { case _: MoveExecutedEvent => true; case _ => false } should be(true)
// Verify the captured pawn was found (computeCaptured EnPassant branch) // Verify the captured pawn was found (computeCaptured EnPassant branch)
val moveEvt = events.collect { case e: MoveExecutedEvent => e }.head val moveEvt = events.collect { case e: MoveExecutedEvent => e }.head
moveEvt.capturedPiece shouldBe defined moveEvt.capturedPiece shouldBe defined
moveEvt.capturedPiece.get should include ("Black") moveEvt.capturedPiece.get should include("Black")
events.clear() events.clear()
engine.undo() engine.undo()
@@ -84,7 +84,8 @@ class GameEngineNotationTest extends AnyFunSuite with Matchers:
// ── Bishop underpromotion notation (line 230) ────────────────────── // ── Bishop underpromotion notation (line 230) ──────────────────────
test("undo after bishop underpromotion emits MoveUndoneEvent with =B notation"): test("undo after bishop underpromotion emits MoveUndoneEvent with =B notation"):
val board = FenParser.parseBoard("8/4P3/8/8/8/8/k7/7K").get // Extra white pawn on h2 ensures K+B+P vs K — sufficient material, so draw is not triggered
val board = FenParser.parseBoard("8/4P3/8/8/8/8/k6P/7K").get
val ctx = GameContext.initial val ctx = GameContext.initial
.withBoard(board) .withBoard(board)
.withTurn(Color.White) .withTurn(Color.White)
@@ -105,8 +106,8 @@ class GameEngineNotationTest extends AnyFunSuite with Matchers:
// ── King normal move notation (line 246) ─────────────────────────── // ── King normal move notation (line 246) ───────────────────────────
test("undo after king move emits MoveUndoneEvent with K notation"): test("undo after king move emits MoveUndoneEvent with K notation"):
// White king on e1, no castling rights, black king far away // White king on e1, white rook on h1 — K+R vs K ensures sufficient material after the king move
val board = FenParser.parseBoard("k7/8/8/8/8/8/8/4K3").get val board = FenParser.parseBoard("k7/8/8/8/8/8/8/4K2R").get
val ctx = GameContext.initial val ctx = GameContext.initial
.withBoard(board) .withBoard(board)
.withTurn(Color.White) .withTurn(Color.White)
@@ -117,11 +118,11 @@ class GameEngineNotationTest extends AnyFunSuite with Matchers:
// King moves e1 -> f1 // King moves e1 -> f1
engine.processUserInput("e1f1") engine.processUserInput("e1f1")
events.exists(_.isInstanceOf[MoveExecutedEvent]) should be (true) events.exists { case _: MoveExecutedEvent => true; case _ => false } should be(true)
events.clear() events.clear()
engine.undo() engine.undo()
val evt = events.collect { case e: MoveUndoneEvent => e }.head val evt = events.collect { case e: MoveUndoneEvent => e }.head
evt.pgnNotation should startWith ("K") evt.pgnNotation should startWith("K")
evt.pgnNotation should include ("f1") evt.pgnNotation should include("f1")
@@ -1,6 +1,7 @@
package de.nowchess.chess.engine package de.nowchess.chess.engine
import de.nowchess.api.board.Color import de.nowchess.api.board.Color
import de.nowchess.api.game.{DrawReason, GameResult}
import de.nowchess.chess.observer.* import de.nowchess.chess.observer.*
import org.scalatest.funsuite.AnyFunSuite import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers import org.scalatest.matchers.should.Matchers
@@ -10,7 +11,7 @@ class GameEngineOutcomesTest extends AnyFunSuite with Matchers:
// ── Checkmate ─────────────────────────────────────────────────── // ── Checkmate ───────────────────────────────────────────────────
test("checkmate ends game with CheckmateEvent"): test("checkmate ends game with CheckmateEvent"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -22,9 +23,10 @@ class GameEngineOutcomesTest extends AnyFunSuite with Matchers:
engine.processUserInput("d8h4") engine.processUserInput("d8h4")
observer.hasEvent[CheckmateEvent] shouldBe true observer.hasEvent[CheckmateEvent] shouldBe true
engine.context.result shouldBe Some(GameResult.Win(Color.Black))
test("checkmate with white winner"): test("checkmate with white winner"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -41,59 +43,81 @@ class GameEngineOutcomesTest extends AnyFunSuite with Matchers:
val evt = observer.getEvent[CheckmateEvent] val evt = observer.getEvent[CheckmateEvent]
evt.isDefined shouldBe true evt.isDefined shouldBe true
evt.get.winner shouldBe Color.White evt.get.winner shouldBe Color.White
engine.context.result shouldBe Some(GameResult.Win(Color.White))
// ── Stalemate ─────────────────────────────────────────────────── // ── Stalemate ───────────────────────────────────────────────────
test("stalemate ends game with StalemateEvent"): test("stalemate ends game with DrawEvent(Stalemate)"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
val moves = List( val moves = List(
"e2e3", "a7a5", "e2e3",
"d1h5", "a8a6", "a7a5",
"h5a5", "h7h5", "d1h5",
"h2h4", "a6h6", "a8a6",
"a5c7", "f7f6", "h5a5",
"c7d7", "e8f7", "h7h5",
"d7b7", "d8d3", "h2h4",
"b7b8", "d3h7", "a6h6",
"b8c8", "f7g6" "a5c7",
"f7f6",
"c7d7",
"e8f7",
"d7b7",
"d8d3",
"b7b8",
"d3h7",
"b8c8",
"f7g6",
) )
moves.foreach(engine.processUserInput) moves.foreach(engine.processUserInput)
observer.clear() observer.clear()
engine.processUserInput("c8e6") engine.processUserInput("c8e6")
observer.hasEvent[StalemateEvent] shouldBe true val evt = observer.getEvent[DrawEvent]
evt.isDefined shouldBe true
evt.get.reason shouldBe DrawReason.Stalemate
engine.context.result shouldBe Some(GameResult.Draw(DrawReason.Stalemate))
test("stalemate when king has no moves and no pieces"): test("stalemate board is not reset after draw"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
val moves = List( val moves = List(
"e2e3", "a7a5", "e2e3",
"d1h5", "a8a6", "a7a5",
"h5a5", "h7h5", "d1h5",
"h2h4", "a6h6", "a8a6",
"a5c7", "f7f6", "h5a5",
"c7d7", "e8f7", "h7h5",
"d7b7", "d8d3", "h2h4",
"b7b8", "d3h7", "a6h6",
"b8c8", "f7g6", "a5c7",
"c8e6" "f7f6",
"c7d7",
"e8f7",
"d7b7",
"d8d3",
"b7b8",
"d3h7",
"b8c8",
"f7g6",
"c8e6",
) )
moves.foreach(engine.processUserInput) moves.foreach(engine.processUserInput)
observer.hasEvent[StalemateEvent] shouldBe true observer.hasEvent[DrawEvent] shouldBe true
engine.turn shouldBe Color.White engine.turn shouldBe Color.Black
// ── Check detection ──────────────────────────────────────────── // ── Check detection ────────────────────────────────────────────
test("check detected after move puts king in check"): test("check detected after move puts king in check"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -108,11 +132,12 @@ class GameEngineOutcomesTest extends AnyFunSuite with Matchers:
observer.hasEvent[CheckDetectedEvent] shouldBe true observer.hasEvent[CheckDetectedEvent] shouldBe true
test("check by knight"): test("check by knight"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
EngineTestHelpers.loadFen(engine, "8/4k3/8/8/3N4/8/8/4K3 w - - 0 1") // White has K+N+Q so the position is not insufficient material after Nd4f5
EngineTestHelpers.loadFen(engine, "8/4k3/8/8/3N4/8/8/3QK3 w - - 0 1")
observer.clear() observer.clear()
engine.processUserInput("d4f5") engine.processUserInput("d4f5")
@@ -122,7 +147,7 @@ class GameEngineOutcomesTest extends AnyFunSuite with Matchers:
// ── Fifty-move rule ──────────────────────────────────────────── // ── Fifty-move rule ────────────────────────────────────────────
test("fifty-move rule triggers when half-move clock reaches 100"): test("fifty-move rule triggers when half-move clock reaches 100"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -155,7 +180,7 @@ class GameEngineOutcomesTest extends AnyFunSuite with Matchers:
// ── Draw claim ──────────────────────────────────────────────── // ── Draw claim ────────────────────────────────────────────────
test("draw can be claimed when fifty-move rule is available"): test("draw can be claimed when fifty-move rule is available"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -164,13 +189,35 @@ class GameEngineOutcomesTest extends AnyFunSuite with Matchers:
engine.processUserInput("draw") engine.processUserInput("draw")
observer.hasEvent[DrawClaimedEvent] shouldBe true val evt = observer.getEvent[DrawEvent]
evt.isDefined shouldBe true
evt.get.reason shouldBe DrawReason.FiftyMoveRule
engine.context.result shouldBe Some(GameResult.Draw(DrawReason.FiftyMoveRule))
test("draw cannot be claimed when not available"): test("draw cannot be claimed when not available"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
engine.processUserInput("draw") engine.processUserInput("draw")
observer.hasEvent[InvalidMoveEvent] shouldBe true observer.hasEvent[InvalidMoveEvent] shouldBe true
// ── Insufficient material ──────────────────────────────────────────
test("insufficient material fires DrawEvent(InsufficientMaterial) after capture"):
val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer)
// White Bishop d4 captures Black Rook g7, leaving K+B vs K (insufficient material).
// Black king on g8 can still move (f7/h7 not controlled), so it is not stalemate.
EngineTestHelpers.loadFen(engine, "6k1/6r1/8/8/3B4/8/8/K7 w - - 0 1")
observer.clear()
engine.processUserInput("d4g7")
val evt = observer.getEvent[DrawEvent]
evt.isDefined shouldBe true
evt.get.reason shouldBe DrawReason.InsufficientMaterial
engine.context.result shouldBe Some(GameResult.Draw(DrawReason.InsufficientMaterial))
@@ -24,54 +24,54 @@ class GameEnginePromotionTest extends AnyFunSuite with Matchers:
test("processUserInput fires PromotionRequiredEvent when pawn reaches back rank") { test("processUserInput fires PromotionRequiredEvent when pawn reaches back rank") {
val promotionBoard = FenParser.parseBoard("8/4P3/4k3/8/8/8/8/8").get val promotionBoard = FenParser.parseBoard("8/4P3/4k3/8/8/8/8/8").get
val engine = engineWith(promotionBoard) val engine = engineWith(promotionBoard)
val events = captureEvents(engine) val events = captureEvents(engine)
engine.processUserInput("e7e8") engine.processUserInput("e7e8")
events.exists(_.isInstanceOf[PromotionRequiredEvent]) should be (true) events.exists { case _: PromotionRequiredEvent => true; case _ => false } should be(true)
events.collect { case e: PromotionRequiredEvent => e }.head.from should be (sq(File.E, Rank.R7)) events.collect { case e: PromotionRequiredEvent => e }.head.from should be(sq(File.E, Rank.R7))
} }
test("isPendingPromotion is true after PromotionRequired input") { test("isPendingPromotion is true after PromotionRequired input") {
val promotionBoard = FenParser.parseBoard("8/4P3/4k3/8/8/8/8/8").get val promotionBoard = FenParser.parseBoard("8/4P3/4k3/8/8/8/8/8").get
val engine = engineWith(promotionBoard) val engine = engineWith(promotionBoard)
captureEvents(engine) captureEvents(engine)
engine.processUserInput("e7e8") engine.processUserInput("e7e8")
engine.isPendingPromotion should be (true) engine.isPendingPromotion should be(true)
} }
test("isPendingPromotion is false before any promotion input") { test("isPendingPromotion is false before any promotion input") {
val engine = new GameEngine() val engine = new GameEngine()
engine.isPendingPromotion should be (false) engine.isPendingPromotion should be(false)
} }
test("completePromotion fires MoveExecutedEvent with promoted piece") { test("completePromotion fires MoveExecutedEvent with promoted piece") {
val promotionBoard = FenParser.parseBoard("8/4P3/4k3/8/8/8/8/8").get val promotionBoard = FenParser.parseBoard("8/4P3/4k3/8/8/8/8/8").get
val engine = engineWith(promotionBoard) val engine = engineWith(promotionBoard)
val events = captureEvents(engine) val events = captureEvents(engine)
engine.processUserInput("e7e8") engine.processUserInput("e7e8")
engine.completePromotion(PromotionPiece.Queen) engine.completePromotion(PromotionPiece.Queen)
engine.isPendingPromotion should be (false) engine.isPendingPromotion should be(false)
engine.board.pieceAt(sq(File.E, Rank.R8)) should be (Some(Piece(Color.White, PieceType.Queen))) engine.board.pieceAt(sq(File.E, Rank.R8)) should be(Some(Piece(Color.White, PieceType.Queen)))
engine.board.pieceAt(sq(File.E, Rank.R7)) should be (None) engine.board.pieceAt(sq(File.E, Rank.R7)) should be(None)
engine.context.moves.last.moveType shouldBe MoveType.Promotion(PromotionPiece.Queen) engine.context.moves.last.moveType shouldBe MoveType.Promotion(PromotionPiece.Queen)
events.exists(_.isInstanceOf[MoveExecutedEvent]) should be (true) events.exists { case _: MoveExecutedEvent => true; case _ => false } should be(true)
} }
test("completePromotion with rook underpromotion") { test("completePromotion with rook underpromotion") {
val promotionBoard = FenParser.parseBoard("8/4P3/4k3/8/8/8/8/8").get val promotionBoard = FenParser.parseBoard("8/4P3/4k3/8/8/8/8/8").get
val engine = engineWith(promotionBoard) val engine = engineWith(promotionBoard)
captureEvents(engine) captureEvents(engine)
engine.processUserInput("e7e8") engine.processUserInput("e7e8")
engine.completePromotion(PromotionPiece.Rook) engine.completePromotion(PromotionPiece.Rook)
engine.board.pieceAt(sq(File.E, Rank.R8)) should be (Some(Piece(Color.White, PieceType.Rook))) engine.board.pieceAt(sq(File.E, Rank.R8)) should be(Some(Piece(Color.White, PieceType.Rook)))
} }
test("completePromotion with no pending promotion fires InvalidMoveEvent") { test("completePromotion with no pending promotion fires InvalidMoveEvent") {
@@ -80,71 +80,71 @@ class GameEnginePromotionTest extends AnyFunSuite with Matchers:
engine.completePromotion(PromotionPiece.Queen) engine.completePromotion(PromotionPiece.Queen)
events.exists(_.isInstanceOf[InvalidMoveEvent]) should be (true) events.exists { case _: InvalidMoveEvent => true; case _ => false } should be(true)
engine.isPendingPromotion should be (false) engine.isPendingPromotion should be(false)
} }
test("completePromotion fires CheckDetectedEvent when promotion gives check") { test("completePromotion fires CheckDetectedEvent when promotion gives check") {
val promotionBoard = FenParser.parseBoard("3k4/4P3/8/8/8/8/8/8").get val promotionBoard = FenParser.parseBoard("3k4/4P3/8/8/8/8/8/8").get
val engine = engineWith(promotionBoard) val engine = engineWith(promotionBoard)
val events = captureEvents(engine) val events = captureEvents(engine)
engine.processUserInput("e7e8") engine.processUserInput("e7e8")
engine.completePromotion(PromotionPiece.Queen) engine.completePromotion(PromotionPiece.Queen)
events.exists(_.isInstanceOf[CheckDetectedEvent]) should be (true) events.exists { case _: CheckDetectedEvent => true; case _ => false } should be(true)
} }
test("completePromotion results in Moved when promotion doesn't give check") { test("completePromotion results in Moved when promotion doesn't give check") {
val board = FenParser.parseBoard("8/4P3/8/8/8/8/k7/8").get val board = FenParser.parseBoard("8/4P3/8/8/8/8/k7/8").get
val engine = engineWith(board) val engine = engineWith(board)
val events = captureEvents(engine) val events = captureEvents(engine)
engine.processUserInput("e7e8") engine.processUserInput("e7e8")
engine.completePromotion(PromotionPiece.Queen) engine.completePromotion(PromotionPiece.Queen)
engine.isPendingPromotion should be (false) engine.isPendingPromotion should be(false)
engine.board.pieceAt(sq(File.E, Rank.R8)) should be (Some(Piece(Color.White, PieceType.Queen))) engine.board.pieceAt(sq(File.E, Rank.R8)) should be(Some(Piece(Color.White, PieceType.Queen)))
events.filter(_.isInstanceOf[MoveExecutedEvent]) should not be empty events.collect { case e: MoveExecutedEvent => e } should not be empty
events.exists(_.isInstanceOf[CheckDetectedEvent]) should be (false) events.exists { case _: CheckDetectedEvent => true; case _ => false } should be(false)
} }
test("completePromotion results in Checkmate when promotion delivers checkmate") { test("completePromotion results in Checkmate when promotion delivers checkmate") {
val board = FenParser.parseBoard("k7/7P/1K6/8/8/8/8/8").get val board = FenParser.parseBoard("k7/7P/1K6/8/8/8/8/8").get
val engine = engineWith(board) val engine = engineWith(board)
val events = captureEvents(engine) val events = captureEvents(engine)
engine.processUserInput("h7h8") engine.processUserInput("h7h8")
engine.completePromotion(PromotionPiece.Queen) engine.completePromotion(PromotionPiece.Queen)
engine.isPendingPromotion should be (false) engine.isPendingPromotion should be(false)
events.exists(_.isInstanceOf[CheckmateEvent]) should be (true) events.exists { case _: CheckmateEvent => true; case _ => false } should be(true)
} }
test("completePromotion results in Stalemate when promotion creates stalemate") { test("completePromotion results in Stalemate when promotion creates stalemate") {
val board = FenParser.parseBoard("k7/1PB5/1K6/8/8/8/8/8").get val board = FenParser.parseBoard("k7/1PB5/1K6/8/8/8/8/8").get
val engine = engineWith(board) val engine = engineWith(board)
val events = captureEvents(engine) val events = captureEvents(engine)
engine.processUserInput("b7b8") engine.processUserInput("b7b8")
engine.completePromotion(PromotionPiece.Knight) engine.completePromotion(PromotionPiece.Knight)
engine.isPendingPromotion should be (false) engine.isPendingPromotion should be(false)
events.exists(_.isInstanceOf[StalemateEvent]) should be (true) events.exists { case _: DrawEvent => true; case _ => false } should be(true)
} }
test("completePromotion with black pawn promotion results in Moved") { test("completePromotion with black pawn promotion results in Moved") {
val board = FenParser.parseBoard("k7/8/8/8/8/7K/4p3/8").get val board = FenParser.parseBoard("k7/8/8/8/8/7K/4p3/8").get
val engine = engineWith(board, Color.Black) val engine = engineWith(board, Color.Black)
val events = captureEvents(engine) val events = captureEvents(engine)
engine.processUserInput("e2e1") engine.processUserInput("e2e1")
engine.completePromotion(PromotionPiece.Queen) engine.completePromotion(PromotionPiece.Queen)
engine.isPendingPromotion should be (false) engine.isPendingPromotion should be(false)
engine.board.pieceAt(sq(File.E, Rank.R1)) should be (Some(Piece(Color.Black, PieceType.Queen))) engine.board.pieceAt(sq(File.E, Rank.R1)) should be(Some(Piece(Color.Black, PieceType.Queen)))
events.filter(_.isInstanceOf[MoveExecutedEvent]) should not be empty events.collect { case e: MoveExecutedEvent => e } should not be empty
events.exists(_.isInstanceOf[CheckDetectedEvent]) should be (false) events.exists { case _: CheckDetectedEvent => true; case _ => false } should be(false)
} }
test("completePromotion fires InvalidMoveEvent when legalMoves returns only Normal moves to back rank") { test("completePromotion fires InvalidMoveEvent when legalMoves returns only Normal moves to back rank") {
@@ -177,21 +177,21 @@ class GameEnginePromotionTest extends AnyFunSuite with Matchers:
DefaultRules.applyMove(context)(move) DefaultRules.applyMove(context)(move)
val promotionBoard = FenParser.parseBoard("8/4P3/4k3/8/8/8/8/8").get val promotionBoard = FenParser.parseBoard("8/4P3/4k3/8/8/8/8/8").get
val initialCtx = GameContext.initial.withBoard(promotionBoard).withTurn(Color.White) val initialCtx = GameContext.initial.withBoard(promotionBoard).withTurn(Color.White)
val engine = new GameEngine(initialCtx, delegatingRuleSet) val engine = new GameEngine(initialCtx, delegatingRuleSet)
val events = captureEvents(engine) val events = captureEvents(engine)
// isPromotionMove will fire because pawn is on rank 7 heading to rank 8, // isPromotionMove will fire because pawn is on rank 7 heading to rank 8,
// and legalMoves returns Normal candidates (still non-empty) — sets pendingPromotion // and legalMoves returns Normal candidates (still non-empty) — sets pendingPromotion
engine.processUserInput("e7e8") engine.processUserInput("e7e8")
engine.isPendingPromotion should be (true) engine.isPendingPromotion should be(true)
// completePromotion looks for Move(e7, e8, Promotion(Queen)) in legalMoves, // completePromotion looks for Move(e7, e8, Promotion(Queen)) in legalMoves,
// but only Normal moves exist → fires InvalidMoveEvent // but only Normal moves exist → fires InvalidMoveEvent
engine.completePromotion(PromotionPiece.Queen) engine.completePromotion(PromotionPiece.Queen)
engine.isPendingPromotion should be (false) engine.isPendingPromotion should be(false)
events.exists(_.isInstanceOf[InvalidMoveEvent]) should be (true) events.exists { case _: InvalidMoveEvent => true; case _ => false } should be(true)
val invalidEvt = events.collect { case e: InvalidMoveEvent => e }.last val invalidEvt = events.collect { case e: InvalidMoveEvent => e }.last
invalidEvt.reason should include ("Error completing promotion") invalidEvt.reason should include("Error completing promotion")
} }
@@ -1,6 +1,6 @@
package de.nowchess.chess.engine package de.nowchess.chess.engine
import de.nowchess.api.board.{Color, File, Rank, Square, Piece} import de.nowchess.api.board.{Color, File, Piece, Rank, Square}
import de.nowchess.api.game.GameContext import de.nowchess.api.game.GameContext
import de.nowchess.chess.observer.* import de.nowchess.chess.observer.*
import de.nowchess.io.fen.FenParser import de.nowchess.io.fen.FenParser
@@ -13,7 +13,7 @@ class GameEngineScenarioTest extends AnyFunSuite with Matchers:
// ── Observer wiring ──────────────────────────────────────────── // ── Observer wiring ────────────────────────────────────────────
test("observer subscribe and unsubscribe behavior"): test("observer subscribe and unsubscribe behavior"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
engine.processUserInput("e2e4") engine.processUserInput("e2e4")
@@ -56,28 +56,28 @@ class GameEngineScenarioTest extends AnyFunSuite with Matchers:
// ── Invalid moves (minimal) ──────────────────────────────────── // ── Invalid moves (minimal) ────────────────────────────────────
test("invalid move forms trigger InvalidMoveEvent and keep turn where relevant"): test("invalid move forms trigger InvalidMoveEvent and keep turn where relevant"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
engine.processUserInput("h3h4") engine.processUserInput("h3h4")
observer.hasEvent[InvalidMoveEvent] shouldBe true observer.hasEvent[InvalidMoveEvent] shouldBe true
engine.turn shouldBe Color.White // turn unchanged engine.turn shouldBe Color.White // turn unchanged
engine.processUserInput("e7e5") // try to move black pawn on white's turn engine.processUserInput("e7e5") // try to move black pawn on white's turn
observer.hasEvent[InvalidMoveEvent] shouldBe true observer.hasEvent[InvalidMoveEvent] shouldBe true
engine.processUserInput("e2e4") engine.processUserInput("e2e4")
engine.processUserInput("e5e4") // pawn backward engine.processUserInput("e5e4") // pawn backward
observer.hasEvent[InvalidMoveEvent] shouldBe true observer.hasEvent[InvalidMoveEvent] shouldBe true
// ── Undo/Redo ──────────────────────────────────────────────── // ── Undo/Redo ────────────────────────────────────────────────
test("undo redo success and empty-history failures"): test("undo redo success and empty-history failures"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -103,7 +103,7 @@ class GameEngineScenarioTest extends AnyFunSuite with Matchers:
// ── Fifty-move rule ──────────────────────────────────────────── // ── Fifty-move rule ────────────────────────────────────────────
test("fifty-move event and draw claim success/failure"): test("fifty-move event and draw claim success/failure"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -122,7 +122,7 @@ class GameEngineScenarioTest extends AnyFunSuite with Matchers:
engine.processUserInput("draw") engine.processUserInput("draw")
observer.hasEvent[DrawClaimedEvent] shouldBe true observer.hasEvent[DrawEvent] shouldBe true
// Initial position has no draw available // Initial position has no draw available
observer.clear() observer.clear()
@@ -11,7 +11,7 @@ class GameEngineSpecialMovesTest extends AnyFunSuite with Matchers:
// ── Castling ──────────────────────────────────────────────────── // ── Castling ────────────────────────────────────────────────────
test("kingside castling executes successfully"): test("kingside castling executes successfully"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -25,7 +25,7 @@ class GameEngineSpecialMovesTest extends AnyFunSuite with Matchers:
engine.turn shouldBe Color.Black engine.turn shouldBe Color.Black
test("queenside castling executes successfully"): test("queenside castling executes successfully"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -39,7 +39,7 @@ class GameEngineSpecialMovesTest extends AnyFunSuite with Matchers:
engine.turn shouldBe Color.Black engine.turn shouldBe Color.Black
test("undo castling emits PGN notation"): test("undo castling emits PGN notation"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -57,7 +57,7 @@ class GameEngineSpecialMovesTest extends AnyFunSuite with Matchers:
// ── En passant ────────────────────────────────────────────────── // ── En passant ──────────────────────────────────────────────────
test("en passant capture executes successfully"): test("en passant capture executes successfully"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -69,10 +69,10 @@ class GameEngineSpecialMovesTest extends AnyFunSuite with Matchers:
observer.hasEvent[MoveExecutedEvent] shouldBe true observer.hasEvent[MoveExecutedEvent] shouldBe true
val moveEvt = observer.getEvent[MoveExecutedEvent] val moveEvt = observer.getEvent[MoveExecutedEvent]
moveEvt.get.capturedPiece shouldBe defined // pawn was captured moveEvt.get.capturedPiece shouldBe defined // pawn was captured
test("undo en passant emits file-x-destination notation"): test("undo en passant emits file-x-destination notation"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -90,7 +90,7 @@ class GameEngineSpecialMovesTest extends AnyFunSuite with Matchers:
// ── Pawn promotion ───────────────────────────────────────────── // ── Pawn promotion ─────────────────────────────────────────────
test("pawn reaching back rank requires promotion"): test("pawn reaching back rank requires promotion"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -143,7 +143,7 @@ class GameEngineSpecialMovesTest extends AnyFunSuite with Matchers:
engine.turn shouldBe Color.Black engine.turn shouldBe Color.Black
test("promotion to Queen with discovered check emits CheckDetectedEvent"): test("promotion to Queen with discovered check emits CheckDetectedEvent"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -157,7 +157,7 @@ class GameEngineSpecialMovesTest extends AnyFunSuite with Matchers:
observer.hasEvent[CheckDetectedEvent] shouldBe true observer.hasEvent[CheckDetectedEvent] shouldBe true
test("promotion to Queen with checkmate emits CheckmateEvent"): test("promotion to Queen with checkmate emits CheckmateEvent"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
@@ -171,11 +171,12 @@ class GameEngineSpecialMovesTest extends AnyFunSuite with Matchers:
observer.hasEvent[CheckmateEvent] shouldBe true observer.hasEvent[CheckmateEvent] shouldBe true
test("undo promotion emits notation with piece suffix"): test("undo promotion emits notation with piece suffix"):
val engine = EngineTestHelpers.makeEngine() val engine = EngineTestHelpers.makeEngine()
val observer = new EngineTestHelpers.MockObserver() val observer = new EngineTestHelpers.MockObserver()
engine.subscribe(observer) engine.subscribe(observer)
EngineTestHelpers.loadFen(engine, "8/4P3/4k3/8/8/8/8/8 w - - 0 1") // White rook on h2 keeps material sufficient (K+B+R vs K) after bishop promotion
EngineTestHelpers.loadFen(engine, "8/4P3/4k3/8/8/8/7R/7K w - - 0 1")
engine.processUserInput("e7e8") engine.processUserInput("e7e8")
engine.completePromotion(PromotionPiece.Bishop) engine.completePromotion(PromotionPiece.Bishop)
observer.clear() observer.clear()
+1 -1
View File
@@ -1,3 +1,3 @@
MAJOR=0 MAJOR=0
MINOR=10 MINOR=13
PATCH=0 PATCH=0
+48
View File
@@ -1 +1,49 @@
## (2026-04-06) ## (2026-04-06)
## (2026-04-07)
## (2026-04-07)
### Features
* NCS-30 FEN Parser using ParserCombinators ([#21](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/21)) ([b4bc72f](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/b4bc72f7e49f94d6e1bc805c68680e5fe8ef8e36))
## (2026-04-08)
### Features
* NCS-30 FEN Parser using ParserCombinators ([#21](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/21)) ([b4bc72f](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/b4bc72f7e49f94d6e1bc805c68680e5fe8ef8e36))
* NCS-31 FastParse FEN ([#22](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/22)) ([7a045d3](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7a045d31d757bbc5aa6f4bad2664ebe8b8519cac))
## (2026-04-12)
### Features
* NCS-30 FEN Parser using ParserCombinators ([#21](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/21)) ([b4bc72f](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/b4bc72f7e49f94d6e1bc805c68680e5fe8ef8e36))
* NCS-31 FastParse FEN ([#22](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/22)) ([7a045d3](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7a045d31d757bbc5aa6f4bad2664ebe8b8519cac))
## (2026-04-12)
### Features
* NCS-30 FEN Parser using ParserCombinators ([#21](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/21)) ([b4bc72f](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/b4bc72f7e49f94d6e1bc805c68680e5fe8ef8e36))
* NCS-31 FastParse FEN ([#22](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/22)) ([7a045d3](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7a045d31d757bbc5aa6f4bad2664ebe8b8519cac))
## (2026-04-12)
### Features
* NCS-29 JSON - Cherry Picked ([#28](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/28)) ([dbcafd2](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/dbcafd286993e0604a6fa286c5543581a149439e))
* NCS-30 FEN Parser using ParserCombinators ([#21](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/21)) ([b4bc72f](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/b4bc72f7e49f94d6e1bc805c68680e5fe8ef8e36))
* NCS-31 FastParse FEN ([#22](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/22)) ([7a045d3](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7a045d31d757bbc5aa6f4bad2664ebe8b8519cac))
## (2026-04-12)
### Features
* NCS-25 Add linters to keep quality up ([#27](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/27)) ([fd4e67d](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/fd4e67d4f782a7e955822d90cb909d0a81676fb2))
* NCS-29 JSON - Cherry Picked ([#28](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/28)) ([dbcafd2](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/dbcafd286993e0604a6fa286c5543581a149439e))
* NCS-30 FEN Parser using ParserCombinators ([#21](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/21)) ([b4bc72f](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/b4bc72f7e49f94d6e1bc805c68680e5fe8ef8e36))
* NCS-31 FastParse FEN ([#22](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/22)) ([7a045d3](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7a045d31d757bbc5aa6f4bad2664ebe8b8519cac))
## (2026-04-14)
### Features
* NCS-14 implemented insufficient moves rule ([#30](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/30)) ([b0399a4](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/b0399a4e489950083066c9538df9a84dcc7a4613))
* NCS-25 Add linters to keep quality up ([#27](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/27)) ([fd4e67d](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/fd4e67d4f782a7e955822d90cb909d0a81676fb2))
* NCS-29 JSON - Cherry Picked ([#28](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/28)) ([dbcafd2](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/dbcafd286993e0604a6fa286c5543581a149439e))
* NCS-30 FEN Parser using ParserCombinators ([#21](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/21)) ([b4bc72f](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/b4bc72f7e49f94d6e1bc805c68680e5fe8ef8e36))
* NCS-31 FastParse FEN ([#22](https://git.janis-eccarius.de/NowChess/NowChessSystems/issues/22)) ([7a045d3](https://git.janis-eccarius.de/NowChess/NowChessSystems/commit/7a045d31d757bbc5aa6f4bad2664ebe8b8519cac))
+10
View File
@@ -19,6 +19,7 @@ scala {
scoverage { scoverage {
scoverageVersion.set(versions["SCOVERAGE"]!!) scoverageVersion.set(versions["SCOVERAGE"]!!)
excludedFiles.set(listOf(".*FenParserFastParse.*"))
} }
tasks.withType<ScalaCompile> { tasks.withType<ScalaCompile> {
@@ -38,9 +39,18 @@ dependencies {
} }
} }
implementation("org.scala-lang.modules:scala-parser-combinators_3:${versions["SCALA_PARSER_COMBINATORS"]!!}")
implementation("com.lihaoyi:fastparse_3:${versions["FASTPARSE"]!!}")
implementation(project(":modules:api")) implementation(project(":modules:api"))
implementation(project(":modules:rule")) implementation(project(":modules:rule"))
// Jackson for JSON serialization/deserialization
implementation("com.fasterxml.jackson.core:jackson-databind:${versions["JACKSON"]!!}")
implementation("com.fasterxml.jackson.core:jackson-core:${versions["JACKSON"]!!}")
implementation("com.fasterxml.jackson.module:jackson-module-scala_3:${versions["JACKSON_SCALA"]!!}")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${versions["JACKSON"]!!}")
testImplementation(platform("org.junit:junit-bom:5.13.4")) testImplementation(platform("org.junit:junit-bom:5.13.4"))
testImplementation("org.junit.jupiter:junit-jupiter") testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.scalatest:scalatest_3:${versions["SCALATEST"]!!}") testImplementation("org.scalatest:scalatest_3:${versions["SCALATEST"]!!}")
@@ -0,0 +1,38 @@
package de.nowchess.io
import de.nowchess.api.game.GameContext
import java.nio.file.{Files, Path}
import java.nio.charset.StandardCharsets
import scala.util.Try
/** Service for persisting and loading game states to/from disk.
*
* Abstracts file I/O operations away from the UI layer. Handles both reading and writing game files.
*/
trait GameFileService:
def saveGameToFile(context: GameContext, path: Path, exporter: GameContextExport): Either[String, Unit]
def loadGameFromFile(path: Path, importer: GameContextImport): Either[String, GameContext]
/** Default implementation using the file system. */
object FileSystemGameService extends GameFileService:
/** Save a game context to a file using the specified exporter. */
def saveGameToFile(context: GameContext, path: Path, exporter: GameContextExport): Either[String, Unit] =
Try {
val json = exporter.exportGameContext(context)
Files.write(path, json.getBytes(StandardCharsets.UTF_8))
()
}.fold(
ex => Left(s"Failed to save file: ${ex.getMessage}"),
_ => Right(()),
)
/** Load a game context from a file using the specified importer. */
def loadGameFromFile(path: Path, importer: GameContextImport): Either[String, GameContext] =
Try {
val json = new String(Files.readAllBytes(path), StandardCharsets.UTF_8)
importer.importGameContext(json)
}.fold(
ex => Left(s"Failed to load file: ${ex.getMessage}"),
result => result,
)
@@ -15,28 +15,22 @@ object FenExporter extends GameContextExport:
/** Build the FEN representation for a single rank. */ /** Build the FEN representation for a single rank. */
private def buildRankString(board: Board, rank: Rank): String = private def buildRankString(board: Board, rank: Rank): String =
val rankSquares = File.values.map(file => Square(file, rank)) val rankSquares = File.values.map(file => Square(file, rank))
val rankChars = scala.collection.mutable.ListBuffer[Char]() val (result, emptyCount) = rankSquares.foldLeft(("", 0)):
var emptyCount = 0 case ((acc, empty), square) =>
board.pieceAt(square) match
for square <- rankSquares do case Some(piece) =>
board.pieceAt(square) match val flushed = if empty > 0 then acc + empty.toString else acc
case Some(piece) => (flushed + pieceToFenChar(piece), 0)
if emptyCount > 0 then case None =>
rankChars += emptyCount.toString.charAt(0) (acc, empty + 1)
emptyCount = 0 if emptyCount > 0 then result + emptyCount.toString else result
rankChars += pieceToFenChar(piece)
case None =>
emptyCount += 1
if emptyCount > 0 then rankChars += emptyCount.toString.charAt(0)
rankChars.mkString
/** Convert a GameContext to a complete FEN string. */ /** Convert a GameContext to a complete FEN string. */
def gameContextToFen(context: GameContext): String = def gameContextToFen(context: GameContext): String =
val piecePlacement = boardToFen(context.board) val piecePlacement = boardToFen(context.board)
val activeColor = if context.turn == Color.White then "w" else "b" val activeColor = if context.turn == Color.White then "w" else "b"
val castling = castlingString(context.castlingRights) val castling = castlingString(context.castlingRights)
val enPassant = context.enPassantSquare.map(_.toString).getOrElse("-") val enPassant = context.enPassantSquare.map(_.toString).getOrElse("-")
val fullMoveNumber = 1 + (context.moves.length / 2) val fullMoveNumber = 1 + (context.moves.length / 2)
s"$piecePlacement $activeColor $castling $enPassant ${context.halfMoveClock} $fullMoveNumber" s"$piecePlacement $activeColor $castling $enPassant ${context.halfMoveClock} $fullMoveNumber"
@@ -44,10 +38,10 @@ object FenExporter extends GameContextExport:
/** Convert castling rights to FEN notation. */ /** Convert castling rights to FEN notation. */
private def castlingString(rights: CastlingRights): String = private def castlingString(rights: CastlingRights): String =
val wk = if rights.whiteKingSide then "K" else "" val wk = if rights.whiteKingSide then "K" else ""
val wq = if rights.whiteQueenSide then "Q" else "" val wq = if rights.whiteQueenSide then "Q" else ""
val bk = if rights.blackKingSide then "k" else "" val bk = if rights.blackKingSide then "k" else ""
val bq = if rights.blackQueenSide then "q" else "" val bq = if rights.blackQueenSide then "q" else ""
val result = s"$wk$wq$bk$bq" val result = s"$wk$wq$bk$bq"
if result.isEmpty then "-" else result if result.isEmpty then "-" else result
@@ -61,4 +55,3 @@ object FenExporter extends GameContextExport:
case PieceType.Queen => 'q' case PieceType.Queen => 'q'
case PieceType.King => 'k' case PieceType.King => 'k'
if piece.color == Color.White then base.toUpper else base if piece.color == Color.White then base.toUpper else base
@@ -6,28 +6,27 @@ import de.nowchess.io.GameContextImport
object FenParser extends GameContextImport: object FenParser extends GameContextImport:
/** Parse a complete FEN string into a GameContext. /** Parse a complete FEN string into a GameContext. Returns Left with error message if the format is invalid.
* Returns Left with error message if the format is invalid. */ */
def parseFen(fen: String): Either[String, GameContext] = def parseFen(fen: String): Either[String, GameContext] =
val parts = fen.trim.split("\\s+") val parts = fen.trim.split("\\s+")
if parts.length != 6 then if parts.length != 6 then Left(s"Invalid FEN: expected 6 space-separated fields, got ${parts.length}")
Left(s"Invalid FEN: expected 6 space-separated fields, got ${parts.length}")
else else
for for
board <- parseBoard(parts(0)).toRight("Invalid FEN: invalid board position") board <- parseBoard(parts(0)).toRight("Invalid FEN: invalid board position")
activeColor <- parseColor(parts(1)).toRight("Invalid FEN: invalid active color (expected 'w' or 'b')") activeColor <- parseColor(parts(1)).toRight("Invalid FEN: invalid active color (expected 'w' or 'b')")
castlingRights <- parseCastling(parts(2)).toRight("Invalid FEN: invalid castling rights") castlingRights <- parseCastling(parts(2)).toRight("Invalid FEN: invalid castling rights")
enPassant <- parseEnPassant(parts(3)).toRight("Invalid FEN: invalid en passant square") enPassant <- parseEnPassant(parts(3)).toRight("Invalid FEN: invalid en passant square")
halfMoveClock <- parts(4).toIntOption.toRight("Invalid FEN: invalid half-move clock (expected integer)") halfMoveClock <- parts(4).toIntOption.toRight("Invalid FEN: invalid half-move clock (expected integer)")
fullMoveNumber <- parts(5).toIntOption.toRight("Invalid FEN: invalid full move number (expected integer)") fullMoveNumber <- parts(5).toIntOption.toRight("Invalid FEN: invalid full move number (expected integer)")
_ <- Either.cond(halfMoveClock >= 0 && fullMoveNumber >= 1, (), "Invalid FEN: invalid move counts") _ <- Either.cond(halfMoveClock >= 0 && fullMoveNumber >= 1, (), "Invalid FEN: invalid move counts")
yield GameContext( yield GameContext(
board = board, board = board,
turn = activeColor, turn = activeColor,
castlingRights = castlingRights, castlingRights = castlingRights,
enPassantSquare = enPassant, enPassantSquare = enPassant,
halfMoveClock = halfMoveClock, halfMoveClock = halfMoveClock,
moves = List.empty moves = List.empty,
) )
def importGameContext(input: String): Either[String, GameContext] = def importGameContext(input: String): Either[String, GameContext] =
@@ -41,25 +40,26 @@ object FenParser extends GameContextImport:
/** Parse castling rights string (e.g. "KQkq", "K", "-") into unified castling rights. */ /** Parse castling rights string (e.g. "KQkq", "K", "-") into unified castling rights. */
private def parseCastling(s: String): Option[CastlingRights] = private def parseCastling(s: String): Option[CastlingRights] =
if s == "-" then if s == "-" then Some(CastlingRights.None)
Some(CastlingRights.None)
else if s.length <= 4 && s.forall(c => "KQkq".contains(c)) then else if s.length <= 4 && s.forall(c => "KQkq".contains(c)) then
Some(CastlingRights( Some(
whiteKingSide = s.contains('K'), CastlingRights(
whiteQueenSide = s.contains('Q'), whiteKingSide = s.contains('K'),
blackKingSide = s.contains('k'), whiteQueenSide = s.contains('Q'),
blackQueenSide = s.contains('q') blackKingSide = s.contains('k'),
)) blackQueenSide = s.contains('q'),
else ),
None )
else None
/** Parse en passant target square ("-" for none, or algebraic like "e3"). */ /** Parse en passant target square ("-" for none, or algebraic like "e3"). */
private def parseEnPassant(s: String): Option[Option[Square]] = private def parseEnPassant(s: String): Option[Option[Square]] =
if s == "-" then Some(None) if s == "-" then Some(None)
else Square.fromAlgebraic(s).map(Some(_)) else Square.fromAlgebraic(s).map(Some(_))
/** Parses a FEN piece-placement string (rank 8 to rank 1, separated by '/') into a Board. /** Parses a FEN piece-placement string (rank 8 to rank 1, separated by '/') into a Board. Returns None if the format
* Returns None if the format is invalid. */ * is invalid.
*/
def parseBoard(fen: String): Option[Board] = def parseBoard(fen: String): Option[Board] =
val rankStrings = fen.split("/", -1) val rankStrings = fen.split("/", -1)
if rankStrings.length != 8 then None if rankStrings.length != 8 then None
@@ -73,28 +73,22 @@ object FenParser extends GameContextImport:
parsePieceRank(rankStr, rank).map(squares => acc :+ squares) parsePieceRank(rankStr, rank).map(squares => acc :+ squares)
parsedRanks.map(ranks => Board(ranks.flatten.toMap)) parsedRanks.map(ranks => Board(ranks.flatten.toMap))
/** Parse a single rank string (e.g. "rnbqkbnr" or "p3p3") into a list of (Square, Piece) pairs. /** Parse a single rank string (e.g. "rnbqkbnr" or "p3p3") into a list of (Square, Piece) pairs. Returns None if the
* Returns None if the rank string contains invalid characters or the wrong number of files. */ * rank string contains invalid characters or the wrong number of files.
*/
private def parsePieceRank(rankStr: String, rank: Rank): Option[List[(Square, Piece)]] = private def parsePieceRank(rankStr: String, rank: Rank): Option[List[(Square, Piece)]] =
var fileIdx = 0 val (fileIdx, failed, squares) = rankStr.foldLeft((0, false, List.empty[(Square, Piece)])):
val squares = scala.collection.mutable.ListBuffer[(Square, Piece)]() case ((idx, true, acc), _) => (idx, true, acc)
var failed = false case ((idx, false, acc), c) =>
if idx > 7 then (idx, true, acc)
for c <- rankStr if !failed do else if c.isDigit then (idx + c.asDigit, false, acc)
if fileIdx > 7 then else
failed = true charToPiece(c) match
else if c.isDigit then case None => (idx, true, acc)
fileIdx += c.asDigit case Some(piece) =>
else (idx + 1, false, acc :+ (Square(File.values(idx), rank) -> piece))
charToPiece(c) match
case None => failed = true
case Some(piece) =>
val file = File.values(fileIdx)
squares += (Square(file, rank) -> piece)
fileIdx += 1
if failed || fileIdx != 8 then None if failed || fileIdx != 8 then None
else Some(squares.toList) else Some(squares)
/** Convert a FEN piece character to a Piece. Uppercase = White, lowercase = Black. */ /** Convert a FEN piece character to a Piece. Uppercase = White, lowercase = Black. */
private def charToPiece(c: Char): Option[Piece] = private def charToPiece(c: Char): Option[Piece] =
@@ -108,4 +102,3 @@ object FenParser extends GameContextImport:
case 'k' => Some(PieceType.King) case 'k' => Some(PieceType.King)
case _ => None case _ => None
pieceTypeOpt.map(pt => Piece(color, pt)) pieceTypeOpt.map(pt => Piece(color, pt))
@@ -0,0 +1,120 @@
package de.nowchess.io.fen
import de.nowchess.api.board.*
import de.nowchess.api.game.GameContext
import de.nowchess.io.GameContextImport
import scala.util.parsing.combinator.RegexParsers
import FenParserSupport.*
object FenParserCombinators extends RegexParsers with GameContextImport:
override val skipWhitespace: Boolean = false
// ── Piece character ──────────────────────────────────────────────────────
private def pieceChar: Parser[Piece] =
"[prnbqkPRNBQK]".r ^^ { s =>
val c = s.head
val color = if c.isUpper then Color.White else Color.Black
Piece(color, charToPieceType(c.toLower))
}
private def emptyCount: Parser[Int] =
"[1-8]".r ^^ { s => s.toInt }
// ── Rank parser ──────────────────────────────────────────────────────────
private def rankToken: Parser[RankToken] =
pieceChar ^^ PieceToken.apply | emptyCount ^^ EmptyToken.apply
private def rankTokens: Parser[List[RankToken]] = rep1(rankToken)
/** Parse rank string for a given Rank, producing (Square, Piece) pairs. Fails if total file count != 8 or any piece
* placement exceeds board bounds.
*/
private def rankParser(rank: Rank): Parser[List[(Square, Piece)]] =
rankTokens >> { tokens =>
buildSquares(rank, tokens) match
case Some(squares) => success(squares)
case None => failure(s"Rank $rank is invalid")
}
// ── Board parser ─────────────────────────────────────────────────────────
private def rankSep: Parser[String] = "/"
/** Parse all 8 rank strings separated by '/', rank 8 down to rank 1. */
private def boardParser: Parser[Board] =
rankParser(Rank.R8) ~
(rankSep ~> rankParser(Rank.R7)) ~
(rankSep ~> rankParser(Rank.R6)) ~
(rankSep ~> rankParser(Rank.R5)) ~
(rankSep ~> rankParser(Rank.R4)) ~
(rankSep ~> rankParser(Rank.R3)) ~
(rankSep ~> rankParser(Rank.R2)) ~
(rankSep ~> rankParser(Rank.R1)) ^^ { case r8 ~ r7 ~ r6 ~ r5 ~ r4 ~ r3 ~ r2 ~ r1 =>
Board((r8 ++ r7 ++ r6 ++ r5 ++ r4 ++ r3 ++ r2 ++ r1).toMap)
}
// ── Color parser ─────────────────────────────────────────────────────────
private def colorParser: Parser[Color] =
("w" | "b") ^^ {
case "w" => Color.White
case _ => Color.Black
}
// ── Castling parser ──────────────────────────────────────────────────────
private def castlingParser: Parser[CastlingRights] =
"-" ^^^ CastlingRights.None |
"[KQkq]{1,4}".r ^^ { s =>
CastlingRights(
whiteKingSide = s.contains('K'),
whiteQueenSide = s.contains('Q'),
blackKingSide = s.contains('k'),
blackQueenSide = s.contains('q'),
)
}
// ── En passant parser ────────────────────────────────────────────────────
private def enPassantParser: Parser[Option[Square]] =
"-" ^^^ Option.empty[Square] |
"[a-h][1-8]".r ^^ { s => Square.fromAlgebraic(s) }
// ── Clock parser ─────────────────────────────────────────────────────────
private def clockParser: Parser[Int] =
"""\d+""".r ^^ { _.toInt }
// ── Full FEN parser ──────────────────────────────────────────────────────
private def fenParser: Parser[GameContext] =
boardParser ~ (" " ~> colorParser) ~ (" " ~> castlingParser) ~
(" " ~> enPassantParser) ~ (" " ~> clockParser) ~ (" " ~> clockParser) ^^ {
case board ~ color ~ castling ~ ep ~ halfMove ~ _ =>
GameContext(
board = board,
turn = color,
castlingRights = castling,
enPassantSquare = ep,
halfMoveClock = halfMove,
moves = List.empty,
)
}
// ── Public API ───────────────────────────────────────────────────────────
def parseFen(fen: String): Either[String, GameContext] =
parseAll(fenParser, fen) match
case Success(ctx, _) => Right(ctx)
case other => Left(s"Invalid FEN: ${other.toString}")
def parseBoard(fen: String): Option[Board] =
parseAll(boardParser, fen) match
case Success(board, _) => Some(board)
case _ => None
def importGameContext(input: String): Either[String, GameContext] =
parseFen(input)
@@ -0,0 +1,120 @@
package de.nowchess.io.fen
import fastparse.*
import fastparse.NoWhitespace.*
import de.nowchess.api.board.*
import de.nowchess.api.game.GameContext
import de.nowchess.io.GameContextImport
import FenParserSupport.*
object FenParserFastParse extends GameContextImport:
// ── Low-level parsers ────────────────────────────────────────────────────
private def pieceChar(using P[Any]): P[Piece] =
CharIn("prnbqkPRNBQK").!.map { s =>
val c = s.head
val color = if c.isUpper then Color.White else Color.Black
Piece(color, charToPieceType(c.toLower))
}
private def emptyCount(using P[Any]): P[Int] =
CharIn("1-8").!.map(_.toInt)
private def rankToken(using P[Any]): P[RankToken] =
pieceChar.map(PieceToken.apply) | emptyCount.map(EmptyToken.apply)
// ── Rank parser ──────────────────────────────────────────────────────────
private def rankParser(rank: Rank)(using P[Any]): P[List[(Square, Piece)]] =
rankToken.rep(1).flatMap { tokens =>
buildSquares(rank, tokens) match
case Some(squares) => Pass(squares)
case None => Fail
}
// ── Board parser ─────────────────────────────────────────────────────────
private def sep(using P[Any]): P[Unit] = LiteralStr("/").map(_ => ())
private def boardParser(using P[Any]): P[Board] =
(rankParser(Rank.R8) ~ sep ~
rankParser(Rank.R7) ~ sep ~
rankParser(Rank.R6) ~ sep ~
rankParser(Rank.R5) ~ sep ~
rankParser(Rank.R4) ~ sep ~
rankParser(Rank.R3) ~ sep ~
rankParser(Rank.R2) ~ sep ~
rankParser(Rank.R1)).map { case (r8, r7, r6, r5, r4, r3, r2, r1) =>
Board((r8 ++ r7 ++ r6 ++ r5 ++ r4 ++ r3 ++ r2 ++ r1).toMap)
}
// ── Color parser ─────────────────────────────────────────────────────────
private def colorParser(using P[Any]): P[Color] =
(LiteralStr("w") | LiteralStr("b")).!.map {
case "w" => Color.White
case _ => Color.Black
}
// ── Castling parser ──────────────────────────────────────────────────────
private def castlingParser(using P[Any]): P[CastlingRights] =
LiteralStr("-").map(_ => CastlingRights.None) |
CharsWhileIn("KQkq").!.map { s =>
CastlingRights(
whiteKingSide = s.contains('K'),
whiteQueenSide = s.contains('Q'),
blackKingSide = s.contains('k'),
blackQueenSide = s.contains('q'),
)
}
// ── En passant parser ────────────────────────────────────────────────────
private def enPassantParser(using P[Any]): P[Option[Square]] =
LiteralStr("-").map(_ => Option.empty[Square]) |
(CharIn("a-h") ~ CharIn("1-8")).!.map(s => Square.fromAlgebraic(s))
// ── Clock parser ─────────────────────────────────────────────────────────
private def clockParser(using P[Any]): P[Int] =
CharsWhileIn("0-9").!.map(_.toInt)
// ── Space helper ─────────────────────────────────────────────────────────
private def sp(using P[Any]): P[Unit] = LiteralStr(" ").map(_ => ())
// ── Full FEN parser ──────────────────────────────────────────────────────
private def fenParser(using P[Any]): P[GameContext] =
(boardParser ~ sp ~ colorParser ~ sp ~ castlingParser ~ sp ~
enPassantParser ~ sp ~ clockParser ~ sp ~ clockParser ~ End).map {
case (board, color, castling, ep, halfMove, _) =>
GameContext(
board = board,
turn = color,
castlingRights = castling,
enPassantSquare = ep,
halfMoveClock = halfMove,
moves = List.empty,
)
}
// ── Public API ───────────────────────────────────────────────────────────
def parseFen(fen: String): Either[String, GameContext] =
parse(fen, fenParser(using _)) match
case Parsed.Success(ctx, _) => Right(ctx)
case f: Parsed.Failure => Left(s"Invalid FEN: ${f.msg}")
private def boardParserFull(using P[Any]): P[Board] =
boardParser ~ End
def parseBoard(fen: String): Option[Board] =
parse(fen, boardParserFull(using _)) match
case Parsed.Success(board, _) => Some(board)
case _ => None
def importGameContext(input: String): Either[String, GameContext] =
parseFen(input)
@@ -0,0 +1,33 @@
package de.nowchess.io.fen
import de.nowchess.api.board.*
private[fen] object FenParserSupport:
sealed trait RankToken
case class PieceToken(piece: Piece) extends RankToken
case class EmptyToken(count: Int) extends RankToken
val charToPieceType: Map[Char, PieceType] = Map(
'p' -> PieceType.Pawn,
'r' -> PieceType.Rook,
'n' -> PieceType.Knight,
'b' -> PieceType.Bishop,
'q' -> PieceType.Queen,
'k' -> PieceType.King,
)
def buildSquares(rank: Rank, tokens: Seq[RankToken]): Option[List[(Square, Piece)]] =
tokens
.foldLeft(Option((List.empty[(Square, Piece)], 0))):
case (None, _) => None
case (Some((acc, fileIdx)), PieceToken(piece)) =>
if fileIdx > 7 then None
else
val sq = Square(File.values(fileIdx), rank)
Some((acc :+ (sq -> piece), fileIdx + 1))
case (Some((acc, fileIdx)), EmptyToken(n)) =>
val next = fileIdx + n
if next > 8 then None
else Some((acc, next))
.flatMap { case (squares, total) => if total == 8 then Some(squares) else None }
@@ -0,0 +1,139 @@
package de.nowchess.io.json
import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}
import com.fasterxml.jackson.core.util.{DefaultIndenter, DefaultPrettyPrinter}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import de.nowchess.api.board.*
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
import de.nowchess.api.game.GameContext
import de.nowchess.io.GameContextExport
import de.nowchess.io.pgn.PgnExporter
import java.time.{LocalDate, ZoneId, ZonedDateTime}
/** Exports a GameContext to a comprehensive JSON format using Jackson.
*
* The JSON includes:
* - Game metadata (players, event, date, result)
* - Board state (all pieces and their positions)
* - Current game state (turn, castling rights, en passant, half-move clock)
* - Move history in both algebraic notation (PGN) and detailed move objects
* - Captured pieces tracking (which pieces have been removed)
* - Timestamp for record-keeping
*/
object JsonExporter extends GameContextExport:
private val mapper = createMapper()
private def createMapper(): ObjectMapper =
val mapper = new ObjectMapper()
.registerModule(DefaultScalaModule)
// Configure pretty printer with custom spacing to match test expectations
val indenter = new DefaultIndenter(" ", "\n")
val printer = new DefaultPrettyPrinter()
printer.indentArraysWith(indenter)
printer.indentObjectsWith(indenter)
mapper.setDefaultPrettyPrinter(printer)
mapper.enable(SerializationFeature.INDENT_OUTPUT)
mapper
def exportGameContext(context: GameContext): String =
val record = buildGameRecord(context)
formatJson(mapper.writeValueAsString(record))
private def buildGameRecord(context: GameContext): JsonGameRecord =
val pgn =
try
Some(PgnExporter.exportGameContext(context))
catch {
case _: Exception => None
}
JsonGameRecord(
metadata = Some(buildMetadata()),
gameState = Some(buildGameState(context)),
moveHistory = pgn,
moves = Some(buildMoves(context.moves)),
capturedPieces = Some(buildCapturedPieces(context.board)),
timestamp = Some(ZonedDateTime.now(ZoneId.of("UTC")).toString),
)
private def buildMetadata(): JsonMetadata =
JsonMetadata(
event = Some("Game"),
players = Some(Map("white" -> "White Player", "black" -> "Black Player")),
date = Some(LocalDate.now().toString),
result = Some("*"),
)
private def buildGameState(context: GameContext): JsonGameState =
JsonGameState(
board = Some(buildBoardPieces(context.board)),
turn = Some(context.turn.label),
castlingRights = Some(buildCastlingRights(context.castlingRights)),
enPassantSquare = context.enPassantSquare.map(_.toString),
halfMoveClock = Some(context.halfMoveClock),
)
private def buildBoardPieces(board: Board): List[JsonPiece] =
board.pieces.toList.map { case (sq, p) =>
JsonPiece(Some(sq.toString), Some(p.color.label), Some(p.pieceType.label))
}
private def buildCastlingRights(rights: CastlingRights): JsonCastlingRights =
JsonCastlingRights(
Some(rights.whiteKingSide),
Some(rights.whiteQueenSide),
Some(rights.blackKingSide),
Some(rights.blackQueenSide),
)
private def buildMoves(moves: List[Move]): List[JsonMove] =
moves.map { m =>
val moveType = convertMoveType(m.moveType)
JsonMove(Some(m.from.toString), Some(m.to.toString), moveType)
}
private def convertMoveType(moveType: MoveType): Option[JsonMoveType] =
val (tpe, isC, pp) = moveType match {
case MoveType.Normal(isCapture) =>
(Some("normal"), Some(isCapture), None)
case MoveType.CastleKingside =>
(Some("castleKingside"), None, None)
case MoveType.CastleQueenside =>
(Some("castleQueenside"), None, None)
case MoveType.EnPassant =>
(Some("enPassant"), Some(true), None)
case MoveType.Promotion(piece) =>
val pName = piece match {
case PromotionPiece.Queen => "queen"
case PromotionPiece.Rook => "rook"
case PromotionPiece.Bishop => "bishop"
case PromotionPiece.Knight => "knight"
}
(Some("promotion"), None, Some(pName))
}
Some(JsonMoveType(tpe, isC, pp))
private def buildCapturedPieces(board: Board): JsonCapturedPieces =
val (byWhite, byBlack) = getCapturedPieces(board)
JsonCapturedPieces(Some(byWhite), Some(byBlack))
private def formatJson(json: String): String =
json
.replace(" : ", ": ")
.replaceAll("\\[\\s*\\]", "[]")
.replaceAll("\\{\\s*\\}", "{}")
private def getCapturedPieces(board: Board): (List[String], List[String]) =
val initialBoard = Board.initial
val captured = Square.all.flatMap { square =>
initialBoard.pieceAt(square).flatMap { initialPiece =>
board.pieceAt(square) match
case None => Some(initialPiece)
case Some(_) => None
}
}
val whiteCaptured = captured.filter(_.color == Color.White).map(_.pieceType.label).toList
val blackCaptured = captured.filter(_.color == Color.Black).map(_.pieceType.label).toList
(blackCaptured, whiteCaptured)
@@ -0,0 +1,55 @@
package de.nowchess.io.json
case class JsonMetadata(
event: Option[String] = None,
players: Option[Map[String, String]] = None,
date: Option[String] = None,
result: Option[String] = None,
)
case class JsonPiece(
square: Option[String] = None,
color: Option[String] = None,
piece: Option[String] = None,
)
case class JsonCastlingRights(
whiteKingSide: Option[Boolean] = None,
whiteQueenSide: Option[Boolean] = None,
blackKingSide: Option[Boolean] = None,
blackQueenSide: Option[Boolean] = None,
)
case class JsonGameState(
board: Option[List[JsonPiece]] = None,
turn: Option[String] = None,
castlingRights: Option[JsonCastlingRights] = None,
enPassantSquare: Option[String] = None,
halfMoveClock: Option[Int] = None,
)
case class JsonCapturedPieces(
byWhite: Option[List[String]] = None,
byBlack: Option[List[String]] = None,
)
case class JsonMoveType(
`type`: Option[String] = None,
isCapture: Option[Boolean] = None,
promotionPiece: Option[String] = None,
)
case class JsonMove(
from: Option[String] = None,
to: Option[String] = None,
`type`: Option[JsonMoveType] = None,
)
case class JsonGameRecord(
metadata: Option[JsonMetadata] = None,
gameState: Option[JsonGameState] = None,
moveHistory: Option[String] = None,
moves: Option[List[JsonMove]] = None,
capturedPieces: Option[JsonCapturedPieces] = None,
timestamp: Option[String] = None,
)
@@ -0,0 +1,119 @@
package de.nowchess.io.json
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import de.nowchess.api.board.*
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
import de.nowchess.api.game.GameContext
import de.nowchess.io.GameContextImport
import scala.util.Try
/** Imports a GameContext from JSON format using Jackson.
*
* Parses JSON exported by JsonExporter and reconstructs the GameContext including:
* - Board state
* - Current turn
* - Castling rights
* - En passant square
* - Half-move clock
* - Move history
*
* Returns Left(error message) if the JSON is malformed or invalid.
*/
object JsonParser extends GameContextImport:
private val mapper = new ObjectMapper()
.registerModule(DefaultScalaModule)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
def importGameContext(input: String): Either[String, GameContext] =
Try(mapper.readValue(input, classOf[JsonGameRecord])).toEither.left
.map(e => "JSON parsing error: " + e.getMessage)
.flatMap { data =>
val gs = data.gameState.getOrElse(JsonGameState())
val rawBoard = gs.board.getOrElse(Nil)
val rawTurn = gs.turn.getOrElse("White")
val rawCr = gs.castlingRights.getOrElse(JsonCastlingRights())
val rawHmc = gs.halfMoveClock.getOrElse(0)
val rawMoves = data.moves.getOrElse(Nil)
for
board <- parseBoard(rawBoard)
turn <- parseTurn(rawTurn)
castlingRights = parseCastlingRights(rawCr)
enPassantSquare = gs.enPassantSquare.flatMap(s => Square.fromAlgebraic(s))
moves <- parseMoves(rawMoves)
yield GameContext(
board = board,
turn = turn,
castlingRights = castlingRights,
enPassantSquare = enPassantSquare,
halfMoveClock = rawHmc,
moves = moves,
)
}
private def parseBoard(pieces: List[JsonPiece]): Either[String, Board] =
val parsedPieces = pieces.flatMap { p =>
for
sq <- p.square.flatMap(Square.fromAlgebraic)
color <- p.color.flatMap(parseColor)
pt <- p.piece.flatMap(parsePieceType)
yield (sq, Piece(color, pt))
}
Right(Board(parsedPieces.toMap))
private def parseTurn(color: String): Either[String, Color] =
parseColor(color).toRight(s"Invalid turn color: $color")
private def parseColor(color: String): Option[Color] =
if color == "White" then Some(Color.White)
else if color == "Black" then Some(Color.Black)
else None
private def parsePieceType(pt: String): Option[PieceType] =
pt match
case "Pawn" => Some(PieceType.Pawn)
case "Knight" => Some(PieceType.Knight)
case "Bishop" => Some(PieceType.Bishop)
case "Rook" => Some(PieceType.Rook)
case "Queen" => Some(PieceType.Queen)
case "King" => Some(PieceType.King)
case _ => None
private def parseCastlingRights(cr: JsonCastlingRights): CastlingRights =
CastlingRights(
cr.whiteKingSide.getOrElse(false),
cr.whiteQueenSide.getOrElse(false),
cr.blackKingSide.getOrElse(false),
cr.blackQueenSide.getOrElse(false),
)
private def parseMoves(moves: List[JsonMove]): Either[String, List[Move]] =
Right(moves.flatMap { m =>
for
from <- m.from.flatMap(Square.fromAlgebraic)
to <- m.to.flatMap(Square.fromAlgebraic)
moveType <- m.`type`.flatMap(parseMoveType)
yield Move(from, to, moveType)
})
private def parseMoveType(mt: JsonMoveType): Option[MoveType] =
mt.`type` match
case Some("normal") =>
Some(MoveType.Normal(mt.isCapture.getOrElse(false)))
case Some("castleKingside") =>
Some(MoveType.CastleKingside)
case Some("castleQueenside") =>
Some(MoveType.CastleQueenside)
case Some("enPassant") =>
Some(MoveType.EnPassant)
case Some("promotion") =>
val piece = mt.promotionPiece match
case Some("queen") => PromotionPiece.Queen
case Some("rook") => PromotionPiece.Rook
case Some("bishop") => PromotionPiece.Bishop
case Some("knight") => PromotionPiece.Knight
case _ => PromotionPiece.Queen // default
Some(MoveType.Promotion(piece))
case _ => None
@@ -11,39 +11,38 @@ object PgnExporter extends GameContextExport:
/** Export a GameContext to PGN format. */ /** Export a GameContext to PGN format. */
def exportGameContext(context: GameContext): String = def exportGameContext(context: GameContext): String =
val headers = Map( val headers = Map(
"Event" -> "?", "Event" -> "?",
"White" -> "?", "White" -> "?",
"Black" -> "?", "Black" -> "?",
"Result" -> "*" "Result" -> "*",
) )
exportGame(headers, context.moves) exportGame(headers, context.moves)
/** Export a game with headers and moves to PGN format. */ /** Export a game with headers and moves to PGN format. */
def exportGame(headers: Map[String, String], moves: List[Move]): String = def exportGame(headers: Map[String, String], moves: List[Move]): String =
val headerLines = headers.map { case (key, value) => val headerLines = headers
s"""[$key "$value"]""" .map { case (key, value) =>
}.mkString("\n") s"""[$key "$value"]"""
val moveText = if moves.isEmpty then ""
else
var ctx = GameContext.initial
val sanMoves = moves.map { move =>
val algebraic = moveToAlgebraic(move, ctx.board)
ctx = DefaultRules.applyMove(ctx)(move)
algebraic
} }
.mkString("\n")
val groupedMoves = sanMoves.zipWithIndex.groupBy(_._2 / 2) val moveText =
val moveLines = for (moveNumber, movePairs) <- groupedMoves.toList.sortBy(_._1) yield if moves.isEmpty then ""
val moveNum = moveNumber + 1 else
val whiteMoveStr = movePairs.find(_._2 % 2 == 0).map(_._1).getOrElse("") val contexts = moves.scanLeft(GameContext.initial)((ctx, move) => DefaultRules.applyMove(ctx)(move))
val blackMoveStr = movePairs.find(_._2 % 2 == 1).map(_._1).getOrElse("") val sanMoves = moves.zip(contexts).map { case (move, ctx) => moveToAlgebraic(move, ctx.board) }
if blackMoveStr.isEmpty then s"$moveNum. $whiteMoveStr"
else s"$moveNum. $whiteMoveStr $blackMoveStr"
val termination = headers.getOrElse("Result", "*") val groupedMoves = sanMoves.zipWithIndex.groupBy(_._2 / 2)
moveLines.mkString(" ") + s" $termination" val moveLines = for (moveNumber, movePairs) <- groupedMoves.toList.sortBy(_._1) yield
val moveNum = moveNumber + 1
val whiteMoveStr = movePairs.find(_._2 % 2 == 0).map(_._1).getOrElse("")
val blackMoveStr = movePairs.find(_._2 % 2 == 1).map(_._1).getOrElse("")
if blackMoveStr.isEmpty then s"$moveNum. $whiteMoveStr"
else s"$moveNum. $whiteMoveStr $blackMoveStr"
val termination = headers.getOrElse("Result", "*")
moveLines.mkString(" ") + s" $termination"
if headerLines.isEmpty then moveText if headerLines.isEmpty then moveText
else if moveText.isEmpty then headerLines else if moveText.isEmpty then headerLines
@@ -55,7 +54,7 @@ object PgnExporter extends GameContextExport:
case MoveType.CastleKingside => "O-O" case MoveType.CastleKingside => "O-O"
case MoveType.CastleQueenside => "O-O-O" case MoveType.CastleQueenside => "O-O-O"
case MoveType.EnPassant => s"${move.from.file.toString.toLowerCase}x${move.to}" case MoveType.EnPassant => s"${move.from.file.toString.toLowerCase}x${move.to}"
case MoveType.Promotion(pp) => case MoveType.Promotion(pp) =>
val promSuffix = pp match val promSuffix = pp match
case PromotionPiece.Queen => "=Q" case PromotionPiece.Queen => "=Q"
case PromotionPiece.Rook => "=R" case PromotionPiece.Rook => "=R"
@@ -76,5 +75,3 @@ object PgnExporter extends GameContextExport:
case PieceType.Rook => s"R$capStr$dest" case PieceType.Rook => s"R$capStr$dest"
case PieceType.Queen => s"Q$capStr$dest" case PieceType.Queen => s"Q$capStr$dest"
case PieceType.King => s"K$capStr$dest" case PieceType.King => s"K$capStr$dest"
@@ -8,38 +8,40 @@ import de.nowchess.rules.sets.DefaultRules
/** A parsed PGN game containing headers and the resolved move list. */ /** A parsed PGN game containing headers and the resolved move list. */
case class PgnGame( case class PgnGame(
headers: Map[String, String], headers: Map[String, String],
moves: List[Move] moves: List[Move],
) )
object PgnParser extends GameContextImport: object PgnParser extends GameContextImport:
/** Strictly validate a PGN text. /** Strictly validate a PGN text. Returns Right(PgnGame) if every move token is a legal move in the evolving position.
* Returns Right(PgnGame) if every move token is a legal move in the evolving position. * Returns Left(error message) on the first illegal or impossible move, or any unrecognised token.
* Returns Left(error message) on the first illegal or impossible move, or any unrecognised token. */ */
def validatePgn(pgn: String): Either[String, PgnGame] = def validatePgn(pgn: String): Either[String, PgnGame] =
val lines = pgn.split("\n").map(_.trim) val lines = pgn.split("\n").map(_.trim)
val (headerLines, rest) = lines.span(_.startsWith("[")) val (headerLines, rest) = lines.span(_.startsWith("["))
val headers = parseHeaders(headerLines) val headers = parseHeaders(headerLines)
val moveText = rest.mkString(" ") val moveText = rest.mkString(" ")
validateMovesText(moveText).map(moves => PgnGame(headers, moves)) validateMovesText(moveText).map(moves => PgnGame(headers, moves))
/** Import a PGN text into a GameContext by validating and replaying all moves. /** Import a PGN text into a GameContext by validating and replaying all moves. Returns Right(GameContext) with all
* Returns Right(GameContext) with all moves applied and .moves populated. * moves applied and .moves populated. Returns Left(error message) if validation fails or move replay encounters an
* Returns Left(error message) if validation fails or move replay encounters an issue. */ * issue.
*/
def importGameContext(input: String): Either[String, GameContext] = def importGameContext(input: String): Either[String, GameContext] =
validatePgn(input).flatMap { game => validatePgn(input).flatMap { game =>
Right(game.moves.foldLeft(GameContext.initial)((ctx, move) => DefaultRules.applyMove(ctx)(move))) Right(game.moves.foldLeft(GameContext.initial)((ctx, move) => DefaultRules.applyMove(ctx)(move)))
} }
/** Parse a complete PGN text into a PgnGame with headers and moves. /** Parse a complete PGN text into a PgnGame with headers and moves. Always succeeds (returns Some); malformed tokens
* Always succeeds (returns Some); malformed tokens are silently skipped. */ * are silently skipped.
*/
def parsePgn(pgn: String): Option[PgnGame] = def parsePgn(pgn: String): Option[PgnGame] =
val lines = pgn.split("\n").map(_.trim) val lines = pgn.split("\n").map(_.trim)
val (headerLines, rest) = lines.span(_.startsWith("[")) val (headerLines, rest) = lines.span(_.startsWith("["))
val headers = parseHeaders(headerLines) val headers = parseHeaders(headerLines)
val moveText = rest.mkString(" ") val moveText = rest.mkString(" ")
val moves = parseMovesText(moveText) val moves = parseMovesText(moveText)
Some(PgnGame(headers, moves)) Some(PgnGame(headers, moves))
/** Parse PGN header lines of the form [Key "Value"]. */ /** Parse PGN header lines of the form [Key "Value"]. */
@@ -51,25 +53,25 @@ object PgnParser extends GameContextImport:
private def parseMovesText(moveText: String): List[Move] = private def parseMovesText(moveText: String): List[Move] =
val tokens = moveText.split("\\s+").filter(_.nonEmpty) val tokens = moveText.split("\\s+").filter(_.nonEmpty)
val (_, _, moves) = tokens.foldLeft( val (_, _, moves) = tokens.foldLeft(
(GameContext.initial, Color.White, List.empty[Move]) (GameContext.initial, Color.White, List.empty[Move]),
): ):
case (state @ (ctx, color, acc), token) => case (state @ (ctx, color, acc), token) =>
if isMoveNumberOrResult(token) then state if isMoveNumberOrResult(token) then state
else else
parseAlgebraicMove(token, ctx, color) match parseAlgebraicMove(token, ctx, color) match
case None => state case None => state
case Some(move) => case Some(move) =>
val nextCtx = DefaultRules.applyMove(ctx)(move) val nextCtx = DefaultRules.applyMove(ctx)(move)
(nextCtx, color.opposite, acc :+ move) (nextCtx, color.opposite, acc :+ move)
moves moves
/** True for move-number tokens ("1.", "12.") and PGN result tokens. */ /** True for move-number tokens ("1.", "12.") and PGN result tokens. */
private def isMoveNumberOrResult(token: String): Boolean = private def isMoveNumberOrResult(token: String): Boolean =
token.matches("""\d+\.""") || token.matches("""\d+\.""") ||
token == "*" || token == "*" ||
token == "1-0" || token == "1-0" ||
token == "0-1" || token == "0-1" ||
token == "1/2-1/2" token == "1/2-1/2"
/** Parse a single algebraic notation token into a Move, given the current game context. */ /** Parse a single algebraic notation token into a Move, given the current game context. */
def parseAlgebraicMove(notation: String, ctx: GameContext, color: Color): Option[Move] = def parseAlgebraicMove(notation: String, ctx: GameContext, color: Color): Option[Move] =
@@ -98,47 +100,52 @@ object PgnParser extends GameContextImport:
if clean.length < 2 then None if clean.length < 2 then None
else else
val destStr = clean.takeRight(2) val destStr = clean.takeRight(2)
Square.fromAlgebraic(destStr).flatMap: toSquare => Square
val disambig = clean.dropRight(2) .fromAlgebraic(destStr)
.flatMap: toSquare =>
val disambig = clean.dropRight(2)
val requiredPieceType: Option[PieceType] = val requiredPieceType: Option[PieceType] =
if disambig.nonEmpty && disambig.head.isUpper then charToPieceType(disambig.head) if disambig.nonEmpty && disambig.head.isUpper then charToPieceType(disambig.head)
else if clean.head.isUpper then charToPieceType(clean.head) else if clean.head.isUpper then charToPieceType(clean.head)
else Some(PieceType.Pawn) else Some(PieceType.Pawn)
val hint = val hint =
if disambig.nonEmpty && disambig.head.isUpper then disambig.tail if disambig.nonEmpty && disambig.head.isUpper then disambig.tail
else disambig else disambig
val promotion = extractPromotion(notation) val promotion = extractPromotion(notation)
// Get all legal moves for this color that reach toSquare // Get all legal moves for this color that reach toSquare
val allLegal = DefaultRules.allLegalMoves(ctx) val allLegal = DefaultRules.allLegalMoves(ctx)
val candidates = allLegal.filter { move => val candidates = allLegal.filter { move =>
move.to == toSquare && move.to == toSquare &&
ctx.board.pieceAt(move.from).exists(p => ctx.board
p.color == color && .pieceAt(move.from)
requiredPieceType.forall(_ == p.pieceType) .exists(p =>
) && p.color == color &&
(hint.isEmpty || matchesHint(move.from, hint)) && requiredPieceType.forall(_ == p.pieceType),
promotionMatches(move, promotion) ) &&
} (hint.isEmpty || matchesHint(move.from, hint)) &&
promotionMatches(move, promotion)
}
candidates.headOption candidates.headOption
/** True if `sq` matches a disambiguation hint (file letter, rank digit, or both). */ /** True if `sq` matches a disambiguation hint (file letter, rank digit, or both). */
private def matchesHint(sq: Square, hint: String): Boolean = private def matchesHint(sq: Square, hint: String): Boolean =
hint.forall(c => hint.forall(c =>
if c >= 'a' && c <= 'h' then sq.file.toString.equalsIgnoreCase(c.toString) if c >= 'a' && c <= 'h' then sq.file.toString.equalsIgnoreCase(c.toString)
else if c >= '1' && c <= '8' then sq.rank.ordinal == (c - '1') else if c >= '1' && c <= '8' then sq.rank.ordinal == (c - '1')
else true else true,
) )
private def promotionMatches(move: Move, promotion: Option[PromotionPiece]): Boolean = private def promotionMatches(move: Move, promotion: Option[PromotionPiece]): Boolean =
promotion match promotion match
case None => move.moveType match case None =>
case MoveType.Normal(_) | MoveType.EnPassant | MoveType.CastleKingside | MoveType.CastleQueenside => true move.moveType match
case _ => false case MoveType.Normal(_) | MoveType.EnPassant | MoveType.CastleKingside | MoveType.CastleQueenside => true
case _ => false
case Some(pp) => move.moveType == MoveType.Promotion(pp) case Some(pp) => move.moveType == MoveType.Promotion(pp)
/** Extract a promotion piece from a notation string containing =Q/=R/=B/=N. */ /** Extract a promotion piece from a notation string containing =Q/=R/=B/=N. */
@@ -168,17 +175,18 @@ object PgnParser extends GameContextImport:
/** Walk all move tokens, failing immediately on any unresolvable or illegal move. */ /** Walk all move tokens, failing immediately on any unresolvable or illegal move. */
private def validateMovesText(moveText: String): Either[String, List[Move]] = private def validateMovesText(moveText: String): Either[String, List[Move]] =
val tokens = moveText.split("\\s+").filter(_.nonEmpty) val tokens = moveText.split("\\s+").filter(_.nonEmpty)
tokens.foldLeft(Right((GameContext.initial, Color.White, List.empty[Move])): Either[String, (GameContext, Color, List[Move])]) { tokens
case (acc, token) => .foldLeft(
Right((GameContext.initial, Color.White, List.empty[Move])): Either[String, (GameContext, Color, List[Move])],
) { case (acc, token) =>
acc.flatMap { case (ctx, color, moves) => acc.flatMap { case (ctx, color, moves) =>
if isMoveNumberOrResult(token) then Right((ctx, color, moves)) if isMoveNumberOrResult(token) then Right((ctx, color, moves))
else else
parseAlgebraicMove(token, ctx, color) match parseAlgebraicMove(token, ctx, color) match
case None => Left(s"Illegal or impossible move: '$token'") case None => Left(s"Illegal or impossible move: '$token'")
case Some(move) => case Some(move) =>
val nextCtx = DefaultRules.applyMove(ctx)(move) val nextCtx = DefaultRules.applyMove(ctx)(move)
Right((nextCtx, color.opposite, moves :+ move)) Right((nextCtx, color.opposite, moves :+ move))
} }
}.map(_._3) }
.map(_._3)
@@ -0,0 +1,132 @@
package de.nowchess.io
import de.nowchess.api.game.GameContext
import de.nowchess.api.board.{File, Rank, Square}
import de.nowchess.api.move.Move
import de.nowchess.io.json.{JsonExporter, JsonParser}
import java.nio.file.{Files, Paths}
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import scala.util.Using
class GameFileServiceSuite extends AnyFunSuite with Matchers:
test("saveGameToFile: writes JSON file successfully") {
val tmpFile = Files.createTempFile("chess_test_", ".json")
try
val context = GameContext.initial
val result = FileSystemGameService.saveGameToFile(context, tmpFile, JsonExporter)
assert(result.isRight)
assert(Files.exists(tmpFile))
assert(Files.size(tmpFile) > 0)
finally Files.deleteIfExists(tmpFile)
}
test("loadGameFromFile: reads JSON file successfully") {
val tmpFile = Files.createTempFile("chess_test_", ".json")
try
val originalContext = GameContext.initial
// Save
FileSystemGameService.saveGameToFile(originalContext, tmpFile, JsonExporter)
// Load
val result = FileSystemGameService.loadGameFromFile(tmpFile, JsonParser)
assert(result.isRight)
val loaded = result.getOrElse(GameContext.initial)
assert(loaded == originalContext)
finally Files.deleteIfExists(tmpFile)
}
test("loadGameFromFile: returns error on missing file") {
val nonExistentFile = Paths.get("/tmp/nonexistent_chess_game_file_12345.json")
val result = FileSystemGameService.loadGameFromFile(nonExistentFile, JsonParser)
assert(result.isLeft)
}
test("saveGameToFile: persists game with moves") {
val tmpFile = Files.createTempFile("chess_test_moves_", ".json")
try
val move1 = Move(Square(File.E, Rank.R2), Square(File.E, Rank.R4))
val move2 = Move(Square(File.E, Rank.R7), Square(File.E, Rank.R5))
val context = GameContext.initial
.withMove(move1)
.withMove(move2)
val saveResult = FileSystemGameService.saveGameToFile(context, tmpFile, JsonExporter)
assert(saveResult.isRight)
val loadResult = FileSystemGameService.loadGameFromFile(tmpFile, JsonParser)
assert(loadResult.isRight)
val loaded = loadResult.getOrElse(GameContext.initial)
assert(loaded.moves.length == 2)
finally Files.deleteIfExists(tmpFile)
}
test("saveGameToFile: overwrites existing file") {
val tmpFile = Files.createTempFile("chess_test_overwrite_", ".json")
try
// Write first file
val context1 = GameContext.initial
FileSystemGameService.saveGameToFile(context1, tmpFile, JsonExporter)
val size1 = Files.size(tmpFile)
// Write second file (should overwrite)
val move = Move(Square(File.E, Rank.R2), Square(File.E, Rank.R4))
val context2 = GameContext.initial.withMove(move)
FileSystemGameService.saveGameToFile(context2, tmpFile, JsonExporter)
val loadResult = FileSystemGameService.loadGameFromFile(tmpFile, JsonParser)
assert(loadResult.isRight)
val loaded = loadResult.getOrElse(GameContext.initial)
assert(loaded.moves.length == 1)
finally Files.deleteIfExists(tmpFile)
}
test("loadGameFromFile: handles invalid JSON in file") {
val tmpFile = Files.createTempFile("chess_test_invalid_", ".json")
try
Files.write(tmpFile, "{ invalid json}".getBytes())
val result = FileSystemGameService.loadGameFromFile(tmpFile, JsonParser)
assert(result.isLeft)
finally Files.deleteIfExists(tmpFile)
}
test("round-trip: save and load preserves game state") {
val tmpFile = Files.createTempFile("chess_test_roundtrip_", ".json")
try
val move1 = Move(Square(File.A, Rank.R2), Square(File.A, Rank.R4))
val move2 = Move(Square(File.H, Rank.R7), Square(File.H, Rank.R5))
val original = GameContext.initial
.withMove(move1)
.withMove(move2)
.withHalfMoveClock(3)
FileSystemGameService.saveGameToFile(original, tmpFile, JsonExporter)
val loadResult = FileSystemGameService.loadGameFromFile(tmpFile, JsonParser)
assert(loadResult.isRight)
val loaded = loadResult.getOrElse(GameContext.initial)
assert(loaded.moves.length == 2)
assert(loaded.halfMoveClock == 3)
finally Files.deleteIfExists(tmpFile)
}
test("saveGameToFile: handles exporter that throws exception") {
val tmpFile = Files.createTempFile("chess_test_exporter_error_", ".json")
try
val context = GameContext.initial
val faultyExporter = new GameContextExport {
def exportGameContext(c: GameContext): String =
throw new RuntimeException("Export failed") // scalafix:ok DisableSyntax.throw
}
val result = FileSystemGameService.saveGameToFile(context, tmpFile, faultyExporter)
assert(result.isLeft)
assert(result.left.toOption.get.contains("Failed to save file"))
finally Files.deleteIfExists(tmpFile)
}
@@ -9,16 +9,18 @@ import org.scalatest.matchers.should.Matchers
class FenExporterTest extends AnyFunSuite with Matchers: class FenExporterTest extends AnyFunSuite with Matchers:
private def context( private def context(
piecePlacement: String, piecePlacement: String,
turn: Color, turn: Color,
castlingRights: CastlingRights, castlingRights: CastlingRights,
enPassantSquare: Option[Square], enPassantSquare: Option[Square],
halfMoveClock: Int, halfMoveClock: Int,
moveCount: Int moveCount: Int,
): GameContext = ): GameContext =
val board = FenParser.parseBoard(piecePlacement).getOrElse( val board = FenParser
fail(s"Invalid test board FEN: $piecePlacement") .parseBoard(piecePlacement)
) .getOrElse(
fail(s"Invalid test board FEN: $piecePlacement"),
)
val dummyMove = Move(Square(File.A, Rank.R2), Square(File.A, Rank.R3)) val dummyMove = Move(Square(File.A, Rank.R2), Square(File.A, Rank.R3))
GameContext( GameContext(
board = board, board = board,
@@ -26,7 +28,7 @@ class FenExporterTest extends AnyFunSuite with Matchers:
castlingRights = castlingRights, castlingRights = castlingRights,
enPassantSquare = enPassantSquare, enPassantSquare = enPassantSquare,
halfMoveClock = halfMoveClock, halfMoveClock = halfMoveClock,
moves = List.fill(moveCount)(dummyMove) moves = List.fill(moveCount)(dummyMove),
) )
test("exportGameContextToFen handles initial and typical developed position"): test("exportGameContextToFen handles initial and typical developed position"):
@@ -39,7 +41,7 @@ class FenExporterTest extends AnyFunSuite with Matchers:
castlingRights = CastlingRights.All, castlingRights = CastlingRights.All,
enPassantSquare = Some(Square(File.E, Rank.R3)), enPassantSquare = Some(Square(File.E, Rank.R3)),
halfMoveClock = 0, halfMoveClock = 0,
moveCount = 0 moveCount = 0,
) )
FenExporter.gameContextToFen(gameContext) shouldBe FenExporter.gameContextToFen(gameContext) shouldBe
"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1" "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"
@@ -51,7 +53,7 @@ class FenExporterTest extends AnyFunSuite with Matchers:
castlingRights = CastlingRights.None, castlingRights = CastlingRights.None,
enPassantSquare = None, enPassantSquare = None,
halfMoveClock = 0, halfMoveClock = 0,
moveCount = 0 moveCount = 0,
) )
FenExporter.gameContextToFen(noCastling) shouldBe FenExporter.gameContextToFen(noCastling) shouldBe
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w - - 0 1" "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w - - 0 1"
@@ -63,11 +65,11 @@ class FenExporterTest extends AnyFunSuite with Matchers:
whiteKingSide = true, whiteKingSide = true,
whiteQueenSide = false, whiteQueenSide = false,
blackKingSide = false, blackKingSide = false,
blackQueenSide = true blackQueenSide = true,
), ),
enPassantSquare = None, enPassantSquare = None,
halfMoveClock = 5, halfMoveClock = 5,
moveCount = 4 moveCount = 4,
) )
FenExporter.gameContextToFen(partialCastling) shouldBe FenExporter.gameContextToFen(partialCastling) shouldBe
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w Kq - 5 3" "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w Kq - 5 3"
@@ -78,7 +80,7 @@ class FenExporterTest extends AnyFunSuite with Matchers:
castlingRights = CastlingRights.All, castlingRights = CastlingRights.All,
enPassantSquare = Some(Square(File.C, Rank.R6)), enPassantSquare = Some(Square(File.C, Rank.R6)),
halfMoveClock = 2, halfMoveClock = 2,
moveCount = 4 moveCount = 4,
) )
FenExporter.gameContextToFen(withEnPassant) shouldBe FenExporter.gameContextToFen(withEnPassant) shouldBe
"rnbqkbnr/pp1ppppp/8/2pP4/8/8/PPPP1PPP/RNBQKBNR w KQkq c6 2 3" "rnbqkbnr/pp1ppppp/8/2pP4/8/8/PPPP1PPP/RNBQKBNR w KQkq c6 2 3"
@@ -90,7 +92,7 @@ class FenExporterTest extends AnyFunSuite with Matchers:
castlingRights = CastlingRights.All, castlingRights = CastlingRights.All,
enPassantSquare = None, enPassantSquare = None,
halfMoveClock = 42, halfMoveClock = 42,
moves = List.empty moves = List.empty,
) )
val fen = FenExporter.gameContextToFen(gameContext) val fen = FenExporter.gameContextToFen(gameContext)
FenParser.parseFen(fen) match FenParser.parseFen(fen) match
@@ -101,4 +103,3 @@ class FenExporterTest extends AnyFunSuite with Matchers:
val ctx = GameContext.initial val ctx = GameContext.initial
FenExporter.exportGameContext(ctx) shouldBe FenExporter.gameContextToFen(ctx) FenExporter.exportGameContext(ctx) shouldBe FenExporter.gameContextToFen(ctx)

Some files were not shown because too many files have changed in this diff Show More