Fixed unit tests. Updated classpath.

This commit is contained in:
2012-11-16 04:21:19 -05:00
parent ca37280ed8
commit d9d6ecda80
13 changed files with 346 additions and 193 deletions

View File

@@ -8,5 +8,6 @@
<classpathentry kind="lib" path="lib/kgsGtp.jar"/> <classpathentry kind="lib" path="lib/kgsGtp.jar"/>
<classpathentry kind="lib" path="lib/antlrworks-1.4.3.jar"/> <classpathentry kind="lib" path="lib/antlrworks-1.4.3.jar"/>
<classpathentry kind="lib" path="lib/neuroph-2.6.jar"/> <classpathentry kind="lib" path="lib/neuroph-2.6.jar"/>
<classpathentry kind="lib" path="lib/encog-engine-2.5.0.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@@ -4,14 +4,16 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GameState { public class GameState {
private int blackPrisoners = 0; private int blackPrisoners;
private int whitePrisoners = 0; private int whitePrisoners;
private GameBoard gameBoard; private GameBoard gameBoard;
private GameConfig gameConfig; private GameConfig gameConfig;
private Player playerToMove; private Player playerToMove;
private List<Action> moveHistory = new ArrayList<Action>(); private List<Action> moveHistory;
public GameState(GameConfig gameConfig) { public GameState(GameConfig gameConfig) {
this.blackPrisoners = 0;
this.whitePrisoners = 0;
this.gameConfig = gameConfig; this.gameConfig = gameConfig;
int size = gameConfig.getSize(); int size = gameConfig.getSize();
if (size < 1 || size > 19) { if (size < 1 || size > 19) {
@@ -19,6 +21,7 @@ public class GameState {
} }
gameBoard = new GameBoard(size); gameBoard = new GameBoard(size);
playerToMove = Player.BLACK; playerToMove = Player.BLACK;
moveHistory = new ArrayList<Action>();
} }
public GameState(GameState that) { public GameState(GameState that) {
@@ -116,6 +119,40 @@ public class GameState {
return whitePrisoners; return whitePrisoners;
} }
/**
* Used for setting up the board. Places the player's stone at the specified coordinates.
*
* Returns false if the requested intersection is occupied or the resulting position is illegal.
* Returns false if the requested action is PASS, RESIGN or NONE.
* Returns false if the moveHistory's size is already >0 (method should only be used to set up board).
*
* Does NOT advance the playerToMove or add the action to the move history.
*
* @param player
* @param action
* @return
*/
public boolean placeStone(Player player, Action action) {
if (action.isPass() || action.isResign() || action.isNone()) {
return false;
}
if (moveHistory.size() > 0) {
return false;
}
Player actualPTM = playerToMove;
playerToMove = player;
boolean validMove = playStone(player,action);
moveHistory.clear();
playerToMove = actualPTM;
return validMove;
}
public boolean playStone(Player player, String move) { public boolean playStone(Player player, String move) {
return playStone(player, Action.getInstance(move)); return playStone(player, Action.getInstance(move));
} }
@@ -315,6 +352,8 @@ public class GameState {
sb.append(" BLACK(X) has captured "); sb.append(" BLACK(X) has captured ");
sb.append(getBlackPrisoners()); sb.append(getBlackPrisoners());
sb.append(" stones"); sb.append(" stones");
} else if (rIndex == boardSize / 2 - 2) {
sb.append(" " + playerToMove + " to move");
} }
sb.append(System.lineSeparator()); sb.append(System.lineSeparator());
} }

View File

