2 Commits

  1. 38
      src/main/java/Game/ChessObj/ChessBoard.java
  2. 2
      src/main/java/Game/ChessObj/ChessFigure.java
  3. 64
      src/test/java/Game/ChessObj/ChessBoardTest.java
  4. 4
      src/test/java/Game/ChessObj/ChessFigureTest.java

38
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;
@ -92,6 +126,8 @@ public class ChessBoard {
sourceY += stepY;
if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY))
return false;
if(Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1)
return true;
while (!(sourceX == destX && sourceY == destY)) {
if (board[sourceY][sourceX] != null)
return false;
@ -119,7 +155,7 @@ public class ChessBoard {
return this.board;
}
protected void setChessBoard(ChessFigure[][] board) {
public void setChessBoard(ChessFigure[][] board) {
this.board = board;
}
}

2
src/main/java/Game/ChessObj/ChessFigure.java

@ -86,7 +86,7 @@ public class ChessFigure {
return true;
break;
case PAWN:
if (dx == 0)
if (dx != 0)
return false;
if (getTeam() == Team.WHITE && (dy == 1))
return true;

64
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));
}
}

4
src/test/java/Game/ChessObj/ChessFigureTest.java

@ -92,11 +92,11 @@ class ChessFigureTest {
}
break;
case PAWN:
if (figure.getTeam() == ChessFigure.Team.WHITE && (y == 1) && x != 0) {
if (figure.getTeam() == ChessFigure.Team.WHITE && (y == 1) && x == 0) {
assertTrue(figure.isRelativeMoveValid(x, y));
continue;
}
if (figure.getTeam() == ChessFigure.Team.BLACK && (y == -1) && x != 0) {
if (figure.getTeam() == ChessFigure.Team.BLACK && (y == -1) && x == 0) {
assertTrue(figure.isRelativeMoveValid(x, y));
continue;
}

Loading…
Cancel
Save