Added feedback when computer is thinking is StandAloneGame mode.

This commit is contained in:
2012-10-15 23:43:14 -04:00
parent db1bed43b4
commit 35d84b1358
3 changed files with 43 additions and 30 deletions

View File

@@ -21,44 +21,55 @@ public class Referee {
} else if (Player.WHITE.equals(player)) { } else if (Player.WHITE.equals(player)) {
return whitePolicy; return whitePolicy;
} else { } else {
throw new IllegalArgumentException("Player must be one of {Player.WHITE, Player.BLACK}."); throw new IllegalArgumentException(
"Player must be one of {Player.WHITE, Player.BLACK}.");
} }
} }
public void setPolicy(Player player, Policy policy) { public void setPolicy(Player player, Policy policy) {
if (Player.BLACK.equals(player)) { if (Player.BLACK.equals(player)) {
blackPolicy = policy; blackPolicy = policy;
} else if (Player.WHITE.equals(player)) { } else if (Player.WHITE.equals(player)) {
whitePolicy = policy; whitePolicy = policy;
} else { } else {
throw new IllegalArgumentException("Player must be one of {Player.WHITE, Player.BLACK}."); throw new IllegalArgumentException(
"Player must be one of {Player.WHITE, Player.BLACK}.");
} }
} }
public void play() { public void play() {
System.out.println("Game started."); System.out.println("Game started.");
Player currentPlayer = Player.BLACK; // Player currentPlayer = Player.BLACK;
while (!gameState.isTerminal()) { try {
System.out.println(gameState); while (!gameState.isTerminal()) {
System.out.println(gameState);
Action action = getPolicy(currentPlayer).getAction(gameConfig, gameState, currentPlayer); Player currentPlayer = gameState.getPlayerToMove();
gameState.playStone(currentPlayer, action); Action action = getPolicy(currentPlayer).getAction(gameConfig,
currentPlayer = GoGame.getNextPlayer(currentPlayer); gameState, currentPlayer);
gameState.playStone(currentPlayer, action);
}
} catch (Exception ex) {
System.out
.println("Game halted early due to the following Exception:");
ex.printStackTrace();
return;
} }
System.out.println("Game over. Result: " + gameState.getResult()); System.out.println("Game over. Result: " + gameState.getResult());
DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmssZ"); DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmssZ");
try { try {
File sgfFile = new File("gogame-" + dateFormat.format(new Date())+ ".sgf"); File sgfFile = new File("gogame-" + dateFormat.format(new Date())
+ ".sgf");
FileOutputStream fos = new FileOutputStream(sgfFile); FileOutputStream fos = new FileOutputStream(sgfFile);
try { try {
SGFWriter.writeSGF(gameState, fos); SGFWriter.writeSGF(gameState, fos);
System.out.println("Game saved as " + sgfFile.getAbsolutePath()); System.out
.println("Game saved as " + sgfFile.getAbsolutePath());
} finally { } finally {
try { try {
fos.close(); fos.close();
@@ -66,8 +77,9 @@ public class Referee {
ioe.printStackTrace(); ioe.printStackTrace();
} }
} }
} catch(IOException ioe) { } catch (IOException ioe) {
System.out.println("Unable to save game file due to IOException: " + ioe.getMessage()); System.out.println("Unable to save game file due to IOException: "
+ ioe.getMessage());
} }
System.out.println("Game finished."); System.out.println("Game finished.");

View File

@@ -12,7 +12,7 @@ public class StandAloneGame {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Policy player1 = new HumanKeyboardInput(); Policy player1 = new HumanKeyboardInput();
Policy player2 = new MonteCarloUCT(new RandomMovePolicy(), 5000L); Policy player2 = new MonteCarloUCT(new RandomMovePolicy(), 10000L);
Referee referee = new Referee(); Referee referee = new Referee();
referee.setPolicy(Player.BLACK, player1); referee.setPolicy(Player.BLACK, player1);

View File

@@ -35,6 +35,7 @@ public abstract class MonteCarlo implements Policy {
@Override @Override
public Action getAction(GameConfig gameConfig, GameState gameState, public Action getAction(GameConfig gameConfig, GameState gameState,
Player player) { Player player) {
System.out.println(player + " is thinking for up to " + (searchTimeLimit / 1000.0) + " seconds...");
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
if (gameState.getPlayerToMove() != player) { if (gameState.getPlayerToMove() != player) {