usertouch V1

This commit is contained in:
Jérôme BEDIER 2025-05-06 15:22:56 +02:00
parent 8e85a12068
commit 4225e0c586
1 changed files with 20 additions and 3 deletions

View File

@ -119,10 +119,27 @@ public class Board {
}
public void userTouch(int x, int y) {
//TODO
}
if (x < 0 || x >= width || y < 0 || y >= height) return;
if (selectedX == -1) { // No selection yet
if (board[x][y] != null) {
selectedX = x;
selectedY = y;
}
} else { // Already selected
if (x == selectedX && y == selectedY) { // Unselect
selectedX = -1;
selectedY = -1;
} else { // Move piece
board[x][y] = board[selectedX][selectedY];
board[selectedX][selectedY] = null;
turn++; // Increment turn
selectedX = -1;
selectedY = -1;
}
}
}
public boolean isSelected(int x, int y) {
return x == selectedX && y == selectedY;
}