diff --git a/OOP_2B1_Project/src/backend/Pawn.java b/OOP_2B1_Project/src/backend/Pawn.java index 2839678..a7a4347 100644 --- a/OOP_2B1_Project/src/backend/Pawn.java +++ b/OOP_2B1_Project/src/backend/Pawn.java @@ -11,28 +11,31 @@ public class Pawn extends Piece { @Override public List getLegalMoves(Board board, int row, int col) { + List moves = new ArrayList<>(); - int direction = isWhite ? 1 : -1; // White moves up, black down + + + int direction = isWhite ? 1 : -1; + int startRow = isWhite ? 1 : 6; - // Move forward - int oneStep = row + direction; - if (board.isInBounds(col, oneStep) && board.getPieceAt(col, oneStep) == null) { - moves.add(new Move(this, row, col, oneStep, col)); - int twoStep = row + 2 * direction; - if (row == startRow && board.isInBounds(col, twoStep) && board.getPieceAt(col, twoStep) == null) { - moves.add(new Move(this, row, col, twoStep, col)); + int oneStepY = row + direction; + if (board.isInBounds(col, oneStepY) && board.getPieceAt(col, oneStepY) == null) { + moves.add(new Move(this, row, col, oneStepY, col)); + + int twoStepY = row + 2 * direction; + if (row == startRow && board.isInBounds(col, twoStepY) && board.getPieceAt(col, twoStepY) == null) { + moves.add(new Move(this, row, col, twoStepY, col)); } } - // Captures int[] dx = {-1, 1}; - for (int d : dx) { - int targetCol = col + d; - if (board.isInBounds(targetCol, oneStep)) { - Piece enemy = board.getPieceAt(targetCol, oneStep); + for (int offsetX : dx) { + int targetX = col + offsetX; + if (board.isInBounds(targetX, oneStepY)) { + Piece enemy = board.getPieceAt(targetX, oneStepY); if (enemy != null && enemy.isWhite() != this.isWhite) { - moves.add(new Move(this, row, col, oneStep, targetCol, enemy)); + moves.add(new Move(this, row, col, oneStepY, targetX, enemy)); } } } diff --git a/OOP_2B1_Project/src/backend/Rook.java b/OOP_2B1_Project/src/backend/Rook.java index 64ae21e..9b2787f 100644 --- a/OOP_2B1_Project/src/backend/Rook.java +++ b/OOP_2B1_Project/src/backend/Rook.java @@ -11,18 +11,25 @@ class Rook extends Piece { @Override public List getLegalMoves(Board board, int row, int col) { List moves = new ArrayList<>(); - int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + + int[][] directions = { + {-1, 0}, // up + { 1, 0}, // down + { 0, -1}, // left + { 0, 1} // right + }; for (int[] dir : directions) { int r = row + dir[0]; int c = col + dir[1]; - while (board.isInBounds(r, c)) { - Piece target = board.getPieceAt(r, c); + while (board.isInBounds(c, r)) { + Piece target = board.getPieceAt(c, r); if (target == null) { moves.add(new Move(this, row, col, r, c)); } else { - if (target.isWhite() != isWhite) + if (target.isWhite() != isWhite) { moves.add(new Move(this, row, col, r, c, target)); + } break; } r += dir[0];