package Game.ChessObj; import java.util.ArrayList; public class ChessBoard { public enum MoveFeedback { ENEMYBEATEN, MOVE, OUTSIDEOFBOARD, INVALID } private ChessFigure[][] board; public ChessBoard() { board = new ChessFigure[8][8]; initBoard(); } protected void initBoard() { ChessFigure.Type[] order = { ChessFigure.Type.CASTLE, ChessFigure.Type.KNIGHT, ChessFigure.Type.BISHOP, ChessFigure.Type.QUEEN, ChessFigure.Type.KING, ChessFigure.Type.BISHOP, ChessFigure.Type.KNIGHT, ChessFigure.Type.CASTLE }; for (int x = 0; x < board[0].length; x++) { //sets all pawns board[1][x] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); board[6][x] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); //sets all others board[0][x] = new ChessFigure(order[x], ChessFigure.Team.BLACK); board[7][x] = new ChessFigure(order[x], ChessFigure.Team.WHITE); } } /* a b c d e f g h ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ 1 │ |T| │ |Z| │ |I| │ |Q| │ |K| │ |I| │ |Z| │ |T| │ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ 2 │ |o| │ |o| │ |o| │ |o| │ |o| │ |o| │ |o| │ |o| │ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ 3 │ │ │ │ │ │ │ │ │ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ 4 │ │ │ │ │ │ │ │ │ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ 5 │ │ │ │ │ │ │ │ │ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ 6 │ │ │ │ │ │ │ │ │ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ 7 │ o │ o │ o │ o │ o │ o │ o │ o │ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ 8 │ T │ Z │ I │ Q │ K │ I │ Z │ T │ └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ */ public ArrayList getOutputBoard() { ArrayList outputBoard = new ArrayList<>(); String[][] cellSymbol = getCellSymbols(); outputBoard.add(" a b c d e f g h"); outputBoard.add(" ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐"); for (int y = 0; y < board.length; y++) { String line = ""; line += " " + (y + 1) + " │ "; for (int x = 0; x < board[0].length; x++) { line += (cellSymbol[y][x] + " │ "); } outputBoard.add(line); if (y < 7) outputBoard.add(" ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤"); } outputBoard.add(" └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘"); return outputBoard; } public String[][] getCellSymbols() { String[][] cellSymbol = new String[8][8]; for (int y = 0; y < board.length; y++) { for (int x = 0; x < board[0].length; x++) { cellSymbol[y][x] = (board[y][x] == null) ? " " : board[y][x].getSymbol(); } } return cellSymbol; } public boolean isCellInBoard(int x, int y) { if (x >= 0 && x < board[0].length && y >= 0 && y < board.length) return true; return false; } public boolean validateMove(int sourceX, int sourceY, int destX, int destY) { //Destination not in board range if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) return false; ChessFigure figure = board[sourceY][sourceX]; //Destination has same Team if (board[destY][destX] != null) if (board[destY][destX].getTeam() == figure.getTeam()) return false; int deltaX = destX - sourceX; int deltaY = sourceY - destY; //Pawn special case if (figure.getType() == ChessFigure.Type.PAWN) { if (board[destY][destX] != null) { if (board[destY][destX].getTeam() == figure.getTeam()) return false; if (figure.getTeam() == ChessFigure.Team.WHITE && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == 1) return true; if (figure.getTeam() == ChessFigure.Team.BLACK && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == -1) return true; } if (figure.isRelativeMoveValid(deltaX, deltaY)) return true; } if (figure.isRelativeMoveValid(deltaX, deltaY)) { if (figure.getType() == ChessFigure.Type.KNIGHT) return true; if (validateCleanPath(sourceX, sourceY, destX, destY)) return true; } return false; } protected boolean validateCleanPath(int sourceX, int sourceY, final int destX, final int destY) { int deltaX = destX - sourceX; int deltaY = destY - sourceY; int stepX = getStepWidth(deltaX); int stepY = getStepWidth(deltaY); sourceX += stepX; sourceY += stepY; if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) return false; if (Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1) return true; while (!(sourceX == destX && sourceY == destY)) { if (board[sourceY][sourceX] != null) return false; sourceX += stepX; sourceY += stepY; if (sourceX == destX && sourceY == destY) return true; if (sourceX > 7 || sourceY > 7) return false; } return false; } public MoveFeedback moveFigure(int sourceX, int sourceY, final int destX, final int destY) { if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) return MoveFeedback.OUTSIDEOFBOARD; if (validateMove(sourceX, sourceY, destX, destY)) { MoveFeedback feedback = MoveFeedback.INVALID; if (board[destY][destX] == null) feedback = MoveFeedback.MOVE; else feedback = MoveFeedback.ENEMYBEATEN; board[destY][destX] = board[sourceY][sourceX]; board[sourceY][sourceX] = null; return feedback; } return MoveFeedback.INVALID; } private int getStepWidth(int delta) { if (delta == 0) return 0; else if (delta > 0) return 1; else 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; } public void setChessBoard(ChessFigure[][] board) { this.board = board; } }