Merge branch 'master' of

https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
This commit is contained in:
PIRANUT_PHLANG 2025-05-07 15:36:40 +02:00
commit 2e04e59529
2 changed files with 173 additions and 15 deletions

View File

@ -115,16 +115,14 @@ public class Board {
} }
return sb.toString(); return sb.toString();
//TODO
} }
public ArrayList<Piece> getPieces() { public ArrayList<Piece> getPieces() {
//TODO
return pieces; return pieces;
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
// If no position has been previously selected // If no position has been previously selected
if (selected == null) { if (selected == null) {
// Check if there is a piece at the selected coordinates // Check if there is a piece at the selected coordinates
@ -179,17 +177,139 @@ public class Board {
//TODO //TODO
Piece clickedPiece = getPieceAt(x, y);
if (selected == null) {
// First click: try selecting a piece
if (clickedPiece != null && clickedPiece.isWhite() == turnWhite) {
selected = new int[]{x, y};
highlighted = getLegalMoves(clickedPiece);
}
} else {
// Second click: check if move is legal
if (isHighlighted(x, y)) {
Piece piece = getPieceAt(selected[0], selected[1]);
Piece captured = getPieceAt(x, y);
Move move = new Move(piece, selected[0], selected[1], x, y, captured);
playMove(move);
} else {
// If clicked on another own piece, reselect
if (clickedPiece != null && clickedPiece.isWhite() == turnWhite) {
selected = new int[]{x, y};
highlighted = getLegalMoves(clickedPiece);
} else {
selected = null;
highlighted.clear();
}
}
}
} }
public boolean isSelected(int x, int y) {
//TODO private ArrayList<int[]> getLegalMoves(Piece piece) {
return false; ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX(), y = piece.getY();
int dir = piece.isWhite() ? 1 : -1;
switch (piece.getType()) {
case Pawn:
int forwardY = y + dir;
if (getPieceAt(x, forwardY) == null)
moves.add(new int[] {x, forwardY});
Piece diagLeft = getPieceAt(x - 1, forwardY);
if (diagLeft != null && diagLeft.isWhite() != piece.isWhite())
moves.add(new int[] {x - 1, forwardY});
Piece diagRight = getPieceAt(x + 1, forwardY);
if (diagRight != null && diagRight.isWhite() != piece.isWhite())
moves.add(new int[] {x + 1, forwardY});
break;
case Rook:
addLinearMoves(moves, piece, 1, 0);
addLinearMoves(moves, piece, -1, 0);
addLinearMoves(moves, piece, 0, 1);
addLinearMoves(moves, piece, 0, -1);
break;
case Bishop:
addLinearMoves(moves, piece, 1, 1);
addLinearMoves(moves, piece, -1, 1);
addLinearMoves(moves, piece, 1, -1);
addLinearMoves(moves, piece, -1, -1);
break;
case Queen:
addLinearMoves(moves, piece, 1, 0);
addLinearMoves(moves, piece, -1, 0);
addLinearMoves(moves, piece, 0, 1);
addLinearMoves(moves, piece, 0, -1);
addLinearMoves(moves, piece, 1, 1);
addLinearMoves(moves, piece, -1, 1);
addLinearMoves(moves, piece, 1, -1);
addLinearMoves(moves, piece, -1, -1);
break;
case King:
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue;
int nx = x + dx, ny = y + dy;
if (isInBounds(nx, ny) && (getPieceAt(nx, ny) == null || getPieceAt(nx, ny).isWhite() != piece.isWhite())) {
moves.add(new int[]{nx, ny});
}
}
}
break;
case Knight:
int[][] knightMoves = {
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
{1, -2}, {2, -1}, {-1, -2}, {-2, -1}
};
for (int[] m : knightMoves) {
int nx = x + m[0], ny = y + m[1];
if (isInBounds(nx, ny) && (getPieceAt(nx, ny) == null || getPieceAt(nx, ny).isWhite() != piece.isWhite())) {
moves.add(new int[]{nx, ny});
}
}
break;
}
return moves;
} }
private void addLinearMoves(ArrayList<int[]> moves, Piece piece, int dx, int dy) {
int x = piece.getX(), y = piece.getY();
while (true) {
x += dx;
y += dy;
if (!isInBounds(x, y)) break;
Piece target = getPieceAt(x, y);
if (target == null) {
moves.add(new int[]{x, y});
} else {
if (target.isWhite() != piece.isWhite())
moves.add(new int[]{x, y});
break;
}
}
}
private boolean isInBounds(int x, int y) {
return x >= 0 && y >= 0 && x < width && y < height;
}
public boolean isSelected(int x, int y) {
return selected != null && selected[0] == x && selected[1] == y;
}
/* saving-loading feature :*/ /* saving-loading feature :*/
public String[] toFileRep() { public String[] toFileRep() {
//TODO
return null; return null;
} }
@ -198,6 +318,16 @@ public class Board {
} }
private Piece getPieceAt(int x, int y) {
for (Piece piece : pieces) {
if (piece.getX() == x && piece.getY() == y) {
return piece;
}
}
return null;
}
/* The following methods require more work ! */ /* The following methods require more work ! */
public boolean isHighlighted(int x, int y) { public boolean isHighlighted(int x, int y) {
@ -214,13 +344,42 @@ public class Board {
} }
public Board(Board board) { public Board(Board board) {
//TODO if (moveHistory.isEmpty()) return;
Move lastMove = moveHistory.remove(moveHistory.size() - 1);
Piece movedPiece = lastMove.getPieceMoved();
movedPiece.setPosition(lastMove.getFromX(), lastMove.getFromY());
if (lastMove.getPieceCaptured() != null) {
pieces.add(lastMove.getPieceCaptured());
}
turnWhite = !turnWhite;
turnNumber--;
} }
public void playMove(Move move) { public void playMove(Move move) {
//TODO Piece piece = move.getPieceMoved();
} // If a piece is captured, remove it from the board
if (move.getPieceCaptured() != null) {
pieces.remove(move.getPieceCaptured());
}
// Update the moved piece's position
piece.setPosition(move.getToX(), move.getToY());
// Save move in history for undo
moveHistory.add(move);
// Switch turn
turnWhite = !turnWhite;
turnNumber++;
// Clear selection/highlight
selected = null;
highlighted.clear();
}
} }

View File

@ -1,10 +1,10 @@
package backend; package backend;
public class Move { public class Move {
private Piece pieceMoved; private final Piece pieceMoved;
private int fromX, fromY; private final int fromX, fromY;
private int toX, toY; private final int toX, toY;
private Piece pieceCaptured; // can be null private final Piece pieceCaptured;
public Move(Piece pieceMoved, int fromX, int fromY, int toX, int toY, Piece pieceCaptured) { public Move(Piece pieceMoved, int fromX, int fromY, int toX, int toY, Piece pieceCaptured) {
this.pieceMoved = pieceMoved; this.pieceMoved = pieceMoved;
@ -21,5 +21,4 @@ public class Move {
public int getToX() { return toX; } public int getToX() { return toX; }
public int getToY() { return toY; } public int getToY() { return toY; }
public Piece getPieceCaptured() { return pieceCaptured; } public Piece getPieceCaptured() { return pieceCaptured; }
} }