Need help designing my craps program.
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Need help designing my craps program.

1. Member
Join Date
May 2012
Posts
57

## 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
Last edited by raptor88; September 26th, 2012 at 03:18 PM.

2. Elite Member
Join Date
Oct 2006
Location
Sweden
Posts
3,649

## 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/

3. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Need help designing my craps program.

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
Last edited by Paul McKenzie; September 26th, 2012 at 04:05 PM.

4. Member
Join Date
May 2012
Posts
57

## Re: Need help designing my craps program.

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):

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
Last edited by raptor88; September 27th, 2012 at 02:48 AM.

5. Member
Join Date
May 2012
Posts
57

## Re: Need help designing my craps program.

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

6. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Need help designing my craps program.

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

7. Member
Join Date
May 2012
Posts
57

## Re: Need help designing my craps program.

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.

For now, I think I might have figured out a way to implement my "betInfo" table. I was thinking about how a database for employees works. One method might be to have a structure containing the employee's name, address, phone numbers, wages, etc. Then there could be an array of the structures. The key is to have the employee's name "in the structure" to identify each record. I think my initial thought of just having the bet amount and chip coordinates in the structure without an identifying name in the structure was the problem.

With the name of the bet "in the structure", (like passP1 passP2, place4P1, place4P2, etc.), I can search for the name when updating the bet amount. For displaying the chips bet, since the structures are in an array, I could use a for loop and increment the array index to scan the array of structures for bet amounts greater than zero. When a bet amount greater than zero is found, draw the sprite for the chips using the coordinates in that structure.

I'll give this a try and see what happens.

Thanks much,
Raptor

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•