package backend; import java.util.ArrayList; public class Board { private Piece[][] board; private int width; private int height; private int turn; private int selectedX = -1; private int selectedY = -1; public Board(int colNum, int lineNum) { this.width = colNum; // col move x this.height = lineNum; // line mov in y this.board = new Piece[width][height]; // 8x8 chess board } public int getWidth() { //width = 8; // setting the width at 8 for the moment can be changed return width; } public int getHeight() { //height = 8; // setting the height at 8 for the moment can be changed return height; } public int getTurnNumber() { return turn; } public boolean isTurnWhite() { return turn % 2 == 0; // White starts on turn 0 and only even turns } public void setPiece(boolean isWhite, PieceType type, int x, int y) { /* Ensure coordinates are inside the board boundaries if (x < 0 || x >= width || y < 0 || y >= height) { System.out.println("Invalid coordinates: (" + x + ", " + y + ")"); return; }*/ // Create and place the new piece board[x][y] = new Piece(isWhite, type, x, y); } public void populateBoard() { // Place Rooks board[0][0] = new Piece(false, PieceType.Rook, 0, 0); //color, piece , x -> , y | board[0][7] = new Piece(true, PieceType.Rook, 0, 7); board[7][0] = new Piece(false, PieceType.Rook, 7, 0); board[7][7] = new Piece(true, PieceType.Rook, 7, 7); // Place Knights board[1][0] = new Piece(false, PieceType.Knight, 1, 0); board[1][7] = new Piece(true, PieceType.Knight, 1, 7); board[6][0] = new Piece(false, PieceType.Knight, 6, 0); board[6][7] = new Piece(true, PieceType.Knight, 6, 7); // Place Bishops board[2][0] = new Piece(false, PieceType.Bishop, 2, 0); board[2][7] = new Piece(true, PieceType.Bishop, 2, 7); board[5][0] = new Piece(false, PieceType.Bishop, 5, 0); board[5][7] = new Piece(true, PieceType.Bishop, 5, 7); // Place Queens board[4][0] = new Piece(false, PieceType.Queen, 4, 0); board[4][7] = new Piece(true, PieceType.Queen, 4, 7); // Place Kings board[3][0] = new Piece(false, PieceType.King, 3, 0); board[3][7] = new Piece(true, PieceType.King, 3, 7); // Place Pawns for (int i = 0; i < 8; i++) { board[i][1] = new Piece(false, PieceType.Pawn, i, 1); //color, piece , x -> , y | board[i][6] = new Piece(true, PieceType.Pawn, i, 6); //color, piece , x -> , y | } } public void cleanBoard() { turn = 0; this.board = new Piece[width][height]; // should work ? //this.isTurnWhite = true; } // force public String toString() { StringBuilder sb = new StringBuilder(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Piece piece = board[x][y]; if (piece == null) { sb.append(" "); } else { // Using Teacher method no breaks sb.append(piece.isWhite() ? "W" : "B") .append(piece.getType().getSummary()); } if (x < width - 1) sb.append(","); } sb.append("\n"); } sb.append("Turn: ").append(isTurnWhite() ? "White" : "Black").append("\n"); return sb.toString(); } public ArrayList getPieces() { ArrayList pieces = new ArrayList<>(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (board[i][j] != null) { pieces.add(board[i][j]); } } } return pieces; } public void userTouch(int x, int y) { if (x < 0 || x >= width || y < 0 || y >= height) return; Piece clickedPiece = board[x][y]; // No piece selected yet if (selectedX == -1 && selectedY == -1) { if (clickedPiece != null) { selectedX = x; selectedY = y; } } else { // Clicked the same piece again → unselect if (x == selectedX && y == selectedY) { selectedX = -1; selectedY = -1; } else { // Move the piece Piece selectedPiece = board[selectedX][selectedY]; board[x][y] = selectedPiece; // Move to new square board[selectedX][selectedY] = null; // Clear old square selectedPiece.x = x; selectedPiece.y = y; turn++; // Next player's turn selectedX = -1; selectedY = -1; // Unselect } } } public boolean isSelected(int x, int y) { return x == selectedX && y == selectedY; } /* saving-loading feature :*/ public String[] toFileRep() { //TODO return null; } public Board(String[] array) { //TODO } /* The following methods require more work ! */ public boolean isHighlighted(int x, int y) { //TODO return false; } public void undoLastMove() { //TODO } public Board(Board board) { //TODO } public void playMove(Move move) { //TODO } }