fix prb 3
This commit is contained in:
parent
f12bdaa709
commit
6b6215f304
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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<>();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
|
@ -10,41 +10,75 @@ public class Pawn extends Piece {
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -3,46 +3,72 @@ 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;
|
|
||||||
this.isWhite = isWhite;
|
|
||||||
}
|
|
||||||
public int getX() {
|
|
||||||
return this.x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getY() {
|
private boolean hasMoved = false;
|
||||||
return this.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PieceType getType() {
|
public Piece(int x, int y, PieceType type, boolean isWhite) {
|
||||||
return this.type;
|
this.x = x;
|
||||||
}
|
this.y = y;
|
||||||
|
this.type = type;
|
||||||
|
this.isWhite = isWhite;
|
||||||
|
this.previousY = y; // Initialize previousY to current Y position
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isWhite() {
|
public int getX() {
|
||||||
return this.isWhite;
|
return this.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board);
|
public int getY() {
|
||||||
|
return this.y;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
public PieceType getType() {
|
||||||
public String toString() {
|
return this.type;
|
||||||
return "Piece [x=" + x + ", y=" + y + ", type=" + type + ", isWhite=" + isWhite + "]";
|
}
|
||||||
}
|
|
||||||
private boolean hasMoved = false;
|
|
||||||
|
|
||||||
public boolean hasMoved() {
|
public boolean isWhite() {
|
||||||
return hasMoved;
|
return this.isWhite;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMoved(boolean moved) {
|
// Getter for previous Y position
|
||||||
this.hasMoved = moved;
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,4 +26,4 @@ public class lastMove {
|
||||||
return toY;
|
return toY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue