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

@@ -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);
}
}