Movement of Pieces
This commit is contained in:
parent
7759adb994
commit
474a24d10e
|
|
@ -111,30 +111,144 @@ public class Board {
|
|||
}
|
||||
|
||||
return sb.toString();
|
||||
//TODO
|
||||
}
|
||||
|
||||
public ArrayList<Piece> getPieces() {
|
||||
//TODO
|
||||
|
||||
return pieces;
|
||||
}
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
Piece clickedPiece = getPieceAt(x, y);
|
||||
|
||||
//TODO
|
||||
|
||||
if (selected == null) {
|
||||
// First click: try selecting a piece
|
||||
if (clickedPiece != null && clickedPiece.isWhite() == turnWhite) {
|
||||
selected = new int[]{x, y};
|
||||
highlighted = getLegalMoves(clickedPiece);
|
||||
}
|
||||
} else {
|
||||
// Second click: check if move is legal
|
||||
if (isHighlighted(x, y)) {
|
||||
Piece piece = getPieceAt(selected[0], selected[1]);
|
||||
Piece captured = getPieceAt(x, y);
|
||||
|
||||
Move move = new Move(piece, selected[0], selected[1], x, y, captured);
|
||||
playMove(move);
|
||||
} else {
|
||||
// If clicked on another own piece, reselect
|
||||
if (clickedPiece != null && clickedPiece.isWhite() == turnWhite) {
|
||||
selected = new int[]{x, y};
|
||||
highlighted = getLegalMoves(clickedPiece);
|
||||
} else {
|
||||
selected = null;
|
||||
highlighted.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ArrayList<int[]> getLegalMoves(Piece piece) {
|
||||
ArrayList<int[]> moves = new ArrayList<>();
|
||||
int x = piece.getX(), y = piece.getY();
|
||||
int dir = piece.isWhite() ? 1 : -1;
|
||||
|
||||
switch (piece.getType()) {
|
||||
case Pawn:
|
||||
int forwardY = y + dir;
|
||||
if (getPieceAt(x, forwardY) == null)
|
||||
moves.add(new int[] {x, forwardY});
|
||||
Piece diagLeft = getPieceAt(x - 1, forwardY);
|
||||
if (diagLeft != null && diagLeft.isWhite() != piece.isWhite())
|
||||
moves.add(new int[] {x - 1, forwardY});
|
||||
Piece diagRight = getPieceAt(x + 1, forwardY);
|
||||
if (diagRight != null && diagRight.isWhite() != piece.isWhite())
|
||||
moves.add(new int[] {x + 1, forwardY});
|
||||
break;
|
||||
|
||||
case Rook:
|
||||
addLinearMoves(moves, piece, 1, 0);
|
||||
addLinearMoves(moves, piece, -1, 0);
|
||||
addLinearMoves(moves, piece, 0, 1);
|
||||
addLinearMoves(moves, piece, 0, -1);
|
||||
break;
|
||||
|
||||
case Bishop:
|
||||
addLinearMoves(moves, piece, 1, 1);
|
||||
addLinearMoves(moves, piece, -1, 1);
|
||||
addLinearMoves(moves, piece, 1, -1);
|
||||
addLinearMoves(moves, piece, -1, -1);
|
||||
break;
|
||||
|
||||
case Queen:
|
||||
addLinearMoves(moves, piece, 1, 0);
|
||||
addLinearMoves(moves, piece, -1, 0);
|
||||
addLinearMoves(moves, piece, 0, 1);
|
||||
addLinearMoves(moves, piece, 0, -1);
|
||||
addLinearMoves(moves, piece, 1, 1);
|
||||
addLinearMoves(moves, piece, -1, 1);
|
||||
addLinearMoves(moves, piece, 1, -1);
|
||||
addLinearMoves(moves, piece, -1, -1);
|
||||
break;
|
||||
|
||||
case King:
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
int nx = x + dx, ny = y + dy;
|
||||
if (isInBounds(nx, ny) && (getPieceAt(nx, ny) == null || getPieceAt(nx, ny).isWhite() != piece.isWhite())) {
|
||||
moves.add(new int[]{nx, ny});
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Knight:
|
||||
int[][] knightMoves = {
|
||||
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
|
||||
{1, -2}, {2, -1}, {-1, -2}, {-2, -1}
|
||||
};
|
||||
for (int[] m : knightMoves) {
|
||||
int nx = x + m[0], ny = y + m[1];
|
||||
if (isInBounds(nx, ny) && (getPieceAt(nx, ny) == null || getPieceAt(nx, ny).isWhite() != piece.isWhite())) {
|
||||
moves.add(new int[]{nx, ny});
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
private void addLinearMoves(ArrayList<int[]> moves, Piece piece, int dx, int dy) {
|
||||
int x = piece.getX(), y = piece.getY();
|
||||
while (true) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
if (!isInBounds(x, y)) break;
|
||||
Piece target = getPieceAt(x, y);
|
||||
if (target == null) {
|
||||
moves.add(new int[]{x, y});
|
||||
} else {
|
||||
if (target.isWhite() != piece.isWhite())
|
||||
moves.add(new int[]{x, y});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isInBounds(int x, int y) {
|
||||
return x >= 0 && y >= 0 && x < width && y < height;
|
||||
}
|
||||
|
||||
|
||||
public boolean isSelected(int x, int y) {
|
||||
//TODO
|
||||
return false;
|
||||
return selected != null && selected[0] == x && selected[1] == y;
|
||||
}
|
||||
|
||||
|
||||
/* saving-loading feature :*/
|
||||
|
||||
public String[] toFileRep() {
|
||||
//TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
@ -143,6 +257,16 @@ public class Board {
|
|||
|
||||
}
|
||||
|
||||
private Piece getPieceAt(int x, int y) {
|
||||
for (Piece piece : pieces) {
|
||||
if (piece.getX() == x && piece.getY() == y) {
|
||||
return piece;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/* The following methods require more work ! */
|
||||
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
|
|
@ -159,13 +283,42 @@ public class Board {
|
|||
}
|
||||
|
||||
public Board(Board board) {
|
||||
//TODO
|
||||
if (moveHistory.isEmpty()) return;
|
||||
|
||||
Move lastMove = moveHistory.remove(moveHistory.size() - 1);
|
||||
Piece movedPiece = lastMove.getPieceMoved();
|
||||
movedPiece.setPosition(lastMove.getFromX(), lastMove.getFromY());
|
||||
|
||||
if (lastMove.getPieceCaptured() != null) {
|
||||
pieces.add(lastMove.getPieceCaptured());
|
||||
}
|
||||
|
||||
turnWhite = !turnWhite;
|
||||
turnNumber--;
|
||||
|
||||
}
|
||||
|
||||
public void playMove(Move move) {
|
||||
//TODO
|
||||
Piece piece = move.getPieceMoved();
|
||||
|
||||
// If a piece is captured, remove it from the board
|
||||
if (move.getPieceCaptured() != null) {
|
||||
pieces.remove(move.getPieceCaptured());
|
||||
}
|
||||
|
||||
// Update the moved piece's position
|
||||
piece.setPosition(move.getToX(), move.getToY());
|
||||
|
||||
// Save move in history for undo
|
||||
moveHistory.add(move);
|
||||
|
||||
// Switch turn
|
||||
turnWhite = !turnWhite;
|
||||
turnNumber++;
|
||||
|
||||
// Clear selection/highlight
|
||||
selected = null;
|
||||
highlighted.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
package backend;
|
||||
|
||||
public class Move {
|
||||
private Piece pieceMoved;
|
||||
private int fromX, fromY;
|
||||
private int toX, toY;
|
||||
private Piece pieceCaptured; // can be null
|
||||
private final Piece pieceMoved;
|
||||
private final int fromX, fromY;
|
||||
private final int toX, toY;
|
||||
private final Piece pieceCaptured;
|
||||
|
||||
public Move(Piece pieceMoved, int fromX, int fromY, int toX, int toY, Piece pieceCaptured) {
|
||||
this.pieceMoved = pieceMoved;
|
||||
|
|
@ -21,5 +21,4 @@ public class Move {
|
|||
public int getToX() { return toX; }
|
||||
public int getToY() { return toY; }
|
||||
public Piece getPieceCaptured() { return pieceCaptured; }
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue