autoplayer work but random for the moment
This commit is contained in:
parent
b5c58ee655
commit
ef14d4973d
|
|
@ -1,17 +1,32 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class AutoPlayer {
|
||||
|
||||
/**
|
||||
* Returns a random legal move for the active player.
|
||||
*/
|
||||
public Move computeBestMove(Board board) {
|
||||
ArrayList<Piece> pieces = board.getPieces();
|
||||
ArrayList<Move> allMoves = new ArrayList<>();
|
||||
boolean isWhite = board.isTurnWhite();
|
||||
|
||||
/**
|
||||
* returns the best Move to try on provided board for active player
|
||||
* @param board
|
||||
* @return
|
||||
*/
|
||||
public Move computeBestMove(Board board) {
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
|
@ -516,9 +516,37 @@ public class Board {
|
|||
}
|
||||
|
||||
|
||||
public Board(Board board) {
|
||||
//TODO
|
||||
public Board(Board original) {
|
||||
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
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue