package net.woodyfolsom.msproj;

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

import org.junit.Test;

public class CaptureTest {
	@Test
	public void testCapture() {
		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("B3")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B1")));
		
		assertTrue(gameState.playStone(Player.BLACK, Action.PASS));
		assertTrue(gameState.playStone(Player.WHITE, Action.getInstance("B2")));
		
		assertEquals(0,gameState.getBlackPrisoners());
		assertEquals(0,gameState.getWhitePrisoners());
		
		assertTrue(gameState.playStone(Player.BLACK, Action.getInstance("C2")));
		
		assertEquals(1,gameState.getBlackPrisoners());
		assertEquals(0,gameState.getWhitePrisoners());
		
		System.out.println(gameState);
	}
	
	@Test
	public void testMultiGroupCapture() {
		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("B3")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B1")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C4")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("D3")));
		
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B2")));
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C3")));
		
		assertEquals(0,gameState.getBlackPrisoners());
		assertEquals(0,gameState.getWhitePrisoners());
		
		assertTrue(gameState.playStone(Player.BLACK, Action.getInstance("C2")));
		
		assertEquals(2,gameState.getBlackPrisoners());
		assertEquals(0,gameState.getWhitePrisoners());
		
		assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("B2")));
		assertFalse(gameState.playStone(Player.WHITE, Action.getInstance("C3")));
		
		System.out.println(gameState);
		
		System.out.println(gameState.getResult());
	}
	
	@Test
	public void testCaptureFromEye() {
		GameConfig gameConfig = new GameConfig(5);
		GameState gameState = new GameState(gameConfig);
		
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("A1")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("B2")));
		assertTrue(gameState.placeStone(Player.BLACK, Action.getInstance("C1")));
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("A2")));
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("B3")));
		assertTrue(gameState.placeStone(Player.WHITE, Action.getInstance("C2")));
		
		//This capture should be allowed.
		assertTrue(gameState.playStone(Player.BLACK, Action.PASS));
		
		System.out.println("State before WHITE move: ");
		System.out.println(gameState);
		
		assertTrue("Capture from within single eye should have been allowed but move was rejected.",
				gameState.playStone(Player.WHITE, Action.getInstance("B1")));

		assertEquals("BLACK should have 0 prisoners.",0,gameState.getBlackPrisoners());
		assertEquals("WHITE should have 2 prisoners.",2,gameState.getWhitePrisoners());
	}
}
