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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int rollDice();
int oddWin(int, int);
int _tmain(int argc, _TCHAR* argv[])
{
cout << setprecision(2) << fixed;
enum Status{CONTINUE, WON, LOST};
int myPoint, gamesPlayed, sumOfDice, wsum = 0, lsum = 0, a = 0, i, k, l, m;
double avg, avgSum = 0.0;
const int rollFreq = 35;
static int wcount = 0, lcount = 0, roll = 0;
int wround[rollFreq] = { 0 };
int lround[rollFreq] = { 0 };
Status gameStatus;
srand(time(0));
for (gamesPlayed = 1; gamesPlayed <= 1000; gamesPlayed++)
{
roll++;
sumOfDice = rollDice();
switch (sumOfDice)
{
case 7:
case 11:
gameStatus = WON;
wcount++;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
lcount++;
break;
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
break;
}
while (gameStatus == CONTINUE)
{
sumOfDice = rollDice();
roll++;
if (sumOfDice == myPoint)
{
gameStatus = WON;
wcount++;
}
else
if (sumOfDice == 7)
{
gameStatus = LOST;
lcount++;
}
}
if (gameStatus == WON)
wround[roll]++;
if (gameStatus == LOST)
lround[roll]++;
roll = 0;
}
//WON GAMES
cout << "\nWins:" << endl;
for (k = 1; k < rollFreq - 15; k++)
cout << wround[k] << setw(13) << " games won on " << k << " rolls" << endl;
for (i = 20; i < rollFreq -1; i++)
wsum += wround[i];
cout << wround[i] << setw(13) << " games won on 20 or more rolls" << endl;
//LOST GAMES
cout << "\n\nLoses: " << endl;
for (m = 1; m < rollFreq-15; m++)
cout << lround[m] << setw(13) << " games lost on " << m << " rolls" << endl;
for (l = 20; l < rollFreq -1; l++)
lsum += lround[l];
cout << lround[l] << setw(13) << " games lost on 20 or more rolls\n\n" << endl;
//ODDS OF WINNING
oddWin(wcount, gamesPlayed);
//AVGWIN
//IM STUCK HERE
system("pause");
return 0;
}
int rollDice()
{
int die1 = 1 + rand() % 6;
int die2 = 1 + rand() % 6;
int sum = die1 + die2;
return sum;
}
int oddWin(int x, int y)
{
int c = 1000;
double odds;
for (; c > 0; c= c - 100)
{
odds = (x / (static_cast<double>(y / c)));
odds = (odds / c) * 100;
cout << "Odds of winning in " << c << " games = " << odds << "%" << endl;
}
return 0;
}
|