From 8dfe543e92bbbd907930bab9ec89c4fd48de4dde Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 2 Feb 2022 19:37:29 +0100 Subject: [PATCH] Added makeMove method to Chess for moving a figure and getting the corresponding output with test --- src/main/java/Game/Chess.java | 55 ++++++++++++++++++++++++++++--- src/test/java/Game/ChessTest.java | 13 ++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/main/java/Game/Chess.java b/src/main/java/Game/Chess.java index 76922f1..d6fef15 100644 --- a/src/main/java/Game/Chess.java +++ b/src/main/java/Game/Chess.java @@ -7,7 +7,8 @@ import java.util.ArrayList; public class Chess extends Game { - ChessBoard chessBoard; + private ChessFigure.Team currentTeam; + private ChessBoard chessBoard; public Chess() { init(); @@ -15,6 +16,7 @@ public class Chess extends Game { private void init() { chessBoard = new ChessBoard(); + currentTeam = ChessFigure.Team.WHITE; outputBuffer.addAll(chessBoard.getOutputBoard()); } @@ -54,20 +56,57 @@ public class Chess extends Game { return temp; } + public ArrayList makeMove(int[] source, int[] target) { + ArrayList result = new ArrayList<>(); + ChessFigure sourceFigure = chessBoard.getBoard()[source[1]][source[0]]; + ChessFigure targetFigure = chessBoard.getBoard()[target[1]][target[0]]; + String sourceFigureName = ""; + String targetFigureName = ""; + String sourceCoords = (char) (source[0] + 97) + "" + (source[1] + 1); + String targetCoords = (char) (target[0] + 97) + "" + (target[1] + 1); + + ChessBoard.MoveFeedback moveFeedback = chessBoard.moveFigure(source[0], source[1], target[0], target[1]); + + if (sourceFigure != null) + sourceFigureName = sourceFigure.getTeam().name().toCharArray()[0] + sourceFigure.getTeam().name().substring(1).toLowerCase() + " " + sourceFigure.getType().name().toLowerCase(); + if (targetFigure != null) + targetFigureName = targetFigure.getTeam().name().toLowerCase() + " " + targetFigure.getType().name().toLowerCase(); + + if (moveFeedback == ChessBoard.MoveFeedback.MOVE) { + result.add("Successfully moved " + sourceFigureName.toLowerCase() + " from " + sourceCoords + " to " + targetCoords); + } else if (moveFeedback == ChessBoard.MoveFeedback.ENEMYBEATEN) { + result.add(sourceFigureName + " successfully beat " + targetFigureName.toLowerCase() + " at " + targetCoords); + } else { + result.add("Invalid input!"); + switch (moveFeedback) { + case INVALID: + if (chessBoard.getBoard()[target[0]][target[1]].getTeam() == getCurrentTeam()) + result.add("You are on the same Team! [" + getCurrentTeam().name() + "]"); + break; + case OUTSIDEOFBOARD: + result.add("Input is not inside the board!"); + break; + default: + } + } + + return result; + } + public ArrayList getSidebarFigures(ArrayList chessFigureArrayList, int maxPerLine) { ArrayList result = new ArrayList<>(); String line = ""; int counter = 0; - for(int i = 0; i < chessFigureArrayList.size(); i++){ - if(i == chessFigureArrayList.size() - 1) { + for (int i = 0; i < chessFigureArrayList.size(); i++) { + if (i == chessFigureArrayList.size() - 1) { line += chessFigureArrayList.get(i).getSymbol() + ""; result.add(line); return result; } line += chessFigureArrayList.get(i).getSymbol() + ","; counter++; - if(counter >= maxPerLine) { + if (counter >= maxPerLine) { result.add(line); line = ""; counter = 0; @@ -77,4 +116,12 @@ public class Chess extends Game { return result; } + public ChessBoard getChessBoard() { + return this.chessBoard; + } + + public ChessFigure.Team getCurrentTeam() { + return this.currentTeam; + } + } diff --git a/src/test/java/Game/ChessTest.java b/src/test/java/Game/ChessTest.java index bf6ff9b..9f298ea 100644 --- a/src/test/java/Game/ChessTest.java +++ b/src/test/java/Game/ChessTest.java @@ -72,4 +72,17 @@ class ChessTest { assertEquals(expectedArray2, chess.getSidebarFigures(array2, 5)); assertEquals(expectedArray3, chess.getSidebarFigures(array3, 5)); } + + @Test + void makeMove() { + int countBlackPawns = chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); + int countWhiteBishops = chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE); + chess.makeMove(chess.convertInput("a2"), chess.convertInput("a3")); + chess.makeMove(chess.convertInput("e7"), chess.convertInput("e6")); + chess.makeMove(chess.convertInput("f8"), chess.convertInput("a3")); + chess.makeMove(chess.convertInput("b2"), chess.convertInput("a3")); + + assertEquals(countBlackPawns - 1, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK)); + assertEquals(countWhiteBishops - 1, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE)); + } } \ No newline at end of file