
/*
   This program simulates 100,000 games of craps and counts the
   number of games that were won.  The number of games that
   were won and the fraction of the games that were won are
   output.  
*/


public class crapstat2 {

   public static void main(String[] args) {

      int winCount = 0;   // Number of games that were won.
      int lossCount = 0;  // Number of games that were lost.  (This
                          //   information is not required, but it is
                          //   good to have it as a "sanity check"
                          //   to help make sure the program is working
                          //   properly.

      PairOfDice dice = new PairOfDice();  // The dice to be used in the game.

      System.out.println("This program is simulating 100000 games of craps...");

      for (int gameNumber = 0; gameNumber < 100000; gameNumber++) {
      
         int roll;        // Total roll that comes up on the pair of dice.
         int point;       // The point is the value of the first roll
                          //    of the dice, in the case where the
                          //    game is not won or lost on the first
                          //    roll. 
         
         roll = dice.roll();
         
         if (roll == 7 || roll == 11) {  // win on first roll
            winCount++;
         }
         else if (roll == 2 || roll == 3 || roll == 12) { // lose on first roll
            lossCount++;
         }
         else {  // keep rolling until 7 or point comes up
            point = roll; // The value of the first roll
            do {
               roll = dice.roll();
            } while (roll != 7 && roll != point);
            // At this point, we know that either roll == 7 or roll == point
            if (roll == point)
               winCount++;
            else
               lossCount++;
         }
         
      } // end for
      
      if (winCount + lossCount != 100000) // Sanity check!
         System.out.println("Whoops! Programming error: number of games counted is not 100000!");

      System.out.println("Out of 100000 games, " + winCount + " were won.");
      double fractionWon = winCount / 100000.0;
      System.out.println("The fraction of games that were won was " + fractionWon);

   } // end main()
   
   
} // end class crapstat

