Question

In: Computer Science

Programming in C Game of Craps PR01 The game of craps is often said to be...

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%.

Solutions

Expert Solution

#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\n", repetitions);

  

      printf("Point\twon\tlost \n");

    

    

    for (i = 0; i <= 19; i++)

    {

      printf("%3d\t%6d\t%6d \n", i + 1, Win_f[i], lose_f[i]);

    }//end for

    

    printf("21+\t%6d\t%6d \n ", Win_f[20], lose_f[20]);

    printf("Won games: %lf%\nLost games: %lf%\n", ((double) wins) / repetitions * 100, ((double) losses) / repetitions * 100);

    printf("\n\n");

}

return 0

}

Output:

Number of repetitions: 100

Point   won     lost 
  1         27       5 
  2          8       9 
  3          8       8 
  4          6       6 
  5          3       4 
  6          2       3 
  7          1       1 
  8          0       1 
  9          2       0 
 10          1       0 
 11          0       1 
 12          0       2 
 13          0       0 
 14          0       2 
 15          0       0 
 16          0       0 
 17          0       0 
 18          0       0 
 19          0       0 
 20          0       0 
21+          0       0 
 Won games: 58.000000%
Lost games: 42.000000%

} /


Related Solutions

Write a C++ program to play the dice game "Craps". Craps is one of the most...
Write a C++ program to play the dice game "Craps". Craps is one of the most popular games of chance and is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the...
use c++ This question is about providing game logic for the game of craps we developed...
use c++ This question is about providing game logic for the game of craps we developed its shell in class. At the end of the game, you should ask the user if wants to play another game, if so, make it happen. Otherwise, quit **Provide the working output of this game and the picture of the output. ** This is an implementation of the famous 'Game of Chance' called 'craps'. It is a dice game. A player rolls 2 dice....
For this assignment, write a program that uses functions to simulate a game of Craps. Craps...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the player...
c++ Programming Assignment 1: Game of Life The objective of this programming assignment is to design...
c++ Programming Assignment 1: Game of Life The objective of this programming assignment is to design and implement what is known as the “Game of Life”, conceptualized by the British mathematician John Horton Conway in 1970 to simulate the evolution patterns in a population of living organisms.   The game board is seeded with an initial population pattern, and then evolves based on the set of rules defining when a cell dies or is born into life. A cell’s life cycle...
Consider the game of craps designed by Econ 261 Hotel students. The game consists of rolling...
Consider the game of craps designed by Econ 261 Hotel students. The game consists of rolling two fair six-sided dice. You win a dollar if the sum of the dots on the two dice is 2, 3, 4, or 5; if the sum of the dots on the two dice is 9, 10, 11, or 12 you lose a dollar. You win nothing, (that is you get $0) if the sum is 6, 7, or 8. The variance of X,...
2) This question is about providing game logic for the game of craps we developed its...
2) This question is about providing game logic for the game of craps we developed its shell in class. THe cpp of the class is attached to this project. At the end of the game, you should ask user if wants to play another game, if so, make it happen. Otherwise quit. Here is the attached cpp file for que2:- /*** This is an implementation of the famous 'Game of Chance' called 'craps'. It is a dice game. A player...
Craps is a dice game in which the players make wagers on the outcome of the...
Craps is a dice game in which the players make wagers on the outcome of the roll, or a series of rolls, of a pair of dice. Most outcomes depend on the sum of the up faces of two, fair, six-sided dice. 1) What is the lower class boundary of the "6" class? 2) Find the mean. 3) Find the standard deviation. 4) Find the z-score for snake eyes.
Overview For this assignment, write a program that uses functions to simulate a game of Craps....
Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the...
Use at least 50000 simulations to answer the following questions. In the game of craps, the...
Use at least 50000 simulations to answer the following questions. In the game of craps, the shooter rolls two dice and wins if the sum of the dice is 7 or 11 ("natural"); he loses if the sum is 2,3, or 12 (craps). If the sum is 4, 5, 6, 8, 9, or 10, then the result is not yet decided. He must roll the dice again and again, as often as is necessary until the initial sum, be it...
In the game of craps, people bet on the outcome of the throw of two, six-sided...
In the game of craps, people bet on the outcome of the throw of two, six-sided dice. Specifically, wagers are placed on the sum of the outcome on the upward facing number on each of the two dice. We are going to discuss relative likelihoods by counting the number of ways (the number of microstates) associated with specific sums (the macrostate). A. How many microstates are associated with a throw summing to 4? B. How many microstates are associated with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT