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;
}
} else {
// System.out.println("a"); // Debug
// System.out.println("a"); // Debug !
select = false;
}

View File

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

View File

@ -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;
}
}
}
//

View File

@ -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<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();
for (int i = 0; i < 8; i++) {
ArrayList<Boolean> 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<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board) {
// Delegate to the full method with null lastMove
return getPossibleMoves(board, null);
}
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();
for (int i = 0; i < 8; i++) {
ArrayList<Boolean> 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;
}
}

View File

@ -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<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> 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<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;
}
}
//