From 35d84b13581614d62d93cfa6a2942c5a4ad558bd Mon Sep 17 00:00:00 2001 From: Woody Folsom Date: Mon, 15 Oct 2012 23:43:14 -0400 Subject: [PATCH] Added feedback when computer is thinking is StandAloneGame mode. --- src/net/woodyfolsom/msproj/Referee.java | 70 +++++++++++-------- .../woodyfolsom/msproj/StandAloneGame.java | 2 +- .../woodyfolsom/msproj/policy/MonteCarlo.java | 1 + 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/net/woodyfolsom/msproj/Referee.java b/src/net/woodyfolsom/msproj/Referee.java index 0ce5e86..90369f1 100644 --- a/src/net/woodyfolsom/msproj/Referee.java +++ b/src/net/woodyfolsom/msproj/Referee.java @@ -14,51 +14,62 @@ public class Referee { private GameState gameState = new GameState(gameConfig); private Policy blackPolicy; private Policy whitePolicy; - + public Policy getPolicy(Player player) { if (Player.BLACK.equals(player)) { return blackPolicy; } else if (Player.WHITE.equals(player)) { return whitePolicy; } 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) { if (Player.BLACK.equals(player)) { - blackPolicy = policy; + blackPolicy = policy; } else if (Player.WHITE.equals(player)) { - whitePolicy = policy; + whitePolicy = policy; } 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() { System.out.println("Game started."); - - Player currentPlayer = Player.BLACK; - - while (!gameState.isTerminal()) { - System.out.println(gameState); - - Action action = getPolicy(currentPlayer).getAction(gameConfig, gameState, currentPlayer); - gameState.playStone(currentPlayer, action); - currentPlayer = GoGame.getNextPlayer(currentPlayer); - } - - System.out.println("Game over. Result: " + gameState.getResult()); - - DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmssZ"); - + + // Player currentPlayer = Player.BLACK; + try { - - File sgfFile = new File("gogame-" + dateFormat.format(new Date())+ ".sgf"); + while (!gameState.isTerminal()) { + System.out.println(gameState); + + Player currentPlayer = gameState.getPlayerToMove(); + Action action = getPolicy(currentPlayer).getAction(gameConfig, + 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()); + + DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmssZ"); + + try { + + File sgfFile = new File("gogame-" + dateFormat.format(new Date()) + + ".sgf"); FileOutputStream fos = new FileOutputStream(sgfFile); try { SGFWriter.writeSGF(gameState, fos); - System.out.println("Game saved as " + sgfFile.getAbsolutePath()); + System.out + .println("Game saved as " + sgfFile.getAbsolutePath()); } finally { try { fos.close(); @@ -66,10 +77,11 @@ public class Referee { ioe.printStackTrace(); } } - } catch(IOException ioe) { - System.out.println("Unable to save game file due to IOException: " + ioe.getMessage()); + } catch (IOException ioe) { + System.out.println("Unable to save game file due to IOException: " + + ioe.getMessage()); } - + System.out.println("Game finished."); } } diff --git a/src/net/woodyfolsom/msproj/StandAloneGame.java b/src/net/woodyfolsom/msproj/StandAloneGame.java index 4ff939a..d94f531 100644 --- a/src/net/woodyfolsom/msproj/StandAloneGame.java +++ b/src/net/woodyfolsom/msproj/StandAloneGame.java @@ -12,7 +12,7 @@ public class StandAloneGame { */ public static void main(String[] args) { Policy player1 = new HumanKeyboardInput(); - Policy player2 = new MonteCarloUCT(new RandomMovePolicy(), 5000L); + Policy player2 = new MonteCarloUCT(new RandomMovePolicy(), 10000L); Referee referee = new Referee(); referee.setPolicy(Player.BLACK, player1); diff --git a/src/net/woodyfolsom/msproj/policy/MonteCarlo.java b/src/net/woodyfolsom/msproj/policy/MonteCarlo.java index 44b53d1..43d5bca 100644 --- a/src/net/woodyfolsom/msproj/policy/MonteCarlo.java +++ b/src/net/woodyfolsom/msproj/policy/MonteCarlo.java @@ -35,6 +35,7 @@ public abstract class MonteCarlo implements Policy { @Override public Action getAction(GameConfig gameConfig, GameState gameState, Player player) { + System.out.println(player + " is thinking for up to " + (searchTimeLimit / 1000.0) + " seconds..."); long startTime = System.currentTimeMillis(); if (gameState.getPlayerToMove() != player) {