///File: craps
//Author: Jesse D
//Date: Feb. 26, 2007
//
//Description: This program simulates a game of Craps. Once the game has been
// completed, it counts the number of winners. Once the approriate
// number of games has been played, a probability for winning
// is calculated and printed to the screen.
//
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int roll(){
int a = rand()%6+1; // First dice roll
int b = rand()%6+1; // Second dice roll
return (a+b); //Return the roll value.
}
int main()
{
srand(time(0));
int winTotal = 0;
int loseTotal =0;
int firstRoll = 1; // Used to check if it the first roll of the game.
int count = 0;
int currentRoll = 0; // Value of the current roll
int point = 0; // Used for the point.
int check =0; //Used to check if a game has ended.
currentRoll = roll();
//Play 10000 games
while (count < 10000){
//chech if its the first roll of the game.
if (firstRoll == 1){
// Checks for winner
if ((currentRoll == 7)||(currentRoll == 11)){
winTotal +=1;
check+=1;
}
//Checks for a loser
if ((currentRoll == 2)||(currentRoll == 3)||(currentRoll == 12)){
check+=1;
loseTotal +=1;
}
//No Winner or loser on first roll.
point = currentRoll; // Set the point.
}
// Skip this part if its the first roll;
if (firstRoll == 0){
// check for a loser
if ((currentRoll) == 7){
loseTotal +=1;
check+=1;
}
//Check for a winner
if ((currentRoll) == point){
winTotal+=1;
check+=1;
}
}
firstRoll = 0; // First roll is done.
// If check is still zero, game has not been completed, roll again.
if (check < 1){
currentRoll = roll();
}
// If check is greater than zero, a game has completed.
if (check > 0){ //Reset the values and increment game count.
firstRoll = 1;
count++;
check = 0;
currentRoll = roll();
}
}
//Print the results.
cout << endl;
cout << "There are " << winTotal << " winners and " << loseTotal;
cout << " losers in " << count << " games" << endl;
cout << "The probability of a win based on this data is ";
cout << (winTotal * (1.0))/((winTotal +loseTotal) * (1.0)) >> endl;
return 0;
}