package org.ggp.base.validator;

import org.ggp.base.util.game.Game;
import org.ggp.base.util.statemachine.MachineState;
import org.ggp.base.util.statemachine.StateMachine;
import org.ggp.base.util.statemachine.exceptions.MoveDefinitionException;
import org.ggp.base.util.statemachine.exceptions.TransitionDefinitionException;
import org.ggp.base.util.statemachine.implementation.prover.ProverStateMachine;

public final class SimulationValidator implements Validator
{
	private final int maxDepth;
	private final int numSimulations;

	public SimulationValidator(int maxDepth, int numSimulations)
	{
		this.maxDepth = maxDepth;
		this.numSimulations = numSimulations;
	}

	@Override
	public void checkValidity(Game theGame) throws ValidatorException {
		for (int i = 0; i < numSimulations; i++)
		{
			StateMachine stateMachine = new ProverStateMachine();
			stateMachine.initialize(theGame.getRules());

			MachineState state = stateMachine.getInitialState();
			for (int depth = 0; !stateMachine.isTerminal(state); depth++)
			{
				if (depth == maxDepth) {
					throw new ValidatorException("Hit max depth while simulating: " + maxDepth);
				}

				try {
					state = stateMachine.getRandomNextState(state);
				} catch (MoveDefinitionException mde) {
					throw new ValidatorException("Could not find legal moves while simulating: " + mde);
				} catch (TransitionDefinitionException tde) {
					throw new ValidatorException("Could not find transition definition while simulating: " + tde);
				}
			}

		}
	}
}

@@ -166,16 +166,90 @@
 achine;%0A
+import org.ggp.base.util.statemachine.exceptions.GoalDefinitionException;%0A
 import o
@@ -306,32 +306,32 @@
 itionException;%0A
-
 import org.ggp.b
@@ -861,19 +861,17 @@
 ns; i++)
-%0A%09%09
+ 
 %7B%0A%09%09%09Sta
@@ -1090,20 +1090,17 @@
 depth++)
-%0A%09%09%09
+ 
 %7B%0A%09%09%09%09if
@@ -1210,17 +1210,16 @@
 ;%0A%09%09%09%09%7D%0A
-%0A
 %09%09%09%09try 
@@ -1569,16 +1569,190 @@
 %7D%0A%09%09%09%7D%0A%0A
+%09%09%09try %7B%0A%09%09%09%09stateMachine.getGoals(state);%0A%09%09%09%7D catch (GoalDefinitionException gde) %7B%0A%09%09%09%09throw new ValidatorException(%22Could not find goals while simulating: %22 + gde);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D

Check for goal values in terminal states when doing simulation-based validation.