refactor: NCS-22 NCS-23 reworked modules and tests #17

Merged
Janis merged 42 commits from refactor/NCS-22 into main 2026-04-06 09:07:40 +02:00
Showing only changes of commit 09a70ed435 - Show all commits
@@ -1,10 +1,41 @@
package de.nowchess.io.pgn
import de.nowchess.api.board.*
import de.nowchess.api.move.PromotionPiece
import de.nowchess.api.game.{GameHistory, HistoryMove}
import de.nowchess.api.move.{PromotionPiece, MoveType}
import de.nowchess.api.game.{GameHistory, HistoryMove, GameContext}
import de.nowchess.io.GameContextExport
import de.nowchess.rules.sets.DefaultRules
object PgnExporter:
object PgnExporter extends GameContextExport:
/** Export a GameContext to PGN format by replaying all moves and reconstructing HistoryMove records. */
def exportGameContext(context: GameContext): String =
val headers = Map(
"Event" -> "?",
"White" -> "?",
"Black" -> "?",
"Result" -> "*"
)
// Replay all moves to reconstruct HistoryMove records with full info
val historyMoves = scala.collection.mutable.ListBuffer[HistoryMove]()
var ctx = GameContext.initial
for move <- context.moves do
val color = ctx.turn
val pieceType = ctx.board.pieceAt(move.from).map(_.pieceType).getOrElse(PieceType.Pawn)
val isCapture = ctx.board.pieceAt(move.to).isDefined || move.moveType == MoveType.EnPassant
val castleSide = move.moveType match
case MoveType.CastleKingside => Some("Kingside")
case MoveType.CastleQueenside => Some("Queenside")
case _ => None
val promotionPiece = move.moveType match
case MoveType.Promotion(pp) => Some(pp)
case _ => None
historyMoves += HistoryMove(move.from, move.to, castleSide, promotionPiece, pieceType, isCapture)
ctx = DefaultRules.applyMove(ctx, move)
val history = GameHistory(historyMoves.toList, context.halfMoveClock)
exportGame(headers, history)
/** Export a game with headers and history to PGN format. */
def exportGame(headers: Map[String, String], history: GameHistory): String =