feat: NCS-10 Implement Pawn Promotion #12

Merged
lq64 merged 20 commits from feat/NCS-10 into main 2026-03-31 22:18:14 +02:00
2 changed files with 26 additions and 1 deletions
Showing only changes of commit 0800c3af1a - Show all commits
@@ -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()
@@ -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))