package Game; import Game.ChessObj.ChessBoard; import Game.ChessObj.ChessFigure; import java.util.ArrayList; public class Chess extends Game { private ChessFigure.Team currentTeam; private ArrayList destroyedWhiteFigures; private ArrayList destroyedBlackFigures; private ChessBoard chessBoard; public Chess() { init(); } private void init() { chessBoard = new ChessBoard(); currentTeam = ChessFigure.Team.WHITE; destroyedWhiteFigures = new ArrayList<>(); destroyedBlackFigures = new ArrayList<>(); outputBuffer.addAll(chessBoard.getOutputBoard()); } @Override public void update(String input) { } //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]); 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 (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; } 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; } public ChessBoard getChessBoard() { return this.chessBoard; } public ChessFigure.Team getCurrentTeam() { return this.currentTeam; } public ArrayList getDestroyedWhiteFigures() { return destroyedWhiteFigures; } public ArrayList getDestroyedBlackFigures() { return destroyedBlackFigures; } }