From 503be4c1ffed2fffc20ff8035598f1165b96ddac Mon Sep 17 00:00:00 2001 From: Tikea TE Date: Tue, 6 May 2025 15:00:08 +0200 Subject: [PATCH] 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 :*/