refactor(50-move-rule): extract handleParsedMove to reduce cognitive complexity in processUserInput
Build & Test (NowChessSystems) TeamCity build finished

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
LQ63
2026-04-01 10:29:11 +02:00
parent c8ea043ccc
commit 0e622e4f54
@@ -90,19 +90,17 @@ class GameEngine(
notifyObservers(event) notifyObservers(event)
case moveInput => case moveInput =>
// Try to parse as a move
Parser.parseMove(moveInput) match Parser.parseMove(moveInput) match
case None => case None =>
val event = InvalidMoveEvent( notifyObservers(InvalidMoveEvent(
currentBoard, currentBoard, currentHistory, currentTurn,
currentHistory,
currentTurn,
s"Invalid move format '$moveInput'. Use coordinate notation, e.g. e2e4." s"Invalid move format '$moveInput'. Use coordinate notation, e.g. e2e4."
) ))
notifyObservers(event)
case Some((from, to)) => case Some((from, to)) =>
// Create a move command with current state snapshot handleParsedMove(from, to, moveInput)
}
private def handleParsedMove(from: Square, to: Square, moveInput: String): Unit =
val cmd = MoveCommand( val cmd = MoveCommand(
from = from, from = from,
to = to, to = to,
@@ -110,14 +108,11 @@ class GameEngine(
previousHistory = Some(currentHistory), previousHistory = Some(currentHistory),
previousTurn = Some(currentTurn) previousTurn = Some(currentTurn)
) )
// Execute the move through GameController
GameController.processMove(currentBoard, currentHistory, currentTurn, moveInput) match GameController.processMove(currentBoard, currentHistory, currentTurn, moveInput) match
case MoveResult.InvalidFormat(_) | MoveResult.NoPiece | MoveResult.WrongColor | MoveResult.IllegalMove | MoveResult.Quit => case MoveResult.InvalidFormat(_) | MoveResult.NoPiece | MoveResult.WrongColor | MoveResult.IllegalMove | MoveResult.Quit =>
handleFailedMove(moveInput) handleFailedMove(moveInput)
case MoveResult.Moved(newBoard, newHistory, captured, newTurn) => case MoveResult.Moved(newBoard, newHistory, captured, newTurn) =>
// Move succeeded - store result and execute through invoker
val updatedCmd = cmd.copy(moveResult = Some(de.nowchess.chess.command.MoveResult.Successful(newBoard, newHistory, newTurn, captured))) val updatedCmd = cmd.copy(moveResult = Some(de.nowchess.chess.command.MoveResult.Successful(newBoard, newHistory, newTurn, captured)))
invoker.execute(updatedCmd) invoker.execute(updatedCmd)
updateGameState(newBoard, newHistory, newTurn) updateGameState(newBoard, newHistory, newTurn)
@@ -126,7 +121,6 @@ class GameEngine(
notifyObservers(FiftyMoveRuleAvailableEvent(currentBoard, currentHistory, currentTurn)) notifyObservers(FiftyMoveRuleAvailableEvent(currentBoard, currentHistory, currentTurn))
case MoveResult.MovedInCheck(newBoard, newHistory, captured, newTurn) => case MoveResult.MovedInCheck(newBoard, newHistory, captured, newTurn) =>
// Move succeeded with check
val updatedCmd = cmd.copy(moveResult = Some(de.nowchess.chess.command.MoveResult.Successful(newBoard, newHistory, newTurn, captured))) val updatedCmd = cmd.copy(moveResult = Some(de.nowchess.chess.command.MoveResult.Successful(newBoard, newHistory, newTurn, captured)))
invoker.execute(updatedCmd) invoker.execute(updatedCmd)
updateGameState(newBoard, newHistory, newTurn) updateGameState(newBoard, newHistory, newTurn)
@@ -136,7 +130,6 @@ class GameEngine(
notifyObservers(FiftyMoveRuleAvailableEvent(currentBoard, currentHistory, currentTurn)) notifyObservers(FiftyMoveRuleAvailableEvent(currentBoard, currentHistory, currentTurn))
case MoveResult.Checkmate(winner) => case MoveResult.Checkmate(winner) =>
// Move resulted in checkmate
val updatedCmd = cmd.copy(moveResult = Some(de.nowchess.chess.command.MoveResult.Successful(Board.initial, GameHistory.empty, Color.White, None))) val updatedCmd = cmd.copy(moveResult = Some(de.nowchess.chess.command.MoveResult.Successful(Board.initial, GameHistory.empty, Color.White, None)))
invoker.execute(updatedCmd) invoker.execute(updatedCmd)
currentBoard = Board.initial currentBoard = Board.initial
@@ -145,7 +138,6 @@ class GameEngine(
notifyObservers(CheckmateEvent(currentBoard, currentHistory, currentTurn, winner)) notifyObservers(CheckmateEvent(currentBoard, currentHistory, currentTurn, winner))
case MoveResult.Stalemate => case MoveResult.Stalemate =>
// Move resulted in stalemate
val updatedCmd = cmd.copy(moveResult = Some(de.nowchess.chess.command.MoveResult.Successful(Board.initial, GameHistory.empty, Color.White, None))) val updatedCmd = cmd.copy(moveResult = Some(de.nowchess.chess.command.MoveResult.Successful(Board.initial, GameHistory.empty, Color.White, None)))
invoker.execute(updatedCmd) invoker.execute(updatedCmd)
currentBoard = Board.initial currentBoard = Board.initial
@@ -156,7 +148,6 @@ class GameEngine(
case MoveResult.PromotionRequired(promFrom, promTo, boardBefore, histBefore, _, promotingTurn) => case MoveResult.PromotionRequired(promFrom, promTo, boardBefore, histBefore, _, promotingTurn) =>
pendingPromotion = Some(PendingPromotion(promFrom, promTo, boardBefore, histBefore, promotingTurn)) pendingPromotion = Some(PendingPromotion(promFrom, promTo, boardBefore, histBefore, promotingTurn))
notifyObservers(PromotionRequiredEvent(currentBoard, currentHistory, currentTurn, promFrom, promTo)) notifyObservers(PromotionRequiredEvent(currentBoard, currentHistory, currentTurn, promFrom, promTo))
}
/** Undo the last move. */ /** Undo the last move. */
def undo(): Unit = synchronized { def undo(): Unit = synchronized {