Browse Source

Added validateCleanPath for ChessBoard class with test

feature-chess
Nick Stolbov 3 years ago
parent
commit
244e633ad3
  1. 37
      src/main/java/Game/ChessObj/ChessBoard.java
  2. 20
      src/test/java/Game/ChessObj/ChessBoardTest.java

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

@ -82,7 +82,44 @@ public class ChessBoard {
return false; return false;
} }
protected boolean validateCleanPath(int sourceX, int sourceY, final int destX, final int destY) {
int deltaX = destX - sourceX;
int deltaY = destY - sourceY;
int stepX = getStepWidth(deltaX);
int stepY = getStepWidth(deltaY);
sourceX += stepX;
sourceY += stepY;
if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY))
return false;
while (!(sourceX == destX && sourceY == destY)) {
if (board[sourceY][sourceX] != null)
return false;
sourceX += stepX;
sourceY += stepY;
if (sourceX == destX && sourceY == destY)
return true;
if (sourceX > 7 || sourceY > 7)
return false;
}
return false;
}
private int getStepWidth(int delta) {
if (delta == 0)
return 0;
else if (delta > 0)
return 1;
else
return -1;
}
public ChessFigure[][] getBoard() { public ChessFigure[][] getBoard() {
return this.board; return this.board;
} }
protected void setChessBoard(ChessFigure[][] board) {
this.board = board;
}
} }

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

@ -5,6 +5,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
class ChessBoardTest { class ChessBoardTest {
@ -77,4 +78,23 @@ class ChessBoardTest {
assertFalse(chessBoard.isCellInBoard(4, 8)); assertFalse(chessBoard.isCellInBoard(4, 8));
assertFalse(chessBoard.isCellInBoard(10, 20)); assertFalse(chessBoard.isCellInBoard(10, 20));
} }
@Test
void validateCleanPath() {
ChessFigure[][] tempBoard = chessBoard.getBoard();
tempBoard[3][0] = mock(ChessFigure.class);
tempBoard[3][5] = mock(ChessFigure.class);
chessBoard.setChessBoard(tempBoard);
assertFalse(chessBoard.validateCleanPath(-2, 0, 1, 4));
assertTrue(chessBoard.validateCleanPath(0, 3, 5, 3));
assertFalse(chessBoard.validateCleanPath(0, 3, 6, 3));
assertFalse(chessBoard.validateCleanPath(0, 3, 7, 3));
assertFalse(chessBoard.validateCleanPath(0, 3, 5, 5));
assertFalse(chessBoard.validateCleanPath(0, 3, 4, 7));
assertTrue(chessBoard.validateCleanPath(0, 3, 2, 5));
assertTrue(chessBoard.validateCleanPath(7, 3, 5, 5));
assertFalse(chessBoard.validateCleanPath(7, 3, 6, 6));
}
} }
Loading…
Cancel
Save