diff --git a/OOP_group1A1_project/src/backend/Board.java b/OOP_group1A1_project/src/backend/Board.java index 7b90b77..9c28ddc 100644 --- a/OOP_group1A1_project/src/backend/Board.java +++ b/OOP_group1A1_project/src/backend/Board.java @@ -7,6 +7,7 @@ public class Board { private int width; private int height; private ArrayList Pieces; + private boolean[][] highlighted; // ── NEW FIELDS FOR userTouch ─────────────────────────────────────── private boolean hasSelection = false; // did we already click to pick up a piece? @@ -19,6 +20,7 @@ public class Board { this.width = width; this.height = height; this.Pieces = new ArrayList<>(); + this.highlighted = new boolean[width][height]; } public int getWidth() { @@ -30,7 +32,6 @@ public class Board { } public int getTurnNumber() { - //TODO return turnNumber; } @@ -133,6 +134,14 @@ public class Board { return sb.toString(); } + private void clearHighlights() { + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + highlighted[i][j] = false; + } + } + } + public void userTouch(int x, int y) { // 1) Find if you clicked on a piece at (x,y) Piece clicked = null; @@ -145,19 +154,25 @@ public class Board { } } - // 2) If nothing is selected yet, select this piece (if any) + // 2) If no piece is currently selected, try to select one if (!hasSelection) { - if (clicked != null) { - hasSelection = true; - selectedX = x; - selectedY = y; + if (clicked != null && clicked.isWhite() == turnWhite) { + // mark that we have a selection + hasSelection = true; + selectedX = x; + selectedY = y; + + // compute and show legal moves: + clearHighlights(); + computeHighlights(clicked); } return; } - // 3) If you click the same square twice → cancel selection + // 3) If you click the same square twice → cancel selection and clear highlights if (selectedX == x && selectedY == y) { hasSelection = false; + clearHighlights(); return; } @@ -188,8 +203,9 @@ public class Board { turnNumber++; turnWhite = !turnWhite; - // 6) Clear the selection so you can pick again next turn + // 6) Clear the selection and clear highlight hasSelection = false; + clearHighlights(); } @@ -199,6 +215,100 @@ public class Board { // hasSelection is true the moment the user clicks on a piece for the first time } + // Fill highlighted[][] = true for every square this piece could legally move to + private void computeHighlights(Piece p) { + PieceType type = p.getType(); + int sx = p.getX(), sy = p.getY(); + + switch(type) { + case Pawn: + int dir = p.isWhite() ? -1 : +1; // white pawns move up the board (y–), black down (y+) + // one step forward + markIfEmpty(sx, sy + dir); + // two-step from home rank + if ((p.isWhite() && sy == height-2) || (!p.isWhite() && sy == 1)) { + markIfEmpty(sx, sy + 2*dir); + } + // captures + markIfEnemy(sx-1, sy+dir, p.isWhite()); + markIfEnemy(sx+1, sy+dir, p.isWhite()); + break; + + case Knight: + int[][] offsets = {{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}}; + for (int[] o: offsets) markIfFreeOrEnemy(sx+o[0], sy+o[1], p.isWhite()); + break; + + case Bishop: + slide(sx, sy, 1,1, p.isWhite()); + slide(sx, sy, 1,-1, p.isWhite()); + slide(sx, sy,-1,1, p.isWhite()); + slide(sx, sy,-1,-1, p.isWhite()); + break; + + case Rook: + slide(sx, sy, 1,0, p.isWhite()); + slide(sx, sy,-1,0, p.isWhite()); + slide(sx, sy, 0,1, p.isWhite()); + slide(sx, sy, 0,-1, p.isWhite()); + break; + + case Queen: + // combine rook + bishop + computeHighlights(new Piece(sx,sy,p.isWhite(),PieceType.Bishop)); + computeHighlights(new Piece(sx,sy,p.isWhite(),PieceType.Rook)); + break; + + case King: + for (int dx=-1; dx<=1; dx++) + for (int dy=-1; dy<=1; dy++) + if (!(dx==0 && dy==0)) + markIfFreeOrEnemy(sx+dx, sy+dy, p.isWhite()); + break; + } + } + + // Helpers to mark a square if it’s on‐board and empty / enemy: + private void markIfEmpty(int x, int y) { + if (onBoard(x,y) && pieceAt(x,y)==null) highlighted[x][y] = true; + } + private void markIfEnemy(int x, int y, boolean isWhite) { + Piece q = pieceAt(x,y); + if (q!=null && q.isWhite()!=isWhite) highlighted[x][y] = true; + } + private void markIfFreeOrEnemy(int x, int y, boolean isWhite) { + if (!onBoard(x,y)) return; + Piece q = pieceAt(x,y); + if (q == null || q.isWhite() != isWhite) highlighted[x][y] = true; + } + + // Ray‐tracing for sliding pieces (rook, bishop, queen): + private void slide(int sx, int sy, int dx, int dy, boolean isWhite) { + int x = sx+dx, y = sy+dy; + while(onBoard(x,y)) { + Piece q = pieceAt(x,y); + if (q == null) { + highlighted[x][y] = true; + } else { + if (q.isWhite() != isWhite) highlighted[x][y] = true; + break; + } + x += dx; y += dy; + } + } + + // Utility to check bounds + private boolean onBoard(int x, int y) { + return x>=0 && x=0 && y