New class MoveConditions to dictate the rules of pieces movement

This commit is contained in:
manon 2025-05-07 14:30:17 +02:00
parent 4f5eb969cf
commit c68fb9f095
2 changed files with 5 additions and 184 deletions

View File

@ -151,7 +151,7 @@ public class Board {
return;
}
// Check if move is valid (square must be highlighted to avoid our previous problem)
// Check if move is valid (square must be highlighted to avoid our previous problem of moving a piece to any square on the board)
boolean isValidMove = false;
for (int[] move : highlightedSquares) {
if (move[0] == x && move[1] == y) {
@ -242,7 +242,7 @@ public class Board {
}
public ArrayList<int[]> getValidMoves(Piece piece) {
Move moveHelper = new Move(piece, this);
MoveConditions moveHelper = new MoveConditions(piece, this);
switch (piece.getType()) {
case Pawn:

View File

@ -1,188 +1,9 @@
package backend;
import java.util.ArrayList;
public class Move {
private Piece piece;
private Board board;
public Move(Piece piece, Board board) {
this.piece = piece;
this.board = board;
}
public ArrayList<int[]> getPawnMoves() {
ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
int dir = isWhite ? -1 : 1;
// Move one square forward (only if empty)
if (board.getPieceAt(x, y + dir) == null) {
moves.add(new int[]{x, y + dir});
// Move two squares from starting position (also only if empty)
boolean isAtStart = (isWhite && y == 6) || (!isWhite && y == 1);
if (isAtStart && board.getPieceAt(x, y + 2 * dir) == null) {
moves.add(new int[]{x, y + 2 * dir});
}
}
// Capture diagonally (because apparently pawns capture that way, I didn't know...)
for (int dx : new int[]{-1, 1}) {
int targetX = x + dx;
int targetY = y + dir;
if (targetX >= 0 && targetX < board.getWidth() && targetY >= 0 && targetY < board.getHeight()) {
Piece target = board.getPieceAt(targetX, targetY);
if (target != null && target.isWhite() != isWhite) {
moves.add(new int[]{targetX, targetY});
}
}
}
return moves;
}
public ArrayList<int[]> getKnightMoves() {
ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
int[][] offsets = {
{2, 1}, {1, 2}, {-1, 2}, {-2, 1},
{-2, -1}, {-1, -2}, {1, -2}, {2, -1}
};
for (int[] offset : offsets) {
int newX = x + offset[0];
int newY = y + offset[1];
if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
Piece target = board.getPieceAt(newX, newY);
if (target == null || target.isWhite() != isWhite) {
moves.add(new int[]{newX, newY});
}
}
}
return moves;
}
public ArrayList<int[]> getRookMoves() {
ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
int[][] directions = {
{1, 0}, {-1, 0}, // Right, Left
{0, 1}, {0, -1} // Down, Up
};
for (int[] dir : directions) {
int dx = dir[0];
int dy = dir[1];
int currX = x + dx;
int currY = y + dy;
boolean blocked = false;
while (!blocked && currX >= 0 && currX < board.getWidth() && currY >= 0 && currY < board.getHeight()) {
Piece target = board.getPieceAt(currX, currY);
if (target == null) {
moves.add(new int[]{currX, currY});
} else {
if (target.isWhite() != isWhite) {
moves.add(new int[]{currX, currY}); // capture allowed
}
blocked = true; // either way, we stop after hitting a piece
}
currX += dx;
currY += dy;
}
}
return moves;
}
public ArrayList<int[]> getBishopMoves() {
ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
int[][] directions = {
{1, 1}, {-1, 1}, // Down-right, Down-left
{1, -1}, {-1, -1} // Up-right, Up-left
};
for (int[] dir : directions) {
int dx = dir[0];
int dy = dir[1];
int currX = x + dx;
int currY = y + dy;
boolean blocked = false;
while (!blocked && currX >= 0 && currX < board.getWidth() && currY >= 0 && currY < board.getHeight()) {
Piece target = board.getPieceAt(currX, currY);
if (target == null) {
moves.add(new int[]{currX, currY});
} else {
if (target.isWhite() != isWhite) {
moves.add(new int[]{currX, currY}); // capture allowed
}
blocked = true;
}
currX += dx;
currY += dy;
}
}
return moves;
}
public ArrayList<int[]> getQueenMoves() {
ArrayList<int[]> moves = new ArrayList<>();
// Combine Rook and Bishop moves
moves.addAll(getRookMoves());
moves.addAll(getBishopMoves());
return moves;
}
public ArrayList<int[]> getKingMoves() {
ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
int[][] offsets = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
for (int[] offset : offsets) {
int newX = x + offset[0];
int newY = y + offset[1];
if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
Piece target = board.getPieceAt(newX, newY);
if (target == null || target.isWhite() != isWhite) {
moves.add(new int[]{newX, newY});
}
}
}
return moves;
}
}