/* Solution to Homework 13 by Craig Persiko - CS 111A
   Dice Runs (Extra Credit) program that simulates the rolling of dice, and displays them, marking runs
*/

import java.util.Scanner;

class DiceRunsSoln
{
  static Scanner keyboard = new Scanner(System.in);

  public static void main(String[] args)
  {
    int[] dieTosses;
    int numDice;
    
    System.out.println("This program will roll dice for you.\nHow many dice do you want me to roll?");
    numDice = keyboard.nextInt();
    
    dieTosses = rollDice(numDice);
    
    System.out.println("Here are the die values, with the runs in parentheses:");
    printDiceWithRunsMarked(dieTosses);
  }
  
  // Create the array of dice throws, and generate them randomly. Then return the array.
  public static int[] rollDice(int numDice)
  {
    int[] dice = new int[numDice];
    
    for(int i=0; i<numDice; i++)
      dice[i] = 1 + (int)(Math.random() * 6);
    
    return dice;
  }

  // Display dice values, with parenthesis around runs.
  public static void printDiceWithRunsMarked(int[] values)
  {
    boolean inRun = false;
    
    for(int i=0; i<values.length; i++)
    {
      if(inRun)
      {
        if(values[i] != values[i-1])
        {
          System.out.print(") ");
          inRun = false;
        }
        else
          System.out.print(" ");
      }
      if(!inRun)  // don't use else, because we might have just ended the run above
      {
        System.out.print(" ");
        if(i < values.length-1 && values[i] == values[i+1])
        {
          System.out.print("(");
          inRun = true;
        }
      }
      System.out.print(values[i]);
    }
    if (inRun)
      System.out.print(")");

    System.out.println();
  }
  
}
        
/* Sample Output:

[cpersiko@fog cs111a]$ java DiceRuns
This program will roll dice for you.
How many dice do you want me to roll?
10
Here are the die values, with the runs in parentheses:
 2 6 4 3 2 5 (1 1) 5 6
[cpersiko@fog cs111a]$ java DiceRuns
This program will roll dice for you.
How many dice do you want me to roll?
5
Here are the die values, with the runs in parentheses:
 5 1 2 (4 4)
[cpersiko@fog cs111a]$ java DiceRuns
This program will roll dice for you.
How many dice do you want me to roll?
20
Here are the die values, with the runs in parentheses:
 2 1 3 5 1 3 (1 1) 6 6 1 5 (4 4) 3 5 6 1 3 2
[cpersiko@fog cs111a]$ java DiceRuns
This program will roll dice for you.
How many dice do you want me to roll?
20
Here are the die values, with the runs in parentheses:
 1 2 5 1 5 3 5 6 4 (3 3) 6 (1 1) 5 (2 2) 3 (2 2)
[cpersiko@fog cs111a]$ java DiceRuns
This program will roll dice for you.
How many dice do you want me to roll?
25
Here are the die values, with the runs in parentheses:
 3 5 (6 6 6 6) 5 (5 5 5) 1 4 (2 2) 5 1 2 6 4 2 4 6 4 2 1
[cpersiko@fog cs111a]$ java DiceRunsSoln
This program will roll dice for you.
How many dice do you want me to roll?
40
Here are the die values, with the runs in parentheses:
 2 4 6 4 6 5 6 (5 5)  1 2 1 (2 2 2)  6 1 3 1 3 (1 1)  (5 5)  1 3 5 3 1 2 3 5 3 4 6 (5 5)  2 5 3
[cpersiko@fog cs111a]$

*/



syntax highlighted by Code2HTML, v. 0.9