diff --git a/src/main/java/Game/Chess.java b/src/main/java/Game/Chess.java index 5c4e7e4..471db47 100644 --- a/src/main/java/Game/Chess.java +++ b/src/main/java/Game/Chess.java @@ -7,6 +7,7 @@ import java.util.ArrayList; public class Chess extends Game { + private int[] firstTurn; private ChessFigure.Team currentTeam; private ArrayList destroyedWhiteFigures; @@ -26,6 +27,56 @@ public class Chess extends Game { @Override public void update(String input) { + outputBuffer.clear(); + ArrayList footer = new ArrayList<>(); + + if (isFinished()) { + resetGame(); + return; + } + + int[] coords = convertInput(input); + + if (coords == null) { + footer.add("Invalid Input!"); + firstTurn = null; + } else { + if (firstTurn == null) { + firstTurn = coords; + if (getChessBoard().getBoard()[firstTurn[1]][firstTurn[0]] != null) { + if (getChessBoard().getBoard()[firstTurn[1]][firstTurn[0]].getTeam() == currentTeam) { + String firstCoords = (char) (firstTurn[0] + 97) + "" + (firstTurn[1] + 1); + footer.add("Currently selected cell: " + firstCoords); + } else { + footer.add("It's " + currentTeam.name() + "'s turn "); + firstTurn = null; + } + } else { + footer.add("Can't select empty cell"); + firstTurn = null; + } + } else { + footer = makeMove(firstTurn, coords); + if(lastFeedback != ChessBoard.MoveFeedback.INVALID && lastFeedback != ChessBoard.MoveFeedback.OUTSIDEOFBOARD) + switchCurrentTeam(); + firstTurn = null; + } + } + + if (chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE) == 0) { + footer.add("Black has Won!"); + footer.add("Press any key to restart game"); + setFinished(true); + } + if (chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK) == 0) { + footer.add("White has Won!"); + footer.add("Press any key to restart game"); + setFinished(true); + } + + outputBuffer.addAll(getHeader()); + outputBuffer.addAll(getUpdatedOutputBoard(destroyedWhiteFigures, destroyedBlackFigures)); + outputBuffer.addAll(footer); } @@ -158,11 +209,20 @@ public class Chess extends Game { return result; } + private void switchCurrentTeam() { + if (currentTeam == ChessFigure.Team.WHITE) + currentTeam = ChessFigure.Team.BLACK; + else + currentTeam = ChessFigure.Team.WHITE; + } + public void resetGame() { chessBoard = new ChessBoard(); + firstTurn = null; currentTeam = ChessFigure.Team.WHITE; destroyedWhiteFigures = new ArrayList<>(); destroyedBlackFigures = new ArrayList<>(); + setFinished(false); outputBuffer.clear(); outputBuffer.addAll(getHeader()); outputBuffer.addAll(chessBoard.getOutputBoard()); diff --git a/src/test/java/Game/ChessTest.java b/src/test/java/Game/ChessTest.java index 9f298ea..a4d403c 100644 --- a/src/test/java/Game/ChessTest.java +++ b/src/test/java/Game/ChessTest.java @@ -85,4 +85,48 @@ class ChessTest { assertEquals(countBlackPawns - 1, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK)); assertEquals(countWhiteBishops - 1, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE)); } + + @Test + void update() { + //starting team = White + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE); + + //Emtpy cell + chess.update("d7"); + chess.update("g5"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE); + + chess.update("7b"); + chess.update("6b"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK); + + chess.update("e2"); + chess.update("e3"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE); + + chess.update("6b"); + chess.update("5b"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK); + + //Defeat enemy + chess.update("f1"); + chess.update("b5"); + assertEquals(7, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE)); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE); + + chess.update("d7"); + chess.update("d6"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK); + + //Invalid Move + chess.update("5b"); + chess.update("a5"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK); + + //King defeated + chess.update("5b"); + chess.update("e8"); + + assertTrue(chess.isFinished()); + } } \ No newline at end of file