completed the step useTouch()

This commit is contained in:
Tikea TE 2025-05-06 15:00:08 +02:00
parent caa537323c
commit 503be4c1ff
1 changed files with 70 additions and 7 deletions

View File

@ -8,6 +8,13 @@ public class Board {
private int height; private int height;
private ArrayList<Piece> Pieces; private ArrayList<Piece> 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) { public Board(int width, int height) {
this.width = width; this.width = width;
this.height = height; this.height = height;
@ -24,12 +31,12 @@ public class Board {
public int getTurnNumber() { public int getTurnNumber() {
//TODO //TODO
return 0; return turnNumber;
} }
public boolean isTurnWhite() { public boolean isTurnWhite() {
//TODO //TODO
return false; return turnWhite;
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
@ -126,14 +133,70 @@ public class Board {
return sb.toString(); 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) { public boolean isSelected(int x, int y) {
//TODO // return true if the exact square is currently picked up
return false; return hasSelection && selectedX == x && selectedY == y;
// hasSelection is true the moment the user clicks on a piece for the first time
} }
/* saving-loading feature :*/ /* saving-loading feature :*/