package Game; import Game.ChessObj.ChessBoard; import Game.ChessObj.ChessFigure; import java.util.ArrayList; public class Chess extends Game { private int[] firstTurn; private ChessFigure.Team currentTeam; private ArrayList destroyedWhiteFigures; private ArrayList destroyedBlackFigures; private ChessBoard.MoveFeedback lastFeedback; private ChessBoard chessBoard; public Chess() { init(); } private void init() { resetGame(); } @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); } private ArrayList getHeader() { ArrayList header = new ArrayList<>(); header.add("Welcome to the good old game Chess!"); header.add("First select one cell like: 'e7', than confirm it with an enter."); header.add("After that, select a second cell and also confirm it with an enter."); header.add("The Goal is to defeat the other teams King(K)."); header.add(""); header.add("It's your Turn " + currentTeam.name().toLowerCase() + "."); return header; } //int[0] = x, int[1] = y; public int[] convertInput(String input) { int[] temp = new int[2]; //input is not 2 chars big if (input.length() != 2) return null; char[] symbols = input.toCharArray(); int counterDigit = 0; int counterChar = 0; //input contains one number and one char for (char x : symbols) { if (Character.isDigit(x)) { counterDigit++; temp[1] = Character.getNumericValue(x) - 1; } if (Character.isAlphabetic(x)) { counterChar++; temp[0] = x; } } if (counterChar != 1 || counterDigit != 1) return null; temp[0] = Character.toLowerCase(temp[0]) - 97; if (!chessBoard.isCellInBoard(temp[0], temp[1])) return null; else 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]); lastFeedback = moveFeedback; 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); if (targetFigure.getTeam() == ChessFigure.Team.WHITE) getDestroyedWhiteFigures().add(targetFigure); else getDestroyedBlackFigures().add(targetFigure); } else { result.add("Invalid input!"); switch (moveFeedback) { case INVALID: if (targetFigure != null) if (targetFigure.getTeam() == sourceFigure.getTeam()) 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; } private ArrayList getUpdatedOutputBoard(ArrayList whiteFigures, ArrayList blackFigures) { ArrayList updatedOutputBoard = chessBoard.getOutputBoard(); ArrayList sidebarWhiteFigures = getSidebarFigures(whiteFigures, 5); ArrayList sidebarBlackFigures = getSidebarFigures(blackFigures, 5); for (int i = 0; i < sidebarWhiteFigures.size(); i++) { int index = i + 3; updatedOutputBoard.set(index, updatedOutputBoard.get(index) + sidebarWhiteFigures.get(i)); } for (int i = 0; i < sidebarBlackFigures.size(); i++) { int index = updatedOutputBoard.size() - (i + 3); updatedOutputBoard.set(index, updatedOutputBoard.get(index) + sidebarBlackFigures.get(i)); } return updatedOutputBoard; } 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) { line += chessFigureArrayList.get(i).getSymbol() + ""; result.add(line); return result; } line += chessFigureArrayList.get(i).getSymbol() + ","; counter++; if (counter >= maxPerLine) { result.add(line); line = ""; counter = 0; } } 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()); } public ChessBoard getChessBoard() { return this.chessBoard; } public ChessFigure.Team getCurrentTeam() { return this.currentTeam; } public ArrayList getDestroyedWhiteFigures() { return destroyedWhiteFigures; } public ArrayList getDestroyedBlackFigures() { return destroyedBlackFigures; } }