refactor: NCS-22 NCS-23 reworked modules and tests (#17)
Build & Test (NowChessSystems) TeamCity build finished

Reviewed-on: #17
This commit was merged in pull request #17.
This commit is contained in:
2026-04-06 09:07:39 +02:00
parent 51ffd7aac9
commit 8f56a82104
98 changed files with 3752 additions and 5940 deletions
@@ -0,0 +1,39 @@
package de.nowchess.rules
import de.nowchess.api.game.GameContext
import de.nowchess.api.board.Square
import de.nowchess.api.move.Move
/** Extension point for chess rule variants (standard, Chess960, etc.).
* All rule queries are stateless: given a GameContext, return the answer.
*/
trait RuleSet:
/** All pseudo-legal moves for the piece on `square` (ignores check). */
def candidateMoves(context: GameContext, square: Square): List[Move]
/** Legal moves for `square`: candidates that don't leave own king in check. */
def legalMoves(context: GameContext, square: Square): List[Move]
/** All legal moves for the side to move. */
def allLegalMoves(context: GameContext): List[Move]
/** True if the side to move's king is in check. */
def isCheck(context: GameContext): Boolean
/** True if the side to move is in check and has no legal moves. */
def isCheckmate(context: GameContext): Boolean
/** True if the side to move is not in check and has no legal moves. */
def isStalemate(context: GameContext): Boolean
/** True if neither side has enough material to checkmate. */
def isInsufficientMaterial(context: GameContext): Boolean
/** True if halfMoveClock >= 100 (50-move rule). */
def isFiftyMoveRule(context: GameContext): Boolean
/** Apply a legal move to produce the next game context.
* Handles all special move types: castling, en passant, promotion.
* Updates castling rights, en passant square, half-move clock, turn, and move history.
*/
def applyMove(context: GameContext, move: Move): GameContext
@@ -0,0 +1,387 @@
package de.nowchess.rules.sets
import de.nowchess.api.board.*
import de.nowchess.api.game.GameContext
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
import de.nowchess.rules.RuleSet
import scala.annotation.tailrec
/** Standard chess rules implementation.
* Handles move generation, validation, check/checkmate/stalemate detection.
*/
object DefaultRules extends RuleSet:
// ── Direction vectors ──────────────────────────────────────────────
private val RookDirs: List[(Int, Int)] = List((1, 0), (-1, 0), (0, 1), (0, -1))
private val BishopDirs: List[(Int, Int)] = List((1, 1), (1, -1), (-1, 1), (-1, -1))
private val QueenDirs: List[(Int, Int)] = RookDirs ++ BishopDirs
private val KnightJumps: List[(Int, Int)] =
List((2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2))
// ── Pawn configuration helpers ─────────────────────────────────────
private def pawnForward(color: Color): Int = if color == Color.White then 1 else -1
private def pawnStartRank(color: Color): Int = if color == Color.White then 1 else 6
private def pawnPromoRank(color: Color): Int = if color == Color.White then 7 else 0
// ── Public API ─────────────────────────────────────────────────────
override def candidateMoves(context: GameContext, square: Square): List[Move] =
context.board.pieceAt(square).fold(List.empty[Move]) { piece =>
if piece.color != context.turn then List.empty[Move]
else piece.pieceType match
case PieceType.Pawn => pawnCandidates(context, square, piece.color)
case PieceType.Knight => knightCandidates(context, square, piece.color)
case PieceType.Bishop => slidingMoves(context, square, piece.color, BishopDirs)
case PieceType.Rook => slidingMoves(context, square, piece.color, RookDirs)
case PieceType.Queen => slidingMoves(context, square, piece.color, QueenDirs)
case PieceType.King => kingCandidates(context, square, piece.color)
}
override def legalMoves(context: GameContext, square: Square): List[Move] =
candidateMoves(context, square).filter { move =>
!leavesKingInCheck(context, move)
}
override def allLegalMoves(context: GameContext): List[Move] =
Square.all.flatMap(sq => legalMoves(context, sq)).toList
override def isCheck(context: GameContext): Boolean =
kingSquare(context.board, context.turn)
.fold(false)(sq => isAttackedBy(context.board, sq, context.turn.opposite))
override def isCheckmate(context: GameContext): Boolean =
isCheck(context) && allLegalMoves(context).isEmpty
override def isStalemate(context: GameContext): Boolean =
!isCheck(context) && allLegalMoves(context).isEmpty
override def isInsufficientMaterial(context: GameContext): Boolean =
insufficientMaterial(context.board)
override def isFiftyMoveRule(context: GameContext): Boolean =
context.halfMoveClock >= 100
// ── Sliding pieces (Bishop, Rook, Queen) ───────────────────────────
private def slidingMoves(
context: GameContext,
from: Square,
color: Color,
dirs: List[(Int, Int)]
): List[Move] =
dirs.flatMap(dir => castRay(context.board, from, color, dir))
private def castRay(
board: Board,
from: Square,
color: Color,
dir: (Int, Int)
): List[Move] =
@tailrec
def loop(sq: Square, acc: List[Move]): List[Move] =
sq.offset(dir._1, dir._2) match
case None => acc
case Some(next) =>
board.pieceAt(next) match
case None => loop(next, Move(from, next) :: acc)
case Some(p) if p.color != color => Move(from, next, MoveType.Normal(isCapture = true)) :: acc
case Some(_) => acc
loop(from, Nil).reverse
// ── Knight ─────────────────────────────────────────────────────────
private def knightCandidates(
context: GameContext,
from: Square,
color: Color
): List[Move] =
KnightJumps.flatMap { (df, dr) =>
from.offset(df, dr).flatMap { to =>
context.board.pieceAt(to) match
case Some(p) if p.color == color => None
case Some(_) => Some(Move(from, to, MoveType.Normal(isCapture = true)))
case None => Some(Move(from, to))
}
}
// ── King ───────────────────────────────────────────────────────────
private def kingCandidates(
context: GameContext,
from: Square,
color: Color
): List[Move] =
val steps = QueenDirs.flatMap { (df, dr) =>
from.offset(df, dr).flatMap { to =>
context.board.pieceAt(to) match
case Some(p) if p.color == color => None
case Some(_) => Some(Move(from, to, MoveType.Normal(isCapture = true)))
case None => Some(Move(from, to))
}
}
steps ++ castlingCandidates(context, from, color)
// ── Castling ───────────────────────────────────────────────────────
private case class CastlingMove(
kingFromAlg: String,
kingToAlg: String,
middleAlg: String,
rookFromAlg: String,
moveType: MoveType
)
private def castlingCandidates(
context: GameContext,
from: Square,
color: Color
): List[Move] =
color match
case Color.White => whiteCastles(context, from)
case Color.Black => blackCastles(context, from)
private def whiteCastles(context: GameContext, from: Square): List[Move] =
val expected = Square.fromAlgebraic("e1").getOrElse(from)
if from != expected then List.empty
else
val moves = scala.collection.mutable.ListBuffer[Move]()
addCastleMove(context, moves, context.castlingRights.whiteKingSide,
CastlingMove("e1", "g1", "f1", "h1", MoveType.CastleKingside))
addCastleMove(context, moves, context.castlingRights.whiteQueenSide,
CastlingMove("e1", "c1", "d1", "a1", MoveType.CastleQueenside))
moves.toList
private def blackCastles(context: GameContext, from: Square): List[Move] =
val expected = Square.fromAlgebraic("e8").getOrElse(from)
if from != expected then List.empty
else
val moves = scala.collection.mutable.ListBuffer[Move]()
addCastleMove(context, moves, context.castlingRights.blackKingSide,
CastlingMove("e8", "g8", "f8", "h8", MoveType.CastleKingside))
addCastleMove(context, moves, context.castlingRights.blackQueenSide,
CastlingMove("e8", "c8", "d8", "a8", MoveType.CastleQueenside))
moves.toList
private def addCastleMove(
context: GameContext,
moves: scala.collection.mutable.ListBuffer[Move],
castlingRight: Boolean,
castlingMove: CastlingMove
): Unit =
if castlingRight then
val clearSqs = List(castlingMove.middleAlg, castlingMove.kingToAlg).flatMap(Square.fromAlgebraic)
if squaresEmpty(context.board, clearSqs) then
for
kf <- Square.fromAlgebraic(castlingMove.kingFromAlg)
km <- Square.fromAlgebraic(castlingMove.middleAlg)
kt <- Square.fromAlgebraic(castlingMove.kingToAlg)
rf <- Square.fromAlgebraic(castlingMove.rookFromAlg)
do
val color = context.turn
val kingPresent = context.board.pieceAt(kf).exists(p => p.color == color && p.pieceType == PieceType.King)
val rookPresent = context.board.pieceAt(rf).exists(p => p.color == color && p.pieceType == PieceType.Rook)
val squaresSafe =
!isAttackedBy(context.board, kf, color.opposite) &&
!isAttackedBy(context.board, km, color.opposite) &&
!isAttackedBy(context.board, kt, color.opposite)
if kingPresent && rookPresent && squaresSafe then
moves += Move(kf, kt, castlingMove.moveType)
private def squaresEmpty(board: Board, squares: List[Square]): Boolean =
squares.forall(sq => board.pieceAt(sq).isEmpty)
// ── Pawn ───────────────────────────────────────────────────────────
private def pawnCandidates(
context: GameContext,
from: Square,
color: Color
): List[Move] =
val fwd = pawnForward(color)
val startRank = pawnStartRank(color)
val promoRank = pawnPromoRank(color)
val single = from.offset(0, fwd).filter(to => context.board.pieceAt(to).isEmpty)
val double = Option.when(from.rank.ordinal == startRank) {
from.offset(0, fwd).flatMap { mid =>
Option.when(context.board.pieceAt(mid).isEmpty) {
from.offset(0, fwd * 2).filter(to => context.board.pieceAt(to).isEmpty)
}.flatten
}
}.flatten
val diagonalCaptures = List(-1, 1).flatMap { df =>
from.offset(df, fwd).flatMap { to =>
context.board.pieceAt(to).filter(_.color != color).map(_ => to)
}
}
val epCaptures: List[Move] = context.enPassantSquare.toList.flatMap { epSq =>
List(-1, 1).flatMap { df =>
from.offset(df, fwd).filter(_ == epSq).map { to =>
Move(from, epSq, MoveType.EnPassant)
}
}
}
def toMoves(dest: Square, isCapture: Boolean): List[Move] =
if dest.rank.ordinal == promoRank then
List(
PromotionPiece.Queen, PromotionPiece.Rook,
PromotionPiece.Bishop, PromotionPiece.Knight
).map(pt => Move(from, dest, MoveType.Promotion(pt)))
else List(Move(from, dest, MoveType.Normal(isCapture = isCapture)))
val stepSquares = single.toList ++ double.toList
val stepMoves = stepSquares.flatMap(dest => toMoves(dest, isCapture = false))
val captureMoves = diagonalCaptures.flatMap(dest => toMoves(dest, isCapture = true))
stepMoves ++ captureMoves ++ epCaptures
// ── Check detection ────────────────────────────────────────────────
private def kingSquare(board: Board, color: Color): Option[Square] =
Square.all.find(sq =>
board.pieceAt(sq).exists(p => p.color == color && p.pieceType == PieceType.King)
)
private def isAttackedBy(board: Board, target: Square, attacker: Color): Boolean =
Square.all.exists { sq =>
board.pieceAt(sq).fold(false) { p =>
p.color == attacker && squareAttacks(board, sq, p, target)
}
}
private def squareAttacks(board: Board, from: Square, piece: Piece, target: Square): Boolean =
val fwd = pawnForward(piece.color)
piece.pieceType match
case PieceType.Pawn =>
from.offset(-1, fwd).contains(target) || from.offset(1, fwd).contains(target)
case PieceType.Knight =>
KnightJumps.exists { (df, dr) => from.offset(df, dr).contains(target) }
case PieceType.Bishop => rayReaches(board, from, BishopDirs, target)
case PieceType.Rook => rayReaches(board, from, RookDirs, target)
case PieceType.Queen => rayReaches(board, from, QueenDirs, target)
case PieceType.King =>
QueenDirs.exists { (df, dr) => from.offset(df, dr).contains(target) }
private def rayReaches(board: Board, from: Square, dirs: List[(Int, Int)], target: Square): Boolean =
dirs.exists { dir =>
@tailrec
def loop(sq: Square): Boolean = sq.offset(dir._1, dir._2) match
case None => false
case Some(next) if next == target => true
case Some(next) if board.pieceAt(next).isEmpty => loop(next)
case Some(_) => false
loop(from)
}
private def leavesKingInCheck(context: GameContext, move: Move): Boolean =
val nextBoard = context.board.applyMove(move)
val nextContext = context.withBoard(nextBoard)
isCheck(nextContext)
// ── Move application ───────────────────────────────────────────────
override def applyMove(context: GameContext, move: Move): GameContext =
val color = context.turn
val board = context.board
val newBoard = move.moveType match
case MoveType.CastleKingside => applyCastle(board, color, kingside = true)
case MoveType.CastleQueenside => applyCastle(board, color, kingside = false)
case MoveType.EnPassant => applyEnPassant(board, move)
case MoveType.Promotion(pp) => applyPromotion(board, move, color, pp)
case MoveType.Normal(_) => board.applyMove(move)
val newCastlingRights = updateCastlingRights(context.castlingRights, board, move, color)
val newEnPassantSquare = computeEnPassantSquare(board, move)
val isCapture = move.moveType match
case MoveType.Normal(capture) => capture
case MoveType.EnPassant => true
case _ => board.pieceAt(move.to).isDefined
val isPawnMove = board.pieceAt(move.from).exists(_.pieceType == PieceType.Pawn)
val newClock = if isPawnMove || isCapture then 0 else context.halfMoveClock + 1
context
.withBoard(newBoard)
.withTurn(color.opposite)
.withCastlingRights(newCastlingRights)
.withEnPassantSquare(newEnPassantSquare)
.withHalfMoveClock(newClock)
.withMove(move)
private def applyCastle(board: Board, color: Color, kingside: Boolean): Board =
val rank = if color == Color.White then Rank.R1 else Rank.R8
val (kingFrom, kingTo, rookFrom, rookTo) =
if kingside then
(Square(File.E, rank), Square(File.G, rank), Square(File.H, rank), Square(File.F, rank))
else
(Square(File.E, rank), Square(File.C, rank), Square(File.A, rank), Square(File.D, rank))
val king = board.pieceAt(kingFrom).getOrElse(Piece(color, PieceType.King))
val rook = board.pieceAt(rookFrom).getOrElse(Piece(color, PieceType.Rook))
board
.removed(kingFrom).removed(rookFrom)
.updated(kingTo, king)
.updated(rookTo, rook)
private def applyEnPassant(board: Board, move: Move): Board =
val capturedRank = move.from.rank // the captured pawn is on the same rank as the moving pawn
val capturedSquare = Square(move.to.file, capturedRank)
board.applyMove(move).removed(capturedSquare)
private def applyPromotion(board: Board, move: Move, color: Color, pp: PromotionPiece): Board =
val promotedType = pp match
case PromotionPiece.Queen => PieceType.Queen
case PromotionPiece.Rook => PieceType.Rook
case PromotionPiece.Bishop => PieceType.Bishop
case PromotionPiece.Knight => PieceType.Knight
board.removed(move.from).updated(move.to, Piece(color, promotedType))
private def updateCastlingRights(rights: CastlingRights, board: Board, move: Move, color: Color): CastlingRights =
val piece = board.pieceAt(move.from)
val isKingMove = piece.exists(_.pieceType == PieceType.King)
val isRookMove = piece.exists(_.pieceType == PieceType.Rook)
// Helper to check if a square is a rook's starting square
val whiteKingsideRook = Square(File.H, Rank.R1)
val whiteQueensideRook = Square(File.A, Rank.R1)
val blackKingsideRook = Square(File.H, Rank.R8)
val blackQueensideRook = Square(File.A, Rank.R8)
var r = rights
if isKingMove then r = r.revokeColor(color)
else if isRookMove then
if move.from == whiteKingsideRook then r = r.revokeKingSide(Color.White)
if move.from == whiteQueensideRook then r = r.revokeQueenSide(Color.White)
if move.from == blackKingsideRook then r = r.revokeKingSide(Color.Black)
if move.from == blackQueensideRook then r = r.revokeQueenSide(Color.Black)
// Also revoke if a rook is captured
if move.to == whiteKingsideRook then r = r.revokeKingSide(Color.White)
if move.to == whiteQueensideRook then r = r.revokeQueenSide(Color.White)
if move.to == blackKingsideRook then r = r.revokeKingSide(Color.Black)
if move.to == blackQueensideRook then r = r.revokeQueenSide(Color.Black)
r
private def computeEnPassantSquare(board: Board, move: Move): Option[Square] =
val piece = board.pieceAt(move.from)
val isDoublePawnPush = piece.exists(_.pieceType == PieceType.Pawn) &&
math.abs(move.to.rank.ordinal - move.from.rank.ordinal) == 2
if isDoublePawnPush then
// EP square is the square the pawn passed through
val epRankOrd = (move.from.rank.ordinal + move.to.rank.ordinal) / 2
Some(Square(move.from.file, Rank.values(epRankOrd)))
else None
// ── Insufficient material ──────────────────────────────────────────
private def insufficientMaterial(board: Board): Boolean =
val pieces = board.pieces.values.toList.filter(_.pieceType != PieceType.King)
pieces match
case Nil => true
case List(p) if p.pieceType == PieceType.Bishop || p.pieceType == PieceType.Knight => true
case List(p1, p2)
if p1.pieceType == PieceType.Bishop && p2.pieceType == PieceType.Bishop
&& p1.color != p2.color => true
case _ => false
@@ -0,0 +1,300 @@
package de.nowchess.rule
import de.nowchess.api.board.{CastlingRights, Color, Piece, PieceType, Square}
import de.nowchess.api.game.GameContext
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
import de.nowchess.io.fen.FenParser
import de.nowchess.rules.sets.DefaultRules
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
class DefaultRulesStateTransitionsTest extends AnyFunSuite with Matchers:
private def contextFromFen(fen: String): GameContext =
FenParser.parseFen(fen).fold(err => fail(err), identity)
private def sq(alg: String): Square =
Square.fromAlgebraic(alg).getOrElse(fail(s"Invalid square in test: $alg"))
test("isCheckmate returns true for a known mate pattern"):
val context = contextFromFen("rnb1kbnr/pppp1ppp/8/4p3/6Pq/5P2/PPPPP2P/RNBQKBNR w KQkq - 1 3")
DefaultRules.isCheck(context) shouldBe true
DefaultRules.isCheckmate(context) shouldBe true
DefaultRules.allLegalMoves(context) shouldBe empty
test("isStalemate returns true for a known stalemate pattern"):
val context = contextFromFen("7k/5K2/6Q1/8/8/8/8/8 b - - 0 1")
DefaultRules.isCheck(context) shouldBe false
DefaultRules.isStalemate(context) shouldBe true
DefaultRules.allLegalMoves(context) shouldBe empty
test("isInsufficientMaterial returns true for king versus king"):
val context = contextFromFen("8/8/8/8/8/8/4k3/4K3 w - - 0 1")
DefaultRules.isInsufficientMaterial(context) shouldBe true
test("isInsufficientMaterial returns true for king and bishop versus king"):
val context = contextFromFen("8/8/8/8/8/8/4k3/3BK3 w - - 0 1")
DefaultRules.isInsufficientMaterial(context) shouldBe true
test("isInsufficientMaterial returns false for king and rook versus king"):
val context = contextFromFen("8/8/8/8/8/8/4k3/3RK3 w - - 0 1")
DefaultRules.isInsufficientMaterial(context) shouldBe false
test("isFiftyMoveRule returns true when halfMoveClock is 100"):
val context = contextFromFen("8/8/8/8/8/8/4k3/4K3 w - - 100 1")
DefaultRules.isFiftyMoveRule(context) shouldBe true
test("applyMove toggles turn and records move"):
val move = Move(sq("e2"), sq("e4"))
val next = DefaultRules.applyMove(GameContext.initial, move)
next.turn shouldBe Color.Black
next.moves.lastOption shouldBe Some(move)
test("applyMove sets en passant square after double pawn push"):
val move = Move(sq("e2"), sq("e4"))
val next = DefaultRules.applyMove(GameContext.initial, move)
next.enPassantSquare shouldBe Some(sq("e3"))
test("applyMove clears en passant square for non double pawn push"):
val context = contextFromFen("4k3/8/8/8/8/8/4P3/4K3 w - d6 3 1")
val move = Move(sq("e2"), sq("e3"))
val next = DefaultRules.applyMove(context, move)
next.enPassantSquare shouldBe None
test("applyMove resets halfMoveClock on pawn move"):
val context = contextFromFen("4k3/8/8/8/8/8/4P3/4K3 w - - 12 1")
val move = Move(sq("e2"), sq("e4"))
val next = DefaultRules.applyMove(context, move)
next.halfMoveClock shouldBe 0
test("applyMove increments halfMoveClock on quiet non pawn move"):
val context = contextFromFen("4k3/8/8/8/8/8/8/4K1N1 w - - 7 1")
val move = Move(sq("g1"), sq("f3"))
val next = DefaultRules.applyMove(context, move)
next.halfMoveClock shouldBe 8
test("applyMove resets halfMoveClock on capture"):
val context = contextFromFen("r3k3/8/8/8/8/8/8/R3K3 w Qq - 9 1")
val move = Move(sq("a1"), sq("a8"), MoveType.Normal(isCapture = true))
val next = DefaultRules.applyMove(context, move)
next.halfMoveClock shouldBe 0
next.board.pieceAt(sq("a8")) shouldBe Some(Piece(Color.White, PieceType.Rook))
test("applyMove updates castling rights after king move"):
val context = contextFromFen("r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1")
val move = Move(sq("e1"), sq("e2"))
val next = DefaultRules.applyMove(context, move)
next.castlingRights.whiteKingSide shouldBe false
next.castlingRights.whiteQueenSide shouldBe false
next.castlingRights.blackKingSide shouldBe true
next.castlingRights.blackQueenSide shouldBe true
test("applyMove updates castling rights after rook move from h1"):
val context = contextFromFen("r3k2r/8/8/8/8/8/8/4K2R w KQkq - 0 1")
val move = Move(sq("h1"), sq("h2"))
val next = DefaultRules.applyMove(context, move)
next.castlingRights.whiteKingSide shouldBe false
next.castlingRights.whiteQueenSide shouldBe true
test("applyMove revokes opponent castling right when rook on starting square is captured"):
val context = contextFromFen("r3k3/8/8/8/8/8/8/R3K3 w Qq - 2 1")
val move = Move(sq("a1"), sq("a8"), MoveType.Normal(isCapture = true))
val next = DefaultRules.applyMove(context, move)
next.castlingRights.blackQueenSide shouldBe false
test("applyMove executes kingside castling and repositions king and rook"):
val context = contextFromFen("4k2r/8/8/8/8/8/8/R3K2R w KQk - 0 1")
val move = Move(sq("e1"), sq("g1"), MoveType.CastleKingside)
val next = DefaultRules.applyMove(context, move)
next.board.pieceAt(sq("g1")) shouldBe Some(Piece(Color.White, PieceType.King))
next.board.pieceAt(sq("f1")) shouldBe Some(Piece(Color.White, PieceType.Rook))
next.board.pieceAt(sq("e1")) shouldBe None
next.board.pieceAt(sq("h1")) shouldBe None
test("applyMove executes queenside castling and repositions king and rook"):
val context = contextFromFen("r3k3/8/8/8/8/8/8/R3K2R w KQq - 0 1")
val move = Move(sq("e1"), sq("c1"), MoveType.CastleQueenside)
val next = DefaultRules.applyMove(context, move)
next.board.pieceAt(sq("c1")) shouldBe Some(Piece(Color.White, PieceType.King))
next.board.pieceAt(sq("d1")) shouldBe Some(Piece(Color.White, PieceType.Rook))
next.board.pieceAt(sq("e1")) shouldBe None
next.board.pieceAt(sq("a1")) shouldBe None
test("applyMove executes en passant and removes captured pawn"):
val context = contextFromFen("k7/8/8/3pP3/8/8/8/7K w - d6 0 1")
val move = Move(sq("e5"), sq("d6"), MoveType.EnPassant)
val next = DefaultRules.applyMove(context, move)
next.board.pieceAt(sq("d6")) shouldBe Some(Piece(Color.White, PieceType.Pawn))
next.board.pieceAt(sq("d5")) shouldBe None
next.board.pieceAt(sq("e5")) shouldBe None
test("applyMove executes promotion with selected piece type"):
val context = contextFromFen("4k3/P7/8/8/8/8/8/4K3 w - - 0 1")
val move = Move(sq("a7"), sq("a8"), MoveType.Promotion(PromotionPiece.Knight))
val next = DefaultRules.applyMove(context, move)
next.board.pieceAt(sq("a8")) shouldBe Some(Piece(Color.White, PieceType.Knight))
next.board.pieceAt(sq("a7")) shouldBe None
test("candidateMoves returns empty for opponent piece on selected square"):
val context = GameContext.initial.withTurn(Color.Black)
DefaultRules.candidateMoves(context, sq("e2")) shouldBe empty
test("legalMoves keeps king safe by filtering pinned bishop moves"):
val context = contextFromFen("8/8/8/8/8/8/r1B1K3/8 w - - 0 1")
val bishopMoves = DefaultRules.legalMoves(context, sq("c2"))
bishopMoves shouldBe empty
test("applyMove preserves black castling rights after white kingside castling"):
val context = contextFromFen("r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1")
val move = Move(sq("e1"), sq("g1"), MoveType.CastleKingside)
val next = DefaultRules.applyMove(context, move)
next.castlingRights.whiteKingSide shouldBe false
next.castlingRights.whiteQueenSide shouldBe false
next.castlingRights.blackKingSide shouldBe true
next.castlingRights.blackQueenSide shouldBe true
test("applyMove can revoke both white castling rights when both rooks are captured"):
val context = GameContext(
board = contextFromFen("4k3/8/8/8/8/8/8/R3K2R w KQ - 0 1").board.updated(sq("a8"), Piece(Color.Black, PieceType.Queen)),
turn = Color.Black,
castlingRights = CastlingRights(true, true, false, false),
enPassantSquare = None,
halfMoveClock = 0,
moves = List.empty
)
val afterA1Capture = DefaultRules.applyMove(context, Move(sq("a8"), sq("a1"), MoveType.Normal(isCapture = true)))
val afterH1Capture = DefaultRules.applyMove(afterA1Capture, Move(sq("a1"), sq("h1"), MoveType.Normal(isCapture = true)))
afterH1Capture.castlingRights.whiteKingSide shouldBe false
afterH1Capture.castlingRights.whiteQueenSide shouldBe false
test("isInsufficientMaterial returns true for opposite color bishops only"):
val context = contextFromFen("8/8/8/8/8/8/4k1b1/3BK3 w - - 0 1")
DefaultRules.isInsufficientMaterial(context) shouldBe true
test("candidateMoves for rook includes enemy capture move"):
val context = contextFromFen("4k3/8/8/8/8/8/4K3/R6r w - - 0 1")
val rookMoves = DefaultRules.candidateMoves(context, sq("a1"))
rookMoves.exists(m => m.to == sq("h1") && m.moveType == MoveType.Normal(isCapture = true)) shouldBe true
test("candidateMoves for knight includes enemy capture move"):
val context = contextFromFen("4k3/8/8/8/8/3p4/5N2/4K3 w - - 0 1")
val knightMoves = DefaultRules.candidateMoves(context, sq("f2"))
knightMoves.exists(m => m.to == sq("d3") && m.moveType == MoveType.Normal(isCapture = true)) shouldBe true
test("candidateMoves includes black kingside and queenside castling options"):
val context = contextFromFen("r3k2r/8/8/8/8/8/8/4K3 b kq - 0 1")
val kingMoves = DefaultRules.candidateMoves(context, sq("e8"))
kingMoves.exists(_.moveType == MoveType.CastleKingside) shouldBe true
kingMoves.exists(_.moveType == MoveType.CastleQueenside) shouldBe true
test("applyMove executes black kingside castling and repositions pieces on rank 8"):
val context = contextFromFen("r3k2r/8/8/8/8/8/8/4K3 b kq - 0 1")
val move = Move(sq("e8"), sq("g8"), MoveType.CastleKingside)
val next = DefaultRules.applyMove(context, move)
next.board.pieceAt(sq("g8")) shouldBe Some(Piece(Color.Black, PieceType.King))
next.board.pieceAt(sq("f8")) shouldBe Some(Piece(Color.Black, PieceType.Rook))
next.board.pieceAt(sq("e8")) shouldBe None
next.board.pieceAt(sq("h8")) shouldBe None
test("applyMove revokes black castling rights when black rook moves from h8"):
val context = contextFromFen("r3k2r/8/8/8/8/8/8/4K3 b kq - 0 1")
val move = Move(sq("h8"), sq("h7"))
val next = DefaultRules.applyMove(context, move)
next.castlingRights.blackKingSide shouldBe false
next.castlingRights.blackQueenSide shouldBe true
test("applyMove revokes black queenside castling right when black rook moves from a8"):
val context = contextFromFen("r3k2r/8/8/8/8/8/8/4K3 b kq - 0 1")
val move = Move(sq("a8"), sq("a7"))
val next = DefaultRules.applyMove(context, move)
next.castlingRights.blackKingSide shouldBe true
next.castlingRights.blackQueenSide shouldBe false
test("applyMove revokes black kingside castling right when rook on h8 is captured"):
val context = contextFromFen("4k2r/8/8/8/8/8/8/4K2R w Kk - 0 1")
val move = Move(sq("h1"), sq("h8"), MoveType.Normal(isCapture = true))
val next = DefaultRules.applyMove(context, move)
next.castlingRights.blackKingSide shouldBe false
test("candidateMoves creates all promotion move variants for black pawn"):
val context = contextFromFen("4k3/8/8/8/8/8/p7/4K3 b - - 0 1")
val to = sq("a1")
val pawnMoves = DefaultRules.candidateMoves(context, sq("a2"))
val promotions = pawnMoves.collect { case Move(_, `to`, MoveType.Promotion(piece)) => piece }
promotions.toSet shouldBe Set(
PromotionPiece.Queen,
PromotionPiece.Rook,
PromotionPiece.Bishop,
PromotionPiece.Knight
)
test("applyMove promotion supports queen rook and bishop targets"):
val base = contextFromFen("4k3/P7/8/8/8/8/8/4K3 w - - 0 1")
val queen = DefaultRules.applyMove(base, Move(sq("a7"), sq("a8"), MoveType.Promotion(PromotionPiece.Queen)))
val rook = DefaultRules.applyMove(base, Move(sq("a7"), sq("a8"), MoveType.Promotion(PromotionPiece.Rook)))
val bishop = DefaultRules.applyMove(base, Move(sq("a7"), sq("a8"), MoveType.Promotion(PromotionPiece.Bishop)))
queen.board.pieceAt(sq("a8")) shouldBe Some(Piece(Color.White, PieceType.Queen))
rook.board.pieceAt(sq("a8")) shouldBe Some(Piece(Color.White, PieceType.Rook))
bishop.board.pieceAt(sq("a8")) shouldBe Some(Piece(Color.White, PieceType.Bishop))
@@ -0,0 +1,152 @@
package de.nowchess.rule
import de.nowchess.api.board.{Board, Color, File, Rank, Square, Piece, PieceType, CastlingRights}
import de.nowchess.api.game.GameContext
import de.nowchess.api.move.{Move, MoveType}
import de.nowchess.io.fen.FenParser
import de.nowchess.rules.sets.DefaultRules
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
class DefaultRulesTest extends AnyFunSuite with Matchers:
private val rules = DefaultRules
// ── Pawn moves ──────────────────────────────────────────────────
test("pawn can move forward one square"):
val fen = "8/8/8/8/8/8/4P3/8 w - - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
val pawnMoves = moves.filter(m => m.from == Square(File.E, Rank.R2))
pawnMoves.exists(m => m.to == Square(File.E, Rank.R3)) shouldBe true
test("pawn can move forward two squares from starting position"):
val context = GameContext.initial
val moves = rules.allLegalMoves(context)
val e2Moves = moves.filter(m => m.from == Square(File.E, Rank.R2))
e2Moves.exists(m => m.to == Square(File.E, Rank.R4)) shouldBe true
test("pawn can capture diagonally"):
// FEN: white pawn e4, black pawn d5
val fen = "8/8/8/3p4/4P3/8/8/8 w - - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
val captures = moves.filter(m => m.from == Square(File.E, Rank.R4) && m.moveType.isInstanceOf[MoveType.Normal])
captures.exists(m => m.to == Square(File.D, Rank.R5)) shouldBe true
test("pawn cannot move backward"):
// FEN: white pawn on e4
val fen = "8/8/8/8/4P3/8/8/8 w - - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
val pawnMoves = moves.filter(m => m.from == Square(File.E, Rank.R4))
pawnMoves.exists(m => m.to == Square(File.E, Rank.R3)) shouldBe false
// ── King in check filtering ──────────────────────────────────────
test("moving king out of check removes it from legal moves if king stays in check"):
// FEN: white king e1, black rook e8, white tries to move away
val fen = "4r3/8/8/8/8/8/8/4K3 w - - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
// King must move; e2 should be valid but d1 might be blocked by rook if still on same file
moves.filter(m => m.from == Square(File.E, Rank.R1)).nonEmpty shouldBe true
test("king cannot move to square attacked by opponent"):
// FEN: white king e1, black rook e2 defended by black king e3
val fen = "8/8/8/8/8/4k3/4r3/4K3 w - - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
// King cannot move to e2 (occupied and attacked)
val kingMovesToE2 = moves.filter(m => m.from == Square(File.E, Rank.R1) && m.to == Square(File.E, Rank.R2))
kingMovesToE2.isEmpty shouldBe true
// ── Castling legality ────────────────────────────────────────────
test("castling kingside is legal when king and rook unmoved and path clear"):
val fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQK2R w KQkq - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
val castles = moves.filter(m => m.moveType == MoveType.CastleKingside)
castles.nonEmpty shouldBe true
test("castling queenside is legal when king and rook unmoved and path clear"):
val fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
val castles = moves.filter(m => m.moveType == MoveType.CastleQueenside)
castles.nonEmpty shouldBe true
test("castling is illegal when castling rights are false"):
// FEN: king and rook in position, but castling rights disabled
val fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQK2R w - - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
val castles = moves.filter(m => m.moveType == MoveType.CastleKingside)
castles.isEmpty shouldBe true
test("castling is illegal when king is in check"):
// FEN: white king e1 in check from black rook e8
val fen = "4r3/8/8/8/8/8/8/R3K2R w KQ - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
val castles = moves.filter(m => m.moveType == MoveType.CastleKingside || m.moveType == MoveType.CastleQueenside)
castles.isEmpty shouldBe true
test("castling is illegal when path has piece in the way"):
// FEN: white king e1, white rook h1, white bishop f1 (blocks f-file)
val fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBR1 w KQkq - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
val castles = moves.filter(m => m.moveType == MoveType.CastleKingside)
castles.isEmpty shouldBe true
// ── En passant legality ──────────────────────────────────────────
test("en passant is legal when en passant square is set"):
// FEN: white pawn e5, black pawn d5 (just double-pushed), en passant square d6
val fen = "k7/8/8/3pP3/8/8/8/7K w - d6 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
val epMoves = moves.filter(m => m.moveType == MoveType.EnPassant)
epMoves.exists(m => m.to == Square(File.D, Rank.R6)) shouldBe true
test("en passant is illegal when en passant square is none"):
// FEN: white pawn e5, black pawn d5, but no en passant square
val fen = "k7/8/8/3pP3/8/8/8/7K w - - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
val epMoves = moves.filter(m => m.moveType == MoveType.EnPassant)
epMoves.isEmpty shouldBe true
// ── Pinned pieces ────────────────────────────────────────────────
test("pinned piece cannot move and expose king to check"):
// FEN: white king e1, white bishop d2 (pinned), black rook a2
val fen = "8/8/8/8/8/8/r1B1K3/8 w - - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
// Bishop on d2 is pinned by rook on a2; it cannot move
val bishopMoves = moves.filter(m => m.from == Square(File.C, Rank.R2))
bishopMoves.isEmpty shouldBe true
test("piece blocking a check is legal"):
// FEN: white king e1, white rook d1, black bishop a4 attacking e1 via d2
// Actually, this is complex. Let's use: white king e1, black rook e8, white pawn blocks on e2
val fen = "4r3/8/8/8/8/8/4P3/4K3 w - - 0 1"
val context = FenParser.parseFen(fen).fold(_ => fail(), identity)
val moves = rules.allLegalMoves(context)
// White is in check; only moves that block or move the king are legal
moves.nonEmpty shouldBe true