Note that asking a full implementation is bit too much ;) And is not allowed if this is an assignment (which I assume it is).

Solved

Posted on 2009-02-11

Craps is a dice game. To play a game, all players first place their bets on the table.

A player then rolls the dice (perhaps several times to determine the outcome) and

the bets are collected or paid depending on the outcome of the dice rolls. A game

can be won or lost on the first roll (if specific numbers come up), or several rolls

may be necessary. The rules of the game are simple:

" On the first roll,

o If the roll is seven (7) or eleven (11), the game is won.

o If the roll is two (2), three (3) or twelve (12), the game is lost.

o If the roll is any other number, that number becomes ``the point''

and further rolls are necessary to determine the outcome.

" After the point has been determined, subsequent rolls determine the

outcome of the game. For each roll

o If the roll is a seven (7), the game is lost.

o If the roll duplicates the point, the game is won.

o If the roll is any other number, the dice must be rolled again in an

attempt to determine the outcome.

It may take several rolls of the dice to determine the outcome of a game. The

longer it takes to duplicate the point, the more money is on the table.

A player is allowed to bet any amount before each roll of the dice. In this project,

we will assume a simplified betting system where the player can only bet on a win or

loss of the game. If a win, the house returns to the player the total bet and an

additional amount equal to the total bet. If a loss, the house keeps the players bet.

I need to simulate the playing of this game. Over a large number of games, gather statistics

that can be used to answer the following questions:

1. What is the approximate probability of winning this game?

2. What is the approximate probability that a player will win the game on the

first roll of the dice?

3. What is the approximate probability that a player will lose the game on the

first roll of the dice?

4. Assuming that the player starts with $10,000, and bets one dollar before

each roll (i.e, the strategy shown in the table above), plot the decrease or

increase of the player's money over time.

5. Suppose a player initially bets one dollar, but doubles his bet each time

before the dice is rolled until a win or loss is obtained. Again assume that

the player starts with $10,000, and plot the decrease or increase of the

player's money over time.

6. Plot a histogram of the dice rolls over the course of your simulation to

insure that your dice simulation acts correctly (histogram appropriately

labeled, of course).

For the random dice roll, you should use rand and srand.

A player then rolls the dice (perhaps several times to determine the outcome) and

the bets are collected or paid depending on the outcome of the dice rolls. A game

can be won or lost on the first roll (if specific numbers come up), or several rolls

may be necessary. The rules of the game are simple:

" On the first roll,

o If the roll is seven (7) or eleven (11), the game is won.

o If the roll is two (2), three (3) or twelve (12), the game is lost.

o If the roll is any other number, that number becomes ``the point''

and further rolls are necessary to determine the outcome.

" After the point has been determined, subsequent rolls determine the

outcome of the game. For each roll

o If the roll is a seven (7), the game is lost.

o If the roll duplicates the point, the game is won.

o If the roll is any other number, the dice must be rolled again in an

attempt to determine the outcome.

It may take several rolls of the dice to determine the outcome of a game. The

longer it takes to duplicate the point, the more money is on the table.

A player is allowed to bet any amount before each roll of the dice. In this project,

we will assume a simplified betting system where the player can only bet on a win or

loss of the game. If a win, the house returns to the player the total bet and an

additional amount equal to the total bet. If a loss, the house keeps the players bet.

I need to simulate the playing of this game. Over a large number of games, gather statistics

that can be used to answer the following questions:

1. What is the approximate probability of winning this game?

2. What is the approximate probability that a player will win the game on the

first roll of the dice?

3. What is the approximate probability that a player will lose the game on the

first roll of the dice?

4. Assuming that the player starts with $10,000, and bets one dollar before

each roll (i.e, the strategy shown in the table above), plot the decrease or

increase of the player's money over time.

5. Suppose a player initially bets one dollar, but doubles his bet each time

before the dice is rolled until a win or loss is obtained. Again assume that

the player starts with $10,000, and plot the decrease or increase of the

player's money over time.

6. Plot a histogram of the dice rolls over the course of your simulation to

insure that your dice simulation acts correctly (histogram appropriately

labeled, of course).

For the random dice roll, you should use rand and srand.

29 Comments

Note that asking a full implementation is bit too much ;) And is not allowed if this is an assignment (which I assume it is).

A dice roll generator

And then a state machine or similar to:

Roll the dice

Check for the win

As you've probably already seen, some of the stats are self evident to calculate (win/loss probability on first roll).

As for checking the dice roll generator works OK, you just need to demonstrate that over a given large number of iterations that the instances of each number are broadly even.

Hope this helps.

You'll have to add code for the game rules, and to decide whether the game is won or lost, etc.

You'll have to add code for gathering statistics.

Start small, and simply write some code that rolls a die (using rand() for a random roll). When that works, you can write code that rolls two dice. Next, add the game logic. And finally, add the statistics gathering.

Although a lot of C++ programmers may check this C zone, not all C programmers will know C++.

You can click the "Request Attention" link, and ask to replace the C zone with the C++ zone. A moderator will then take care of that :)

maxul

