/* dice4.cpp simulates a given number of pairs of dice rolls,
 * and counts the number of times a specified number occurs.
 *
 * Input:  number of dice rolls, the number to be counted,
 *         and user's response to "More rolls?" query
 * Output: user prompts, and the relative frequency of 
 *         the number of spots
 ********************************************************************/

#include <iostream.h>                   // cout, cin, <<, >>
#include "RandomInt.h"                  // the type RandomInt

int main()
{
   cout 
      << "This program simulates a given number of dice-pair rolls,\n"
      << "counting the number of times a given roll occurs.\n";
        
   int numRolls;                        // number of rolls of dice
      
   cout << "\nHow many times are the dice to be rolled? ";
   cin >> numberOfRolls;

   RandomInt die1(1, 6),                // the first die
             die2(1, 6);                // the second die
   int pair,                            // the sum of the dice
       numberOfSpots,                   // number of spots to count
       occurrences;                     // the counter variable   
   


   for(;;)                              // input loop:
   {                                    //   get outcome to count
      cout << "Enter number of spots to count (2-12, 0 to stop): ";
      cin >> numberOfSpots;

      if (numberOfSpots <= 0) break;    //   terminate input loop

      occurrences = 0;                  //   set counter to zero
                                        //   for loop to repeat
      for (int rollCount = 1;           //     the following
           rollCount <= numRolls;       //     numRolls times:
           rollCount++)
      {                                 //      roll the dice
         pair = die1.Generate() + die2.Generate();
         
         if (pair == numberOfSpots)     //      if the number came up
            occurrences++;              //        increment the counter
      }                                 //   end for loop

                                        //   display the result
      cout << "The relative frequency of " << numberOfSpots
           << " was " << double(occurrences) / double(numRolls)
           << "\n\n";
   }                                    // end input loop

   return 0;
}


