Updated RandomMoveGenerator to support MonteCarloUCT.
Now possible to prohibit a Collection of Actions from being included in the List of returned actions (or as a single returned value). All unit tests now pass with the exception of MonteCarloUCT. TODO: playerToMove and previousPlayerPassed should be made part of the GameState. This would remove the superfluous Player parameter from many methods and make it possible to check for the "I'm ahead and my opponent is offering to end the game" killer move.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package net.woodyfolsom.msproj.policy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import net.woodyfolsom.msproj.Action;
|
||||
@@ -9,34 +10,20 @@ import net.woodyfolsom.msproj.GameState;
|
||||
import net.woodyfolsom.msproj.Player;
|
||||
|
||||
|
||||
public class RandomMovePolicy implements Policy {
|
||||
public class RandomMovePolicy implements Policy, ActionGenerator {
|
||||
|
||||
/**
|
||||
* Does NOT modify the gameState.
|
||||
*/
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Player color) {
|
||||
GameState gameStateCopy = new GameState(gameState);
|
||||
List<String> emptyCoordinates = gameStateCopy.getEmptyCoords();
|
||||
|
||||
while (emptyCoordinates.size() > 0) {
|
||||
Action randomMove = Action.getInstance(emptyCoordinates
|
||||
.get((int) (Math.random() * emptyCoordinates.size())));
|
||||
|
||||
if (gameStateCopy.playStone(color, randomMove)) {
|
||||
return randomMove;
|
||||
} else {
|
||||
emptyCoordinates.remove(randomMove);
|
||||
}
|
||||
}
|
||||
|
||||
return Action.PASS;
|
||||
Collection<Action> prohibitedMoves, Player player) {
|
||||
return getActions(gameConfig, gameState, prohibitedMoves, player, 1).get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to generate up to nMoves random moves on behalf of the specified
|
||||
* player. Will return at least one move, which may be 'pass' if random
|
||||
* search does not success in discovering a valid move. Does NOT modify the
|
||||
* player. Will return at least one move, which may be 'NONE' if random
|
||||
* search does not succeeed in discovering a valid move. Does NOT modify the
|
||||
* gameState.
|
||||
*
|
||||
* @param gameConfig
|
||||
@@ -45,25 +32,47 @@ public class RandomMovePolicy implements Policy {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<Action> genMoves(GameConfig gameConfig, GameState gameState,
|
||||
Player color, int nMoves) {
|
||||
public List<Action> getActions(GameConfig gameConfig, GameState gameState,
|
||||
Collection<Action> prohibitedMoves, Player player, int nMoves) {
|
||||
GameState gameStateCopy = new GameState(gameState);
|
||||
List<String> emptyCoordinates = gameStateCopy.getEmptyCoords();
|
||||
List<Action> randomMoves = new ArrayList<Action>();
|
||||
|
||||
while (emptyCoordinates.size() > 0 && randomMoves.size() < nMoves) {
|
||||
Action randomMove = Action.getInstance(emptyCoordinates
|
||||
.get((int) (Math.random() * emptyCoordinates.size())));
|
||||
if (gameStateCopy.playStone(color, randomMove)) {
|
||||
randomMoves.add(randomMove);
|
||||
}
|
||||
emptyCoordinates.remove(randomMove);
|
||||
ActionGenerator actionGenerator = new ValidMoveGenerator();
|
||||
|
||||
List<Action> possibleActions = actionGenerator.getActions(gameConfig, gameStateCopy, prohibitedMoves, player, ActionGenerator.ALL_ACTIONS);
|
||||
List<Action> randomActions = new ArrayList<Action>();
|
||||
|
||||
while (possibleActions.size() > 0 && randomActions.size() < nMoves) {
|
||||
Action randomAction = possibleActions
|
||||
.remove((int) (Math.random() * possibleActions.size()));
|
||||
|
||||
randomActions.add(randomAction);
|
||||
}
|
||||
|
||||
if (randomMoves.size() == 0) {
|
||||
randomMoves.add(Action.PASS);
|
||||
if (randomActions.size() == 0) {
|
||||
randomActions.add(Action.NONE);
|
||||
}
|
||||
|
||||
return randomMoves;
|
||||
|
||||
return randomActions;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* RandomMoveGenerator does not evaluate any states, but simply returns elements of
|
||||
* a set of uniformly distributed, distinct valid moves.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getNumStateEvaluations() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Action> getActions(GameConfig gameConfig, GameState gameState,
|
||||
Player color, int numActions) {
|
||||
return getActions(gameConfig, gameState, new ArrayList<Action>(), color, numActions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Player player) {
|
||||
return getActions(gameConfig,gameState,player,1).get(0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user