package net.woodyfolsom.msproj.policy;

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

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


public class RandomMovePolicy implements Policy {

	/**
	 * Does NOT modify the gameState.
	 */
	public String getAction(GameConfig gameConfig, GameState gameState,
			String color) {
		GameState gameStateCopy = new GameState(gameState);
		List<String> emptyCoordinates = gameStateCopy.getEmptyCoords();

		while (emptyCoordinates.size() > 0) {
			String randomMove = emptyCoordinates
					.get((int) (Math.random() * emptyCoordinates.size()));
			if (gameStateCopy.playStone(color, randomMove)) {
				return randomMove;
			} else {
				emptyCoordinates.remove(randomMove);
			}
		}

		return PASS;
	}

	/**
	 * Attempts to generate up to nMoves random moves on behalf of the specified
	 * player. Will return at least one move, which may be 'pass' if random
	 * search does not success in discovering a valid move. Does NOT modify the
	 * gameState.
	 * 
	 * @param gameConfig
	 * @param gameState
	 * @param color
	 * 
	 * @return
	 */
	public List<String> genMoves(GameConfig gameConfig, GameState gameState,
			String color, int nMoves) {
		GameState gameStateCopy = new GameState(gameState);
		List<String> emptyCoordinates = gameStateCopy.getEmptyCoords();
		List<String> randomMoves = new ArrayList<String>();

		while (emptyCoordinates.size() > 0 && randomMoves.size() < nMoves) {
			String randomMove = emptyCoordinates
					.get((int) (Math.random() * emptyCoordinates.size()));
			if (gameStateCopy.playStone(color, randomMove)) {
				randomMoves.add(randomMove);
			}
			emptyCoordinates.remove(randomMove);
		}

		if (randomMoves.size() == 0) {
			randomMoves.add(PASS);
		}

		return randomMoves;
	}
}
