Added feedback when computer is thinking is StandAloneGame mode.
This commit is contained in:
@@ -14,51 +14,62 @@ public class Referee {
|
|||||||
private GameState gameState = new GameState(gameConfig);
|
private GameState gameState = new GameState(gameConfig);
|
||||||
private Policy blackPolicy;
|
private Policy blackPolicy;
|
||||||
private Policy whitePolicy;
|
private Policy whitePolicy;
|
||||||
|
|
||||||
public Policy getPolicy(Player player) {
|
public Policy getPolicy(Player player) {
|
||||||
if (Player.BLACK.equals(player)) {
|
if (Player.BLACK.equals(player)) {
|
||||||
return blackPolicy;
|
return blackPolicy;
|
||||||
} 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()) {
|
|
||||||
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");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
while (!gameState.isTerminal()) {
|
||||||
File sgfFile = new File("gogame-" + dateFormat.format(new Date())+ ".sgf");
|
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);
|
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,10 +77,11 @@ 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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user