diff --git a/OOP_2B1_Project/src/backend/Board.java b/OOP_2B1_Project/src/backend/Board.java index 11db6a7..c1dc11b 100644 --- a/OOP_2B1_Project/src/backend/Board.java +++ b/OOP_2B1_Project/src/backend/Board.java @@ -225,7 +225,7 @@ public class Board { this.turnColor = !this.turnColor; } } else { -// System.out.println("a"); // Debug +// System.out.println("a"); // Debug ! select = false; } diff --git a/OOP_2B1_Project/src/backend/Knight.java b/OOP_2B1_Project/src/backend/Knight.java index df0f3f3..b38fd4c 100644 --- a/OOP_2B1_Project/src/backend/Knight.java +++ b/OOP_2B1_Project/src/backend/Knight.java @@ -14,7 +14,7 @@ public class Knight extends Piece { return getPossibleMoves(board, null); } - @Override + public ArrayList> getPossibleMoves(ArrayList> board, Move lastMove) { ArrayList> moves = new ArrayList<>(); diff --git a/OOP_2B1_Project/src/backend/Move.java b/OOP_2B1_Project/src/backend/Move.java index aa2d1b6..665c833 100644 --- a/OOP_2B1_Project/src/backend/Move.java +++ b/OOP_2B1_Project/src/backend/Move.java @@ -31,9 +31,5 @@ public class Move { public String toString() { return "(" + fromX + "," + fromY + ") → (" + toX + "," + toY + ")"; } - - public Pawn getMovedPiece() { - // TODO Auto-generated method stub - return null; - } -} \ No newline at end of file +} +// \ No newline at end of file diff --git a/OOP_2B1_Project/src/backend/Pawn.java b/OOP_2B1_Project/src/backend/Pawn.java index e6dc4d6..c03c2e8 100644 --- a/OOP_2B1_Project/src/backend/Pawn.java +++ b/OOP_2B1_Project/src/backend/Pawn.java @@ -1,50 +1,84 @@ -package backend; - -import java.util.ArrayList; - -public class Pawn extends Piece { - - public Pawn(int x, int y, boolean isWhite) { - super(x, y, PieceType.Pawn, isWhite); - } - - @Override - public ArrayList> getPossibleMoves(ArrayList> board) { - ArrayList> moves = new ArrayList<>(); - for (int i = 0; i < 8; i++) { - ArrayList row = new ArrayList<>(); - for (int j = 0; j < 8; j++) { - row.add(false); - } - moves.add(row); - } - - int direction = isWhite ? -1 : 1; - int startRow = isWhite ? 6 : 1; - - // Move forward - int nextY = y + direction; - if (nextY >= 0 && nextY < 8 && board.get(nextY).get(x) == null) { - moves.get(nextY).set(x, true); - - // Two steps forward - int doubleY = y + 2 * direction; - if (y == startRow && board.get(doubleY).get(x) == null) { - moves.get(doubleY).set(x, true); - } - } - - // Captures - for (int dx : new int[]{-1, 1}) { - int newX = x + dx; - if (newX >= 0 && newX < 8 && nextY >= 0 && nextY < 8) { - Piece target = board.get(nextY).get(newX); - if (target != null && target.isWhite() != this.isWhite) { - moves.get(nextY).set(newX, true); - } - } - } - - return moves; - } +package backend; + +import java.util.ArrayList; + +public class Pawn extends Piece { + + public Pawn(int x, int y, boolean isWhite) { + super(x, y, PieceType.Pawn, isWhite); + } + + @Override + public ArrayList> getPossibleMoves(ArrayList> board) { + // Delegate to the full method with null lastMove + return getPossibleMoves(board, null); + } + + @Override + public ArrayList> getPossibleMoves(ArrayList> board, Move lastMove) { + ArrayList> moves = new ArrayList<>(); + for (int i = 0; i < 8; i++) { + ArrayList row = new ArrayList<>(); + for (int j = 0; j < 8; j++) { + row.add(false); + } + moves.add(row); + } + + int direction = isWhite ? -1 : 1; + int startRow = isWhite ? 6 : 1; + + // Move forward + int nextY = y + direction; + if (isValid(nextY, x) && board.get(nextY).get(x) == null) { + moves.get(nextY).set(x, true); + + // Two steps forward + int doubleY = y + 2 * direction; + if (y == startRow && board.get(doubleY).get(x) == null) { + moves.get(doubleY).set(x, true); + } + } + + // Captures + for (int dx : new int[]{-1, 1}) { + int newX = x + dx; + if (isValid(nextY, newX)) { + Piece target = board.get(nextY).get(newX); + if (target != null && target.isWhite() != this.isWhite) { + moves.get(nextY).set(newX, true); + } + } + } + + // En Passant mode + if (lastMove != null && lastMove.getToY() != lastMove.getFromY()) { + + if (Math.abs(lastMove.getToY() - lastMove.getFromY()) == 2) { + for (int dx : new int[]{-1, 1}) { + int newX = x + dx; + if (isValid(y, newX)) { + Piece adjacent = board.get(y).get(newX); + if (adjacent instanceof Pawn && adjacent.isWhite() != this.isWhite) { + if (lastMove.getToX() == newX && lastMove.getToY() == y) { + int enPassantY = y + direction; + if (isValid(enPassantY, newX)) { + moves.get(enPassantY).set(newX, true); + } + } + } + } + } + } + } + + return moves; + } + + + //on est bien dans les bornes ! + private boolean isValid(int row, int col) { + return row >= 0 && row < 8 && col >= 0 && col < 8; + } + } \ No newline at end of file diff --git a/OOP_2B1_Project/src/backend/Piece.java b/OOP_2B1_Project/src/backend/Piece.java index cb288b9..17979e7 100644 --- a/OOP_2B1_Project/src/backend/Piece.java +++ b/OOP_2B1_Project/src/backend/Piece.java @@ -1,48 +1,74 @@ -package backend; - -import java.util.ArrayList; - -public abstract class Piece { - public int x; - public int y; - public PieceType type; - public boolean isWhite; - - public Piece(int x, int y, PieceType type, boolean isWhite) { - this.x = x; - this.y = y; - this.type = type; - this.isWhite = isWhite; - } - public int getX() { - return this.x; - } - - public int getY() { - return this.y; - } - - public PieceType getType() { - return this.type; - } - - public boolean isWhite() { - return this.isWhite; - } - - public abstract ArrayList> getPossibleMoves(ArrayList> board); - - @Override - public String toString() { - return "Piece [x=" + x + ", y=" + y + ", type=" + type + ", isWhite=" + isWhite + "]"; - } - private boolean hasMoved = false; - - public boolean hasMoved() { - return hasMoved; - } - - public void setMoved(boolean moved) { - this.hasMoved = moved; - } -} +package backend; + +import java.util.ArrayList; + +public abstract class Piece { + public int x; + public int y; + public PieceType type; + public boolean isWhite; + + // Track the previous Y position + protected int previousY; + + private boolean hasMoved = false; + + public Piece(int x, int y, PieceType type, boolean isWhite) { + this.x = x; + this.y = y; + this.type = type; + this.isWhite = isWhite; + this.previousY = y; // Initialize previousY to current Y position + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public PieceType getType() { + return this.type; + } + + public boolean isWhite() { + return this.isWhite; + } + + // Getter for previous Y position + public int getPreviousY() { + return previousY; + } + + // Set the moved flag and update previous Y when the piece moves + public void moveTo(int newX, int newY) { + this.previousY = this.y; // Update previous Y before changing the position + this.x = newX; + this.y = newY; + this.hasMoved = true; // Mark that the piece has moved + } + + public boolean hasMoved() { + return hasMoved; + } + + public void setMoved(boolean moved) { + this.hasMoved = moved; + } + + // Abstract method to get possible moves (to be implemented by subclasses) + public abstract ArrayList> getPossibleMoves(ArrayList> board); + + + @Override + public String toString() { + return "Piece [x=" + x + ", y=" + y + ", type=" + type + ", isWhite=" + isWhite + "]"; + } + + // This is the overloaded method for getting possible moves with lastMove + public ArrayList> getPossibleMoves(ArrayList> board, Move lastMove) { + return null; // Will be implemented in specific piece subclasses (e.g., Pawn) + } +} diff --git a/OOP_2B1_Project/src/backend/lastMove.java b/OOP_2B1_Project/src/backend/lastMove.java index d126dc4..e0f54e3 100644 --- a/OOP_2B1_Project/src/backend/lastMove.java +++ b/OOP_2B1_Project/src/backend/lastMove.java @@ -26,4 +26,4 @@ public class lastMove { return toY; } } - +//