Browse Source

Added validateMove method to ChessBoard with test for validating every move on the board

feature-chess
Nick Stolbov 3 years ago
parent
commit
f60515c644
  1. 34
      src/main/java/Game/ChessObj/ChessBoard.java
  2. 64
      src/test/java/Game/ChessObj/ChessBoardTest.java

34
src/main/java/Game/ChessObj/ChessBoard.java

@ -82,6 +82,40 @@ public class ChessBoard {
return false; 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) { protected boolean validateCleanPath(int sourceX, int sourceY, final int destX, final int destY) {
int deltaX = destX - sourceX; int deltaX = destX - sourceX;
int deltaY = destY - sourceY; int deltaY = destY - sourceY;

64
src/test/java/Game/ChessObj/ChessBoardTest.java

@ -97,4 +97,68 @@ class ChessBoardTest {
assertTrue(chessBoard.validateCleanPath(7, 3, 5, 5)); assertTrue(chessBoard.validateCleanPath(7, 3, 5, 5));
assertFalse(chessBoard.validateCleanPath(7, 3, 6, 6)); 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));
}
} }
Loading…
Cancel
Save