// Racquetball Simulation

// Programmer:	Gregory A. Perkins
// Class:		CS 66
// Due:			20 February 1997

// This program accepts the probabilities that a person serving will win the
// serve for two players and the number of games to play.  It then simulates
// the number of games played and outputs the results.  The program is used to
// determine whether a player should work on offense or on defense.

// File: rball.cc -- Group code for a raquetball simulation (CS 66/ Spring 97).
// Collated and slightly edited by JMZ.
// Date: 2/16/97

//========================>  MODIFICATIONS TO CODE <=============================
//  Boolean declaration - removed (commented out) to prevent declaration errors
//	getInputs - 1> changed formatting of output, included some "\n"
//              2> two nested while loops changed from "&&" to "||" to correct logic
//				3> external while loops changed from "=" to "==" to correct logic
//  printSummary - 1> add if statement to prevent printing if no games were played
//                 2> add if statements to adjust for error in printing percentage
//						of shutouts if player did not win ANY games
//===============================================================================

#include 
#include 
#include 
#include 
#include 
#include "Racquetball.h"

//===============================> MAIN <========================================
// Your main goes here.
int
main()
{
  srand(clock());		// seed the random number generator
  
  float probA;
  float probB;
  int numGames;
  
  getInputs(probA, probB, numGames);
  
  int scoreA;
  int scoreB;
  int winsA = 0;
  int winsB = 0;
  int shutsA = 0;
  int shutsB = 0;
  
  for (int i = 1; i <= numGames; i++)
  {
  	playGame(probA, probB, scoreA, scoreB);
  	updateStats(scoreA, scoreB, winsA, winsB, shutsA, shutsB);
  }
  
  printSummary(shutsA, shutsB, winsA, winsB, probA, probB);
  
  return (0);
}

//===============================> FUNCTION DEFINITIONS <=========================

void getInputs(/*out*/ float& aProb,
                       float& bProb,
                       int& num_of_games)
{
  cout<<"You will now be prompted for the probabilities and number of\n"
      <<"games to be played. Please enter probabilities as decimals\n"
      <<"between 0 and 1, inclusive. Note that only one probability\n"
      <<"can be 0, not both. Please enter the number of games to be\n"
      <<"played as an integer greater or equal to zero."
      <> aProb;
          cout<1));
      
      do
        {
          cout<<"Enter the probability for Player B.\n"
              <<"Please remeber that it must be between 0 and 1\n"
              <<"inclusive and that only one probability can be 0\n"
              <<"Enter probability: ";
          cin>> bProb;
          cout<1));
      
    }
  while((aProb==0)&&(bProb==0));
  
  do
    {
      cout<<"Please enter the number of games to play: ";
      cin>>num_of_games;
      cout< doRally <=============================
// DoRally Function
// Group 6
// Mike Schumacher, Mike Kramer, Yasima Said, Sreshta W., Han Nguyen

void doRally(float serverProb, 
             int& serverScore,
             Boolean& aServing)
{
  if (serverWins(serverProb))
    serverScore++;        		//Server's score incremented if they win
  else
      aServing = !aServing;    	//To change server if server loses
}


Boolean serverWins(/* in */ float prob)
{ 
  return Boolean(rand() <= prob * RAND_MAX);
}


Boolean gameOver(/* in */ int aScore, int bScore)
{
  Boolean temp;
  temp = ((aScore > 20) || (bScore > 20)) || ((aScore == 11) &&
                                              (bScore ==0)) || ((bScore == 11) && (aScore == 0));
  return temp;
}

void 
updateStats ( /* in */   
             int aScore,
             int bScore,
             /* inout */ 
             int& aWins,
             int& bWins,
             int& aShuts,
             int& bShuts)
// This function accepts both players' scores and updates the
// appropriate stats for the winner of the game.  Any questions
// related to logic or any errors in the function should be
// e-mailed to gp7621s@acad.drake.edu.
{
  if (aScore > bScore)    	// if player A wins
    {
      aWins++;    			// increment # of wins by one
      
      if (bScore == 0)  	// if it was a shutout
        aShuts++; 			// increment # of shutouts by one
    }
  else        				// if player B wins
    {
      bWins++;    			// increment # of wins by one
      
      if (aScore == 0)  	// if it was a shutout
        bShuts++; 			// increment # of shutouts by one
    }

  return;
}

void printSummary (/* in */ 
                   int aShutout,
                   int bShutout,
                   int aWin,
                   int bWin,
                   float aProb,
                   float bProb)
{
	if (aWin + bWin > 0)			//only print summary if some games were actually played
	{
		  cout  << "A won "      	//output num of a wins
		        << aWin
		        << " game";
		  
		  if (aWin != 1)      		//output an 's' only if != 1
		    cout  << "s";
		  
		  cout  << "."
		        << endl;
		  
		  cout  << "B won "      	// output num of b wins
		        << bWin
		        << " game";  
		  
		  if (bWin != 1)         	//output an 's' only if != 1
		    cout << "s";
		  
		  cout  << "."  
		        << endl;
		  cout  << "A had "   		//output num of a shutouts
		        << aShutout
		        << " shutout";
		  
		  if (aShutout != 1)
		    cout  << "s";         	//output an 's' only if != 1
		  
		  cout  << "."
		        << endl;  
		  
		  cout  << "B had "        	//output num of b shutouts
		        << bShutout
		        << " shutout";
		  
		  if (bShutout != 1)
		    cout  << "s";        	//output an 's' only if != 1
		  
		  cout  << "."
		        << endl;
		  
		  cout  << "______________________________________________________________________"
		        << endl;
		  cout  << endl;
		  
		  cout  << "A's probability of winning was "  //output a percentages
		        << aProb * 100
		        << "%."
		        << endl
		        << "Player A actually won "
		        << setprecision (3)
		        << float (aWin) / (aWin + bWin) * 100
		        << "% of games played.  "
		        << endl;
		  
		  if (aWin > 0)								 // if player actually won some games
		  	cout  << setprecision (3)				 // print player's percentage of shutouts
		  	      << float (aShutout) / (aWin) * 100
		  	 	  << "% of A's wins were shutouts." 
		  	 	  << endl;
		  
		  cout  << endl;
		  
		  cout  << "B's probability of winning was "  //output b percentages
		        << bProb * 100
		        << "%."
		        << endl
		        << "Player B actually won "
		        << setprecision (3)
		        << float (bWin) / (aWin + bWin) * 100
		        << "% of games played."
		        << endl;
		  
		  if (bWin > 0)								 // if player actually won some games
		  	cout  << setprecision (3)				 // print player's percentage of shutouts
		          << float (bShutout) / (bWin) * 100
		          << "% of B's wins were shutouts." 
		          << endl;  
		  
		  cout  << endl;
	}
	else
		  cout  << "______________________________________________________________________"
		  		<< endl
		  	 	<< "************************ NO GAMES WERE PLAYED ************************"
		  	 	<< endl;
}