/* This program calculates the average rolls of game before a gambler runs out of money.
 *  
 * 
 * Adam Tran
 * 10/20/08
 * 
 */

import java.util.*;

class Roller {
	
	/* This class creates a roller with money. The roller can roll until out of money, and number of rolls will be returned.
	 * The roller can also play a set number of games, and number of rolls taken per game will be averaged.
	 */
	
	//Instance variables
	
	private Random generator; //Generator object
	private int dollars; //Keeps track of how much money the gambler starts out with
	
	//Constructors
	
	public Roller(){
		this(100); //By default, start with 100 dollars
	}
	
	public Roller(int dollars){ //Build a roller with specified dollars
		this.dollars = dollars;
		generator = new Random(); //Assign generator
	}
	
	public void setDollars(int dollars){ //Accessor method for dollars
		this.dollars = dollars;
	}
	
	public int getDollars(){ //Mutator method for dollars
		return dollars;
	}
	
	/* This methods is a loop that rolls until out of money.
	 * A 7 returns 4 dollars and anything else takes one away.
	 */
	
	public int rollLoop(){ 
		int die1, die2;
		int count = 0; //Tracks number of rolls, starting at 0
		int dollars = this.dollars; //Assigns local dollars to specified starting dollars 
		
		/* Input assertions:
		 * dollars > 0
		 * count == 0
		 */
		
		while (dollars > 0){ //While money is left,
			count = count + 1; //Keeps track of rolls (invariant assertion)
			
			die1 = generator.nextInt(6) + 1; //Generate two random integers from 1-7
			die2 = generator.nextInt(6) + 1;
			
			if (die1 + die2 == 7) //If the sum is 7, award 4 dollars
				dollars += 4;
			else                  //Else take a dollar away
				dollars -= 1;
		}
		return count; //Return rolls to took to run out of money
		
		/* Output assertions:
		 * dollars == 0
		 * count is the number of rolls taken before all money is lost
		 */
		
	}
	
	/* This method is a loop that loops the roll loop a specified number times
	 * and averages how many rolls it takes to run out of money.
	 */
	
	public int loopALot(int timesToLoop){  
		int sumLoop = 0; //Sum of number of rolls, starts at 0
		
		/*Input assertions:
		 * timesToLoop > 0
		 */
		
		assert timesToLoop > 0;
		
		for (int numLoop = 1; numLoop <= timesToLoop; numLoop = numLoop + 1){
			/* numLoop will eventually exceed timesToLoop, and the loop will end
			 * (variant assertion)
			 */
			sumLoop = sumLoop + this.rollLoop();
			/* sumLoop is the sum of all rolls before the roller runs out of money 
			 * (invariant assertion)
			 */
			
			/* Output assertion:
			 * sumLoop is the sum of all rolls
			 */
		}
		return sumLoop / timesToLoop; //Return average number of rolls to run out of money
	}
	
	public String toString(){
		return "Dollars: " + dollars;
	}
	
}

//-------------------- End of Roller class --------------------//  

/* This is a tester class for roller. It gets starting money from the user.
 * 
 */

public class LuckySevens {
	public static void main (String [] args){
		
		assert false; //THIS LINE DOESN'T WORK WTF
		
		Scanner reader = new Scanner(System.in);
		
		final int timesToLoop = 100; //How many times to loop
		
		int dollars = 0;
		
		/* Input assertion:
		 * dollars == 0
		 */
		while (dollars <= 0){
			System.out.print("Enter starting money: ");
			dollars = reader.nextInt();
			if (dollars <= 0){
				System.out.println("Must be greater than 0 dollars\n");
			}
		}
		/* Output assertion:
		 * dollars > 0
		 */
		
		Roller roller = new Roller(dollars);
		System.out.println("Average rolls until all money is lost: " + roller.loopALot(timesToLoop));
		
		
	}
}

