package net.woodyfolsom.msproj.policy;

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

import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.Player;

public class Rollout {
	private GameState initialGameState;
	private List<Action> blackPlays;
	private List<Action> playout;
	private List<Action> whitePlays;
	private int reward;
	
	public Rollout(GameState initialGameState, List<Action> playout, int reward) {
		this.initialGameState = initialGameState;
		this.playout = playout;
		this.reward = reward;
		
		blackPlays = new ArrayList<Action>();
		whitePlays = new ArrayList<Action>();
		
		Player playerToMove = initialGameState.getPlayerToMove();
		
		List<List<Action>> plays = new ArrayList<List<Action>>();
		
		if (playerToMove == Player.BLACK) {
			plays.add(blackPlays);
			plays.add(whitePlays);
		} else if (playerToMove == Player.WHITE) {
			plays.add(whitePlays);
			plays.add(blackPlays);
		} else {
			throw new RuntimeException("Invalid player: " + playerToMove);
		}
		
		for (int i = 0; i < playout.size(); i++) {
			plays.get(i%2).add(playout.get(i));
		}
	}
	
	public GameState getInitialGameState() {
		return initialGameState;
	}
	
	public List<Action> getPlayout() {
		return playout;
	}
	
	public int getReward() {
		return reward;
	}
	
	public boolean hasPlay(Player player, Action action) {
		if (player == Player.BLACK) {
			return blackPlays.contains(action);
		} else if (player == Player.WHITE) {
			return whitePlays.contains(action);
		} else {
			throw new RuntimeException("Invalid player: " + player);
		}
	}
}