From 0800c3af1a22f10d2eb52da97299f4c70e74ad39 Mon Sep 17 00:00:00 2001 From: Janis Date: Sun, 29 Mar 2026 16:22:43 +0200 Subject: [PATCH] feat: extend GameHistory.Move to track promotionPiece Co-Authored-By: Claude Sonnet 4.6 --- .../de/nowchess/chess/logic/GameHistory.scala | 12 +++++++++++- .../de/nowchess/chess/logic/GameHistoryTest.scala | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/modules/core/src/main/scala/de/nowchess/chess/logic/GameHistory.scala b/modules/core/src/main/scala/de/nowchess/chess/logic/GameHistory.scala index 1cea5cd..0236286 100644 --- a/modules/core/src/main/scala/de/nowchess/chess/logic/GameHistory.scala +++ b/modules/core/src/main/scala/de/nowchess/chess/logic/GameHistory.scala @@ -1,12 +1,14 @@ package de.nowchess.chess.logic import de.nowchess.api.board.Square +import de.nowchess.api.move.PromotionPiece /** A single move recorded in the game history. Distinct from api.move.Move which represents user intent. */ case class HistoryMove( from: Square, to: Square, - castleSide: Option[CastleSide] + castleSide: Option[CastleSide], + promotionPiece: Option[PromotionPiece] = None ) /** Complete game history: ordered list of moves. */ @@ -20,5 +22,13 @@ case class GameHistory(moves: List[HistoryMove] = List.empty): def addMove(from: Square, to: Square, castleSide: Option[CastleSide]): GameHistory = addMove(HistoryMove(from, to, castleSide)) + def addMove( + from: Square, + to: Square, + castleSide: Option[CastleSide] = None, + promotionPiece: Option[PromotionPiece] = None + ): GameHistory = + addMove(HistoryMove(from, to, castleSide, promotionPiece)) + object GameHistory: val empty: GameHistory = GameHistory() diff --git a/modules/core/src/test/scala/de/nowchess/chess/logic/GameHistoryTest.scala b/modules/core/src/test/scala/de/nowchess/chess/logic/GameHistoryTest.scala index 7b9a878..cc85f59 100644 --- a/modules/core/src/test/scala/de/nowchess/chess/logic/GameHistoryTest.scala +++ b/modules/core/src/test/scala/de/nowchess/chess/logic/GameHistoryTest.scala @@ -1,6 +1,7 @@ package de.nowchess.chess.logic import de.nowchess.api.board.* +import de.nowchess.api.move.PromotionPiece import org.scalatest.funsuite.AnyFunSuite import org.scalatest.matchers.should.Matchers @@ -39,3 +40,17 @@ class GameHistoryTest extends AnyFunSuite with Matchers: val history = GameHistory.empty.addMove(sq(File.E, Rank.R2), sq(File.E, Rank.R4)) history.moves should have length 1 history.moves.head.castleSide shouldBe None + + test("Move with promotion records the promotion piece"): + val move = HistoryMove(sq(File.E, Rank.R7), sq(File.E, Rank.R8), None, Some(PromotionPiece.Queen)) + move.promotionPiece should be (Some(PromotionPiece.Queen)) + + test("Normal move has no promotion piece"): + val move = HistoryMove(sq(File.E, Rank.R2), sq(File.E, Rank.R4), None, None) + move.promotionPiece should be (None) + + test("addMove with promotion stores promotionPiece"): + val history = GameHistory.empty + val newHistory = history.addMove(sq(File.E, Rank.R7), sq(File.E, Rank.R8), None, Some(PromotionPiece.Rook)) + newHistory.moves should have length 1 + newHistory.moves.head.promotionPiece should be (Some(PromotionPiece.Rook))