OOP_2A5_Project/src/backend/MoveConditions.java

217 lines
7.0 KiB
Java

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 i = 0; i < offsets.length; i++) {
int newX = x + offsets[i][0];
int newY = y + offsets[i][1];
if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
Piece target = board.getPieceAt(newX, newY);
if (target == null || target.isWhite() != isWhite) {
Board simBoard = new Board(board);
// Find this same King in the simulated board
Piece simKing = null;
for (int j = 0; j < simBoard.getPieces().size(); j++) {
Piece p = simBoard.getPieces().get(j);
if (p.getX() == x && p.getY() == y &&
p.getType() == PieceType.King &&
p.isWhite() == isWhite) {
simKing = p;
}
}
if (simKing != null) {
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
}
simKing.setX(newX);
simKing.setY(newY);
boolean stillInCheck = simBoard.isKingInCheck(isWhite);
if (!stillInCheck) {
moves.add(new int[]{newX, newY});
}
}
}
}
}
return moves;
}
}