usertouch
This commit is contained in:
parent
3034482e08
commit
238a74eed4
|
|
@ -103,12 +103,58 @@ public class Board {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
private Piece getPieceAt(int x, int y) {
|
||||
for (Piece p : pieces) {
|
||||
if (p.getX() == x && p.getY() == y) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Integer selectedX = null;
|
||||
private Integer selectedY = null;
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
//TODO
|
||||
|
||||
Piece clickedPiece = getPieceAt(x, y);
|
||||
|
||||
if (selectedX == null || selectedY == null) {
|
||||
// No piece selected yet
|
||||
if (clickedPiece != null && clickedPiece.isWhite() == isWhiteTurn) {
|
||||
selectedX = x;
|
||||
selectedY = y;
|
||||
}
|
||||
} else {
|
||||
if (selectedX == x && selectedY == y) {
|
||||
// Unselection
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
} else {
|
||||
// Attempt to move
|
||||
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
||||
if (selectedPiece != null) {
|
||||
// Remove captured piece (if any)
|
||||
Piece toRemove = getPieceAt(x, y);
|
||||
if (toRemove != null && toRemove.isWhite() != isWhiteTurn) {
|
||||
pieces.remove(toRemove);
|
||||
}
|
||||
|
||||
// Move piece
|
||||
selectedPiece.moveTo(x, y);
|
||||
|
||||
// Advance turn
|
||||
turnNumber++;
|
||||
isWhiteTurn = !isWhiteTurn;
|
||||
|
||||
// Unselect
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isSelected(int x, int y) {
|
||||
//TODO
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -28,4 +28,8 @@ public class Piece {
|
|||
public boolean isWhite() {
|
||||
return isWhite; // Returns true if the piece is white, false if black
|
||||
}
|
||||
public void moveTo(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue