import java.util.*;
import java.lang.String;

public class DiceRoll {

	/*
	 * Array practice
	 * 2007-09-03
	 * Philip Peng
	 * 
	 */
	public static void main(String[] args) {
		// Introduction
		System.out.println("==========================================\n" +
							"  Welcome to the dice rolling simulator!\n" +
							"==========================================\n");
		
		boolean continuePlaying = true;
		String reply;
		Scanner yes_no = new Scanner(System.in);
		int roundNum = 1;
		
		while (continuePlaying) {
			
			// Main game
			gameStart(roundNum);
			
			// Play again?
			roundNum++;
			System.out.print("Next round? ");
			reply = yes_no.nextLine();
			if (reply.equalsIgnoreCase("no") || reply.equals("n") || reply.equals("false") || reply.equals("0")) {
				continuePlaying = false;
			}
		System.out.println("");
		}
		
		System.out.println("==========================================\n" +
							"           Thanks for Playing!\n" +
							"==========================================\n");
	}
	
	public static void gameStart(int roundNum) {
		System.out.println("> Round " + roundNum + " <\n");
		
		// Variables
		// Initiate to 0 to prevent error
		int diceNum = 0, sideNum = 0, rollNum = 0;
		Scanner input = new Scanner(System.in);
		
		// Get info
		System.out.print("How many dice do you want to roll? ");
		
		boolean inputCheck = true;
		while (inputCheck){
			try {
				diceNum = input.nextInt();
				inputCheck = false;
			} catch (InputMismatchException e) {
				System.out.println("Sorry, please give an integer!");
			}	
		}

		System.out.print("How many sides do you want your dice to have? ");
		sideNum = input.nextInt();
		System.out.print("How many times do you want to roll? ");
		rollNum = input.nextInt();
		gameSimulation(diceNum, sideNum, rollNum);
	}
	
	public static void gameSimulation(int diceNum, int sideNum, int rollNum){
	
		// Variables
		Random rnd = new Random(5); // Seeded
		int[] cumulativeOutcomes = new int[diceNum*sideNum+1];
		
		// Simulation
		System.out.println("\nSimulating dice rolls ...\n");
		
		for (int roll = 1; roll <= rollNum; roll++) {
			int outcome = 0; // Start clean
			for (int dice = 1; dice <= diceNum; dice++) {
				outcome += (rnd.nextInt(sideNum) + 1);
			}
			cumulativeOutcomes[outcome] += 1;
		}
		
		// Show results
		System.out.println("Results:");
		int gameTotal = 0; // Start clean
		for (int i = diceNum; i <= sideNum*diceNum; i++) {
			if (cumulativeOutcomes[i] != 0){
				System.out.println(i + ": " + cumulativeOutcomes[i]);	
			}
			gameTotal += i * cumulativeOutcomes[i];
		}
		// Calculate statistics
		gameCalcAvg(gameTotal, rollNum);

	}
	
	public static void gameCalcAvg(int gameTotal, int rollNum) {
		
		float gameAverage = gameTotal / rollNum;
		System.out.println("\nGame Average: " + gameAverage + "\n");	
		
	}
	
}
