Refactoring in progress.
Player and Action classes are now singletons (factory pattern) rather than String values. Implementing more general treesearch code for minimax, alpha-beta, monte carlo using simplified backup logic.
This commit is contained in:
@@ -1,47 +1,44 @@
|
||||
package net.woodyfolsom.msproj.policy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
//import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import net.woodyfolsom.msproj.Action;
|
||||
import net.woodyfolsom.msproj.GameConfig;
|
||||
import net.woodyfolsom.msproj.GameScore;
|
||||
import net.woodyfolsom.msproj.GameState;
|
||||
import net.woodyfolsom.msproj.GoGame;
|
||||
import net.woodyfolsom.msproj.Player;
|
||||
import net.woodyfolsom.msproj.StateEvaluator;
|
||||
|
||||
//import org.apache.log4j.Logger;
|
||||
|
||||
|
||||
public class Minimax implements Policy {
|
||||
//private static final Logger LOGGER = Logger.getLogger(Minimax.class.getName());
|
||||
|
||||
private static final int DEFAULT_RECURSIVE_PLAYS = 1;
|
||||
|
||||
private final ValidMoveGenerator validMoveGenerator = new ValidMoveGenerator();
|
||||
|
||||
@Override
|
||||
public String getAction(GameConfig gameConfig, GameState gameState,
|
||||
String color) {
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Player color) {
|
||||
MoveCandidate moveCandidate = findBestMinimaxResult(
|
||||
DEFAULT_RECURSIVE_PLAYS * 2,
|
||||
gameConfig, gameState, color, false, Policy.PASS);
|
||||
gameConfig, gameState, color, false, Action.PASS);
|
||||
|
||||
return moveCandidate.move;
|
||||
}
|
||||
|
||||
private MoveCandidate findBestMinimaxResult(int recursionLevels,
|
||||
GameConfig gameConfig, GameState gameState,
|
||||
String initialColor, boolean playAsOpponent, String bestPrevMove) {
|
||||
Player initialColor, boolean playAsOpponent, Action bestPrevMove) {
|
||||
|
||||
StateEvaluator stateEvaluator = new StateEvaluator(gameConfig);
|
||||
List<MoveCandidate> randomMoveCandidates = new ArrayList<MoveCandidate>();
|
||||
|
||||
String colorPlaying = getColorToPlay(initialColor, playAsOpponent);
|
||||
Player colorPlaying = GoGame.getColorToPlay(initialColor, playAsOpponent);
|
||||
|
||||
List<String> validMoves = validMoveGenerator.getActions(gameConfig,
|
||||
List<Action> validMoves = validMoveGenerator.getActions(gameConfig,
|
||||
gameState, colorPlaying, ActionGenerator.ALL_ACTIONS);
|
||||
|
||||
for (String randomMove : validMoves) {
|
||||
for (Action randomMove : validMoves) {
|
||||
GameState stateCopy = new GameState(gameState);
|
||||
stateCopy.playStone(colorPlaying, randomMove);
|
||||
if (recursionLevels > 1) {
|
||||
@@ -77,19 +74,4 @@ public class Minimax implements Policy {
|
||||
return bestMove;
|
||||
}
|
||||
}
|
||||
|
||||
private String getColorToPlay(String color, boolean playAsOpponent) {
|
||||
if (playAsOpponent) {
|
||||
if ("w".equals(color)) {
|
||||
return "b";
|
||||
} else if ("b".equals(color)) {
|
||||
return "w";
|
||||
} else {
|
||||
return "?"; // invalid color will cause randomMoveGenerator to
|
||||
// PASS
|
||||
}
|
||||
} else {
|
||||
return color;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user