@@ -1,8 +1,6 @@
package net.woodyfolsom.msproj; package net.woodyfolsom.msproj;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
@@ -115,16 +113,18 @@ public class StandAloneGame {
try { try {
File txtFile = new File("gotournament-" + dateFormat.format(new Date()) File txtFile = new File("gotournament-"
+ ".txt"); + dateFormat.format(new Date()) + ".txt");
FileWriter writer = new FileWriter(txtFile); FileWriter writer = new FileWriter(txtFile);
try { try {
logResults(writer, round1results, playerType1.toString(), playerType2.toString()); logResults(writer, round1results, playerType1.toString(),
logResults(writer, round2results, playerType2.toString(), playerType1.toString()); playerType2.toString());
logResults(writer, round2results, playerType2.toString(),
playerType1.toString());
System.out System.out.println("Game tournament saved as "
.println("Game tournament saved as " + txtFile.getAbsolutePath()); + txtFile.getAbsolutePath());
} finally { } finally {
try { try {
writer.close(); writer.close();
@@ -139,17 +139,17 @@ public class StandAloneGame {
} }
private void logResults(FileWriter writer, List<GameResult> results, String player1, String player2) throws IOException { private void logResults(FileWriter writer, List<GameResult> results,
String player1, String player2) throws IOException {
String header = "Cumulative results for " + results.size() String header = "Cumulative results for " + results.size()
+ " games (BLACK=" + player1 + ", WHITE=" + player2 + " games (BLACK=" + player1 + ", WHITE=" + player2 + ")";
+ ")";
System.out.println(header); System.out.println(header);
writer.write(header); writer.write(header);
writer.write("\n"); writer.write("\n");
for (int i = 0; i < results.size(); i++) { for (int i = 0; i < results.size(); i++) {
String resultLine = (i+1) + ". " + results.get(i); String resultLine = (i + 1) + ". " + results.get(i);
System.out.println(resultLine); System.out.println(resultLine);
writer.write(resultLine); writer.write(resultLine);
writer.write("\n"); writer.write("\n");

View File

@@ -8,15 +8,8 @@ import java.io.IOException;
import net.woodyfolsom.msproj.GameRecord; import net.woodyfolsom.msproj.GameRecord;
import net.woodyfolsom.msproj.Referee; import net.woodyfolsom.msproj.Referee;
import net.woodyfolsom.msproj.sgf.SGFLexer;
import net.woodyfolsom.msproj.sgf.SGFNodeCollection;
import net.woodyfolsom.msproj.sgf.SGFParser;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException; import org.antlr.runtime.RecognitionException;
import org.junit.Test;
public class PassLearner { public class PassLearner {
private File[] getDataFiles(String dirName) { private File[] getDataFiles(String dirName) {
@@ -47,7 +40,7 @@ public class PassLearner {
try { try {
sgfInputStream = new FileInputStream(sgfFile); sgfInputStream = new FileInputStream(sgfFile);
GameRecord gameRecord = Referee.replay(sgfInputStream); GameRecord gameRecord = Referee.replay(sgfInputStream);
//... // ...
} catch (FileNotFoundException fnfe) { } catch (FileNotFoundException fnfe) {
fnfe.printStackTrace(); fnfe.printStackTrace();
} catch (RecognitionException re) { } catch (RecognitionException re) {

View File

@@ -11,9 +11,12 @@ public class CaptureTest {
public void testCapture() { public void testCapture() {
GameConfig gameConfig = new GameConfig(5); GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B3")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A2")));
gameState.playStone(Player.BLACK, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B3")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B1")));
assertTrue(gameState.playStone(Player.BLACK, Action.PASS));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("B2"))); assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("B2")));
assertEquals(0,gameState.getBlackPrisoners()); assertEquals(0,gameState.getBlackPrisoners());
@@ -32,14 +35,14 @@ public class CaptureTest {
GameConfig gameConfig = new GameConfig(5); GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("A2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A2")));
gameState.playStone(Player.BLACK, Action.getInstance("B3")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B3")));
gameState.playStone(Player.BLACK, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B1")));
gameState.playStone(Player.BLACK, Action.getInstance("C4")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C4")));
gameState.playStone(Player.BLACK, Action.getInstance("D3")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("D3")));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("B2"))); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B2")));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("C3"))); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C3")));
assertEquals(0,gameState.getBlackPrisoners()); assertEquals(0,gameState.getBlackPrisoners());
assertEquals(0,gameState.getWhitePrisoners()); assertEquals(0,gameState.getWhitePrisoners());
@@ -62,14 +65,16 @@ public class CaptureTest {
GameConfig gameConfig = new GameConfig(5); GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("A1")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A1")));
gameState.playStone(Player.BLACK, Action.getInstance("B2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B2")));
gameState.playStone(Player.BLACK, Action.getInstance("C1")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C1")));
gameState.playStone(Player.WHITE, Action.getInstance("A2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A2")));
gameState.playStone(Player.WHITE, Action.getInstance("B3")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B3")));
gameState.playStone(Player.WHITE, Action.getInstance("C2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C2")));
//This capture should be allowed. //This capture should be allowed.
assertTrue(gameState.playStone(Player.BLACK, Action.PASS));
System.out.println("State before WHITE move: "); System.out.println("State before WHITE move: ");
System.out.println(gameState); System.out.println(gameState);

View File

@@ -17,7 +17,7 @@ public class IllegalMoveTest {
public void testIllegalMoveOnOwnStone() { public void testIllegalMoveOnOwnStone() {
GameConfig gameConfig = new GameConfig(5); GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("B3")); gameState.placeStone(Player.BLACK, Action.getInstance("B3"));
assertFalse(gameState.playStone(Player.BLACK, Action.getInstance("B3"))); assertFalse(gameState.playStone(Player.BLACK, Action.getInstance("B3")));
} }
@@ -33,12 +33,16 @@ public class IllegalMoveTest {
public void testIllegalMoveNoLiberties() { public void testIllegalMoveNoLiberties() {
GameConfig gameConfig = new GameConfig(5); GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("A2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A2")));
gameState.playStone(Player.BLACK, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B1")));
gameState.playStone(Player.BLACK, Action.getInstance("B3")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B3")));
gameState.playStone(Player.BLACK, Action.getInstance("C2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C2")));
System.out.println(gameState); System.out.println(gameState);
assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("B2"))); assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("B2")));
System.out.println(gameState); System.out.println(gameState);
} }
@@ -46,12 +50,16 @@ public class IllegalMoveTest {
public void testIllegalMoveFormsTrappedGroup() { public void testIllegalMoveFormsTrappedGroup() {
GameConfig gameConfig = new GameConfig(9); GameConfig gameConfig = new GameConfig(9);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("A5"));
gameState.playStone(Player.BLACK, Action.getInstance("B6")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A5")));
gameState.playStone(Player.BLACK, Action.getInstance("B7")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B6")));
gameState.playStone(Player.BLACK, Action.getInstance("A8")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B7")));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("A6"))); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A8")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A6")));
assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("A7"))); assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("A7")));
System.out.println(gameState); System.out.println(gameState);
} }
@@ -59,16 +67,20 @@ public class IllegalMoveTest {
public void testIllegalMoveFormsTrappedGroup2() { public void testIllegalMoveFormsTrappedGroup2() {
GameConfig gameConfig = new GameConfig(9); GameConfig gameConfig = new GameConfig(9);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("G1"));
gameState.playStone(Player.BLACK, Action.getInstance("H2"));
gameState.playStone(Player.BLACK, Action.getInstance("H3"));
gameState.playStone(Player.BLACK, Action.getInstance("J4"));
gameState.playStone(Player.BLACK, Action.getInstance("A8")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("G1")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("H2")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("H3")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("J4")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A8")));
assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("H1"))); assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("H1")));
assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("J2"))); assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("J2")));
assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("J3"))); assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("J3")));
assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
System.out.println("State before move: "); System.out.println("State before move: ");
System.out.println(gameState); System.out.println(gameState);
@@ -80,15 +92,16 @@ public class IllegalMoveTest {
GameConfig gameConfig = new GameConfig(3); GameConfig gameConfig = new GameConfig(3);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.WHITE, Action.getInstance("A1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A1")));
gameState.playStone(Player.WHITE, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
gameState.playStone(Player.WHITE, Action.getInstance("B2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B2")));
gameState.playStone(Player.WHITE, Action.getInstance("A3")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A3")));
gameState.playStone(Player.WHITE, Action.getInstance("B3")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B3")));
System.out.println("State before move: "); System.out.println("State before move: ");
System.out.println(gameState); System.out.println(gameState);
assertFalse("Play by BLACK at A2 should have failed.",gameState.playStone(Player.BLACK, Action.getInstance("A2"))); assertFalse("Play by BLACK at A2 should have failed.",gameState.playStone(Player.BLACK, Action.getInstance("A2")));
List<Action> validMoves = new ValidMoveGenerator().getActions(gameConfig, gameState, Player.BLACK, ActionGenerator.ALL_ACTIONS); List<Action> validMoves = new ValidMoveGenerator().getActions(gameConfig, gameState, Player.BLACK, ActionGenerator.ALL_ACTIONS);
assertEquals(4, validMoves.size()); assertEquals(4, validMoves.size());
assertTrue(validMoves.contains(Action.PASS)); assertTrue(validMoves.contains(Action.PASS));

View File

@@ -29,47 +29,50 @@ public class LegalMoveTest {
//Illegal move detected by gokgs.com server //Illegal move detected by gokgs.com server
GameConfig gameConfig = new GameConfig(9); GameConfig gameConfig = new GameConfig(9);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("G5"));
gameState.playStone(Player.BLACK, Action.getInstance("G7"));
gameState.playStone(Player.BLACK, Action.getInstance("F6"));
gameState.playStone(Player.BLACK, Action.getInstance("H6"));
gameState.playStone(Player.BLACK, Action.getInstance("C7"));
gameState.playStone(Player.BLACK, Action.getInstance("D7"));
gameState.playStone(Player.BLACK, Action.getInstance("E7"));
gameState.playStone(Player.BLACK, Action.getInstance("F7"));
gameState.playStone(Player.BLACK, Action.getInstance("G8"));
gameState.playStone(Player.BLACK, Action.getInstance("H9"));
gameState.playStone(Player.BLACK, Action.getInstance("J7"));
gameState.playStone(Player.BLACK, Action.getInstance("E5"));
gameState.playStone(Player.BLACK, Action.getInstance("F4"));
gameState.playStone(Player.BLACK, Action.getInstance("G3"));
gameState.playStone(Player.BLACK, Action.getInstance("D4"));
gameState.playStone(Player.BLACK, Action.getInstance("E3"));
gameState.playStone(Player.BLACK, Action.getInstance("B4"));
gameState.playStone(Player.BLACK, Action.getInstance("C3"));
gameState.playStone(Player.BLACK, Action.getInstance("D2"));
gameState.playStone(Player.BLACK, Action.getInstance("E1"));
gameState.playStone(Player.WHITE, Action.getInstance("H8")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("G5")));
gameState.playStone(Player.WHITE, Action.getInstance("H7")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("G7")));
gameState.playStone(Player.WHITE, Action.getInstance("D9")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("F6")));
gameState.playStone(Player.WHITE, Action.getInstance("D8")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("H6")));
gameState.playStone(Player.WHITE, Action.getInstance("E8")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C7")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("D7")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("E7")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("F7")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("G8")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("H9")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("J7")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("E5")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("F4")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("G3")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("D4")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("E3")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B4")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C3")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("D2")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("E1")));
gameState.playStone(Player.WHITE, Action.getInstance("A7")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("H8")));
gameState.playStone(Player.WHITE, Action.getInstance("A6")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("H7")));
gameState.playStone(Player.WHITE, Action.getInstance("B8")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("D9")));
gameState.playStone(Player.WHITE, Action.getInstance("B7")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("D8")));
gameState.playStone(Player.WHITE, Action.getInstance("B6")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("E8")));
gameState.playStone(Player.WHITE, Action.getInstance("C5"));
gameState.playStone(Player.WHITE, Action.getInstance("D5"));
gameState.playStone(Player.WHITE, Action.getInstance("D6"));
gameState.playStone(Player.WHITE, Action.getInstance("A3")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A7")));
gameState.playStone(Player.WHITE, Action.getInstance("B3")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A6")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B8")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B7")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B6")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C5")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("D5")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("D6")));
gameState.playStone(Player.WHITE, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A3")));
gameState.playStone(Player.WHITE, Action.getInstance("F1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B3")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("F1")));
assertTrue(gameState.playStone(Player.BLACK, Action.PASS));
System.out.println("State before move: "); System.out.println("State before move: ");
System.out.println(gameState); System.out.println(gameState);

View File

@@ -1,5 +1,7 @@
package net.woodyfolsom.msproj; package net.woodyfolsom.msproj;
import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
public class TerritoryFinderTest { public class TerritoryFinderTest {
@@ -8,11 +10,11 @@ public class TerritoryFinderTest {
GameConfig gameConfig = new GameConfig(5); GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("A2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A2")));
gameState.playStone(Player.BLACK, Action.getInstance("B3")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B3")));
gameState.playStone(Player.BLACK, Action.getInstance("C2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C2")));
gameState.playStone(Player.BLACK, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B1")));
gameState.playStone(Player.WHITE, Action.getInstance("E5")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("E5")));
TerritoryMarker.markTerritory(gameState.getGameBoard()); TerritoryMarker.markTerritory(gameState.getGameBoard());
System.out.println(gameState); System.out.println(gameState);

View File

@@ -1,6 +1,7 @@
package net.woodyfolsom.msproj.policy; package net.woodyfolsom.msproj.policy;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import net.woodyfolsom.msproj.Action; import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameConfig; import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameState; import net.woodyfolsom.msproj.GameState;
@@ -14,10 +15,11 @@ public class MinimaxTest {
Policy treeSearch = new Minimax(); Policy treeSearch = new Minimax();
GameConfig gameConfig = new GameConfig(6); GameConfig gameConfig = new GameConfig(6);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.WHITE, Action.getInstance("A2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A2")));
gameState.playStone(Player.WHITE, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
gameState.playStone(Player.WHITE, Action.getInstance("C2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C2")));
gameState.playStone(Player.BLACK, Action.getInstance("B2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B2")));
assertTrue(gameState.playStone(Player.BLACK, Action.PASS));
Action move = treeSearch.getAction(gameConfig, gameState, Action move = treeSearch.getAction(gameConfig, gameState,
Player.WHITE); Player.WHITE);
@@ -28,7 +30,7 @@ public class MinimaxTest {
System.out.println("NumStateEvaluations: " + treeSearch.getNumStateEvaluations()); System.out.println("NumStateEvaluations: " + treeSearch.getNumStateEvaluations());
assertEquals(Action.getInstance("B3"), move); assertEquals(Action.getInstance("B3"), move);
gameState.playStone(Player.WHITE, move); assertTrue(gameState.playStone(Player.WHITE, move));
System.out.println(gameState); System.out.println(gameState);
} }
@@ -38,10 +40,10 @@ public class MinimaxTest {
Policy treeSearch = new Minimax(); Policy treeSearch = new Minimax();
GameConfig gameConfig = new GameConfig(6); GameConfig gameConfig = new GameConfig(6);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("A2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A2")));
gameState.playStone(Player.BLACK, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B1")));
gameState.playStone(Player.BLACK, Action.getInstance("C2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C2")));
gameState.playStone(Player.WHITE, Action.getInstance("B2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B2")));
Action move = treeSearch.getAction(gameConfig, gameState, Action move = treeSearch.getAction(gameConfig, gameState,
Player.BLACK); Player.BLACK);
@@ -52,7 +54,7 @@ public class MinimaxTest {
System.out.println("NumStateEvaluations: " + treeSearch.getNumStateEvaluations()); System.out.println("NumStateEvaluations: " + treeSearch.getNumStateEvaluations());
assertEquals("Expected B3 but was: " + move, Action.getInstance("B3"), move); assertEquals("Expected B3 but was: " + move, Action.getInstance("B3"), move);
gameState.playStone(Player.BLACK, move); assertTrue(gameState.playStone(Player.BLACK, move));
System.out.println(gameState); System.out.println(gameState);
} }

View File

@@ -1,78 +1,80 @@
package net.woodyfolsom.msproj.policy; package net.woodyfolsom.msproj.policy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import net.woodyfolsom.msproj.Action; import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameConfig; import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameState; import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.Player; import net.woodyfolsom.msproj.Player;
import net.woodyfolsom.msproj.policy.Policy;
import org.junit.Test; import org.junit.Test;
public class MonteCarloUCTTest { public class MonteCarloUCTTest {
@Test @Test
public void testGenmoveAsW() { public void testGenmoveAsW() {
Policy treeSearch = new MonteCarloUCT(new RandomMovePolicy(),10000L); Policy treeSearch = new MonteCarloUCT(new RandomMovePolicy(), 2000L);
GameConfig gameConfig = new GameConfig(6); GameConfig gameConfig = new GameConfig(6);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("PASS"));
gameState.playStone(Player.WHITE, Action.getInstance("A2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A2")));
gameState.playStone(Player.BLACK, Action.getInstance("PASS")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
gameState.playStone(Player.WHITE, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C2")));
gameState.playStone(Player.BLACK, Action.getInstance("PASS"));
gameState.playStone(Player.WHITE, Action.getInstance("C2")); // if this were PLACE stone instad of PLAY stone, white should pass as
gameState.playStone(Player.BLACK, Action.getInstance("B2")); // well, and win.
assertTrue(gameState.playStone(Player.BLACK, Action.getInstance("B2")));
Action move = treeSearch.getAction(gameConfig, gameState, Player.WHITE); Action move = treeSearch.getAction(gameConfig, gameState, Player.WHITE);
System.out.println(gameState); System.out.println(gameState);
System.out.println("Generated move: " + move); System.out.println("Generated move: " + move);
System.out.println("NumStateEvaluations: " + treeSearch.getNumStateEvaluations()); System.out.println("NumStateEvaluations: "
+ treeSearch.getNumStateEvaluations());
assertEquals("Expected B3 but was: " + move, "B3", move); assertTrue(gameState.playStone(Player.WHITE, move));
gameState.playStone(Player.WHITE, move);
System.out.println(gameState); System.out.println(gameState);
} }
@Test @Test
public void testGenmoveAsB() { public void testGenmoveAsB() {
Policy treeSearch = new MonteCarloUCT(new RandomMovePolicy(),10000L); Policy treeSearch = new MonteCarloUCT(new RandomMovePolicy(), 2000L);
GameConfig gameConfig = new GameConfig(5); GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A2")));
gameState.playStone(Player.BLACK, Action.getInstance("C2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B1")));
gameState.playStone(Player.WHITE, Action.getInstance("B2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C2")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B2")));
Action move = treeSearch.getAction(gameConfig, gameState, Player.BLACK); Action move = treeSearch.getAction(gameConfig, gameState, Player.BLACK);
System.out.println(gameState); System.out.println(gameState);
System.out.println("Generated move: " + move); System.out.println("Generated move: " + move);
System.out.println("NumStateEvaluations: " + treeSearch.getNumStateEvaluations()); System.out.println("NumStateEvaluations: "
+ treeSearch.getNumStateEvaluations());
assertEquals("Expected B3 but was: " + move, "B3", move); assertTrue(gameState.playStone(Player.BLACK, move));
gameState.playStone(Player.BLACK, move);
System.out.println(gameState); System.out.println(gameState);
} }
@Test @Test
public void testIllegalMoveRejection() { public void testIllegalMoveRejection() {
Policy treeSearch = new MonteCarloUCT(new RandomMovePolicy(),2000L); Policy treeSearch = new MonteCarloUCT(new RandomMovePolicy(), 1000L);
GameConfig gameConfig = new GameConfig(5); GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.WHITE, Action.getInstance("A2"));
gameState.playStone(Player.WHITE, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A2")));
gameState.playStone(Player.WHITE, Action.getInstance("C2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
gameState.playStone(Player.WHITE, Action.getInstance("B3")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C2")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B3")));
Action move; Action move;
// verify that UCT does not generate an illegal move in 20 calls
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
move = treeSearch.getAction(gameConfig, gameState, Player.BLACK); move = treeSearch.getAction(gameConfig, gameState, Player.BLACK);
System.out.println("Generated move: " + move); System.out.println("Generated move: " + move);

View File

@@ -34,22 +34,27 @@ public class RandomTest {
public void testAlternativeToIllegalMove() { public void testAlternativeToIllegalMove() {
GameConfig gameConfig = new GameConfig(4); GameConfig gameConfig = new GameConfig(4);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.BLACK, Action.getInstance("A1"));
gameState.playStone(Player.BLACK, Action.getInstance("A2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A1")));
gameState.playStone(Player.BLACK, Action.getInstance("A3")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A2")));
gameState.playStone(Player.BLACK, Action.getInstance("A4")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A3")));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));; assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A4")));
gameState.playStone(Player.BLACK, Action.getInstance("B2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B1")));
gameState.playStone(Player.BLACK, Action.getInstance("B4")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B2")));
gameState.playStone(Player.BLACK, Action.getInstance("C2")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B4")));
gameState.playStone(Player.BLACK, Action.getInstance("C3")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C2")));
gameState.playStone(Player.BLACK, Action.getInstance("C4")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C3")));
gameState.playStone(Player.BLACK, Action.getInstance("D4")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C4")));
gameState.playStone(Player.WHITE, Action.getInstance("C1")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("D4")));
gameState.playStone(Player.WHITE, Action.getInstance("D2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C1")));
gameState.playStone(Player.WHITE, Action.getInstance("D3")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("D2")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("D3")));
assertTrue(gameState.playStone(Player.BLACK, Action.PASS));
System.out.println("State before random WHITE move selection:"); System.out.println("State before random WHITE move selection:");
System.out.println(gameState); System.out.println(gameState);
//This is correct - checked vs. MFOG //This is correct - checked vs. MFOG
//PASS would otherwise be a valid move //PASS would otherwise be a valid move
List<Action> prohibitedMoves = new ArrayList<Action>(); List<Action> prohibitedMoves = new ArrayList<Action>();
@@ -65,11 +70,11 @@ public class RandomTest {
GameConfig gameConfig = new GameConfig(3); GameConfig gameConfig = new GameConfig(3);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.WHITE, Action.getInstance("A1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A1")));
gameState.playStone(Player.WHITE, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
gameState.playStone(Player.WHITE, Action.getInstance("B2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B2")));
gameState.playStone(Player.WHITE, Action.getInstance("A3")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A3")));
gameState.playStone(Player.WHITE, Action.getInstance("B3")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B3")));
System.out.println("State before move: "); System.out.println("State before move: ");
System.out.println(gameState); System.out.println(gameState);
@@ -89,13 +94,13 @@ public class RandomTest {
GameConfig gameConfig = new GameConfig(4); GameConfig gameConfig = new GameConfig(4);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.WHITE, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
gameState.playStone(Player.WHITE, Action.getInstance("A2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A2")));
gameState.playStone(Player.WHITE, Action.getInstance("C2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C2")));
gameState.playStone(Player.WHITE, Action.getInstance("B3")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B3")));
gameState.playStone(Player.BLACK, Action.getInstance("A3")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A3")));
gameState.playStone(Player.BLACK, Action.getInstance("C3")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C3")));
gameState.playStone(Player.BLACK, Action.getInstance("B4")); assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B4")));
System.out.println("State before move: "); System.out.println("State before move: ");
System.out.println(gameState); System.out.println(gameState);

View File

@@ -0,0 +1,87 @@
package net.woodyfolsom.msproj.policy;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.Player;
import org.junit.Test;
public class RootParTest {
@Test
public void testGenmoveAsW() {
Policy treeSearch = new RootParallelization(2, 2000L);
GameConfig gameConfig = new GameConfig(6);
GameState gameState = new GameState(gameConfig);
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A2")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C2")));
// if this were PLACE stone instad of PLAY stone, white should pass as
// well, and win.
assertTrue(gameState.playStone(Player.BLACK, Action.getInstance("B2")));
Action move = treeSearch.getAction(gameConfig, gameState, Player.WHITE);
System.out.println(gameState);
System.out.println("Generated move: " + move);
System.out.println("NumStateEvaluations: "
+ treeSearch.getNumStateEvaluations());
assertTrue(gameState.playStone(Player.WHITE, move));
System.out.println(gameState);
}
@Test
public void testGenmoveAsB() {
Policy treeSearch = new RootParallelization(2, 2000L);
GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig);
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A2")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B1")));
assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C2")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B2")));
Action move = treeSearch.getAction(gameConfig, gameState, Player.BLACK);
System.out.println(gameState);
System.out.println("Generated move: " + move);
System.out.println("NumStateEvaluations: "
+ treeSearch.getNumStateEvaluations());
assertTrue(gameState.playStone(Player.BLACK, move));
System.out.println(gameState);
}
@Test
public void testIllegalMoveRejection() {
Policy treeSearch = new RootParallelization(2, 2000L);
GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig);
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A2")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C2")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B3")));
Action move;
// verify that UCT does not generate an illegal move in 20 calls
for (int i = 0; i < 10; i++) {
move = treeSearch.getAction(gameConfig, gameState, Player.BLACK);
System.out.println("Generated move: " + move);
GameState stateCopy = new GameState(gameState);
stateCopy.playStone(Player.BLACK, move);
System.out.println(stateCopy);
assertFalse(Action.getInstance("B2").equals(move));
}
}
}

View File

@@ -28,10 +28,11 @@ public class ValidMoveGeneratorTest {
*/ */
GameConfig gameConfig = new GameConfig(5); GameConfig gameConfig = new GameConfig(5);
GameState gameState = new GameState(gameConfig); GameState gameState = new GameState(gameConfig);
gameState.playStone(Player.WHITE, Action.getInstance("A2"));
gameState.playStone(Player.WHITE, Action.getInstance("B1")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A2")));
gameState.playStone(Player.WHITE, Action.getInstance("B4")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
gameState.playStone(Player.WHITE, Action.getInstance("C2")); assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B4")));
assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C2")));
assertFalse(gameState.playStone(Player.BLACK, Action.getInstance("A1"))); assertFalse(gameState.playStone(Player.BLACK, Action.getInstance("A1")));