package net.woodyfolsom.msproj;

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

import java.util.List;

import net.woodyfolsom.msproj.policy.ActionGenerator;
import net.woodyfolsom.msproj.policy.ValidMoveGenerator;

import org.junit.Test;

public class IllegalMoveTest {
	
	@Test
	public void testIllegalMoveOnOwnStone() {
		GameConfig gameConfig = new GameConfig(5);
		GameState gameState = new GameState(gameConfig);
		gameState.placeStone(Player.BLACK, Action.getInstance("B3"));
		assertFalse(gameState.playStone(Player.BLACK, Action.getInstance("B3")));
	}
	
	@Test
	public void testIllegalMoveOnOtherStone() {
		GameConfig gameConfig = new GameConfig(5);
		GameState gameState = new GameState(gameConfig);
		gameState.playStone(Player.BLACK, Action.getInstance("B3"));
		assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("B3")));
	}
	
	@Test
	public void testIllegalMoveNoLiberties() {
		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("B3")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C2")));
		
		System.out.println(gameState);
		
		assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
		assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("B2")));
		
		System.out.println(gameState);
	}
	
	@Test
	public void testIllegalMoveFormsTrappedGroup() {
		GameConfig gameConfig = new GameConfig(9);
		GameState gameState = new GameState(gameConfig);
		
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A5")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B6")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B7")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A8")));
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A6")));
		
		assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
		assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("A7")));
		
		System.out.println(gameState);
	}
	
	@Test
	public void testIllegalMoveFormsTrappedGroup2() {
		GameConfig gameConfig = new GameConfig(9);
		GameState gameState = new GameState(gameConfig);
		
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("G1")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("H2")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("H3")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("J4")));	
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A8")));
		
		assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
		assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("H1")));
		assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
		assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("J2")));
		assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
		assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("J3")));
		assertTrue(gameState.playStone(Player.BLACK,Action.PASS));
		
		System.out.println("State before move: ");
		System.out.println(gameState);
		assertFalse("Play by WHITE at J1 should have failed.",gameState.playStone(Player.WHITE, Action.getInstance("J1")));
	}
	
	@Test
	public void testIllegalMoveSuicide() {
		GameConfig gameConfig = new GameConfig(3);
		GameState gameState = new GameState(gameConfig);
		
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A1")));
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B1")));
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B2")));
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A3")));
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B3")));
		
		System.out.println("State before move: ");
		System.out.println(gameState);
		assertFalse("Play by BLACK at A2 should have failed.",gameState.playStone(Player.BLACK, Action.getInstance("A2")));
		
		List<Action> validMoves = new ValidMoveGenerator().getActions(gameConfig, gameState, Player.BLACK, ActionGenerator.ALL_ACTIONS);
		assertEquals(4, validMoves.size());
		assertTrue(validMoves.contains(Action.PASS));
		assertTrue(validMoves.contains(Action.getInstance("C1")));
		assertTrue(validMoves.contains(Action.getInstance("C2")));
		assertTrue(validMoves.contains(Action.getInstance("C3")));
		
		assertFalse(validMoves.contains(Action.getInstance("A2")));
	}
}