Link to home
Start Free TrialLog in
Avatar of maxul
maxul

asked on

How to wirte a craps program in C++ using abstract data types?

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.
Avatar of Infinity08
Infinity08
Flag of Belgium image

And what's the question ?

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

ASKER

I am not asking for a full implementation. I would just like to get help started with the program. Like which classes and I need and which functions they should contain.  That would be very helpful.
C doesn't have classes.
Is this for C or C++ ?
Avatar of maxul

ASKER

This is for C++.  
As I see it, you need the following:

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 model the game, dice, a roll, etc.
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.
Avatar of maxul

ASKER

thanks for the help guys.  I am going to get started on writing some code and will post back when I run into any problems.
For maximum exposure on subsequent C++ questions you may want to consider posting to the C++ zone.

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

ASKER

Sorry I could not find a zone C++ when I put the question up and I dont know how to change it.
>> and I dont know how to change it.

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 :)
Cool beans, didn't realise you could change zones!
So, maxul ... How far did you get with this ? Maybe you can give a status report for the newcomers, and tell us what you still need ?
Avatar of maxul

ASKER

hey guys i have been working on this for quite a bit now. using various help from other question posted on the site i have been able to write some code.  i have yet to compile as i am still working on the implementation of my code.  I was hoping you guys could take a look at it and help me fix some of the functions in my CrapsGame class and look at it to see if I am on the right track.  I will attach code snippets since I can not upload .cpp and .h files.

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

Open in new window

>> and help me fix some of the functions in my CrapsGame class

What specific problems do you have ?
Avatar of maxul

ASKER

In my code I have left functions empty because I do not how to implement them:
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.
>> void Reset()//set wins ad number of rolls to 0

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 ...
Avatar of maxul

ASKER

Okay for reset do i need to take any input such as the old Wins and Number of rolls and then return the new WIns and NumOfRolls?

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?
>> Okay for reset do i need to take any input such as the old Wins and Number of rolls and then return the new WIns and NumOfRolls?

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.
Avatar of maxul

ASKER

Okay. So how would I make the getters return the right values. I am not sure whether I am supposed to use counters here or not. Could you show me an example?
>> So how would I make the getters return the right values.

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'.
Avatar of maxul

ASKER

Thanks Infinity08 i will get to working on my code and be back with more questions but for now i am calling it a night.
Ok. Good luck :) And I'll be here when you have further questions. Do not hesitate to post your progress here, so I can have a look at it.
Avatar of maxul

ASKER

hey guys. I have completed most of my program using the tips i have gotten here. But now I am stuck at compiling.  I think there is something wrong with my makefile as the compiler tells me the function in my classes do not exist.  Also I can no upload them since they are .h and .cpp files.  What do i need to change the extensions to so you guys can take a look. Sorry i do all my programming on a unix based machine.
You can either paste the code in here (using "attach code snippet"), or you can put all files in a zip file and attach it, or you can rename the file extensions to .txt for example.
Avatar of maxul

ASKER

Alright here it is. The files that have header in their names were originally .h files and the .txt file were .cpp files.  Hope it doesnt cause any trouble for you.
craps.zip
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maxul

ASKER

Thanks Infinity08 for all the help. I was finally able to get my code to work using all the tips and feedback  you gave me.  Also thanks to the mod for changing the zone for my question.  This site really does help a lot. Thanks guys.
Avatar of maxul

ASKER

You helped me a lot by giving good tips that I could understand and follow up on.  You did not give me the answer which is good because I would not learn how to program if you had done so. Thanks.
I'm glad you got it :) Feels good, doesn't it ;)