From caa537323cd6fb53fd7a02a57b1952144f7f9a1c Mon Sep 17 00:00:00 2001 From: Tikea TE Date: Tue, 6 May 2025 14:11:14 +0200 Subject: [PATCH 1/5] completed the setPiece step(but also remove the previous piece) --- OOP_group1A1_project/src/backend/Board.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/OOP_group1A1_project/src/backend/Board.java b/OOP_group1A1_project/src/backend/Board.java index 81f150e..21b738c 100644 --- a/OOP_group1A1_project/src/backend/Board.java +++ b/OOP_group1A1_project/src/backend/Board.java @@ -33,10 +33,17 @@ public class Board { } public void setPiece(boolean isWhite, PieceType type, int x, int y) { - //TODO - Piece newPiece = new Piece(x, y, isWhite, type); - Pieces.add(newPiece); - + // 1) Remove any piece already at this square + for (int i = 0; i < Pieces.size(); i++) { + Piece p = Pieces.get(i); + if (p.getX() == x && p.getY() == y) { + Pieces.remove(i); + break; + } + } + + // 2) Add the new piece + Pieces.add(new Piece(x, y, isWhite, type)); } public void populateBoard() { From 503be4c1ffed2fffc20ff8035598f1165b96ddac Mon Sep 17 00:00:00 2001 From: Tikea TE Date: Tue, 6 May 2025 15:00:08 +0200 Subject: [PATCH 2/5] completed the step useTouch() --- OOP_group1A1_project/src/backend/Board.java | 77 +++++++++++++++++++-- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/OOP_group1A1_project/src/backend/Board.java b/OOP_group1A1_project/src/backend/Board.java index 21b738c..7b90b77 100644 --- a/OOP_group1A1_project/src/backend/Board.java +++ b/OOP_group1A1_project/src/backend/Board.java @@ -8,6 +8,13 @@ public class Board { private int height; private ArrayList Pieces; + // ── NEW FIELDS FOR userTouch ─────────────────────────────────────── + private boolean hasSelection = false; // did we already click to pick up a piece? + private int selectedX, selectedY; // if so, which square is “in hand”? + private int turnNumber = 0; // how many half-moves have been played? + private boolean turnWhite = true; // true = White to play, false = Black + // ───────────────────────────────────────────────────────────── + public Board(int width, int height) { this.width = width; this.height = height; @@ -24,12 +31,12 @@ public class Board { public int getTurnNumber() { //TODO - return 0; + return turnNumber; } public boolean isTurnWhite() { //TODO - return false; + return turnWhite; } public void setPiece(boolean isWhite, PieceType type, int x, int y) { @@ -126,14 +133,70 @@ public class Board { return sb.toString(); } + public void userTouch(int x, int y) { + // 1) Find if you clicked on a piece at (x,y) + Piece clicked = null; + for (int i = 0; i < Pieces.size(); i++) { + Piece p = Pieces.get(i); + if (p.getX() == x && p.getY() == y) { + // compare the piece coordinate to the square clicked coordinate + clicked = p; + break; + } + } + + // 2) If nothing is selected yet, select this piece (if any) + if (!hasSelection) { + if (clicked != null) { + hasSelection = true; + selectedX = x; + selectedY = y; + } + return; + } + + // 3) If you click the same square twice → cancel selection + if (selectedX == x && selectedY == y) { + hasSelection = false; + return; + } + + // 4) Otherwise, move the previously selected piece to (x,y): + // a) remove any piece already on the destination (simple capture) + Pieces.removeIf(p -> p.getX() == x && p.getY() == y); + // remove p if p.getX() == x && p.getY() == y + + // b) locate the selected piece object + Piece toMove = null; + for (int i = 0; i < Pieces.size(); i++) { + Piece p = Pieces.get(i); + if (p.getX() == selectedX && p.getY() == selectedY) { + toMove = p; + break; + } + } + + + // c) re-place it at the new coordinates + if (toMove != null) { + Pieces.remove(toMove); + // we need to remove it from its old place + Pieces.add(new Piece(x, y, toMove.isWhite(), toMove.getType())); + } // change the x y but still the same color and type + + // 5) Advance half-move counter and switch player + turnNumber++; + turnWhite = !turnWhite; + + // 6) Clear the selection so you can pick again next turn + hasSelection = false; + } - public void userTouch(int x, int y) { - - } public boolean isSelected(int x, int y) { - //TODO - return false; + // return true if the exact square is currently picked up + return hasSelection && selectedX == x && selectedY == y; + // hasSelection is true the moment the user clicks on a piece for the first time } /* saving-loading feature :*/ From 3ff691cdac0fff687db57cfc8b398938f215ca4d Mon Sep 17 00:00:00 2001 From: Tikea TE Date: Tue, 6 May 2025 15:38:33 +0200 Subject: [PATCH 3/5] adding the highlights function --- OOP_group1A1_project/src/backend/Board.java | 130 ++++++++++++++++++-- 1 file changed, 120 insertions(+), 10 deletions(-) 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 Date: Tue, 6 May 2025 15:42:27 +0200 Subject: [PATCH 4/5] able to move only within the highlighted cells --- OOP_group1A1_project/src/backend/Board.java | 25 +++++++++------------ 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/OOP_group1A1_project/src/backend/Board.java b/OOP_group1A1_project/src/backend/Board.java index 9c28ddc..7994651 100644 --- a/OOP_group1A1_project/src/backend/Board.java +++ b/OOP_group1A1_project/src/backend/Board.java @@ -176,34 +176,29 @@ public class Board { return; } - // 4) Otherwise, move the previously selected piece to (x,y): - // a) remove any piece already on the destination (simple capture) - Pieces.removeIf(p -> p.getX() == x && p.getY() == y); - // remove p if p.getX() == x && p.getY() == y + // 4) Otherwise it’s a move: but only if (x,y) was highlighted + if (!isHighlighted(x, y)) { + // if clicked somewhere illegal, ignore it (keep your piece “in hand”) + return; + } - // b) locate the selected piece object + // 5) Now perform the capture+relocate exactly as before + Pieces.removeIf(p -> p.getX() == x && p.getY() == y); Piece toMove = null; - for (int i = 0; i < Pieces.size(); i++) { - Piece p = Pieces.get(i); + for (Piece p : Pieces) { if (p.getX() == selectedX && p.getY() == selectedY) { toMove = p; break; } } - - - // c) re-place it at the new coordinates if (toMove != null) { Pieces.remove(toMove); - // we need to remove it from its old place Pieces.add(new Piece(x, y, toMove.isWhite(), toMove.getType())); - } // change the x y but still the same color and type + } - // 5) Advance half-move counter and switch player + // 6) Advance turn and clear selection/highlights turnNumber++; turnWhite = !turnWhite; - - // 6) Clear the selection and clear highlight hasSelection = false; clearHighlights(); } From 595d20069f26d1f58fbb1f2dd8f95ccf39b156bb Mon Sep 17 00:00:00 2001 From: Tikea TE Date: Tue, 6 May 2025 16:12:48 +0200 Subject: [PATCH 5/5] added some rules can only move if its highlighted --- OOP_group1A1_project/src/backend/Board.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OOP_group1A1_project/src/backend/Board.java b/OOP_group1A1_project/src/backend/Board.java index 7994651..aacabcc 100644 --- a/OOP_group1A1_project/src/backend/Board.java +++ b/OOP_group1A1_project/src/backend/Board.java @@ -154,8 +154,8 @@ public class Board { } } - // 2) If no piece is currently selected, try to select one - if (!hasSelection) { + // 2) If no piece is currently selected, try to select one + if (!hasSelection) { // hasSeletion is false at the beginning if (clicked != null && clicked.isWhite() == turnWhite) { // mark that we have a selection hasSelection = true;