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:
@@ -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, "?"));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
53
test/net/woodyfolsom/msproj/policy/MonteCarloUCTTest.java
Normal file
53
test/net/woodyfolsom/msproj/policy/MonteCarloUCTTest.java
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user