My Programming project

Modder man

[H]ard|Gawd
Joined
May 13, 2009
Messages
1,770
my project is to write a blackjack game in object oriented C++. i am a bit stuck on decideing what my classes should be. does anyone have any input?
 
It'll depend on the scope of your game and the interface you'll provide.

What classes have you thought of creating so far?
 
well to be honest i would say im almost completely lost....i have written a list of possible functions

i was thinking maybe

player
dealer
deck
game
 
It heavily depends on the scope, as mikeblas said. In addition,

Are you using a GUI, or console based?

As far as actually processing and deciding what should be split up into classes, try to think of the seperate pieces of a blackjack game. What is required in this game? Try to model it after the real-life things that compose the problem (a blackjack game, in this instance).

Some beginning questions that would probably help you:
Are there any logical connections between the DATA to be used, and the FUNCTIONS that need to use that data?
What interfaces can be separated from implementation?
What kind of data structures are you going to be using, and how would they fit into classes, if needed?
How (and where) are you going to process the logic of "scoring" (keeping track or analyzing hands to see what a combination of cards is worth)?
etc

I think you have the right idea with player, dealer, deck, game. I think this is a good starting point.
 
text or gui based?

IMO-

deck - cards in the deck (x4 for each suit). you can use a random number generator and match it to a suit type, also based off of a random number. Also make sure that only one card for each suit.

game - rules of the game. Values of the cards would be attached to this class, as well as anything over 21 being a bust. Rules for the ties (push), and the payouts for blackjack (normally 1.5 x amount bet). The will be more or less the engine. Keep track of the money in the pot and over all payouts. Hit and Stay rules. The player finishes 'hitting' before the dealer does anything. So when a player hits stay, the dealer does what it needs to do. Also rules (checks really) for splits (must be on pairs). Double will need a rule/ check too. Then this class figures out the winner.
Also if a dealer shows a 10, face card or an Ace, there should be a check to see if the game is already over. This gets into buying insurance too.

Dealer - In most casino's their are certain rules for the dealer. ie Must hit on soft 17 and so on. It should call the deck class and should also make sure that their are no duplicates in the cards called from the deck during the game being dealt. Unless of course you are using more than one deck in which case you will need to make rules for that as well. Also if you shuffle between each game. (those are the rules that need to be thought of). The cards are dealt to the player first, dealer one face down, player, dealer face up. Keep a constant total of the values of the cards and return the value to the game class for a rules check.

Player - call the deck class. Keep a constant total of the values of the cards and return the value to the game class for a rules check. Also, keep track of the players chip stack. Once it reaches 0, they cannot place anymore bets. Keep this class and the dealer class modular so that if more players are added to the program, all you have to do is call this class again. A player just hits or stays. Doubles and splits would be controlled here too. A split effectively creates another player object with all the same rules, with a chipstack equal to the original bet. A double obviously doubles the money being bet, however the player get only one more card (a boolean var can be used for this) basically telling the game to hit, then stay automatically once a double has been activated.

And of course a main to help control the flow.
 
One thing I always tell people is whatever you see yourself being able to take away from the project to use on another project make into a separate class. Such as the deck class. That class can be used for any other game that uses a regular 52 card deck. You can call the class twice to use in games that need 2 decks. And so on.
 
It would be best to break down the classes into several methods
What do you do with a deck of cards when playing a card game? You shuffle them. And you deal them. When you deal them you have the cards dealt, and the cards not dealt.
For example your Deck class would have these methods:
deck - contains the rules for the deck. 52 cards, 4 suits, 13 cards to each suit. This will be the hardest part. But you can use an array of 0-51 slots, each slot holding 2 numbers. One denotes the card, the other denotes the suit. So 0-1 could be a 2 of hearts, 14-1 could be the 2 of diamonds. However you decide to do it.
shuffle - which puts all the cards from the deck method into random order. And resets any counters or variables for things such as the cardsDealt, or cardsLeft. You have an array already index 0-51 with the above method, make use of that.
cardsDealt - duh, pulls cards from the cardsLeft method
cardsLeft - after a shuffle they should all be in here, and subtracted out for each card dealt. Also provide a counter so you can figure out when you're low and its time to shuffle.

If you break down the game into all the different things that need to happen in order to play it, you will see the classes more clearly. Lots of things happen at the same time, and thats ok, think about the similarities and the differences of each of them when designing classes. Diagrams, outlines or whatever helps planning. Then psudo code.

If you make another card based game, this class with its methods will work perfectly. Thats the idea behind object oriented lang.
 
Last edited:
oh also, in the game class dont forget a check for when the player or dealer has over 21 but there is an Ace involved
 
it is going to be IMPOSSIBLE for me to get all this done in a week.
this is the first lab he has given us that isnt step by step lol
and now im completely lost
 
Last edited:
the more i work at this the more i get confused there are way to many things going on for me to keep them straight
 
it is going to be IMPOSSIBLE for me to get all this done in a week.
this is the first lab he has given us that isnt step by step lol
and now im completely lost

most teachers give plenty of time for programming projects. fwiw, I'm terrible at writing code (read: not a programmer) and this would take me 2 hours tops...sounds like someone hasn't been paying attention in class.


as for constructive advice, I would start by outlining the frameworks for each class using placer functions and variables. do you even have any code to show here that we might be able to help with?
 
the more i work at this the more i get confused there are way to many things going on for me to keep them straight

Such as? I would try to keep things simple. You are in the right direction with a deck, player, dealer and game class. I would have the game class ask how many players will play. Based on that you can create x amount of player objects which the game class can store in an array. So when your game deals to the players your Game class loops through its player array and lastly the dealer.

