// Arup Guha
// 8/15/2019
// Solution for 2018 AP FR Question #1: Frog Simulation

import java.util.*;

public class FrogSimulation {

	// I added this to implement hopDistance.
	private static Random r = new Random();
	
	// I added this so you can see each hop (or not...)
	// When you write your simulate method, just do if (DEBUG) System.out.println(...)
	// for each hop.
	private static boolean DEBUG = true;
	
	private int goalDistance;
	private int maxHops;
	
	public FrogSimulation(int dist, int numHops) {
		goalDistance = dist;
		maxHops = numHops;
	}
	
	// I made this randomly, so the distance hopped ranges from -5 to 14,
	// inclusive, each value equally likely. Avg jump = (14-5)/2 = 4.5
	private int hopDistance() {
		return r.nextInt(20) - 5;
	}
	
	public boolean simulate() {
		
		// We start at 0.
		int curPos = 0;
		
		// Try our hops.
		for (int i=0; i<maxHops; i++) {
			
			// Hop...
			int hop = hopDistance();
			if (DEBUG) System.out.print(hop+" ");
			
			// Update new position.
			curPos += hop;
			
			// Return true if we made it.
			if (curPos >= goalDistance) {
				if (DEBUG) System.out.println("made it");
				return true;
			}
		}
		/*** Without the debug code, it's a lot shorter:
		for (int i=0; i<maxHops; i++) {
			curPos += hopDistance();
			if (curPos >= goalDistance) return true;
		}
		***/
		if (DEBUG) System.out.println("oops");
		
		// If we get here, we never made it.
		return false;
	}
	
	public double runSimulations(int num) {
		int numSuccess = 0;
		
		// Simulate num times, counting each success.
		for (int i=0; i<num; i++)
			if (simulate())
				numSuccess++;
			
		// This is our corresponding probability, we do 1.0 to force double calculation.
		return 1.0*numSuccess/num;
	}
	
	// A little test.
	public static void main(String[] args) {
		
		// For my test here with printing, I just did 10 simulations...
		FrogSimulation kermit = new FrogSimulation(24, 5);
		double success = kermit.runSimulations(10);
		System.out.printf("Prob of success = %.3f\n", success);
	}
}