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