1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <iostream>
#include <cstdlib>
using namespace std;
int simulateGames (int games);
int main ()
{
int games;
char choice;
double probability;
cout << "Welcome to Craps Pass Line Simulator. This application inputs a number of games and tells "
<< "\nyou the number of the times you won, the number of times you loss, and your probability of winning. "
<< endl;
do
{
cout << "How many games would you like to simulate? " << endl;
cin >> games;
probability = simulateGames (games);
cout << "Your probability of winning is about " << probability << "%. " << endl;
cout << "Would you like to simulate more games? (y/n)" << endl;
cin >> choice;
} while (choice == 'y' || choice == 'Y');
cout << "Thanks for using Craps Pass Line Simulator! " << endl;
}
//Simulates a specific number of games and returns the probability of winning
int simulateGames (int games)
{
int wins = 0, losses = 0, roll[2], point, total = 0;
double probability = 0;
bool won = false;
//Loop through the specific number of games
for (int i = 0; i < games; i++)
{
//Roll the two die
roll[0] = (rand() % 6) + 1;
roll[1] = (rand() % 6) + 1;
//If the two rolls are either 7 or 11, you win
if (roll[0] + roll[1] == 7 || roll[0] + roll[1] == 11)
wins++;
//If the two rolls are 2, 3, or 12, you lose
else if (roll[0] + roll[1] == 2 || roll[0] + roll[1] == 3 || roll[0] + roll[1] == 12)
losses++;
//If it is not 2, 3, 4, 7, 11, 12, do this:
else
{
//The "point" is the next two rolls. You must roll a point to win.
point = roll[0] + roll[1];
//Loop through until the total equals the point or 7
while (total != point)
{
//The total of the two die
total = roll[0] + roll[1];
//If the total is the point, you win
if (total == point)
{
won = true;
continue;
}
//If the total is 7, you lose
else if (total == 7)
{
won = false;
break;
}
}
//If you won, increase the number of wins
if (won)
wins++;
//If not, increase the number of losses
else if (!won)
losses++;
}
}
cout << "You won: " << wins << " times. " << endl;
cout << "You loss: " << losses << " times. " << endl;
//Calculate probability; THIS SEEMS NOT TO BE WORKING
probability = wins/games;
return probability;
}
|