Update on AutoPlayer Class

This commit is contained in:
Lymeng LY 2025-05-14 13:49:50 +02:00
parent 5607ae2e57
commit 807d13ffe9
1 changed files with 22 additions and 2 deletions

View File

@ -1,6 +1,8 @@
package backend;
import java.util.ArrayList;
import java.util.Random;
public class AutoPlayer {
private Random rand = new Random();
/**
@ -9,7 +11,25 @@ public class AutoPlayer {
* @return
*/
public Move computeBestMove(Board board) {
ArrayList<Move> allMoves = new ArrayList<>();
//Go through all pieces on the board
for(Piece piece : board.getPieces()) {
//only choose the active color's piece
if(piece.isWhite() == board.isTurnWhite()) {
//Get valid moves for this piece
ArrayList<int[]> validMoves = board.getValidMoves(piece);
//convert each position into a move object
for (int[] dest : validMoves) {
Move move = new Move(piece, dest[0], dest[1]);
allMoves.add(move);
}
}
}
//Pick a random move from the list
if (!allMoves.isEmpty()) {
return allMoves.get(rand.nextInt(allMoves.size()));
}
return null;
}