|
| 1 | +public class ChessBoard { |
| 2 | + private static final int SIZE = 8; |
| 3 | + private final Piece[][] grid; |
| 4 | + |
| 5 | + public ChessBoard() { |
| 6 | + this.grid = new Piece[SIZE][SIZE]; |
| 7 | + initializeStandardSetup(); |
| 8 | + } |
| 9 | + |
| 10 | + public void initializeStandardSetup() { |
| 11 | + for (int row = 0; row < SIZE; row++) { |
| 12 | + for (int col = 0; col < SIZE; col++) { |
| 13 | + grid[row][col] = null; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + setupBackRank(Color.BLACK, 0); |
| 18 | + setupPawns(Color.BLACK, 1); |
| 19 | + setupPawns(Color.WHITE, 6); |
| 20 | + setupBackRank(Color.WHITE, 7); |
| 21 | + } |
| 22 | + |
| 23 | + private void setupBackRank(Color color, int row) { |
| 24 | + grid[row][0] = new Rook(color); |
| 25 | + grid[row][1] = new Knight(color); |
| 26 | + grid[row][2] = new Bishop(color); |
| 27 | + grid[row][3] = new Queen(color); |
| 28 | + grid[row][4] = new King(color); |
| 29 | + grid[row][5] = new Bishop(color); |
| 30 | + grid[row][6] = new Knight(color); |
| 31 | + grid[row][7] = new Rook(color); |
| 32 | + } |
| 33 | + |
| 34 | + private void setupPawns(Color color, int row) { |
| 35 | + for (int col = 0; col < SIZE; col++) { |
| 36 | + grid[row][col] = new Pawn(color); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public Piece getPiece(Position position) { |
| 41 | + validatePosition(position); |
| 42 | + return grid[position.getRow()][position.getCol()]; |
| 43 | + } |
| 44 | + |
| 45 | + public void setPiece(Position position, Piece piece) { |
| 46 | + validatePosition(position); |
| 47 | + grid[position.getRow()][position.getCol()] = piece; |
| 48 | + } |
| 49 | + |
| 50 | + public Piece movePiece(Position from, Position to) { |
| 51 | + validatePosition(from); |
| 52 | + validatePosition(to); |
| 53 | + Piece moving = getPiece(from); |
| 54 | + Piece captured = getPiece(to); |
| 55 | + setPiece(to, moving); |
| 56 | + setPiece(from, null); |
| 57 | + return captured; |
| 58 | + } |
| 59 | + |
| 60 | + public boolean isPathClear(Position from, Position to) { |
| 61 | + int rowStep = Integer.compare(to.getRow(), from.getRow()); |
| 62 | + int colStep = Integer.compare(to.getCol(), from.getCol()); |
| 63 | + |
| 64 | + int currentRow = from.getRow() + rowStep; |
| 65 | + int currentCol = from.getCol() + colStep; |
| 66 | + |
| 67 | + while (currentRow != to.getRow() || currentCol != to.getCol()) { |
| 68 | + if (grid[currentRow][currentCol] != null) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + currentRow += rowStep; |
| 72 | + currentCol += colStep; |
| 73 | + } |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + public Position findKing(Color color) { |
| 78 | + for (int row = 0; row < SIZE; row++) { |
| 79 | + for (int col = 0; col < SIZE; col++) { |
| 80 | + Piece piece = grid[row][col]; |
| 81 | + if (piece instanceof King && piece.getColor() == color) { |
| 82 | + return new Position(row, col); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + public int getSize() { |
| 90 | + return SIZE; |
| 91 | + } |
| 92 | + |
| 93 | + public boolean isInside(Position position) { |
| 94 | + return position.getRow() >= 0 && position.getRow() < SIZE |
| 95 | + && position.getCol() >= 0 && position.getCol() < SIZE; |
| 96 | + } |
| 97 | + |
| 98 | + private void validatePosition(Position position) { |
| 99 | + if (!isInside(position)) { |
| 100 | + throw new ChessInvalidMoveException("Invalid board position: " + position); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public void printBoard() { |
| 105 | + System.out.println(); |
| 106 | + for (int row = 0; row < SIZE; row++) { |
| 107 | + System.out.print((8 - row) + " "); |
| 108 | + for (int col = 0; col < SIZE; col++) { |
| 109 | + Piece piece = grid[row][col]; |
| 110 | + char symbol = piece == null ? '.' : piece.getSymbol(); |
| 111 | + System.out.print(symbol + " "); |
| 112 | + } |
| 113 | + System.out.println(); |
| 114 | + } |
| 115 | + System.out.println(" a b c d e f g h"); |
| 116 | + System.out.println(); |
| 117 | + } |
| 118 | +} |
0 commit comments