Output result in tabular form
Mar 17, 2015 at 2:39am UTC
Ok So I am doing an assignment(I will not lie about this..)
And Now I got stuck at the very last bit of it. So my program needs to run the craps game(roll dice.) And now the problem is I have no clue how to save the 1000 times games' result and output them in a tabular form.
This is the specific question.
How many games are won on the 1st roll, 2nd roll, …, 20th roll, and after the 20th roll?
How many games are lost on the 1st roll, 2nd roll, …, 20th roll, and after the 20th roll?
And my code is here.
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
unsigned int rollDice(); // function prototype
int main()
{
int selection;
cout << "Please make your selection: " << endl;
cout << "1). Play 1,000 craps games." << endl;
cout << "2). Play your own modified times of craps games." << endl;
cout << "3). Show answers to \n What are the chances of winning at craps?\n"
<< " Do the chances of winning improve with the length of the game?" << endl;
cout << "4). Exit." << endl;
cin >> selection;
if (selection == 1)
{
for (int x = 0; x < 1000; x++)
{
enum Status { CONTINUE, WON, LOST };
unsigned int myPoint = 0;
Status gameStatus = CONTINUE;
unsigned int sumOfDice = rollDice();
switch (sumOfDice)
{
case 7:
case 11:
gameStatus = WON;
break ;
case 2:
case 3:
case 12:
gameStatus = LOST;
break ;
default :
gameStatus = CONTINUE;
myPoint = sumOfDice;
cout << "Point is " << myPoint << endl;
break ;
}
while (CONTINUE == gameStatus)
{
sumOfDice = rollDice();
if (sumOfDice == myPoint)
gameStatus = WON;
else
if (sumOfDice == 7)
gameStatus = LOST;
}
if (WON == gameStatus)
cout << "Player Wins!!!" << endl;
else
cout << "Player Loses..." << endl;
}
}
if (selection == 2)
{
int fnum;
cout << "Choose the times that you want the game to be played: \n(Please input numbers which are greater than 500.)" << endl;
cin >> fnum;
for (int x = 0; x < fnum; x++)
{
enum Status { CONTINUE, WON, LOST };
unsigned int myPoint = 0;
Status gameStatus = CONTINUE;
unsigned int sumOfDice = rollDice();
switch (sumOfDice)
{
case 7:
case 11:
gameStatus = WON;
break ;
case 2:
case 3:
case 12:
gameStatus = LOST;
break ;
default :
gameStatus = CONTINUE;
myPoint = sumOfDice;
cout << "Point is " << myPoint << endl;
break ;
}
while (CONTINUE == gameStatus)
{
sumOfDice = rollDice();
if (sumOfDice == myPoint)
gameStatus = WON;
else
if (sumOfDice == 7)
gameStatus = LOST;
}
if (WON == gameStatus)
cout << "Player Wins!!!" << endl;
else
cout << "Player Loses..." << endl;
}
}
if (selection == 3)
{
int counter1 = 0;
int counter2 = 0;
for (int x = 0; x < 1000; x++)
{
enum Status { CONTINUE, WON, LOST };
unsigned int myPoint = 0;
Status gameStatus = CONTINUE;
unsigned int sumOfDice = rollDice();
switch (sumOfDice)
{
case 7:
case 11:
gameStatus = WON;
break ;
case 2:
case 3:
case 12:
gameStatus = LOST;
break ;
default :
gameStatus = CONTINUE;
myPoint = sumOfDice;
cout << "Point is " << myPoint << endl;
break ;
}
while (CONTINUE == gameStatus)
{
sumOfDice = rollDice();
if (sumOfDice == myPoint)
gameStatus = WON;
else
if (sumOfDice == 7)
gameStatus = LOST;
}
if (WON == gameStatus)
counter1++;
else
counter2++;
if (WON == gameStatus)
cout << "Player Wins!!!" << endl;
else
cout << "Player Loses..." << endl;
}
cout << "\nWinning times are " << counter1 << endl;
cout << "\nLosing times are " << counter2 << endl;
cout << "\nThus, the chances of winning this craps game based 1000 times results is " << counter1 / 10 << " percent." << endl;
}
if (selection = 4)
/*enum Status { CONTINUE, WON, LOST };
srand(static_cast<unsigned int>(time(0)));
unsigned int myPoint = 0;
Status gameStatus = CONTINUE;
unsigned int sumOfDice = rollDice();
switch (sumOfDice)
{
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
cout << "Point is " << myPoint << endl;
break;
}
while (CONTINUE == gameStatus)
{
sumOfDice = rollDice();
if (sumOfDice == myPoint)
gameStatus = WON;
else
if (sumOfDice == 7)
gameStatus = LOST;
}
if (WON == gameStatus)
cout << "Player Wins!!!" << endl;
else
cout << "Player Loses..." << endl;
*/
getchar();
getchar();
getchar();
}
unsigned int rollDice()
{
unsigned int die1 = 1 + rand() % 6;
unsigned int die2 = 1 + rand() % 6;
unsigned int sum = die1 + die2;
cout << "Players Rolled " << die1 << " + " << die2 << " = " << sum << endl;
return sum;
}
So how I can output the result in table? I appreciate any kind of help but just a sample of how to deal with this will be nice...
Mar 17, 2015 at 2:40am UTC
As you can see I am being lazy and copying the same process again and again... But I cannot figure out other ways to cope with them...So just bare with me...
Mar 17, 2015 at 4:00am UTC
Instead of repeating the code over and over and over again, you should do
1 2 3 4 5 6 7
int fnum = 1000;
if (selection == 2)
{
cout << "Choose the times that you want the game to be played: \n(Please input numbers which are greater than 500.)" << endl;
cin >> fnum;
}
//rest of your code for simulating the games
As for your question, I would keep track of the 2 stats using 2 arrays.
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
int winsOnRoll[21] = {0};
int lossesOnRoll[21] = {0};
//more code
int rolls = 1;
while (CONTINUE == gameStatus)
{
sumOfDice = rollDice();
++rolls;
if (sumOfDice == myPoint)
{
gameStatus = WON;
if (rolls < 21)
++winsOnRoll[rolls];
else
++winsOnRoll[0];
}
else if (sumOfDice == 7)
{
gameStatus = LOST;
if (rolls < 21)
++lossesOnRoll[rolls];
else
++lossesOnRoll[0];
}
}
//more code
I forgot to include the code for wins and losses on the first roll, but you get the picture. You would output the stats like this:
1 2 3 4 5 6 7 8
cout << "Wins on roll\n" ;
for (int i = 1; i < 21; ++i)
cout << i <<" = " << winsOnRoll[i] << endl;
cout << "21+ = " << winsOnRoll[0] << endl << endl;
cout << "Losses on roll\n" ;
for (int i = 1; i < 21; ++i)
cout << i <<" = " << lossesOnRoll[i] << endl;
cout << "21+ = " << lossesOnRoll[0] << endl;
Last edited on Mar 17, 2015 at 4:01am UTC
Mar 17, 2015 at 4:13am UTC
The first roll means the selection 1 right?
I will read through this and try to understand it.
Thanks bro!
Topic archived. No new replies allowed.