Skip to content

Commit

Permalink
fix: change a BasicChessGameServer to check if a piece can move based…
Browse files Browse the repository at this point in the history
… on the current turn before applying a move
  • Loading branch information
jahrim committed Aug 29, 2023
1 parent d581a8d commit 8d98ef1
Showing 1 changed file with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,32 @@ class BasicChessGameServer(val vertx: Vertx)
asyncActivity(s"Apply move '$move'")(
onlyIfRunning {
onlyIfNotWaitingForPromotion {
chessboard << {
val chessBoard = state.gameState.chessboard.movePiece(move.from, move.to)
move match
case castlingMove: LegacyCastlingMove =>
chessBoard.movePiece(castlingMove.rookFromPosition, castlingMove.rookToPosition)
case enPassantMove: LegacyEnPassantMove =>
chessBoard.removePiece(enPassantMove.capturedPiecePosition)
case _ => chessBoard
}
moveHistory << {
state.gameState.chessboard.pieces.get(move.to) match
case Some(piece) => state.gameState.moveHistory.save(piece, move)
case None => state.gameState.moveHistory
}

LegacyChessGameAnalyzer.promotion(state.gameState.legacy) match
case Some(promotingPawnPosition) =>
this._timerManager.stop(state.gameState.currentTurn)
gameSituation << GameSituation.Promotion(promotingPawnPosition)
case _ =>
switchTurn()
state.gameState.chessboard
.pieces(state.gameState.currentTurn)
.get(move.from)
.foreach(_ =>
chessboard << {
val chessBoard = state.gameState.chessboard.movePiece(move.from, move.to)
move match
case castlingMove: LegacyCastlingMove =>
chessBoard.movePiece(castlingMove.rookFromPosition, castlingMove.rookToPosition)
case enPassantMove: LegacyEnPassantMove =>
chessBoard.removePiece(enPassantMove.capturedPiecePosition)
case _ => chessBoard
}
moveHistory << {
state.gameState.chessboard.pieces.get(move.to) match
case Some(piece) => state.gameState.moveHistory.save(piece, move)
case None => state.gameState.moveHistory
}

LegacyChessGameAnalyzer.promotion(state.gameState.legacy) match
case Some(promotingPawnPosition) =>
this._timerManager.stop(state.gameState.currentTurn)
gameSituation << GameSituation.Promotion(promotingPawnPosition)
case _ =>
switchTurn()
)
}
}
).onFailure(failure => serverError << failure)
Expand Down

0 comments on commit 8d98ef1

Please sign in to comment.