From 1f27eec9aa317c92b18d61e30004352ba2764369 Mon Sep 17 00:00:00 2001 From: "charles.duteil" Date: Sun, 18 May 2025 15:08:32 +0200 Subject: [PATCH] Bishop movement updated --- OOP_2B1_Project/src/backend/Bishop.java | 19 +++++++++++++++---- OOP_2B1_Project/src/backend/Queen.java | 12 ++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/OOP_2B1_Project/src/backend/Bishop.java b/OOP_2B1_Project/src/backend/Bishop.java index fd920b3..8d3b12e 100644 --- a/OOP_2B1_Project/src/backend/Bishop.java +++ b/OOP_2B1_Project/src/backend/Bishop.java @@ -11,20 +11,31 @@ class Bishop extends Piece { @Override public List getLegalMoves(Board board, int row, int col) { List moves = new ArrayList<>(); - int[][] directions = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; + // diagonals only + int[][] directions = { + {-1, -1}, // up-left + {-1, 1}, // up-right + { 1, -1}, // down-left + { 1, 1} // down-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]; c += dir[1]; } diff --git a/OOP_2B1_Project/src/backend/Queen.java b/OOP_2B1_Project/src/backend/Queen.java index c5e62c1..d77fc3a 100644 --- a/OOP_2B1_Project/src/backend/Queen.java +++ b/OOP_2B1_Project/src/backend/Queen.java @@ -11,7 +11,7 @@ class Queen extends Piece { @Override public List getLegalMoves(Board board, int row, int col) { List moves = new ArrayList<>(); - // directions: each [dRow, dCol], effectively [dy, dx] + int[][] directions = { {-1, 0}, // up { 1, 0}, // down @@ -23,24 +23,24 @@ class Queen extends Piece { { 1, 1} // down-right }; - // For each direction, keep going while in bounds + for (int[] dir : directions) { int r = row + dir[0]; int c = col + dir[1]; while (board.isInBounds(c, r)) { Piece target = board.getPieceAt(c, r); if (target == null) { - // empty square + moves.add(new Move(this, row, col, r, c)); } else { - // if opposite color, can capture + if (target.isWhite() != isWhite) { moves.add(new Move(this, row, col, r, c, target)); } - // cannot jump beyond this piece + break; } - // move further in the same direction + r += dir[0]; c += dir[1]; }