package net.woodyfolsom.msproj.policy;

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

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

public class ValidMoveGenerator implements ActionGenerator {

	@Override
	public List<Action> getActions(GameConfig gameConfig, GameState gameState,
			Player color, int nMoves) {
		return getActions(gameConfig, gameState, new ArrayList<Action>(),
				color, nMoves);
	}

	@Override
	public List<Action> getActions(GameConfig gameConfig, GameState gameState,
			Collection<Action> prohibitedMoves, Player color, int nMoves) {

		GameState gameStateCopy = new GameState(gameState);
		List<String> emptyCoordinates = gameStateCopy.getEmptyCoords();
		List<Action> validMoves = new ArrayList<Action>();

		while (emptyCoordinates.size() > 0) {
			Action nextMove = Action.getInstance(emptyCoordinates
					.remove(emptyCoordinates.size() - 1));
			if (!prohibitedMoves.contains(nextMove)
					&& gameStateCopy.playStone(color, nextMove)) {
				validMoves.add(nextMove);
				gameStateCopy = new GameState(gameState); // play successful?
															// regenerate copy
															// of gameState
			}
		}

		if (!prohibitedMoves.contains(Action.PASS)) {
			validMoves.add(Action.PASS);
		}

		return validMoves;
	}
}
