# Need help designing my craps program.

• September 26th, 2012, 03:10 PM
raptor88
Need help designing my craps program.
This will be my first program in C++ and it will be a crap game with every possible bet, just like on a Vegas crap table. I'm using SFML to display half of a crap table and to display the bets (chips) for up to 4 players on the crap table.

Every bet will be positioned in the proper place in each bet box just like on a Vegas crap table. This means there will be 4 positions on the passline (for 4 players), 4 positions for passline backup bets, 4 positions for each "place bet", etc.

To give you an idea of what I need, here's some sample bets in a struct:
Code:

```struct BetInfo {     int betAmount     const float coordX    // Chip coord X     const float coordY    // Chip coord Y } // P1=Player1, P2=Player2, etc. BetInfo passP1 = {0, 300.0, 700.0}; BetInfo passP2 = {0, 400.0, 700.0}; BetInfo passP3 = {0, 500.0, 700.0}; BetInfo passP4 = {0, 600.0, 700.0}; BetInfo place4P1 = {0, 350.0, 220.0}; BetInfo place4P2 = {0, 360.0, 220.0}; BetInfo place4P3 = {0, 350.0, 200.0}; BetInfo place4P4 = {0, 360.0, 200.0};```
REQUIREMENTS:

1. When displaying the chips on the table using SFML, I need to scan all of the "betAmount" values. If zero, don't display. If greater than zero, display the proper amount of chips at the coordinates given.

2. When a player makes a bet, I need to update "betAmount" easily.

3. When a player loses a bet, I need to zero "betAmount" easily.

QUESTION:

1. To display the bets (chips) on the table, I need a for loop to scan every bet position to see if the "betAmount" is greater than zero. But the way I showed the code above does not seem to be a good way to do that.

2. When a player clicks in a bet box to make a bet, I need to translate the mouse coordinates to know which bet box was clicked in, and then update the "betAmount" for that player.

Any suggestions on how to store all of the bet info that will allow easy updating of the "betAmount" when bets are made or lost, and yet allow scanning with a for loop to display the bets made.