Your Dealer should inherit from your Player class because after all a dealer "is a" Player. This way you can have one function called getCurrentCardSumTotal that stores how much the cards add up to. This function will be accessible to both classes and only needs to be written once. Your Game class can control when your Dealer hits or stays. When a player/dealer hits (deckObj.nextCard) you can pass this value to your player.nextCardValue functioin which will update the value returned from getCurrentCardSumTotal.

Your deck class can probably just store an array of 52 cards which random returns a card. You could also create a Card class because the composition of a deck is made up of several cards. But to keep things simple start with just the Deck class. Your Deck class could contain an array of 52 cards which get randomly shuffled (deckObj.shuffle). When your game calls for a new card from the deck (deckObj.nextCard) it can return the next number in your array.
 
Your Game class is going to handle all of the logic. The game is linear so you will be going from step A to step B, if step B is this then step C, else do step D.
 
thanks for the help blackout no need to be a DICK, i have been working in class we have had 9 labs and i have finsihed them all. This one he just has not given us any specific instructions, whereas the others were all step by step. so now that i have to write a program from scratch i am kinda lost. i am also not the only person in class that is stuff i think out of the 14 of us 1 person knows what he is doing.

PS. no way you would do this in a hour unless some of the classes were already done
 
the more i work at this the more i get confused there are way to many things going on for me to keep them straight

It sounds to me like you're just starting to learn to decompose problems, and that you don't yet know how to let abstraction help you out. Personally I found that the hardest thing to teach; syntax and such is easy, a different way of thinking... not so much :)

What you have to do is make the big problem into a lot of little problems. Preferably do it in a hierachial manner. i.e., make one big problem into say... 5-10 medium sized problems, then make each of those medium sized problems into another realtively small set of smaller problems, and so-on. Eventually you'll wind up with really small problems that are easy to solve.

Here's an example of how to think your way through it.

Start off asking yourself what you need to do to play a game. Only answer at a really high level.

As a top of my head example (and there's definately other good answers at this level)

You need a game.

What does the game need to do?

The game needs a dealer and players.
The game needs the dealer to shuffle.
The game needs the dealer to deal 2 cards to each player.
The game needs each player to decide what actions to take. (hit, stay, whatever)
The game needs the dealer to decide what actions to take.
The game needs the dealer to collect and pay out.

For right now, assume that each of those things is something that something already exists to do. Assume there's already a player class with a determineAction function. Assume there's already a dealer class with a shuffle function. Assume all of those classes and functions exist. Throw together some pseudocode detailing how your program will flow.
Now, look at what information you need flowing between these classes for them to be able to do thier job. This may mean that you add more steps. Maybe you need an extra step before the dealer deals the cards out where he's told who the players are? Then use this information flow to help determine the inputs and return values from the functions you're saying that you'll need. For example you'll need to provide your Player classes with the Cards they're dealt so they can make the decision to hit or stay... and when your Game tells the player it's his turn to make a decision the Player needs a way to tell the Dealer what that decision is.

Once you have this high-level mostly worked out, then go down a level. For example, you decided that to make your high level work, your dealer needed to be able to deal a card. Assume you already have a shuffled Deck of Cards. What's the dealer going to do? Well, he'll get a Card from the Deck, and provide that card to a Player. He'll do that once for each player and himself, then he'll do it again. So same as above, figure out what objects are interacting, what those interactions are, and what information is flowing between them. Then figure out how you want the dealer to do that. Maybe you have a loop that does something like player[currentPlayer]->giveCard(deck.getCard()) and increments currentPlayer. Maybe you do that loop twice. Remember, right now, you're not worried about how giveCard or getCard works, you're just interested in defining what they *do*. Later you'll go into your Deck class and figure out how getCard works... but that's later. For right now, you're interested in how you use them to ensure that each player and the dealer gets two cards.

Eventually you'll find that you have really simple problems to solve. Like the Deck's getCard function. Internally the Deck might be implemented by an array of 52 Cards and an topCardIndex into it. Then your getCard function turns into nothing more than returning the top card of the deck and adding one to the index. something like "return deckOfCard[topCardIndex++]" In other words you're taking the top card off of the deck, saying that there's now a new top card and giving the card that you took off to whoever called you.

All that said, don't take the examples I've provided as the best way to break things down. They're probably not :) Think through it for yourself; you'll likely come up with better ways... more importantly, you'll start to learn how to approach decomposing problems in a way that keeps you from trying to remember how the whole thing works all at once.

Good luck!
 
I haven't done any c++ or any programming in probably 8+ years but I could finish this in probably an hour :]

just lay it all out first. dont even think about the code. then take it step by step
 
well he has decided that this project is to much for us and we are going to take a written test instead
 
What class are you taking? What did it teach you?

Yea, since they can't program it, maybe they can write about it? I don't know about the rest of you, but I learn by doing. Being able to actually write code and write the theory behind doing it is completely different.
 
I just finished object oriented c++
the second programming class at devry university

I think the problem was that he has taught us much more of the code not so much of the problem solving
 
I just finished object oriented c++
the second programming class at devry university

I think the problem was that he has taught us much more of the code not so much of the problem solving

If you continue in programming classes, you'll probably find that problem solving is a much bigger (or important) skill than knowing code syntax and etc. Of course there is always a need to know the technical side of things like how a specific language works, syntax differences, etc. But writing code, when you understand the problem (AND the solution), should more or less be fill in the blank. You might not exactly know what code to type, but that is what references are for. You can find syntax and simple examples. Knowing the skills on how to solve problems, using algorithms, etc, that is what you really need to be effective at programming.

IMO :)
 
Back
Top