?
Solved

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

Posted on 2009-02-11
30
Medium Priority
?
2,336 Views
Last Modified: 2013-12-13
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.
0
Comment
Question by:maxul
  • 13
  • 13
  • 3
29 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 23609989
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).
0
 

Author Comment

by:maxul
ID: 23610014
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23610022
C doesn't have classes.
Is this for C or C++ ?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:maxul
ID: 23610030
This is for C++.  
0
 
LVL 4

Expert Comment

by:rd707
ID: 23610236
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23610335
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.
0
 

Author Comment

by:maxul
ID: 23614628
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.
0
 
LVL 4

Expert Comment

by:rd707
ID: 23620086
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++.
0
 

Author Comment

by:maxul
ID: 23620163
Sorry I could not find a zone C++ when I put the question up and I dont know how to change it.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23620332
>> 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 :)
0
 
LVL 4

Expert Comment

by:rd707
ID: 23630779
Cool beans, didn't realise you could change zones!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23631033
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 ?
0
 

Author Comment

by:maxul
ID: 23657466
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

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23657572
>> and help me fix some of the functions in my CrapsGame class

What specific problems do you have ?
0
 

Author Comment

by:maxul
ID: 23657632
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23657681
>> 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 ...
0
 

Author Comment

by:maxul
ID: 23657724
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?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23657803
>> 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.
0
 

Author Comment

by:maxul
ID: 23657825
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?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23657886
>> 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'.
0
 

Author Comment

by:maxul
ID: 23657939
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23657996
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.
0
 

Author Comment

by:maxul
ID: 23678884
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23679102
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.
0
 

Author Comment

by:maxul
ID: 23679160
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
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 23679751
You didn't need to rename the files inside the zip ;) You just have to rename them if you attach them separately. But no worries.


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

Author Comment

by:maxul
ID: 23689659
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.
0
 

Author Closing Comment

by:maxul
ID: 31545495
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23689694
I'm glad you got it :) Feels good, doesn't it ;)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Successful collaboration among team members is essential for the growth of your business. When employees work together on projects, share ideas and communicate effectively they get better results.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question