Browse Source

Added isCellInBoard for checking positions with test

feature-chess
Nick Stolbov 2 years ago
parent
commit
b7125715f3
  1. 8
      src/main/java/Game/ChessObj/ChessBoard.java
  2. 11
      src/test/java/Game/ChessObj/ChessBoardTest.java

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

@ -70,12 +70,18 @@ public class ChessBoard {
String[][] cellSymbol = new String[8][8];
for (int y = 0; y < board.length; y++) {
for (int x = 0; x < board[0].length; x++) {
cellSymbol[y][x] = (board[y][x] == null)?" ": board[y][x].getSymbol();
cellSymbol[y][x] = (board[y][x] == null) ? " " : board[y][x].getSymbol();
}
}
return cellSymbol;
}
public boolean isCellInBoard(int x, int y){
if(x >= 0 && x < board[0].length && y >= 0 && y < board.length)
return true;
return false;
}
public ChessFigure[][] getBoard() {
return this.board;
}

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

@ -66,4 +66,15 @@ class ChessBoardTest {
};
assertArrayEquals(expectedArray, chessBoard.getCellSymbols());
}
@Test
void isCellInBoard() {
assertTrue(chessBoard.isCellInBoard(0, 0));
assertTrue(chessBoard.isCellInBoard(7, 7));
assertTrue(chessBoard.isCellInBoard(2, 1));
assertTrue(chessBoard.isCellInBoard(5, 7));
assertFalse(chessBoard.isCellInBoard(-1, 0));
assertFalse(chessBoard.isCellInBoard(4, 8));
assertFalse(chessBoard.isCellInBoard(10, 20));
}
}
Loading…
Cancel
Save