OOP_3A6_Project/src/backend/AutoPlayer.java

37 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package backend;
import java.util.List;
import java.util.Random;
public class AutoPlayer {
private final Random rnd = new Random();
private int valueOf(PieceType t) {
switch (t) {
case Pawn: return 1;
case Knight:
case Bishop: return 3;
case Rook: return 5;
case Queen: return 9;
default: return 0;
}
}
/** Greedycapture AI with random tiebreak */
public Move computeBestMove(Board board) {
List<Move> moves = ChessRules.generateLegalMoves(board, board.isTurnWhite());
if (moves.isEmpty()) return null;
int bestScore = Integer.MIN_VALUE;
Move best = null;
for (Move m : moves) {
int score = (m.getCapturedPiece()==null ? 0 : valueOf(m.getCapturedPiece().getType()));
if (score > bestScore || (score==bestScore && rnd.nextBoolean())) {
bestScore = score;
best = m;
}
}
return best;
}
}