
com.fathzer.games.ai.SearchContext 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;
import java.util.function.Supplier;
import com.fathzer.games.MoveGenerator;
import com.fathzer.games.ai.evaluation.Evaluator;
import com.fathzer.games.util.exec.Forkable;
/** The context of a best move search.
*
It encapsulates the game position and an position evaluator.
* @param The type of moves
* @param The type of the evaluator
*/
public class SearchContext> implements Forkable> {
private final B gamePosition;
private final Evaluator evaluator;
private SearchStatistics statistics;
private SearchContext(B gamePosition, Evaluator evaluator, SearchStatistics statistics) {
this.gamePosition = gamePosition;
this.evaluator = evaluator;
this.statistics = statistics;
}
/** Gets the game position.
* @return a {@link MoveGenerator} instance
*/
public B getGamePosition() {
return gamePosition;
}
/** Gets the evaluator.
* @return an evaluator instance
*/
public Evaluator getEvaluator() {
return evaluator;
}
/** Gets the search statistics.
* @return a {@link SearchStatistics} instance
*/
public SearchStatistics getStatistics() {
return statistics;
}
/** Makes a move.
* @param move The move to make
* @param confidence The confidence of the move
* @return true if the move was made, false otherwise (if it was illegal)
*/
public boolean makeMove(M move, MoveGenerator.MoveConfidence confidence) {
evaluator.prepareMove(gamePosition, move);
if (gamePosition.makeMove(move, confidence)) {
evaluator.commitMove();
return true;
}
return false;
}
/** Unmakes the last move. */
public void unmakeMove() {
evaluator.unmakeMove();
gamePosition.unmakeMove();
}
@SuppressWarnings("unchecked")
@Override
public SearchContext fork() {
final B mg = (B)gamePosition.fork();
final Evaluator ev = evaluator.fork();
return new SearchContext<>(mg, ev, statistics);
}
/** Gets a new search context.
* @param board The game position
* @param evaluatorBuilder A supplier that can create an evaluator
* @param The type of moves
* @param The type of the move generator
* @return a new search context
*/
public static > SearchContext get(B board, Supplier> evaluatorBuilder) {
@SuppressWarnings("unchecked")
final B b = (B) board.fork();
final Evaluator evaluator = evaluatorBuilder.get();
evaluator.init(board);
return new SearchContext<>(b, evaluator, new SearchStatistics());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy