Browse Source

Added scanForOccurringFigure method to ChessBoard with test

Nick Stolbov 3 years ago
parent
commit
d47a6689f6
  1. 11
      src/main/java/Game/ChessObj/ChessBoard.java
  2. 17
      src/test/java/Game/ChessObj/ChessBoardTest.java

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

@ -174,6 +174,17 @@ public class ChessBoard {
return -1; return -1;
} }
public int scanForOccurringFigure(ChessFigure.Type type, ChessFigure.Team team){
int count = 0;
ChessFigure template = new ChessFigure(type, team);
for(int y = 0; y < board.length; y++)
for(int x = 0; x < board[0].length; x++)
count += (template.equals(board[y][x]))?1:0;
return count;
}
public ChessFigure[][] getBoard() { public ChessFigure[][] getBoard() {
return this.board; return this.board;
} }

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

@ -208,4 +208,21 @@ class ChessBoardTest {
assertArrayEquals(resultBoard, chessBoard.getBoard()); assertArrayEquals(resultBoard, chessBoard.getBoard());
} }
@Test
void scanForOccurringFigure() {
assertEquals(8, chessBoard.scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE));
assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.WHITE));
assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE));
assertEquals(8, chessBoard.scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.BLACK));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.BLACK));
assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.BLACK));
assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK));
assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK));
}
} }
Loading…
Cancel
Save