Browse Source

Added validateCleanPath for ChessBoard class with test

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

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

@ -76,13 +76,50 @@ public class ChessBoard {
return cellSymbol;
}
public boolean isCellInBoard(int x, int y){
if(x >= 0 && x < board[0].length && y >= 0 && y < board.length)
public boolean isCellInBoard(int x, int y) {
if (x >= 0 && x < board[0].length && y >= 0 && y < board.length)
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;
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() {
return this.board;
}
protected void setChessBoard(ChessFigure[][] board) {
this.board = board;
}
}

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

@ -5,6 +5,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
class ChessBoardTest {
@ -33,16 +34,16 @@ class ChessBoardTest {
assertEquals(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK), board[y][x]);
continue;
}
if(y == 6) {
if (y == 6) {
assertEquals(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE), board[y][x]);
continue;
}
if(y == 0){
if (y == 0) {
assertEquals(new ChessFigure(startOrder[x], ChessFigure.Team.BLACK), board[y][x]);
continue;
}
if(y == 7){
if (y == 7) {
assertEquals(new ChessFigure(startOrder[x], ChessFigure.Team.WHITE), board[y][x]);
continue;
}
@ -55,14 +56,14 @@ class ChessBoardTest {
@Test
void getCellSymbols() {
String[][] expectedArray = {
{"|T|","|Z|","|I|","|Q|","|K|","|I|","|Z|","|T|"},
{"|o|","|o|","|o|","|o|","|o|","|o|","|o|","|o|"},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" o "," o "," o "," o "," o "," o "," o "," o "},
{" T "," Z "," I "," Q "," K "," I "," Z "," T "}
{"|T|", "|Z|", "|I|", "|Q|", "|K|", "|I|", "|Z|", "|T|"},
{"|o|", "|o|", "|o|", "|o|", "|o|", "|o|", "|o|", "|o|"},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" o ", " o ", " o ", " o ", " o ", " o ", " o ", " o "},
{" T ", " Z ", " I ", " Q ", " K ", " I ", " Z ", " T "}
};
assertArrayEquals(expectedArray, chessBoard.getCellSymbols());
}
@ -77,4 +78,23 @@ class ChessBoardTest {
assertFalse(chessBoard.isCellInBoard(4, 8));
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