working but not finished to be continued

This commit is contained in:
Jérôme BEDIER 2025-05-06 17:18:49 +02:00
parent 64650ca279
commit 1dbd33488a
2 changed files with 198 additions and 156 deletions

View File

@ -9,11 +9,15 @@ public class Board {
private int turn;
private int selectedX = -1;
private int selectedY = -1;
private boolean[][] highlightedSquares;
public Board(int colNum, int lineNum) {
this.width = colNum; // col move x
this.height = lineNum; // line mov in y
this.board = new Piece[width][height]; // 8x8 chess board
this.highlightedSquares = new boolean[width][height];
}
public int getWidth() {
//width = 8; // setting the width at 8 for the moment can be changed
@ -120,16 +124,19 @@ public class Board {
// No piece selected yet
if (selectedX == -1 && selectedY == -1) {
if (clickedPiece != null) {
selectedX = x;
selectedY = y;
}
if (clickedPiece != null && clickedPiece.isWhite() == isTurnWhite()) {
selectedX = x;
selectedY = y;
calculateValidMoves(clickedPiece);
}
} else {
// Clicked the same piece again : unselect
if (x == selectedX && y == selectedY) {
selectedX = -1;
selectedY = -1;
} else {
clearHighlights();
} else if (highlightedSquares[x][y]) {
// Move the piece
Piece selectedPiece = board[selectedX][selectedY];
@ -144,6 +151,7 @@ public class Board {
selectedX = -1;
selectedY = -1; // Unselect
clearHighlights();
System.out.println(toString()); // Work but not sure ?
}
}
@ -165,13 +173,168 @@ public class Board {
}
private void calculateValidMoves(Piece piece) {
clearHighlights();
if (piece == null) return;
ArrayList<Move> legalMoves = getLegalMoves(piece); // Make sure getLegalMoves is implemented
for (Move move : legalMoves) {
highlightedSquares[move.getToX()][move.getToY()] = true;
}
}
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
//TODO
return false;
return highlightedSquares[x][y];
}
private void clearHighlights() {
highlightedSquares = new boolean[width][height];
}
private void highlightLegalMoves(Piece piece) {
// Clear previous highlights
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
highlightedSquares[i][j] = false;
if (piece == null) return;
ArrayList<Move> legalMoves = getLegalMoves(piece); // your existing method
for (int i = 0; i < legalMoves.size(); i++) {
Move move = legalMoves.get(i);
highlightedSquares[move.getToX()][move.getToY()] = true; // assuming Move has toX and toY fields
}
}
private ArrayList<Move> getLegalMoves(Piece piece) {
ArrayList<Move> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
switch(piece.getType()) {
case Pawn:
handlePawnMoves(moves, piece, x, y);
break;
case Knight:
handleKnightMoves(moves, piece, x, y);
break;
case Bishop:
handleSlidingMoves(moves, piece, x, y, new int[][]{{1,1}, {1,-1}, {-1,1}, {-1,-1}});
break;
case Rook:
handleSlidingMoves(moves, piece, x, y, new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}});
break;
case Queen:
handleSlidingMoves(moves, piece, x, y, new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}, {1,1}, {1,-1}, {-1,1}, {-1,-1}});
break;
case King:
handleKingMoves(moves, piece, x, y);
break;
}
return moves;
}
// Helper methods for movement calculations
private void handlePawnMoves(ArrayList<Move> moves, Piece pawn, int x, int y) {
int direction = pawn.isWhite() ? -1 : 1;
int startRow = pawn.isWhite() ? 6 : 1;
// Forward moves
if (isEmpty(x, y + direction)) {
moves.add(new Move(x, y, x, y + direction));
if (y == startRow && isEmpty(x, y + 2*direction)) {
moves.add(new Move(x, y, x, y + 2*direction));
}
}
// Captures
checkPawnCapture(moves, pawn, x, y, direction, -1);
checkPawnCapture(moves, pawn, x, y, direction, 1);
}
private void checkPawnCapture(ArrayList<Move> moves, Piece pawn, int x, int y, int dir, int dx) {
int nx = x + dx;
int ny = y + dir;
if (isEnemy(nx, ny, pawn.isWhite())) {
Move move = new Move(x, y, nx, ny);
move.setCapturedPiece(board[nx][ny]);
moves.add(move);
}
}
private void handleKnightMoves(ArrayList<Move> moves, Piece knight, int x, int y) {
int[][] offsets = {{2,1}, {2,-1}, {-2,1}, {-2,-1},
{1,2}, {1,-2}, {-1,2}, {-1,-2}};
for (int[] offset : offsets) {
int nx = x + offset[0];
int ny = y + offset[1];
if (isValidPosition(nx, ny) && !isAlly(nx, ny, knight.isWhite())) {
Move move = new Move(x, y, nx, ny);
if (isEnemy(nx, ny, knight.isWhite())) {
move.setCapturedPiece(board[nx][ny]);
}
moves.add(move);
}
}
}
private void handleSlidingMoves(ArrayList<Move> moves, Piece piece, int x, int y, int[][] directions) {
for (int[] dir : directions) {
int nx = x;
int ny = y;
while (true) {
nx += dir[0];
ny += dir[1];
if (!isValidPosition(nx, ny)) break;
if (isAlly(nx, ny, piece.isWhite())) break;
Move move = new Move(x, y, nx, ny);
if (isEnemy(nx, ny, piece.isWhite())) {
move.setCapturedPiece(board[nx][ny]);
moves.add(move);
break;
}
moves.add(move);
}
}
}
private void handleKingMoves(ArrayList<Move> moves, Piece king, int x, int y) {
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue;
int nx = x + dx;
int ny = y + dy;
if (isValidPosition(nx, ny) && !isAlly(nx, ny, king.isWhite())) {
Move move = new Move(x, y, nx, ny);
if (isEnemy(nx, ny, king.isWhite())) {
move.setCapturedPiece(board[nx][ny]);
}
moves.add(move);
}
}
}
}
// Utility methods
private boolean isValidPosition(int x, int y) {
return x >= 0 && x < width && y >= 0 && y < height;
}
private boolean isEmpty(int x, int y) {
return isValidPosition(x, y) && board[x][y] == null;
}
private boolean isEnemy(int x, int y, boolean isWhite) {
return isValidPosition(x, y) && board[x][y] != null && board[x][y].isWhite() != isWhite;
}
private boolean isAlly(int x, int y, boolean isWhite) {
return isValidPosition(x, y) && board[x][y] != null && board[x][y].isWhite() == isWhite;
}
public void undoLastMove() {
//TODO
@ -183,8 +346,12 @@ public class Board {
}
public void playMove(Move move) {
//TODO
Piece piece = board[move.getFromX()][move.getFromY()];
board[move.getToX()][move.getToY()] = piece;
board[move.getFromX()][move.getFromY()] = null;
piece.x = move.getToX();
piece.y = move.getToY();
turn++;
}
}

