All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fathzer.games.ai.toys.NaiveAI Maven / Gradle / Ivy

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