Merge branch 'master' of

https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
This commit is contained in:
manon 2025-05-07 14:37:50 +02:00
parent 4c612b398d
commit befb06f3ae
1 changed files with 188 additions and 0 deletions

View File

@ -0,0 +1,188 @@
package backend;
import java.util.ArrayList;
public class MoveConditions {
private Piece piece;
private Board board;
public MoveConditions(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;
}
}