Skip to content

Commit

Permalink
Hex heuristic unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sandorw committed Nov 20, 2015
1 parent d7e5d4d commit c932f26
Showing 1 changed file with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.github.sandorw.mocabogaso.Game;
import com.github.sandorw.mocabogaso.games.GameResult;
import com.github.sandorw.mocabogaso.games.defaults.DefaultGameMove;
import com.github.sandorw.mocabogaso.games.defaults.DefaultGameResult;
import com.github.sandorw.mocabogaso.players.PlayerFactory;

/**
Expand All @@ -37,6 +38,7 @@ public void copyStateTest() {
gameState.applyMove(new DefaultGameMove("O", 3));
copy.applyMove(new DefaultGameMove("O", 3));
assertTrue(gameState.equals(copy));
assertEquals(gameState.hashCode(), copy.hashCode());
}

@Test
Expand Down Expand Up @@ -157,4 +159,81 @@ public void playFullGameTest() {
GameResult gameResult = game.getGameResult();
assertFalse(gameResult.isTie());
}

/**
* Hex heuristic tests
*/

@Test
public void initialStateHeuristic_weightTest() {
InitialStateHeuristic heuristic = new InitialStateHeuristic(5);
assertEquals(heuristic.getWeight(), 5);
}

@Test
public void initialStateHeuristic_isTieTest() {
InitialStateHeuristic heuristic = new InitialStateHeuristic(5);
HexGameState gameState = HexGameState.of(5);
for (DefaultGameMove move : gameState.getAllValidMoves()) {
assertTrue(heuristic.evaluateMove(move, gameState).isTie());
}
}

@Test
public void firstLineHeuristic_weightTest() {
FirstLineHexHeuristic heuristic = new FirstLineHexHeuristic(5);
assertEquals(heuristic.getWeight(), 5);
}

@Test
public void firstLineHeuristic_badFirstLinePlayTest() {
FirstLineHexHeuristic heuristic = new FirstLineHexHeuristic(5);
HexGameState gameState = HexGameState.of(5);
for (int i=0; i < 5; ++i) {
DefaultGameMove move = new DefaultGameMove("X", i);
DefaultGameResult gameResult = heuristic.evaluateMove(move, gameState);
assertFalse(gameResult.isTie());
assertEquals(gameResult.getWinningPlayer(), "O");
}
for (int i=20; i < 25; ++i) {
DefaultGameMove move = new DefaultGameMove("X", i);
DefaultGameResult gameResult = heuristic.evaluateMove(move, gameState);
assertFalse(gameResult.isTie());
assertEquals(gameResult.getWinningPlayer(), "O");
}
for (int i=0; i < 25; i += 5) {
DefaultGameMove move = new DefaultGameMove("X", i);
DefaultGameResult gameResult = heuristic.evaluateMove(move, gameState);
assertFalse(gameResult.isTie());
assertEquals(gameResult.getWinningPlayer(), "O");
}
for (int i=4; i < 25; i += 5) {
DefaultGameMove move = new DefaultGameMove("X", i);
DefaultGameResult gameResult = heuristic.evaluateMove(move, gameState);
assertFalse(gameResult.isTie());
assertEquals(gameResult.getWinningPlayer(), "O");
}
}

@Test
public void firstLineHeuristic_goodFirstLinePlayTest() {
FirstLineHexHeuristic heuristic = new FirstLineHexHeuristic(5);
HexGameState gameState = HexGameState.of(5);
DefaultGameMove move = new DefaultGameMove("X", 6);
gameState.applyMove(move);
move = new DefaultGameMove("O", 11);
gameState.applyMove(move);
move = new DefaultGameMove("X", 1);
DefaultGameResult gameResult = heuristic.evaluateMove(move, gameState);
assertNull(gameResult);
}

@Test
public void firstLineHeuristic_interiorPlayTest() {
FirstLineHexHeuristic heuristic = new FirstLineHexHeuristic(5);
HexGameState gameState = HexGameState.of(5);
DefaultGameMove move = new DefaultGameMove("X", 6);
DefaultGameResult gameResult = heuristic.evaluateMove(move, gameState);
assertNull(gameResult);
}
}

0 comments on commit c932f26

Please sign in to comment.