From f60515c6444b37882c33f88e743c3061195a7bf7 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sun, 30 Jan 2022 23:48:21 +0100 Subject: [PATCH] Added validateMove method to ChessBoard with test for validating every move on the board --- src/main/java/Game/ChessObj/ChessBoard.java | 34 ++++++++++ .../java/Game/ChessObj/ChessBoardTest.java | 64 +++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index f7f0f3b..f46138c 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -82,6 +82,40 @@ public class ChessBoard { return false; } + public boolean validateMove(int sourceX, int sourceY, int destX, int destY) { + //Destination not in board range + if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) + return false; + ChessFigure figure = board[sourceY][sourceX]; + //Destination has same Team + if(board[destY][destX] != null) + if(board[destY][destX].getTeam() == figure.getTeam()) + return false; + int deltaX = destX - sourceX; + int deltaY = sourceY - destY; + //Pawn special case + if(figure.getType() == ChessFigure.Type.PAWN){ + if(board[destY][destX] != null) { + if (board[destY][destX].getTeam() == figure.getTeam()) + return false; + if (figure.getTeam() == ChessFigure.Team.WHITE && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == 1) + return true; + if (figure.getTeam() == ChessFigure.Team.BLACK && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == -1) + return true; + } + if(figure.isRelativeMoveValid(deltaX, deltaY)) + return true; + } + if(figure.isRelativeMoveValid(deltaX, deltaY)){ + if(figure.getType() == ChessFigure.Type.KNIGHT) + return true; + if(validateCleanPath(sourceX, sourceY, destX, destY)) + return true; + } + + return false; + } + protected boolean validateCleanPath(int sourceX, int sourceY, final int destX, final int destY) { int deltaX = destX - sourceX; int deltaY = destY - sourceY; diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java index 459b95a..489f0df 100644 --- a/src/test/java/Game/ChessObj/ChessBoardTest.java +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -97,4 +97,68 @@ class ChessBoardTest { assertTrue(chessBoard.validateCleanPath(7, 3, 5, 5)); assertFalse(chessBoard.validateCleanPath(7, 3, 6, 6)); } + + /* + 0 1 2 3 4 5 6 7 + ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ + 0 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 1 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 2 │ │ │ │ │ o │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 3 │ │ │ │ |o| │ │ |o| │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 4 │ │ │ o │ │ o │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 5 │ │ │ │ |o| │ │ │ |T| │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 6 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 7 │ │ │ T │ │ B │ │ │ │ + └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ + */ + @Test + void validateMove() { + ChessFigure[][] tempBoard = new ChessFigure[8][8]; + + tempBoard[4][2] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); + tempBoard[4][4] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); + tempBoard[2][4] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); + tempBoard[3][3] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); + tempBoard[3][5] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); + tempBoard[5][3] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); + tempBoard[7][2] = new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE); + tempBoard[7][4] = new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE); + tempBoard[5][6] = new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.BLACK); + + chessBoard.setChessBoard(tempBoard); + + //White pawn normal move + assertTrue(chessBoard.validateMove(4, 2, 4, 1)); + assertFalse(chessBoard.validateMove(4, 2, 4, 3)); + assertFalse(chessBoard.validateMove(4, 2, 3, 1)); + assertFalse(chessBoard.validateMove(4, 2, 5, 1)); + //White pawn beats Black + assertTrue(chessBoard.validateMove(4, 4, 3, 3)); + assertTrue(chessBoard.validateMove(4, 4, 5, 3)); + //Black pawn normal move + assertTrue(chessBoard.validateMove(3, 5, 3, 6)); + assertFalse(chessBoard.validateMove(3, 5, 3, 4)); + assertFalse(chessBoard.validateMove(3, 5, 2, 6)); + assertFalse(chessBoard.validateMove(3, 5, 6, 6)); + //Black pawn beats White + assertTrue(chessBoard.validateMove(3, 3, 2, 4)); + assertTrue(chessBoard.validateMove(3, 3, 4, 4)); + + //CastleMoves for testing global rules + assertTrue(chessBoard.validateMove(2, 7, 2, 5)); + assertFalse(chessBoard.validateMove(2, 7, 2, 4)); + assertFalse(chessBoard.validateMove(2, 7, 2, 2)); + + //Bishop Moves for testing global moves + assertTrue(chessBoard.validateMove(4, 7, 5, 6)); + assertTrue(chessBoard.validateMove(4, 7, 6, 5)); + assertFalse(chessBoard.validateMove(4, 7, 7, 4)); + } } \ No newline at end of file