autoplayer work but random for the moment

This commit is contained in:
Jérôme BEDIER 2025-05-22 22:38:51 +02:00
parent b5c58ee655
commit ef14d4973d
2 changed files with 59 additions and 16 deletions

View File

@ -1,17 +1,32 @@
package backend; package backend;
import java.util.ArrayList;
import java.util.Random;
public class AutoPlayer { public class AutoPlayer {
/**
/** * Returns a random legal move for the active player.
* returns the best Move to try on provided board for active player */
* @param board public Move computeBestMove(Board board) {
* @return ArrayList<Piece> pieces = board.getPieces();
*/ ArrayList<Move> allMoves = new ArrayList<>();
public Move computeBestMove(Board board) { boolean isWhite = board.isTurnWhite();
return null; // Collect all legal moves for all of the active player's pieces
} for (Piece piece : pieces) {
if (piece.isWhite() == isWhite) {
ArrayList<Move> moves = board.getLegalMoves(piece);
} allMoves.addAll(moves);
}
}
if (allMoves.isEmpty()) {
return null; // No moves = checkmate or stalemate
}
// Pick a random move
Random rand = new Random();
return allMoves.get(rand.nextInt(allMoves.size()));
}
}

View File

@ -516,9 +516,37 @@ public class Board {
} }
public Board(Board board) { public Board(Board original) {
//TODO this.width = original.width;
this.height = original.height;
this.turn = original.turn;
this.selectedX = original.selectedX;
this.selectedY = original.selectedY;
this.enPassantCol = original.enPassantCol;
this.enPassantRow = original.enPassantRow;
// Deep copy the board array and pieces
this.board = new Piece[width][height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Piece p = original.board[x][y];
if (p != null) {
this.board[x][y] = new Piece(p.isWhite(), p.getType(), x, y);
} else {
this.board[x][y] = null;
}
}
}
// Deep copy highlightedSquares
this.highlightedSquares = new boolean[width][height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
this.highlightedSquares[x][y] = original.highlightedSquares[x][y];
}
}
// Undo stack is not copied (empty)
this.undoStack = new Stack<>();
}//test }//test