moveset of pieces
This commit is contained in:
parent
a7d7f5a7dc
commit
64650ca279
|
|
@ -1,5 +1,158 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Move {
|
public class Move {
|
||||||
|
|
||||||
|
// Returns a list of valid rook moves (horizontal and vertical)
|
||||||
|
public static List<int[]> getRookMoves(Board board, int x, int y) {
|
||||||
|
List<int[]> moves = new ArrayList<>();
|
||||||
|
Piece piece = board.getPiece(x, y);
|
||||||
|
if (piece == null) return moves;
|
||||||
|
|
||||||
|
int[][] directions = {
|
||||||
|
{1, 0}, {-1, 0}, // Right, Left
|
||||||
|
{0, 1}, {0, -1} // Up, Down
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int[] dir : directions) {
|
||||||
|
int dx = dir[0], dy = dir[1];
|
||||||
|
int cx = x + dx, cy = y + dy;
|
||||||
|
while (board.isInBounds(cx, cy)) {
|
||||||
|
Piece target = board.getPiece(cx, cy);
|
||||||
|
if (target == null) {
|
||||||
|
moves.add(new int[]{cx, cy});
|
||||||
|
} else {
|
||||||
|
if (target.isWhite() != piece.isWhite()) {
|
||||||
|
moves.add(new int[]{cx, cy}); // Capture
|
||||||
|
}
|
||||||
|
break; // Stop at first piece
|
||||||
|
}
|
||||||
|
cx += dx;
|
||||||
|
cy += dy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a list of valid knight moves (L-shaped)
|
||||||
|
public static List<int[]> getKnightMoves(Board board, int x, int y) {
|
||||||
|
List<int[]> moves = new ArrayList<>();
|
||||||
|
Piece piece = board.getPiece(x, y);
|
||||||
|
if (piece == null) return moves;
|
||||||
|
|
||||||
|
int[][] deltas = {
|
||||||
|
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
|
||||||
|
{1, -2}, {2, -1}, {-1, -2}, {-2, -1}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int[] d : deltas) {
|
||||||
|
int nx = x + d[0], ny = y + d[1];
|
||||||
|
if (board.isInBounds(nx, ny)) {
|
||||||
|
Piece target = board.getPiece(nx, ny);
|
||||||
|
if (target == null || target.isWhite() != piece.isWhite()) {
|
||||||
|
moves.add(new int[]{nx, ny});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Example: pawn movement (basic, no promotion/en passant)
|
||||||
|
public static List<int[]> getPawnMoves(Board board, int x, int y) {
|
||||||
|
List<int[]> moves = new ArrayList<>();
|
||||||
|
Piece pawn = board.getPiece(x, y);
|
||||||
|
if (pawn == null) return moves;
|
||||||
|
|
||||||
|
int direction = pawn.isWhite() ? -1 : 1;
|
||||||
|
int startRow = pawn.isWhite() ? 6 : 1;
|
||||||
|
|
||||||
|
// Forward 1
|
||||||
|
if (board.isInBounds(x, y + direction) && board.getPiece(x, y + direction) == null) {
|
||||||
|
moves.add(new int[]{x, y + direction});
|
||||||
|
|
||||||
|
// Forward 2 from starting row
|
||||||
|
if (y == startRow && board.getPiece(x, y + 2 * direction) == null) {
|
||||||
|
moves.add(new int[]{x, y + 2 * direction});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Captures
|
||||||
|
for (int dx : new int[]{-1, 1}) {
|
||||||
|
int nx = x + dx;
|
||||||
|
int ny = y + direction;
|
||||||
|
if (board.isInBounds(nx, ny)) {
|
||||||
|
Piece target = board.getPiece(nx, ny);
|
||||||
|
if (target != null && target.isWhite() != pawn.isWhite()) {
|
||||||
|
moves.add(new int[]{nx, ny});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stub for bishop (you can implement like the rook but with diagonal directions)
|
||||||
|
public static List<int[]> getBishopMoves(Board board, int x, int y) {
|
||||||
|
List<int[]> moves = new ArrayList<>();
|
||||||
|
Piece piece = board.getPiece(x, y);
|
||||||
|
if (piece == null) return moves;
|
||||||
|
|
||||||
|
int[][] directions = {
|
||||||
|
{1, 1}, {-1, 1}, {1, -1}, {-1, -1}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int[] dir : directions) {
|
||||||
|
int dx = dir[0], dy = dir[1];
|
||||||
|
int cx = x + dx, cy = y + dy;
|
||||||
|
while (board.isInBounds(cx, cy)) {
|
||||||
|
Piece target = board.getPiece(cx, cy);
|
||||||
|
if (target == null) {
|
||||||
|
moves.add(new int[]{cx, cy});
|
||||||
|
} else {
|
||||||
|
if (target.isWhite() != piece.isWhite()) {
|
||||||
|
moves.add(new int[]{cx, cy});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cx += dx;
|
||||||
|
cy += dy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Queen = rook + bishop moves
|
||||||
|
public static List<int[]> getQueenMoves(Board board, int x, int y) {
|
||||||
|
List<int[]> moves = new ArrayList<>();
|
||||||
|
moves.addAll(getRookMoves(board, x, y));
|
||||||
|
moves.addAll(getBishopMoves(board, x, y));
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
// King = 1 step in any direction
|
||||||
|
public static List<int[]> getKingMoves(Board board, int x, int y) {
|
||||||
|
List<int[]> moves = new ArrayList<>();
|
||||||
|
Piece king = board.getPiece(x, y);
|
||||||
|
if (king == null) return moves;
|
||||||
|
|
||||||
|
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 (board.isInBounds(nx, ny)) {
|
||||||
|
Piece target = board.getPiece(nx, ny);
|
||||||
|
if (target == null || target.isWhite() != king.isWhite()) {
|
||||||
|
moves.add(new int[]{nx, ny});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue