Refactoring in progress.

Player and Action classes are now singletons (factory pattern) rather than String values.
Implementing more general treesearch code for minimax, alpha-beta, monte carlo using simplified backup logic.
This commit is contained in:
cs6601
2012-08-30 08:41:03 -04:00
parent b44b666663
commit 2e40440838
26 changed files with 647 additions and 433 deletions

View File

@@ -4,27 +4,21 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import net.woodyfolsom.msproj.GameBoard;
import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameScore;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.StateEvaluator;
import org.junit.Test;
public class CaptureTest {
@Test
public void testCapture() {
GameState gameState = new GameState(5);
gameState.playStone('A', 2, GameBoard.BLACK_STONE);
gameState.playStone('B', 3, GameBoard.BLACK_STONE);
gameState.playStone('B', 1, GameBoard.BLACK_STONE);
assertTrue(gameState.playStone('B', 2, GameBoard.WHITE_STONE));
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B3"));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("B2")));
assertEquals(0,gameState.getBlackPrisoners());
assertEquals(0,gameState.getWhitePrisoners());
assertTrue(gameState.playStone('C', 2, GameBoard.BLACK_STONE));
assertTrue(gameState.playStone(Player.BLACK, Action.getInstance("C2")));
assertEquals(1,gameState.getBlackPrisoners());
assertEquals(0,gameState.getWhitePrisoners());
@@ -37,25 +31,25 @@ public class CaptureTest {
GameConfig gameConfig = new GameConfig();
GameState gameState = new GameState(5);
gameState.playStone('A', 2, GameBoard.BLACK_STONE);
gameState.playStone('B', 3, GameBoard.BLACK_STONE);
gameState.playStone('B', 1, GameBoard.BLACK_STONE);
gameState.playStone('C', 4, GameBoard.BLACK_STONE);
gameState.playStone('D', 3, GameBoard.BLACK_STONE);
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B3"));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));
gameState.playStone(Player.BLACK, Action.getInstance("C4"));
gameState.playStone(Player.BLACK, Action.getInstance("D3"));
assertTrue(gameState.playStone('B', 2, GameBoard.WHITE_STONE));
assertTrue(gameState.playStone('C', 3, GameBoard.WHITE_STONE));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("B2")));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("C3")));
assertEquals(0,gameState.getBlackPrisoners());
assertEquals(0,gameState.getWhitePrisoners());
assertTrue(gameState.playStone('C', 2, GameBoard.BLACK_STONE));
assertTrue(gameState.playStone(Player.BLACK, Action.getInstance("C2")));
assertEquals(2,gameState.getBlackPrisoners());
assertEquals(0,gameState.getWhitePrisoners());
assertFalse(gameState.playStone('B', 2, GameBoard.WHITE_STONE));
assertFalse(gameState.playStone('C', 3, GameBoard.WHITE_STONE));
assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("B2")));
assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("C3")));
System.out.println(gameState);
@@ -67,20 +61,21 @@ public class CaptureTest {
public void testCaptureFromEye() {
GameState gameState = new GameState(5);
gameState.playStone('A', 1, GameBoard.BLACK_STONE);
gameState.playStone('B', 2, GameBoard.BLACK_STONE);
gameState.playStone('C', 1, GameBoard.BLACK_STONE);
gameState.playStone('A', 2, GameBoard.WHITE_STONE);
gameState.playStone('B', 3, GameBoard.WHITE_STONE);
gameState.playStone('C', 2, GameBoard.WHITE_STONE);
gameState.playStone(Player.BLACK, Action.getInstance("A1"));
gameState.playStone(Player.BLACK, Action.getInstance("B2"));
gameState.playStone(Player.BLACK, Action.getInstance("C1"));
gameState.playStone(Player.WHITE, Action.getInstance("A2"));
gameState.playStone(Player.WHITE, Action.getInstance("B3"));
gameState.playStone(Player.WHITE, Action.getInstance("C2"));
//This capture should be allowed.
assertTrue("Capture from within single eye should have been allowed but move was rejected.",
gameState.playStone('B', 1, GameBoard.WHITE_STONE));
assertEquals(0,gameState.getBlackPrisoners());
assertEquals(2,gameState.getWhitePrisoners());
System.out.println("State before WHITE move: ");
System.out.println(gameState);
assertTrue("Capture from within single eye should have been allowed but move was rejected.",
gameState.playStone(Player.WHITE, Action.getInstance("B1")));
assertEquals("BLACK should have 0 prisoners.",0,gameState.getBlackPrisoners());
assertEquals("WHITE should have 2 prisoners.",2,gameState.getWhitePrisoners());
}
}

View File

@@ -3,9 +3,6 @@ package net.woodyfolsom.msproj;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import net.woodyfolsom.msproj.GameBoard;
import net.woodyfolsom.msproj.GameState;
import org.junit.Test;
public class IllegalMoveTest {
@@ -13,53 +10,57 @@ public class IllegalMoveTest {
@Test
public void testIllegalMoveOnOwnStone() {
GameState gameState = new GameState(5);
gameState.playStone('B', 3, GameBoard.BLACK_STONE);
assertFalse(gameState.playStone('B', 3, GameBoard.BLACK_STONE));
gameState.playStone(Player.BLACK, Action.getInstance("B3"));
assertFalse(gameState.playStone(Player.BLACK, Action.getInstance("B3")));
}
@Test
public void testIllegalMoveOnOtherStone() {
GameState gameState = new GameState(5);
gameState.playStone('B', 3, GameBoard.BLACK_STONE);
assertFalse(gameState.playStone('B', 3, GameBoard.WHITE_STONE));
gameState.playStone(Player.BLACK, Action.getInstance("B3"));
assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("B3")));
}
@Test
public void testIllegalMoveNoLiberties() {
GameState gameState = new GameState(5);
gameState.playStone('A', 2, GameBoard.BLACK_STONE);
gameState.playStone('B', 3, GameBoard.BLACK_STONE);
gameState.playStone('B', 1, GameBoard.BLACK_STONE);
gameState.playStone('C', 2, GameBoard.BLACK_STONE);
assertFalse(gameState.playStone('B', 2, GameBoard.WHITE_STONE));
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));
gameState.playStone(Player.BLACK, Action.getInstance("B3"));
gameState.playStone(Player.BLACK, Action.getInstance("C2"));
System.out.println(gameState);
assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("B2")));
System.out.println(gameState);
}
@Test
public void testIllegalMoveFormsTrappedGroup() {
GameState gameState = new GameState(9);
gameState.playStone('A', 5, GameBoard.BLACK_STONE);
gameState.playStone('B', 6, GameBoard.BLACK_STONE);
gameState.playStone('B', 7, GameBoard.BLACK_STONE);
gameState.playStone('A', 8, GameBoard.BLACK_STONE);
assertTrue(gameState.playStone('A', 6, GameBoard.WHITE_STONE));
assertFalse(gameState.playStone('A', 7, GameBoard.WHITE_STONE));
gameState.playStone(Player.BLACK, Action.getInstance("A5"));
gameState.playStone(Player.BLACK, Action.getInstance("B6"));
gameState.playStone(Player.BLACK, Action.getInstance("B7"));
gameState.playStone(Player.BLACK, Action.getInstance("A8"));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("A6")));
assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("A7")));
System.out.println(gameState);
}
@Test
public void testIllegalMoveFormsTrappedGroup2() {
GameState gameState = new GameState(9);
gameState.playStone('G', 1, GameBoard.BLACK_STONE);
gameState.playStone('H', 2, GameBoard.BLACK_STONE);
gameState.playStone('H', 3, GameBoard.BLACK_STONE);
gameState.playStone('J', 4, GameBoard.BLACK_STONE);
gameState.playStone('A', 8, GameBoard.BLACK_STONE);
assertTrue(gameState.playStone('H', 1, GameBoard.WHITE_STONE));
assertTrue(gameState.playStone('J', 2, GameBoard.WHITE_STONE));
assertTrue(gameState.playStone('J', 3, GameBoard.WHITE_STONE));
System.out.println(gameState);
assertFalse(gameState.playStone('J', 1, GameBoard.WHITE_STONE));
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.playStone(Player.WHITE, Action.getInstance("H1")));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("J2")));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("J3")));
System.out.println("State before move: ");
System.out.println(gameState);
assertFalse("Play by WHITE at J1 should have failed.",gameState.playStone(Player.WHITE, Action.getInstance("J1")));
}
}

View File

@@ -2,19 +2,16 @@ package net.woodyfolsom.msproj;
import static org.junit.Assert.assertTrue;
import net.woodyfolsom.msproj.GameBoard;
import net.woodyfolsom.msproj.GameState;
import org.junit.Test;
public class LegalMoveTest {
@Test
public void testLegalMove1Liberty() {
GameState gameState = new GameState(5);
gameState.playStone('A', 2, GameBoard.BLACK_STONE);
gameState.playStone('B', 3, GameBoard.BLACK_STONE);
gameState.playStone('B', 1, GameBoard.BLACK_STONE);
assertTrue(gameState.playStone('B', 2, GameBoard.WHITE_STONE));
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B3"));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));
assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("B2")));
System.out.println(gameState);
}
}

View File

@@ -3,98 +3,96 @@ package net.woodyfolsom.msproj;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import net.woodyfolsom.msproj.GameBoard;
import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameScore;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.StateEvaluator;
import org.junit.Test;
public class StateEvaluatorTest {
GameConfig gameConfig = new GameConfig();
@Test
public void testScoreEmptyBoard() {
GameState gameState = new GameState(5);
GameScore gameScore = new StateEvaluator(gameConfig).scoreGame(gameState);
assertEquals(0.0,gameScore.getWhiteScore(),0.5);
assertEquals(0.0,gameScore.getBlackScore(),0.5);
GameScore gameScore = new StateEvaluator(gameConfig)
.scoreGame(gameState);
assertEquals(0.0, gameScore.getWhiteScore(), 0.5);
assertEquals(0.0, gameScore.getBlackScore(), 0.5);
}
@Test
public void testScoreFirstMove() {
GameState gameState = new GameState(5);
gameState.playStone('B',3,GameBoard.BLACK_STONE);
GameScore gameScore = new StateEvaluator(gameConfig).scoreGame(gameState);
gameState.playStone(Player.BLACK, Action.getInstance("B3"));
GameScore gameScore = new StateEvaluator(gameConfig)
.scoreGame(gameState);
System.out.println(gameScore.getScoreReport());
assertEquals(0.0,gameScore.getWhiteScore(),0.5);
assertEquals(25.0,gameScore.getBlackScore(),0.5);
assertEquals(0.0, gameScore.getWhiteScore(), 0.5);
assertEquals(25.0, gameScore.getBlackScore(), 0.5);
}
@Test
public void testScoreTiedAtMove2() {
GameState gameState = new GameState(5);
gameState.playStone('B',3,GameBoard.BLACK_STONE);
gameState.playStone('A',1,GameBoard.WHITE_STONE);
GameScore gameScore = new StateEvaluator(gameConfig).scoreGame(gameState);
gameState.playStone(Player.BLACK, Action.getInstance("B3"));
gameState.playStone(Player.WHITE, Action.getInstance("A1"));
GameScore gameScore = new StateEvaluator(gameConfig)
.scoreGame(gameState);
System.out.println(gameScore.getScoreReport());
assertEquals(1.0,gameScore.getWhiteScore(),0.5);
assertEquals(1.0,gameScore.getBlackScore(),0.5);
assertEquals(1.0, gameScore.getWhiteScore(), 0.5);
assertEquals(1.0, gameScore.getBlackScore(), 0.5);
}
@Test
public void testScoreTerritory() {
GameState gameState = new GameState(5);
gameState.playStone('A',2,GameBoard.BLACK_STONE);
gameState.playStone('B',3,GameBoard.BLACK_STONE);
gameState.playStone('C',2,GameBoard.BLACK_STONE);
gameState.playStone('B',1,GameBoard.BLACK_STONE);
gameState.playStone('E',5,GameBoard.WHITE_STONE);
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B3"));
gameState.playStone(Player.BLACK, Action.getInstance("C2"));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));
gameState.playStone(Player.WHITE, Action.getInstance("E5"));
System.out.println(gameState);
GameScore gameScore = new StateEvaluator(gameConfig).scoreGame(gameState);
GameScore gameScore = new StateEvaluator(gameConfig)
.scoreGame(gameState);
System.out.println(gameScore.getScoreReport());
assertEquals(1.0,gameScore.getWhiteScore(),0.5);
assertEquals(6.0,gameScore.getBlackScore(),0.5);
//Black should be up by 5 if Black's territory at A1 & B2 is scored correctly.
assertEquals(1.0, gameScore.getWhiteScore(), 0.5);
assertEquals(6.0, gameScore.getBlackScore(), 0.5);
// Black should be up by 5 if Black's territory at A1 & B2 is scored
// correctly.
}
@Test
public void testCaptureAggScore() {
GameState gameState = new GameState(9);
gameState.playStone('A', 2, GameBoard.WHITE_STONE);
gameState.playStone('B', 1, GameBoard.WHITE_STONE);
gameState.playStone('C', 2, GameBoard.WHITE_STONE);
gameState.playStone('B', 2, GameBoard.BLACK_STONE);
gameState.playStone(Player.WHITE, Action.getInstance("A2"));
gameState.playStone(Player.WHITE, Action.getInstance("B1"));
gameState.playStone(Player.WHITE, Action.getInstance("C2"));
gameState.playStone(Player.BLACK, Action.getInstance("B2"));
GameState moveToA1 = new GameState(gameState);
GameState capAtB3 = new GameState(gameState);
moveToA1.playStone("w","A1");
capAtB3.playStone("w", "B3");
moveToA1.playStone(Player.WHITE, Action.getInstance("A1"));
capAtB3.playStone(Player.WHITE, Action.getInstance("B3"));
System.out.println(moveToA1);
System.out.println(capAtB3);
StateEvaluator eval = new StateEvaluator(new GameConfig());
int scoreA1 = eval.scoreGame(moveToA1).getAggregateScore();
int scoreB3 = eval.scoreGame(capAtB3).getAggregateScore();
System.out.println("Score at A1: " + scoreA1);
System.out.println("Score at B3: " + scoreB3);
//moving as white, lower is better
// moving as white, lower is better
assertTrue(scoreA1 > scoreB3);
}
}

View File

@@ -1,22 +1,18 @@
package net.woodyfolsom.msproj;
import net.woodyfolsom.msproj.GameBoard;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.TerritoryMarker;
import org.junit.Test;
public class TerritoryFinderTest {
@Test
public void testMarkTerritory() {
GameState gameState = new GameState(5);
gameState.playStone('A',2,GameBoard.BLACK_STONE);
gameState.playStone('B',3,GameBoard.BLACK_STONE);
gameState.playStone('C',2,GameBoard.BLACK_STONE);
gameState.playStone('B',1,GameBoard.BLACK_STONE);
gameState.playStone('E',5,GameBoard.WHITE_STONE);
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B3"));
gameState.playStone(Player.BLACK, Action.getInstance("C2"));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));
gameState.playStone(Player.WHITE, Action.getInstance("E5"));
TerritoryMarker.markTerritory(gameState.getGameBoard());
System.out.println(gameState);
}

View File

@@ -1,12 +1,10 @@
package net.woodyfolsom.msproj.policy;
import static org.junit.Assert.assertEquals;
import net.woodyfolsom.msproj.GameBoard;
import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.policy.AlphaBeta;
import net.woodyfolsom.msproj.policy.Policy;
import net.woodyfolsom.msproj.Player;
import org.junit.Test;
@@ -15,43 +13,40 @@ public class AlphaBetaTest {
public void testGenmoveAsW() {
Policy treeSearch = new AlphaBeta();
GameState gameState = new GameState(6);
gameState.playStone('A', 2, GameBoard.WHITE_STONE);
gameState.playStone('B', 1, GameBoard.WHITE_STONE);
gameState.playStone('C', 2, GameBoard.WHITE_STONE);
gameState.playStone('B', 2, GameBoard.BLACK_STONE);
gameState.playStone(Player.WHITE, Action.getInstance("A2"));
gameState.playStone(Player.WHITE, Action.getInstance("B1"));
gameState.playStone(Player.WHITE, Action.getInstance("C2"));
gameState.playStone(Player.BLACK, Action.getInstance("B2"));
String move = treeSearch.getAction(new GameConfig(), gameState, "b");
Action move = treeSearch.getAction(new GameConfig(), gameState, Player.WHITE);
System.out.println(gameState);
System.out.println("Generated move: " + move);
assertEquals("Expected B3 but was: " + move, "B3", move);
gameState.playStone("b", move);
assertEquals("Expected B3 but was: " + move, Action.getInstance("B3"), move);
gameState.playStone(Player.WHITE, move);
System.out.println("Final board state:");
System.out.println(gameState);
assertEquals(Policy.PASS,
treeSearch.getAction(new GameConfig(), gameState, "?"));
}
@Test
public void testGenmoveAsB() {
Policy treeSearch = new AlphaBeta();
GameState gameState = new GameState(6);
gameState.playStone('A', 2, GameBoard.BLACK_STONE);
gameState.playStone('B', 1, GameBoard.BLACK_STONE);
gameState.playStone('C', 2, GameBoard.BLACK_STONE);
gameState.playStone('B', 2, GameBoard.WHITE_STONE);
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));
gameState.playStone(Player.BLACK, Action.getInstance("C2"));
gameState.playStone(Player.WHITE, Action.getInstance("B2"));
String move = treeSearch.getAction(new GameConfig(), gameState, "b");
Action move = treeSearch.getAction(new GameConfig(), gameState, Player.BLACK);
System.out.println(gameState);
System.out.println("Generated move: " + move);
assertEquals("Expected B3 but was: " + move, "B3", move);
gameState.playStone("b", move);
assertEquals("Expected B3 but was: " + move, Action.getInstance("B3"), move);
gameState.playStone(Player.BLACK, move);
System.out.println("Final board state:");
System.out.println(gameState);
assertEquals(Policy.PASS,
treeSearch.getAction(new GameConfig(), gameState, "?"));
}
}

View File

@@ -1,34 +1,53 @@
package net.woodyfolsom.msproj.policy;
import static org.junit.Assert.assertEquals;
import net.woodyfolsom.msproj.GameBoard;
import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.policy.Policy;
import net.woodyfolsom.msproj.Player;
import org.junit.Test;
public class MinimaxTest {
@Test
public void testGenmove() {
Policy moveGenerator = new Minimax();
GameState gameState = new GameState(5);
gameState.playStone('A', 2, GameBoard.BLACK_STONE);
gameState.playStone('B', 1, GameBoard.BLACK_STONE);
gameState.playStone('C', 2, GameBoard.BLACK_STONE);
gameState.playStone('B', 4, GameBoard.BLACK_STONE);
String move = moveGenerator.getAction(new GameConfig(), gameState, "w");
System.out.println("Generated move: " + move);
gameState.playStone("w", move);
public void testGenmoveAsW() {
Policy treeSearch = new Minimax();
GameState gameState = new GameState(6);
gameState.playStone(Player.WHITE, Action.getInstance("A2"));
gameState.playStone(Player.WHITE, Action.getInstance("B1"));
gameState.playStone(Player.WHITE, Action.getInstance("C2"));
gameState.playStone(Player.BLACK, Action.getInstance("B2"));
Action move = treeSearch.getAction(new GameConfig(), gameState,
Player.WHITE);
System.out.println(gameState);
assertEquals(Policy.PASS,moveGenerator.getAction(new GameConfig(), gameState, "?"));
System.out.println("Generated move: " + move);
assertEquals("Expected B3 but was: " + move, "B3", move);
gameState.playStone(Player.WHITE, move);
System.out.println(gameState);
}
}
@Test
public void testGenmoveAsB() {
Policy treeSearch = new Minimax();
GameState gameState = new GameState(6);
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));
gameState.playStone(Player.BLACK, Action.getInstance("C2"));
gameState.playStone(Player.WHITE, Action.getInstance("B2"));
Action move = treeSearch.getAction(new GameConfig(), gameState,
Player.BLACK);
System.out.println(gameState);
System.out.println("Generated move: " + move);
assertEquals("Expected B3 but was: " + move, "B3", move);
gameState.playStone(Player.BLACK, move);
System.out.println(gameState);
}
}

View File

@@ -0,0 +1,53 @@
package net.woodyfolsom.msproj.policy;
import static org.junit.Assert.assertEquals;
import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.Player;
import net.woodyfolsom.msproj.policy.Policy;
import org.junit.Test;
public class MonteCarloUCTTest {
@Test
public void testGenmoveAsW() {
Policy treeSearch = new MonteCarloUCT(new RandomMovePolicy(),2000L);
GameState gameState = new GameState(6);
gameState.playStone(Player.WHITE, Action.getInstance("A2"));
gameState.playStone(Player.WHITE, Action.getInstance("B1"));
gameState.playStone(Player.WHITE, Action.getInstance("C2"));
gameState.playStone(Player.BLACK, Action.getInstance("B2"));
Action move = treeSearch.getAction(new GameConfig(), gameState, Player.WHITE);
System.out.println(gameState);
System.out.println("Generated move: " + move);
assertEquals("Expected B3 but was: " + move, "B3", move);
gameState.playStone(Player.WHITE, move);
System.out.println(gameState);
}
@Test
public void testGenmoveAsB() {
Policy treeSearch = new MonteCarloUCT(new RandomMovePolicy(),2000L);
GameState gameState = new GameState(6);
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));
gameState.playStone(Player.BLACK, Action.getInstance("C2"));
gameState.playStone(Player.WHITE, Action.getInstance("B2"));
Action move = treeSearch.getAction(new GameConfig(), gameState, Player.BLACK);
System.out.println(gameState);
System.out.println("Generated move: " + move);
assertEquals("Expected B3 but was: " + move, "B3", move);
gameState.playStone(Player.BLACK, move);
System.out.println(gameState);
}
}

View File

@@ -1,53 +1,56 @@
package net.woodyfolsom.msproj.policy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import net.woodyfolsom.msproj.GameBoard;
import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.policy.Policy;
import net.woodyfolsom.msproj.policy.RandomMovePolicy;
import net.woodyfolsom.msproj.Player;
import org.junit.Test;
public class RandomTest {
@Test
public void testGenmove() {
@Test(expected = IllegalArgumentException.class)
public void testGenmoveForNone() {
Policy moveGenerator = new RandomMovePolicy();
GameState gameState = new GameState(5);
moveGenerator.getAction(new GameConfig(), gameState, "b");
moveGenerator.getAction(new GameConfig(), gameState, Player.BLACK);
gameState = new GameState(5);
moveGenerator.getAction(new GameConfig(), gameState, "w");
moveGenerator.getAction(new GameConfig(), gameState, Player.WHITE);
assertEquals(Policy.PASS,moveGenerator.getAction(new GameConfig(), gameState, "?"));
System.out.println(gameState);
assertEquals(Action.PASS, moveGenerator.getAction(new GameConfig(), gameState, Player.NONE));
}
@Test
public void testAlternativeToIllegalMove() {
GameState gameState = new GameState(4);
gameState.playStone('A', 1, GameBoard.BLACK_STONE);
gameState.playStone('A', 2, GameBoard.BLACK_STONE);
gameState.playStone('A', 3, GameBoard.BLACK_STONE);
gameState.playStone('A', 4, GameBoard.BLACK_STONE);
gameState.playStone('B', 1, GameBoard.BLACK_STONE);
gameState.playStone('B', 2, GameBoard.BLACK_STONE);
gameState.playStone(Player.BLACK, Action.getInstance("A1"));
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
gameState.playStone(Player.BLACK, Action.getInstance("A3"));
gameState.playStone(Player.BLACK, Action.getInstance("A4"));
gameState.playStone(Player.BLACK, Action.getInstance("B1"));;
gameState.playStone(Player.BLACK, Action.getInstance("B2"));
//gameState.playStone('B', 3, GameBoard.BLACK_STONE);
gameState.playStone('B', 4, GameBoard.BLACK_STONE);
gameState.playStone('C', 2, GameBoard.BLACK_STONE);
gameState.playStone('C', 3, GameBoard.BLACK_STONE);
gameState.playStone('C', 4, GameBoard.BLACK_STONE);
gameState.playStone('D', 4, GameBoard.BLACK_STONE);
assertTrue(gameState.playStone('C', 1, GameBoard.WHITE_STONE));
assertTrue(gameState.playStone('D', 2, GameBoard.WHITE_STONE));
assertTrue(gameState.playStone('D', 3, GameBoard.WHITE_STONE));
gameState.playStone(Player.BLACK, Action.getInstance("B4"));
gameState.playStone(Player.BLACK, Action.getInstance("C2"));
gameState.playStone(Player.BLACK, Action.getInstance("C3"));
gameState.playStone(Player.BLACK, Action.getInstance("C4"));
gameState.playStone(Player.BLACK, Action.getInstance("D4"));
gameState.playStone(Player.WHITE, Action.getInstance("C1"));
gameState.playStone(Player.WHITE, Action.getInstance("D2"));
gameState.playStone(Player.WHITE, Action.getInstance("D3"));
System.out.println("State before random WHITE move selection:");
System.out.println(gameState);
//This is correct - checked vs. MFOG
assertEquals("B3", new RandomMovePolicy().getAction(new GameConfig(), gameState, "w"));
assertEquals(Action.getInstance("B3"), new RandomMovePolicy().getAction(new GameConfig(), gameState, Player.WHITE));
System.out.println(gameState);
}
}

View File

@@ -1,13 +1,14 @@
package net.woodyfolsom.msproj.policy;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.List;
import net.woodyfolsom.msproj.GameBoard;
import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.policy.ValidMoveGenerator;
import net.woodyfolsom.msproj.Player;
import org.junit.Test;
@@ -26,18 +27,22 @@ public class ValidMoveGeneratorTest {
A B C D E
*/
GameState gameState = new GameState(5);
gameState.playStone('A', 2, GameBoard.WHITE_STONE);
gameState.playStone('B', 1, GameBoard.WHITE_STONE);
gameState.playStone('B', 4, GameBoard.WHITE_STONE);
gameState.playStone('C', 2, GameBoard.WHITE_STONE);
assertFalse(gameState.playStone('A', 1, GameBoard.BLACK_STONE));
gameState.playStone(Player.WHITE, Action.getInstance("A2"));
gameState.playStone(Player.WHITE, Action.getInstance("B1"));
gameState.playStone(Player.WHITE, Action.getInstance("B4"));
gameState.playStone(Player.WHITE, Action.getInstance("C2"));
assertFalse(gameState.playStone(Player.BLACK, Action.getInstance("A1")));
List<Action> validMoves = new ValidMoveGenerator().getActions(new GameConfig(), gameState, Player.BLACK,0);
List<String> validMoves = new ValidMoveGenerator().getActions(new GameConfig(), gameState, "b",0);
assertTrue(validMoves.size() > 0);
for (String vm : validMoves) {
for (Action vm : validMoves) {
System.out.println(vm);
}
assertFalse(validMoves.contains("A1"));
assertFalse(validMoves.contains(Action.getInstance("A1")));
System.out.println(gameState);
}