class left out of previous commit.
This commit is contained in:
85
src/model/comPlayer/generator/MonteCarloMoveGenerator.java
Normal file
85
src/model/comPlayer/generator/MonteCarloMoveGenerator.java
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
package model.comPlayer.generator;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import model.Board;
|
||||||
|
import model.BoardScorer;
|
||||||
|
import model.Move;
|
||||||
|
import model.SearchResult;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
public class MonteCarloMoveGenerator implements MoveGenerator {
|
||||||
|
private static final Logger LOGGER = Logger
|
||||||
|
.getLogger(MonteCarloMoveGenerator.class.getName());
|
||||||
|
private static final int DEFAULT_RECURSIVE_PLAYS = 3;
|
||||||
|
private static final int MOVES_PER_LEVEL = 12;
|
||||||
|
|
||||||
|
private final BoardScorer scorer = new BoardScorer();
|
||||||
|
private final ValidMoveGenerator validMoveGenerator = new ValidMoveGenerator();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Move genMove(Board board, boolean asHuman) {
|
||||||
|
|
||||||
|
SearchResult bestResult = genMove(board, asHuman, DEFAULT_RECURSIVE_PLAYS * 2);
|
||||||
|
|
||||||
|
return bestResult.move;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SearchResult genMove(Board board, boolean asHuman, int recursionLevel) {
|
||||||
|
if (recursionLevel < 1) {
|
||||||
|
return new SearchResult(Move.NONE,scorer.getScore(board));
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Move> validMoves = validMoveGenerator.genMoves(board, asHuman,
|
||||||
|
MOVES_PER_LEVEL);
|
||||||
|
|
||||||
|
SearchResult bestResult;
|
||||||
|
|
||||||
|
if (asHuman) {
|
||||||
|
bestResult = new SearchResult(Move.NONE,Integer.MAX_VALUE);
|
||||||
|
} else {
|
||||||
|
bestResult = new SearchResult(Move.NONE,Integer.MIN_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (validMoves.size() == 0) {
|
||||||
|
return bestResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
int totalScore = 0;
|
||||||
|
for (Move nextMove : validMoves) {
|
||||||
|
Board nextBoard = new Board(board);
|
||||||
|
|
||||||
|
if (!nextBoard.playTile(nextMove.getCell(), nextMove.getColor())) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
"Illegal move attempted during search!");
|
||||||
|
}
|
||||||
|
|
||||||
|
SearchResult nextResult = new SearchResult(nextMove,genMove(nextBoard, !asHuman, recursionLevel - 1).score);
|
||||||
|
|
||||||
|
if (asHuman) {
|
||||||
|
if (nextResult.compareTo(bestResult) < 0) {
|
||||||
|
bestResult = nextResult;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (nextResult.compareTo(bestResult) > 0) {
|
||||||
|
bestResult = nextResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
totalScore += nextResult.score;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new SearchResult(bestResult.move, (int)Math.round((double)totalScore / validMoves.size()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AlphaBetaMoveGenerator2 does not support this method.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<Move> genMoves(Board board, boolean asHuman, int nMoves) {
|
||||||
|
Move[] doNothing = new Move[] { Move.NONE };
|
||||||
|
LOGGER.info("Minimax genMoves() stub returning []");
|
||||||
|
return Arrays.asList(doNothing);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user