42 lines
1.2 KiB
Java
42 lines
1.2 KiB
Java
package backend;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
public class AutoPlayer {
|
|
|
|
|
|
/**
|
|
* returns the best Move to try on provided board for active player
|
|
* @param board
|
|
* @return
|
|
*/
|
|
public Move computeBestMove(Board board) {
|
|
List<Move> allPossibleMoves = new ArrayList<>();
|
|
boolean isWhiteTurn = board.isTurnWhite();
|
|
|
|
for (Piece piece : board.getPieces()) {
|
|
if (piece.isWhite() == isWhiteTurn) {
|
|
board.userTouch(piece.getX(), piece.getY()); // triggers highlighting
|
|
|
|
for (int newX = 0; newX < board.getWidth(); newX++) {
|
|
for (int newY = 0; newY < board.getHeight(); newY++) {
|
|
if (board.isHighlighted(newX, newY)) {
|
|
allPossibleMoves.add(new Move(piece.getX(), piece.getY(), newX, newY, piece));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (allPossibleMoves.isEmpty()) return null;
|
|
|
|
Random rand = new Random();
|
|
return allPossibleMoves.get(rand.nextInt(allPossibleMoves.size()));
|
|
}
|
|
}
|
|
|
|
|
|
|