View File

@ -1,158 +1,33 @@
package backend;
import java.util.ArrayList;
import java.util.List;
public class Move {
private final int fromX;
private final int fromY;
private final int toX;
private final int toY;
private Piece capturedPiece;
// Returns a list of valid rook moves (horizontal and vertical)
public static List<int[]> getRookMoves(Board board, int x, int y) {
List<int[]> moves = new ArrayList<>();
Piece piece = board.getPiece(x, y);
if (piece == null) return moves;
int[][] directions = {
{1, 0}, {-1, 0}, // Right, Left
{0, 1}, {0, -1} // Up, Down
};
for (int[] dir : directions) {
int dx = dir[0], dy = dir[1];
int cx = x + dx, cy = y + dy;
while (board.isInBounds(cx, cy)) {
Piece target = board.getPiece(cx, cy);
if (target == null) {
moves.add(new int[]{cx, cy});
} else {
if (target.isWhite() != piece.isWhite()) {
moves.add(new int[]{cx, cy}); // Capture
}
break; // Stop at first piece
}
cx += dx;
cy += dy;
}
}
return moves;
public Move(int fromX, int fromY, int toX, int toY) {
this.fromX = fromX;
this.fromY = fromY;
this.toX = toX;
this.toY = toY;
}
// Returns a list of valid knight moves (L-shaped)
public static List<int[]> getKnightMoves(Board board, int x, int y) {
List<int[]> moves = new ArrayList<>();
Piece piece = board.getPiece(x, y);
if (piece == null) return moves;
// Getters
public int getFromX() { return fromX; }
public int getFromY() { return fromY; }
public int getToX() { return toX; }
public int getToY() { return toY; }
public Piece getCapturedPiece() { return capturedPiece; }
int[][] deltas = {
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
{1, -2}, {2, -1}, {-1, -2}, {-2, -1}
};
for (int[] d : deltas) {
int nx = x + d[0], ny = y + d[1];
if (board.isInBounds(nx, ny)) {
Piece target = board.getPiece(nx, ny);
if (target == null || target.isWhite() != piece.isWhite()) {
moves.add(new int[]{nx, ny});
}
}
}
return moves;
// Setter for captured piece
public void setCapturedPiece(Piece piece) {
this.capturedPiece = piece;
}
// Example: pawn movement (basic, no promotion/en passant)
public static List<int[]> getPawnMoves(Board board, int x, int y) {
List<int[]> moves = new ArrayList<>();
Piece pawn = board.getPiece(x, y);
if (pawn == null) return moves;
int direction = pawn.isWhite() ? -1 : 1;
int startRow = pawn.isWhite() ? 6 : 1;
// Forward 1
if (board.isInBounds(x, y + direction) && board.getPiece(x, y + direction) == null) {
moves.add(new int[]{x, y + direction});
// Forward 2 from starting row
if (y == startRow && board.getPiece(x, y + 2 * direction) == null) {
moves.add(new int[]{x, y + 2 * direction});
}
}
// Captures
for (int dx : new int[]{-1, 1}) {
int nx = x + dx;
int ny = y + direction;
if (board.isInBounds(nx, ny)) {
Piece target = board.getPiece(nx, ny);
if (target != null && target.isWhite() != pawn.isWhite()) {
moves.add(new int[]{nx, ny});
}
}
}
return moves;
}
// Stub for bishop (you can implement like the rook but with diagonal directions)
public static List<int[]> getBishopMoves(Board board, int x, int y) {
List<int[]> moves = new ArrayList<>();
Piece piece = board.getPiece(x, y);
if (piece == null) return moves;
int[][] directions = {
{1, 1}, {-1, 1}, {1, -1}, {-1, -1}
};
for (int[] dir : directions) {
int dx = dir[0], dy = dir[1];
int cx = x + dx, cy = y + dy;
while (board.isInBounds(cx, cy)) {
Piece target = board.getPiece(cx, cy);
if (target == null) {
moves.add(new int[]{cx, cy});
} else {
if (target.isWhite() != piece.isWhite()) {
moves.add(new int[]{cx, cy});
}
break;
}
cx += dx;
cy += dy;
}
}
return moves;
}
// Queen = rook + bishop moves
public static List<int[]> getQueenMoves(Board board, int x, int y) {
List<int[]> moves = new ArrayList<>();
moves.addAll(getRookMoves(board, x, y));
moves.addAll(getBishopMoves(board, x, y));
return moves;
}
// King = 1 step in any direction
public static List<int[]> getKingMoves(Board board, int x, int y) {
List<int[]> moves = new ArrayList<>();
Piece king = board.getPiece(x, y);
if (king == null) return moves;
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 (board.isInBounds(nx, ny)) {
Piece target = board.getPiece(nx, ny);
if (target == null || target.isWhite() != king.isWhite()) {
moves.add(new int[]{nx, ny});
}
}
}
}
return moves;
@Override
public String toString() {
return String.format("Move from (%d,%d) to (%d,%d)", fromX, fromY, toX, toY);
}
}