package net.woodyfolsom.msproj.policy;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;

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 RandomTest {
	
	@Test(expected = IllegalArgumentException.class)
	public void testGenmoveForNone() {
		Policy moveGenerator = new RandomMovePolicy();
		GameState gameState = new GameState(5);
		moveGenerator.getAction(new GameConfig(), gameState, Player.BLACK);
		gameState = new GameState(5);
		moveGenerator.getAction(new GameConfig(), gameState, Player.WHITE);
		
		assertEquals(Action.PASS, moveGenerator.getAction(new GameConfig(), gameState, Player.NONE));
	}
	
	@Test
	public void testAlternativeToIllegalMove() {
		GameState gameState = new GameState(4);
		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(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
		//PASS would otherwise be a valid move
		List<Action> prohibitedMoves = new ArrayList<Action>();
		prohibitedMoves.add(Action.PASS);
		
		assertEquals(Action.getInstance("B3"), new RandomMovePolicy().getAction(new GameConfig(), gameState, prohibitedMoves, Player.WHITE));
		
		System.out.println(gameState);
	}
	
	@Test
	public void testIllegalMoveSuicide() {
		GameState gameState = new GameState(3);
		
		gameState.playStone(Player.WHITE, Action.getInstance("A1"));
		gameState.playStone(Player.WHITE, Action.getInstance("B1"));
		gameState.playStone(Player.WHITE, Action.getInstance("B2"));
		gameState.playStone(Player.WHITE, Action.getInstance("A3"));
		gameState.playStone(Player.WHITE, Action.getInstance("B3"));
		
		System.out.println("State before move: ");
		System.out.println(gameState);
		RandomMovePolicy randomMovePolicy = new RandomMovePolicy();
		
		//There is only a minute chance (5E-7) that RandomMoveGenerator fails to return an invalid move with probability 1/4
		//after 50 calls, if this bug recurs.
		for (int i = 0; i < 50; i++) {
			Action action = randomMovePolicy.getAction(new GameConfig(),gameState,Player.BLACK);
			//System.out.println(action);
			assertFalse("RandomMovePolicy returned illegal suicide move A2",action.equals(Action.getInstance("A2")));
		}
	}
	
	@Test
	public void testIllegalMoveKo() {
		GameState gameState = new GameState(4);
		
		gameState.playStone(Player.WHITE, Action.getInstance("B1"));
		gameState.playStone(Player.WHITE, Action.getInstance("A2"));
		gameState.playStone(Player.WHITE, Action.getInstance("C2"));
		gameState.playStone(Player.WHITE, Action.getInstance("B3"));
		gameState.playStone(Player.BLACK, Action.getInstance("A3"));
		gameState.playStone(Player.BLACK, Action.getInstance("C3"));
		gameState.playStone(Player.BLACK, Action.getInstance("B4"));
		
		System.out.println("State before move: ");
		System.out.println(gameState);
		assertTrue(gameState.playStone(Player.BLACK, Action.getInstance("B2")));
		System.out.println("State after move: ");
		System.out.println(gameState);
		
		RandomMovePolicy randomMovePolicy = new RandomMovePolicy();
		//Test that after 50 moves, the policy never returns B3, which would be a Ko violation
		for (int i = 0; i < 50; i++) {
			Action action = randomMovePolicy.getAction(new GameConfig(),gameState,Player.WHITE);
			//System.out.println(action);
			assertFalse(action.equals(Action.NONE));
			assertFalse("RandomMovePolicy returned Ko violation move B3",action.equals(Action.getInstance("B3")));
		}
	}
}
