Changed the getValidMoves

This commit is contained in:
Marleen PETERSON 2025-05-01 22:02:00 +02:00
parent e50a933874
commit 4658af653d
1 changed files with 115 additions and 86 deletions

View File

@ -136,102 +136,131 @@ public class Board {
board[fromY][fromX] = null; board[fromY][fromX] = null;
} }
private Set<String> getValidMoves(Piece p) { private Set<String> getValidMoves(Piece piece) {
Set<String> valid = new HashSet<>(); Set<String> validMoves = new HashSet<>();
int x = p.getX();
int y = p.getY();
PieceType type = p.getType();
boolean isWhite = p.isWhite();
int dir = isWhite ? -1 : 1; int x = piece.getX(); // Current position coordinate x
int y = piece.getY(); // Current position coordinate y
boolean isWhite = piece.isWhite(); // Returns true if a piece is white, if black then false
PieceType type = piece.getType(); // What type of piece is considered (pawn, rook...)
switch (type) { // Direction (y coordinate change): White moves "up" (-1), Black moves "down" (+1)
case Pawn: int direction = isWhite ? -1 : 1;
if (inBounds(x, y + dir) && board[y + dir][x] == null) {
valid.add(x + "," + (y + dir));
}
// First move 2-steps
if ((isWhite && y == 6) || (!isWhite && y == 1)) {
if (board[y + dir][x] == null && board[y + 2 * dir][x] == null) {
valid.add(x + "," + (y + 2 * dir));
}
}
// Capture diagonally
if (inBounds(x - 1, y + dir) && board[y + dir][x - 1] != null && board[y + dir][x - 1].isWhite() != isWhite)
valid.add((x - 1) + "," + (y + dir));
if (inBounds(x + 1, y + dir) && board[y + dir][x + 1] != null && board[y + dir][x + 1].isWhite() != isWhite)
valid.add((x + 1) + "," + (y + dir));
break;
case Rook: switch (type) {
addLinearMoves(valid, x, y, isWhite, 1, 0); case Pawn: // moves one square forward (1st move 2 squares possible)
addLinearMoves(valid, x, y, isWhite, -1, 0); // Move one step forward (if the square in front is inside the board and empty)
addLinearMoves(valid, x, y, isWhite, 0, 1); if (inBounds(x, y + direction) && board[y + direction][x] == null) {
addLinearMoves(valid, x, y, isWhite, 0, -1); validMoves.add(x + "," + (y + direction));
break; }
case Bishop: // First move: two steps forward (initial position - white piece 6th row, black 1st row)
addLinearMoves(valid, x, y, isWhite, 1, 1); boolean onStartRow = (isWhite && y == 6) || (!isWhite && y == 1);
addLinearMoves(valid, x, y, isWhite, 1, -1); // The square in front and one after that must be empty
addLinearMoves(valid, x, y, isWhite, -1, 1); if (onStartRow && board[y + direction][x] == null && board[y + 2 * direction][x] == null) {
addLinearMoves(valid, x, y, isWhite, -1, -1); validMoves.add(x + "," + (y + 2 * direction));
break; }
case Queen: // Capture diagonally
addLinearMoves(valid, x, y, isWhite, 1, 0); int[] dx = {-1, 1};
addLinearMoves(valid, x, y, isWhite, -1, 0); // Checking diagonal squares in both directions
addLinearMoves(valid, x, y, isWhite, 0, 1); for (int i : dx) {
addLinearMoves(valid, x, y, isWhite, 0, -1); int targetX = x + i;
addLinearMoves(valid, x, y, isWhite, 1, 1); int targetY = y + direction;
addLinearMoves(valid, x, y, isWhite, 1, -1); // Checking if the target square is inside the board and contains opponent's piece
addLinearMoves(valid, x, y, isWhite, -1, 1); if (inBounds(targetX, targetY)) {
addLinearMoves(valid, x, y, isWhite, -1, -1); Piece target = board[targetY][targetX];
break; if (target != null && target.isWhite() != isWhite) {
validMoves.add(targetX + "," + targetY);
}
}
}
break;
case Knight: case Rook: // Moves in straight lines
int[][] knightMoves = { addLinearMoves(validMoves, x, y, isWhite, 1, 0); // Right
{1, 2}, {2, 1}, {-1, 2}, {-2, 1}, addLinearMoves(validMoves, x, y, isWhite, -1, 0); // Left
{1, -2}, {2, -1}, {-1, -2}, {-2, -1} addLinearMoves(validMoves, x, y, isWhite, 0, 1); // Down
}; addLinearMoves(validMoves, x, y, isWhite, 0, -1); // Up
for (int[] m : knightMoves) { break;
int nx = x + m[0];
int ny = y + m[1];
if (inBounds(nx, ny) && (board[ny][nx] == null || board[ny][nx].isWhite() != isWhite)) {
valid.add(nx + "," + ny);
}
}
break;
case King: case Bishop: // Moves diagonally
for (int dx = -1; dx <= 1; dx++) { addLinearMoves(validMoves, x, y, isWhite, 1, 1); // Down-Right
for (int dy = -1; dy <= 1; dy++) { addLinearMoves(validMoves, x, y, isWhite, 1, -1); // Up-Right
if (dx == 0 && dy == 0) continue; addLinearMoves(validMoves, x, y, isWhite, -1, 1); // Down-Left
int nx = x + dx, ny = y + dy; addLinearMoves(validMoves, x, y, isWhite, -1, -1); // Up-Left
if (inBounds(nx, ny) && (board[ny][nx] == null || board[ny][nx].isWhite() != isWhite)) { break;
valid.add(nx + "," + ny);
} case Queen: // Combines Rook + Bishop movements
} int[][] queenDirections = {
} {1, 0}, {-1, 0}, {0, 1}, {0, -1},
break; {1, 1}, {1, -1}, {-1, 1}, {-1, -1}
} };
return valid; for (int[] dir : queenDirections) {
addLinearMoves(validMoves, x, y, isWhite, dir[0], dir[1]);
}
break;
case Knight: // Jumps in L shapes (3 straight, 1 to the side)
int[][] knightMoves = {
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
{1, -2}, {2, -1}, {-1, -2}, {-2, -1}
};
for (int[] move : knightMoves) {
int nx = x + move[0];
int ny = y + move[1];
if (inBounds(nx, ny)) {
Piece target = board[ny][nx];
if (target == null || target.isWhite() != isWhite) {
validMoves.add(nx + "," + ny);
}
}
}
break;
case King: // Moves 1 square in any direction
for (int dxKing = -1; dxKing <= 1; dxKing++) {
for (int dyKing = -1; dyKing <= 1; dyKing++) {
if (dxKing == 0 && dyKing == 0) continue;
int nx = x + dxKing;
int ny = y + dyKing;
if (inBounds(nx, ny)) {
Piece target = board[ny][nx];
if (target == null || target.isWhite() != isWhite) {
validMoves.add(nx + "," + ny);
}
}
}
}
break;
}
return validMoves; // Returning the list of valid moves for each piece
} }
private void addLinearMoves(Set<String> valid, int x, int y, boolean isWhite, int dx, int dy) { private void addLinearMoves(Set<String> valid, int x, int y, boolean isWhite, int dx, int dy) {
int nx = x + dx; int nx = x + dx;
int ny = y + dy; int ny = y + dy;
while (inBounds(nx, ny)) {
if (board[ny][nx] == null) { while (inBounds(nx, ny)) {
valid.add(nx + "," + ny); Piece target = board[ny][nx];
} else {
if (board[ny][nx].isWhite() != isWhite) { if (target == null) {
valid.add(nx + "," + ny); // If empty square then add valid move
} valid.add(nx + "," + ny);
break; } else {
} // If it's an opponent's piece capture is allowed
nx += dx; if (target.isWhite() != isWhite) {
ny += dy; valid.add(nx + "," + ny);
} }
// Stop moving further (can't jump over pieces)
break;
}
// Move one step further in the same direction
nx += dx;
ny += dy;
}
} }
private boolean inBounds(int x, int y) { private boolean inBounds(int x, int y) {