From d47a6689f6975a0d051fff3b9285f7cc01d75a28 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Mon, 31 Jan 2022 22:03:59 +0100 Subject: [PATCH] Added scanForOccurringFigure method to ChessBoard with test --- src/main/java/Game/ChessObj/ChessBoard.java | 11 +++++++++++ src/test/java/Game/ChessObj/ChessBoardTest.java | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index f5dd704..7fc06a4 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -174,6 +174,17 @@ public class ChessBoard { 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() { return this.board; } diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java index 4793902..f0bca8b 100644 --- a/src/test/java/Game/ChessObj/ChessBoardTest.java +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -208,4 +208,21 @@ class ChessBoardTest { 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)); + } } \ No newline at end of file