Thanks,
Raptor
• September 26th, 2012, 03:17 PM
S_M_A
Re: Need help designing my craps program.
A vector of vectors maybe or maybe a single vector (I don't know how a crap table look)? http://cplusplus.com/reference/stl/vector/
• September 26th, 2012, 03:57 PM
Paul McKenzie
Re: Need help designing my craps program.
Quote:

Originally Posted by raptor88
This will be my first program in C++ and it will be a crap game with every possible bet, just like on a Vegas crap table. I'm using SFML to display half of a crap table and to display the bets (chips) for up to 4 players on the crap table.

I don't know this game, but your overall approach of combining the UI with the logic of the game has many drawbacks. Just the struct you have of having the bet amount coupled with UI information is a source of these issues. What does a bet amount have to do with a coordinate position? What if you now want to create a text based version of the game, or you want to change the UI in some other way?

I would suggest you read up on the MVC (Model-View-Controller) design pattern. This is where the logic (rules) of the game is separated from the UI (or view). For example, the bet would be placed, and then the code that placed the bet sends a message, calls a function, etc. to the view, and then the view does whatever it needs to do to update itself given the information passed.

http://en.wikipedia.org/wiki/Model%E...0%93controller

If you go down the road you're going now, you will eventually have an intertwining of UI code with game rules, making the application harder to change, debug, and maintain.

Regards,

Paul McKenzie
• September 27th, 2012, 12:14 AM
raptor88
Re: Need help designing my craps program.
Quote:

Originally Posted by S_M_A
A vector of vectors maybe or maybe a single vector (I don't know how a crap table look)? http://cplusplus.com/reference/stl/vector/

Here's an example of a craps table (pic from http://www.freerepublic.com/focus/f-news/998310/posts):

Attachment 30391

With regard to the sample code that I posted:

1. The four pass line bets (chips) would be shown on the horizontal row labeled "PASS LINE".

2. The four "Place4Pn" bets would be above and below the block containing the big yellow 4 in the bet area labeled "PLACE BETS". Something like this, where P1 is actually the chips bet for Player1, etc.

P4 P3
= 4 =
P1 P2

(Ignore the equals signs. They are just used to space the '4' in the center.)

I was considering using a vector which would be easy to push_back when a bet is made. But I need a "data" table to hold the coordinates for EVERY bet position on the craps table. So I don't think a vector would work for that, or would it?

Thanks,
Raptor
• September 27th, 2012, 12:32 AM
raptor88
Re: Need help designing my craps program.
Quote:

Originally Posted by Paul McKenzie
I don't know this game, but your overall approach of combining the UI with the logic of the game has many drawbacks. Just the struct you have of having the bet amount coupled with UI information is a source of these issues. What does a bet amount have to do with a coordinate position? What if you now want to create a text based version of the game, or you want to change the UI in some other way?

I would suggest you read up on the MVC (Model-View-Controller) design pattern. This is where the logic (rules) of the game is separated from the UI (or view). For example, the bet would be placed, and then the code that placed the bet sends a message, calls a function, etc. to the view, and then the view does whatever it needs to do to update itself given the information passed.

http://en.wikipedia.org/wiki/Model%E...0%93controller

If you go down the road you're going now, you will eventually have an intertwining of UI code with game rules, making the application harder to change, debug, and maintain.

Regards,

Paul McKenzie

Very good points and thanks for pointing them out. I'll use your suggestions in future projects.

But since this will be my first C++ project, I want to keep the code as simple as possible. My current idea is to have a global table that contains the bets made and the respective coordinates where the chips should be shown on the table. I know global items are not recommended in C++ but I just want to use this method for my first project.

- My GetBets class would get bets from each player and update the corresponding bet amount in the table.
- Then my display class would just scan that table and display chips for any bets made in their proper positions.

I'm not sure how to make that table which would be easy to scan using a "for" loop in my display class. Any suggestions?

Thanks for the help,
Raptor
• September 27th, 2012, 03:46 AM
Paul McKenzie
Re: Need help designing my craps program.
Quote:

Originally Posted by raptor88
Very good points and thanks for pointing them out. I'll use your suggestions in future projects.

But since this will be my first C++ project, I want to keep the code as simple as possible.

What is to say that using MVC is not simpler? Do you have the rules of the game coded? What good is the UI if the game doesn't properly implement the rules? It seems you're making the mistake that many beginner coders make, and that is concentrating and coding nice or "cool" looking output, and neglecting the most important parts of the program (the calculations required, in your case the rules of the game, etc.).

Assume there is no UI. Do you have the individual functions coded to handle bets, randomly "throw dice", react to the number thrown, handle multiple persons playing the game? None of these require a user interface. At most, you would have a console main() app that randomly places bets, rolls dice, etc. thereby exercising the rules of the game to see if the rules are implemented correctly. Once that is done, then you go from there. Or maybe there is an input file that has a simulated game -- you read the file and see if the rules are implemented correctly. Again, no UI is required.

Regards,

Paul McKenzie
• September 27th, 2012, 04:46 AM
raptor88
Re: Need help designing my craps program.
Quote:

Originally Posted by Paul McKenzie
What is to say that using MVC is not simpler? Do you have the rules of the game coded? What good is the UI if the game doesn't properly implement the rules? It seems you're making the mistake that many beginner coders make, and that is concentrating and coding nice or "cool" looking output, and neglecting the most important parts of the program (the calculations required, in your case the rules of the game, etc.).

Assume there is no UI. Do you have the individual functions coded to handle bets, randomly "throw dice", react to the number thrown, handle multiple persons playing the game? None of these require a user interface. At most, you would have a console main() app that randomly places bets, rolls dice, etc. thereby exercising the rules of the game to see if the rules are implemented correctly. Once that is done, then you go from there. Or maybe there is an input file that has a simulated game -- you read the file and see if the rules are implemented correctly. Again, no UI is required.