Programming in C Game of Craps PR01 The game of craps is often said to be the “f
ID: 3795111 • Letter: P
Question
Programming in C Game of Craps
PR01
The game of craps is often said to be the “fairest” casino game of pure chance (meaning that
there is no player strategy involved) in that the house has the smallest advantage over the
player. What is that advantage? To answer this question we need to first define, precisely, what
we mean by “advantage”. The house advantage is simply the fraction of bets placed that will go
to the house, on average.
To estimate the house advantage for craps perform a Monte Carlo simulation of the game for
many millions of games, keeping track of the total amount bet and the total amount collected
by the house.
The rules of craps are very simple (note that we are not considering “side bets”). A player
places a wager and they will either lose the game (and their wager) or they will win the game
(and get both their wager and an equal payout from the house). Each game consists of a
number of throws of two fair six-sided dice (with sides equal to {1,2,3,4,5,6}. On each roll the
sum of the two dice is calculated. On the first roll, if a player rolls a 7 or an 11 they win
immediately. If the first roll is 2, 3, or 12 they lose immediately. Any other result establishes the
player’s “point” for that game. They then continue rolling the dice until they either roll their
point again (and win) or roll a 7 (and lose).
Write a predicate function that plays a single game of craps and returns TRUE if the player wins
and FALSE if the player loses. On each game place a random bet ranging from $1 to $1000
(whole dollar increments is fine). Collect data not only on the total amount wagered and the
total (net) amount taken by the house, but also aggregate data on how long games last and
their outcome. The end result should be output similar to the following (fake data). Note that
the percentages in parens on each line are the percentage of games that lasted that length, not
the fraction of total games played. The last column is the percentage of all games that lasted
that number of rolls.
GAMES PLAYED:........ 1000000
LONGEST GAME:........ 31 rolls
HOUSE ADVANTAGE:..... 1.734%
ROLLS WON LOST % OF GAMES
1 222222 (66.667%) 111111 (33.333%) 33.333
2 22222 ( 2.222%) 11111 ( 1.111%) 17.234
3 2222 ( 0.222%) 11111 ( 1.111%) 8.645
4 222 ( 0.022%) 1111 ( 0.111%) 0.935
...
20 22 ( 0.002%) 1 ( 0.000%) 0.006
>20 2222 ( 0.222%) 111 ( 0.011%) 0.521
PR02
Take a slightly different look at the game of craps by tabulating the odds of winning (the
fraction of the time that the player wins) for each possible mark value. This table should look
something like:
GAMES PLAYED:........ 1000000
FIRST ROLL WIN:...... 22.222%
FIRST ROLL LOSS:..... 11.111%
POINT WON LOST
4 222222 (22.222%) 111111 (11.111%)
5 22222 (22.222%) 111111 (11.111%)
6 2222 (22.222%) 111111 (11.111%)
8 26 (13.222%) 173 (86.778%)
9 222222 (22.222%) 111111 (11.111%)
10 222222 (22.222%) 111111 (11.111%)
Again, note that the numbers above are just effectively random placeholder values.
The percentages for the first-roll figures should be as a fraction of all games played. The
percentages for the values in the table should be as a fraction of all games that used that row’s
point value. The idea is for the player to know that IF their point is 8, then they have a 13%
change of winning that game – so the percentages on each row should sum to 100%.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
enum Status{ CONTINUE, WON, LOST};
int rollDice(void);
int winQ(void);
int loseQ(void);
int craps(int win[], int lose[]);
int rollDice(void)
{
int die1;
int die2;
int workS;
die1 = 1 + (rand() % 6);
die2 = 1 + (rand() % 6);
workS = die1 + die2;
return workS;
}
int craps(int win[], int lose[])
{
int sum = 0;
int Point = 0;
enum Status game;
int tries = 1;
sum = rollDice();
switch(sum)
{
case 7:
case 11:
game = WON;
break;
case 2:
case 3:
case 12:
game = LOST;
break;
default:
game = CONTINUE;
Point = sum;
break;
}
for(; game == CONTINUE; tries++)
{
sum = rollDice();
if(sum == Point)
{
game = WON;
}
else
{
if(sum == 7)
{
game = LOST;
}
if(tries > 20)
tries = 21;
}
}
if(game == WON)
{
++win[tries - 1];
}//end if
else
{
++lose[tries -1];
}//end if
// }//end while
return 0;
}
int main(void)
{
srand(time(NULL));
long repetitions;
for (repetitions = 100; repetitions <= 100000; repetitions *= 10)
{
int i;
int time_a;
int time_b;
int Win_f[21] = { 0 };
int lose_f[21] = { 0 };
int wins = 0;
int losses =0;
time_a = time(NULL);
for (i = 1; i <= repetitions; i++)
{
craps(Win_f, lose_f);
time_b = time(NULL);
}//end for
for (i = 0; i <= 20; i++)
{
wins += Win_f[i];
losses += lose_f[i];
}
printf("Number of repetitions: %ld ", repetitions);
printf("Point won lost ");
for (i = 0; i <= 19; i++)
{
printf("%3d %6d %6d ", i + 1, Win_f[i], lose_f[i]);
}//end for
printf("21+ %6d %6d ", Win_f[20], lose_f[20]);
printf("Won games: %lf% Lost games: %lf% ", ((double) wins) / repetitions * 100, ((double) losses) / repetitions * 100);
printf(" ");
}
return 0
}
Output:
} /
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.