From ab6fa7e52f843388fed9a04a6c396642d16e7990 Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 2 Jul 2019 20:40:28 +0200 Subject: [PATCH] Fixed king movements with regards to check detection --- src/dev/kske/chess/Board.java | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/dev/kske/chess/Board.java b/src/dev/kske/chess/Board.java index 526c6aa..dbe9825 100644 --- a/src/dev/kske/chess/Board.java +++ b/src/dev/kske/chess/Board.java @@ -40,24 +40,37 @@ public class Board { else { // Move piece // Save destination piece for possible canceling of the move - Piece tmpPiece = getDest(move); + Piece capturePiece = getDest(move); setDest(move, getPos(move)); setPos(move, null); - // Revert move if it caused a check for its team - if (checkCheck(piece.getColor())) { - setPos(move, getDest(move)); - setDest(move, tmpPiece); - return false; - } - - // Update the king's position if the moved piece is a king + // Update the king's position if the moved piece is the king if (piece.getType() == Type.KING) kingPos.put(piece.getColor(), move.dest); + // Revert move if it caused a check for its team + if (checkCheck(piece.getColor())) { + revert(move, capturePiece); + return false; + } return true; } } + /** + * Reverts a move. + * + * @param move The move to revert + * @param capturedPiece The piece that has been captured when the move has been + * applied + */ + public void revert(Move move, Piece capturedPiece) { + setPos(move, getDest(move)); + setDest(move, capturedPiece); + + // Update the king's position if the moved piece is the king + if (getPos(move).getType() == Type.KING) kingPos.put(getPos(move).getColor(), move.pos); + } + public boolean checkCheck(Color color) { for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++)