From fd4d717e6d6f4edc0ae1b20a514d592a60d6bd70 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 2 Feb 2022 18:57:52 +0100 Subject: [PATCH] Added scan For OccurringFigure 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