```
//This is my crapsGame interface
#ifndef _CRAPS_GAME_H
#define _CRAPS_GAME_H
#include <iostream>
namespace AM {
class CrapsGame {
private:
/* A pair of dice */
dice _PairOfDice;
/* A Win equals 1, 0 otherwise */
int Win;
/* The number of rolls in the game */
int NumRolls;
public:
/* A constructor that creates a craps game and sets Win and NumRolls to 0 */
CrapsGame();
/* Sets Win and NumRolls back to 0 */
void Reset();
/* Play a game of craps */
void PlayGame();
/* Return the number of wins */
int GetWins();
/* Return the Number of NumRolls */
int GetNumRolls();
} /* Class CrapsGame */
} /* End Namespace AM */
#endif
//This is my crapsGame implementation
#include <iostream>
#include "crapsGame.h"
namespace AM {
CrapsGame :: CrapsGame()
{
Win = 0;
NumRolls = 0;
}
//How would i reset my wins and number of rolls
void CrapsGame :: Reset()
{
}
void CrapsGame :: PlayGame()
{
enum Status { CONTINUE, WIN, LOSE };
int sum, myPoint, wins = 0, loss = 0;
float bet, totalMoney;
totalMoney = 1000;
bet = 1;
Status gameStatus;
do
{
srand(time(NULL));
sum = RollDice();
switch( sum )
{
case 7:
case 11:
gameStatus = WIN;
break;
case 2:
case 3:
case 12:
gameStatus = LOSE;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout << "Point is " << myPoint << endl;
break;
}//switch
while( gameStatus == CONTINUE )
{
sum = RollDice();
if ( sum == myPoint )
{
gameStatus = WIN;
}//if
else if ( sum == 7 || sum == 11)
gameStatus = LOSE;
}//while
if ( gameStatus == WIN )
{
cout << "You won " << bet*2 << " dollars\n";
totalMoney = totalMoney + bet*2;
wins = wins + 1;
cout << "You have won " << wins << " many times.\n";
cout << "You have lost " << loss << " many times.\n";
cout << "You now have " << totalMoney << " dollars\n";
}//if
else
{
cout << "You lost " << bet << " dollars\n";
totalMoney = totalMoney - bet;
loss = loss + 1;
cout << "You have won " << wins << " many times.\n";
cout << "You have lost " << loss << " many times.\n";
cout << "You now have " << totalMoney << " dollars\n";
}
if (totalMoney == 0)
{
cout << "No Mony Left, Game Over... \n";
cout << "Your win/loss ratio is " << wins/loss;
exit(1);
}
} while (bet != 0);
}
//How do i get the wins to calculate in this function. Seperate it from my PlayGames function
int CrapsGame :: GetWins()
{
{
//How do i calculate the number of rolls
int CrapsGame :: GetNumOfRolls()
{
}
}//end namespace AM
```

void Reset()//set wins ad number of rolls to 0

int GetWins()//should get the number of wins

int GetNumOfRolls()//should get the number of rolls it takes for the player to run out of money

If you need more info please ask. I will be monitoring this frequently.

isn't this as simple as setting the wins and number of rolls to 0 ?

>> int GetWins()//should get the number of wins

Just return the number of wins ...

>> int GetNumOfRolls()//should get the number of rolls it takes for the player to run out of money

Just return the number of rolls ...

For GetWins and GetNumOfRoll, could i just leave them out and let the PlayGame function calculate them to make it simpler. I have already calculated the wins in the PlayGame function. All i would need to do is create a counter for the number of rolls and put it in my playGames function. Does this sound better or should I keep them as separated functions?

Since this is a method for the CrapsGame class, you are already working with a specific CrapsGame object. Every class method is linked to an instance of the class when it's called. For example, the reset can be called like this :

CrapsGame game; // <--- the instance

game.Reset(); // <--- the Reset() method is called for the 'game' object

which would reset the game.

So, you can simply re-set the values of the current object inside the method.

Note that the method doesn't return anything (void)

>> For GetWins and GetNumOfRoll, could i just leave them out and let the PlayGame function calculate them to make it simpler.

The GetWins and GetNumOfRoll methods are called getters - they are used to get the state of the object. For example, while the user could do :

int wins = game.GetWins();

at any time to know how many wins he had with the game. If those getters wouldn't be there, the user wouldn't have access to the values.

Note that the GetWins and GetNumOfRoll methods do not calculate anything. They simply return the values that were already calculated by the PlayGame method.

Remember that methods are always called for a specific object. If you use Wins inside the method, you'll use the Wins member of the object for which the method was called. So :

game.GetWins()

will return the wins for the object 'game'.

craps.zip

In crapsGame.cpp, you have this :

#include "crapsGameGame.h"

That file doesn't exist ... crapsGame.h exists though.

You also call :

player1.bet(currentbet);

But class Player doesn't have a method named 'bet'. It has a method named 'Bet' though (notice the uppercase first character). This same mistake is made in several locations : note that case is important in C++ : 'Bet' is not the same as 'bet'.

There are other typo's in your code. Use the compiler errors to find them all, and fix them. Make sure that if you want to call a method, you use the exact name of the method.

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

distributed computing in Python | 4 | 90 | |

Trying to understand report usage (standard reports, ad-hoc reports) in a BusinessObjects environment | 4 | 56 | |

How to split this in C++ | 4 | 48 | |

White board coding practice | 3 | 27 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**7** Experts available now in Live!