
com.fathzer.games.ai.toys.NaiveAI Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of games-core Show documentation
Show all versions of games-core Show documentation
A core library to help implement two players games.
The newest version!
package com.fathzer.games.ai.toys;
import java.util.List;
import com.fathzer.games.MoveGenerator;
import com.fathzer.games.MoveGenerator.MoveConfidence;
import com.fathzer.games.Status;
import com.fathzer.games.ai.AI;
import com.fathzer.games.ai.evaluation.Evaluation;
import com.fathzer.games.ai.evaluation.Evaluator;
/**
* A very basic {@link AI} that that gives to a move the best score according to a given evaluator after the opponent played.
*
Of course in complex games like chess, the move chosen by this engine may be far for the best one due to
* horizon effect.
* @param The type of moves
* @param The type of board
*/
public class NaiveAI> extends BasicAI {
private final Evaluator evaluator;
/** Constructor
* @param board The board
* @param evaluator The evaluator to use
*/
public NaiveAI (B board, Evaluator evaluator) {
super(board);
this.evaluator = evaluator;
}
@Override
protected Evaluation getEvaluation(M move) {
return evaluator.toEvaluation(evaluate(move));
}
int evaluate(M move) {
// Play the evaluated move
evaluator.prepareMove(board, move);
board.makeMove(move, MoveConfidence.LEGAL);
try {
evaluator.commitMove();
// Gets the opponent responses
final List moves = board.getLegalMoves();
if (moves.isEmpty()) {
// End of game
final Status status = board.getEndGameStatus();
return status == Status.DRAW ? 0 : evaluator.getWinScore(1);
}
int min = 0;
for (M m : moves) {
// For all opponent responses
final int value = evaluateOpponentMove(board, m);
if (value
© 2015 - 2025 Weber Informatics LLC | Privacy Policy