fix prb 3

This commit is contained in:
martinbst 2025-05-01 14:19:42 +02:00
parent f12bdaa709
commit 6b6215f304
6 changed files with 162 additions and 106 deletions

View File

@ -225,7 +225,7 @@ public class Board {
this.turnColor = !this.turnColor; this.turnColor = !this.turnColor;
} }
} else { } else {
// System.out.println("a"); // Debug // System.out.println("a"); // Debug !
select = false; select = false;
} }

View File

@ -14,7 +14,7 @@ public class Knight extends Piece {
return getPossibleMoves(board, null); return getPossibleMoves(board, null);
} }
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) { public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>(); ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();

View File

@ -31,9 +31,5 @@ public class Move {
public String toString() { public String toString() {
return "(" + fromX + "," + fromY + ") → (" + toX + "," + toY + ")"; return "(" + fromX + "," + fromY + ") → (" + toX + "," + toY + ")";
} }
}
public Pawn getMovedPiece() { //
// TODO Auto-generated method stub
return null;
}
}

View File

@ -1,50 +1,84 @@
package backend; package backend;
import java.util.ArrayList; import java.util.ArrayList;
public class Pawn extends Piece { public class Pawn extends Piece {
public Pawn(int x, int y, boolean isWhite) { public Pawn(int x, int y, boolean isWhite) {
super(x, y, PieceType.Pawn, isWhite); super(x, y, PieceType.Pawn, isWhite);
} }
@Override @Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board) { public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>(); // Delegate to the full method with null lastMove
for (int i = 0; i < 8; i++) { return getPossibleMoves(board, null);
ArrayList<Boolean> row = new ArrayList<>(); }
for (int j = 0; j < 8; j++) {
row.add(false); @Override
} public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) {
moves.add(row); ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();
} for (int i = 0; i < 8; i++) {
ArrayList<Boolean> row = new ArrayList<>();
int direction = isWhite ? -1 : 1; for (int j = 0; j < 8; j++) {
int startRow = isWhite ? 6 : 1; row.add(false);
}
// Move forward moves.add(row);
int nextY = y + direction; }
if (nextY >= 0 && nextY < 8 && board.get(nextY).get(x) == null) {
moves.get(nextY).set(x, true); int direction = isWhite ? -1 : 1;
int startRow = isWhite ? 6 : 1;
// Two steps forward
int doubleY = y + 2 * direction; // Move forward
if (y == startRow && board.get(doubleY).get(x) == null) { int nextY = y + direction;
moves.get(doubleY).set(x, true); if (isValid(nextY, x) && board.get(nextY).get(x) == null) {
} moves.get(nextY).set(x, true);
}
// Two steps forward
// Captures int doubleY = y + 2 * direction;
for (int dx : new int[]{-1, 1}) { if (y == startRow && board.get(doubleY).get(x) == null) {
int newX = x + dx; moves.get(doubleY).set(x, true);
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); // Captures
} for (int dx : new int[]{-1, 1}) {
} int newX = x + dx;
} if (isValid(nextY, newX)) {
Piece target = board.get(nextY).get(newX);
return moves; 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;
}
} }

View File

@ -1,48 +1,74 @@
package backend; package backend;
import java.util.ArrayList; import java.util.ArrayList;
public abstract class Piece { public abstract class Piece {
public int x; public int x;
public int y; public int y;
public PieceType type; public PieceType type;
public boolean isWhite; public boolean isWhite;
public Piece(int x, int y, PieceType type, boolean isWhite) { // Track the previous Y position
this.x = x; protected int previousY;
this.y = y;
this.type = type; private boolean hasMoved = false;
this.isWhite = isWhite;
} public Piece(int x, int y, PieceType type, boolean isWhite) {
public int getX() { this.x = x;
return this.x; this.y = y;
} this.type = type;
this.isWhite = isWhite;
public int getY() { this.previousY = y; // Initialize previousY to current Y position
return this.y; }
}
public int getX() {
public PieceType getType() { return this.x;
return this.type; }
}
public int getY() {
public boolean isWhite() { return this.y;
return this.isWhite; }
}
public PieceType getType() {
public abstract ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board); return this.type;
}
@Override
public String toString() { public boolean isWhite() {
return "Piece [x=" + x + ", y=" + y + ", type=" + type + ", isWhite=" + isWhite + "]"; return this.isWhite;
} }
private boolean hasMoved = false;
// Getter for previous Y position
public boolean hasMoved() { public int getPreviousY() {
return hasMoved; return previousY;
} }
public void setMoved(boolean moved) { // Set the moved flag and update previous Y when the piece moves
this.hasMoved = moved; 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<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> 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<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) {
return null; // Will be implemented in specific piece subclasses (e.g., Pawn)
}
}

View File

@ -26,4 +26,4 @@ public class lastMove {
return toY; return toY;
} }
} }
//