Could someone walk me through a deck of cards problem?

Pages: 12
Hello, after trying a bunch of different things I'm just asking if someone can help walk me through a deck of cards problem, that includes: multiple player hands and betting but the deck of cards is very important to get started. The requirements are to use inheritance. Start with a deck of 52 cards, deal 1 card to a player, ask them if they'd like to bet. Then deal the next card, ask for bet, and so on, for a game of 2-6 players. The pot goes to best hand, using standard poker hand rules.

I have tried doing a function for the deck, but I don't think that would quite work with inheritance. Every time I tried, I also ended up having a problem passing the card to a player hand and dealing with the random card issue.

Thanks for the help!
closed account (D80DSL3A)
I was under the impression that you already solved these problems, as indicated by this thread marked as solved:
http://www.cplusplus.com/forum/beginner/156822/

How is inheritance involved?
Deck would not inherit card. It would contain an array of cards (has-a vs. is-a relationship).
What would be inheriting from what?

Could you post the actual assignment requirements?
This may help a lot.
Last edited on
Hi androidguy,

Since this is homework, they don't want us giving the answers. They want us to point you in the right directions so you can learn it it for yourself. That's cool. So we need to start with some code. The first thing you need to do is to start with a class. So please show us what you have so far, and someone will be glad to help.

Last edited on
What variation of poker? 5 card stud? Texas hold-em? or ???

If you're going to create a poker game, you probably want to create classes for Card, Deck and Hand. I can't really see Deck using inheiritance, although it could inherit std::vector.

What are the functions for classes need?
Card:
- Get suit
- Get face value
- Get rank
- Display card

Deck:
- Populate the deck
- Shuffle
- Draw a card
- Print the deck (useful for debugging)

Hand:
- Draw a card
- Discard one or more cards (except holdem)
- Display hand
- Compare two hands (not trivial)

You could create a Player class that inherits Hand. That would make sense.

Do a little searching in the forum. You will find lots of examples of Deck and Card classes.

Last edited on
Yeah, @fun2code, I solved that problem and a few others but ended up stuck because the inheritance wouldn't work with what I had. @pearlyman, I have some code, but it is so jumbled that I just need to restart. @AbstractionAnon, thanks for the help on the outline above. I have searched quite a bit but haven't found a way to setup the deck, then deal one card at a time to separate players. I know that people don't want to simply give the answers, which is understandable, but I do like to see some code examples.

So here are the requirements:

You will create a program to play a game of Poker. This will be an easy game of Poker.
You will need to create the StudPoker class. We will use the following rules:
Players: 2 – 6

Supplies: 1 Deck of cards (52 total)
Objective: Get as many points as possible by playing n games/hands of the following
Stud Poker rules. If there is more than one player with the same high hand then they
split the pot for that game. After a card has been dealt to each player, they have a
chance to bet. They specify a number of points, which are added to the pot while
decreasing the amount of Points they have. Unlike real poker they cannot pass. At the
end of the game, after 5 rounds the player with the highest hand gets the current pot.
That amount is added to his set of Points and the pot is emptied. After n games, the
player with the most Points wins. This game is not table stakes, so the players can have
a negative balance.

Playing: Start by dealing 1 card to each player. The player who received the first card is
the first to bet. They can choose to drop out by betting 0. They will forfeit all money they
have put in the pot and will not be dealt any more cards. After all players have bet,
another card is dealt. This continues until the last player has 5 cards or if all but one player has dropped. All cards are then revealed and the winner is determined according
to this list, hands ranked lowest to highest value: (then the assignment shows the standard hands and rankings, such as pairs, three of a kind, etc.

Here is some stuff I have started, but I really think I need to start from scratch because this isn't going to work out.

functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "card_games.h"
#include <iostream>
#include <ctime>
#include <cstdlib> //for rand and srand
#include <cstdio> 
#include <string>

using std::cin;
using std::cout;
using std::endl;

string StudPoker::deckFunction () {
	
	srand(time(0));

	 struct card deck[52];  // An array of cards named deck, size 52
	
	const string ranks[ ] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen", "King" };
		
	const string suits[ ] = { "Diamonds", "Hearts", "Spades", "Clubs" };

	int k = 0; 

	for ( int i = 0; i < 13; i++)
	{
		for ( int j = 0; j < 4; j++)
		{
			deck[ k ].rank = ranks[ i ];
			deck[ k ].suit = suits[ j ];
			k++;
		}
	}
	int RandIndex = rand() % 13;
	int RandIndex2 = rand() % 4;
	return ranks[RandIndex] + " of " + suits[RandIndex2];
}

void StudPoker::player1hand() {
	
	
}
	


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "card_games.h"

using std::cin;
using std::cout;
using std::endl;

int main(int argc, char **argv)
{
	StudPoker deckF;
	cout << deckF.deckFunction() << endl;
	
	
	
	
	return 0;
}



card_games.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef HEADER_H
#define HEADER_H
#include <string>

using namespace std;

class StudPoker{
	
public:
	
	string deckFunction();
	void player1hand();
	//void playerHand(ranks, suits);
	struct card{
		string rank;
		string suit;
		int value;
		
	};

private: 

	string ranks;
	string suits;
	
	};

#endif // HEADER_H



Thanks guys!
Think seriously about implementing the classes I outlined earlier.

Some problems with your code:
functions.cpp
Line 14: srand() should only ever be called one (from main).
Line 16: deck has local scope and will go out of scope when functionDeck exits.
Lines 34-36: These lines will generate a random card, but won't remove it from the deck and do nothing to place it in a player's hand. No guarantee you're not going to generate the same card twice.

cardgames.h
Lines 14-17: You have a Card struct. It really doesn't belong as a member of StudPoker.
When you deal a card, you want to return a card struct (or class instance).

Lines 23-24: What is the purpose of these lines?

Last edited on
Yeah, I think I'll start from scratch and use the classes you outlined. I'll update with some code after it's done. Thanks for the help.
I wouldn't start totally from scratch. Since your instructions say to create a StudPoker class, I'd keep that. In fact, since your instructions say to use inheritance, you can have your StudPoker class inherit the Deck class.

What you have for your card struct is actually a good basis for your Card class.

THis post might be helpful:
http://www.cplusplus.com/forum/beginner/26851/#msg143269
Note the deck is simply vector<Card>.
Last edited on
Okay, so I looked at that post and started setting up some functions based around the vector deck. I want to make sure the deck works and a card can be drawn before I continue with the hand and betting stuff. Right now I have a few errors that I could use some help with. Also, how can I setup the player1hand array so each drawn card is added to the hand, then later used for the comparison?

card_games.h errors:
Line 14: C++ forbids declaration of 'drawRandomCard' with no type. What should be the type for this? Since output is drawnCard.
Line 15: 'drawnCard' has not been declared. I figure this is an easy fix, but I always have trouble passing the return value of a function, into the new function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef HEADER_H
#define HEADER_H
#include <string>
#include <iostream>
#include <vector>

using namespace std;

class StudPoker{
	
public:
	
	void deckOfCards();
	drawRandomCard();
	void printDrawnCard(drawnCard);
	void player1hand(drawnCard);
	
	struct Card
	{
		int value;
		int suit;
	
		Card() {}
		Card(int value, char suit) : value(value), suit(suit) {}
	
	};
vector<Card> deck;
		
	};



#endif // HEADER_H  


functions.cpp errors:
Some of these will probably be fixed once the header is fixed though.
Line 11: expected unqualified-id before '[' token
Line 17: 'Card' does not name a type. I'm not sure how to use this in the function, but the example you posted showed it like this.
Line 26: variable or field 'printDrawnCard declared void, 'drawnCard' was not declared in this scope
Line 30: Same error as above, player1hand declared void, drawncard not declared

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "card_games.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void StudPoker::deckOfCards() {
	
	deck.clear();
	static const char [] = {'C','D','H','S'};
	for (int suit=0; suit < 4; suit++)
		for (int val=1; val <=13; val++)
			deck.push_back(Card(val,suit));
}

Card StudPoker::drawRandomCard()
{
	int RandIndex=rand() % deck.size();
	Card drawnCard=deck[RandIndex];
	deck.erase(deck.begin()+RandIndex);
	return drawnCard;

}

void StudPoker::printDrawnCard(drawnCard) { //for debugging purposes
	cout << "Randomly drawn card" << drawnCard << endl;
}

void StudPoker::player1hand(drawnCard) {
	string p1hand[];

}


main.cpp is empty right now, not sure if I need to populate it with anything and part of the assignment keeps it pretty limited to simply calling the StudPoker class, or DrawPoker (used later in the assignment).

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <iostream>
#include <vectore>
#include "card_games.h"

using namespace std;

int main(int argc, char **argv)
{
	
	
	
}


Again, thank you so much for all the help. Once I get one player hand and the deck and betting system working, I'm sure I can figure out the other players and winnings, etc.

Thanks!!
card_games.h
line 14: Your implementation of drawRandomCard returns a Card, which is what you would expect. Your function delcaration at line 14 does not match your implementation. Change your function declaration to match your implementation.

line 15: The compiler is expecting drawnCard to be a type. You have no type named drawnCard. What you want here is to pass a Card instance.
 
void printDrawnCard(Card & drawnCard);

Really, this should be a method of Card. I'll get back to that in a minute.

Lines 18-28: As I mentioned before, your declaration of Card doesn't belong as a nested struct. Move this up to line 8. Your struct Card needs a print function so you can display a single card.

functions.cpp
Line 11: You need a name for the array.
 
static const char suits[] = {'C','D','H','S'};


Line 14: You're passing an integer for suit, while Card's constructor is expecting a char.

Line 17: The compiler doesn't see the declaration for struct Card because it's inside StudPoker. You can either move struct Card to line 8 as i suggested, or qualify the scope as StudPoker::Card.

Line 26: Same problem as line 15 in the header.

Line 27: cout doesn't know how to format a Card. You can overload the << operator in Card so cout works like you would expect, or you can provide a Card.Print() function as suggested.

Line 30-33: This needs some thought. You can have up to 6 players. This suggests an array or vector of players. Each player can hold 5 cards. This suggests that each player have an array or vector that can hold 5 cards. Remember I suggested earlier having a class for Hand. Player and Hand can be the same class, or player can inherit Hand.

As I suggested earlier, I would have a separate class for Deck. My rationale is that a game of StudPoker and a card Deck are different objects and have different methods. For example, in a game of StudPoker, there is the deal. That should be a function of the StudPoker class. Your Deck class should not be specific to dealing a StudPoker game, but should be generic enough to deal a card to a player when asked and hence can be used in any card game.

Last edited on
Okay, thank you, I changed some stuff, but I'm trying to use inheritance like you suggest. Would the follower header file work, after making some changes? Right now the errors are on 27, 28, 29, 30 and it says 'Card has not been declared' now that I moved it into the Deck class. Would the print function go right in the struct?

card_games.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef HEADER_H
#define HEADER_H
#include <string>
#include <iostream>
#include <vector>

using namespace std;

class Deck{
	
	struct Card
	{
		int value;
		int suit;
		string drawnCard;
	
		Card() {}
		Card(int value, char suit) : value(value), suit(suit) {}
	
	};
};

class StudPoker: public Card{
	
public:
	
	void deckOfCards(Card & suit);
	string drawRandomCard(Card & drawnCard);
	void printDrawnCard(Card & drawnCard);
	void player1hand(Card & drawnCard);
	
	vector<Card> deck;
		
	};

#endif // HEADER_H 


Then how do the function statements change since Deck needs to be used, as well as calling the card struct and declaring the StudPoker functions.

functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "card_games.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;


void StudPoker::Card deckOfCards(suit) {
	deck.clear();
	static const char suits[] = {'C','D','H','S'};
	for (int suit=0; suit < 4; suit++)
		for (int val=1; val <=13; val++)
			deck.push_back(Card(val,suit));
}

string StudPoker::Card drawRandomCard()
{
	int RandIndex=rand() % deck.size();
	Card drawnCard=deck[RandIndex];
	deck.erase(deck.begin()+RandIndex);
	return drawnCard;

}

void StudPoker::printDrawnCard(drawnCard) { //for debugging purposes
	cout << "Randomly drawn card" << drawnCard << endl;
}

void StudPoker::player1hand(drawnCard) {
	string p1hand[];

}


Sorry, I know I'm asking for a lot of help, but I don't have much experience with inheritance and this assignment is just asking for a bunch of stuff all at once, so I'm quite overwhelmed. I really appreciate your help, @Abstraction.
Last edited on
Okay. You're making progress.

card_games.h
------------------
Glad to see you made Deck a class. You need a constructor for it that populates the deck.
As I suggested before, move lines 11-20 to after line 8. You can leave it as a struct, or you can make it a class.

Line 9: Deck needs some member functons. See my original outlne for ideas. Also see my note below about StudPoker members.

Line 14: You're passing suit in as a char, but you're storing it as an int. Suggest you change it to a char to match the argument on line 18.

Line 15: Delete this. You don't need it.

Line 32: The vector should be a member of the Deck class. What you want here is an instance of the Deck class.

functions.cpp
-----------------
Line 9,17: You're using a scope qualifier (Deck::Card), but the function declarations are not in Card. They don't need a scope qualifier.

Line 9: This code really should be Deck's constructor. i.e. It populates the Deck. Why are you passing suit as an argument (which BTW has no type)?

Line 14: Card's constructor is expecting a char as the second argument. Should be:
 
  deck.push_back(Card(val,suits[suit]));

BTW, I liked your original code better where you had the suit of the card as a string rather than a char.

Line 17: The return type should be a Card, not a string.

Line 17-24: This function should be a member of Deck.

Lines 26-28: This should be a member of the Card class.
1
2
3
4
void Card::Print ()  const
{   //  Does not do anything fancy about formatting the name of the card
    cout << value << " of " << suit << endl;  // Assumes suit is a char
}


Lines 33-33: This function doesn't do anything.

As I pointed out in my previous message, you now need to think about players and hands. I've already given you idea for whats hands and players need, but here's an outliine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Hand
{  vector<Card> cards_in_hand;
public:
    void add_card_to_hand (const Card & card);
    void print () const;  //  Print all cards in hand using Card::print()
};

class Player : public Hand  // Note use of inheritance
{  int     player_chips;
    bool  folded;
public:
   Player (int starting_chips);
   bool make_bet (int chips);  // returns false if bet > player_chips
};


By moving the functions I suggested from StudPoker to Deck, you're kind of left with no functions in the StudPoker class. So think about what functions you need in StudPoker. One I've mentioned already is the deal() function. This is different from Deck's draw() function. StudPoker::deal() should have two loops. One for the number of card to deal (5) and the second for the number of players. You will need an array (or vector) of players. Each iteration you should call deck.draw() and pass the returned card to player[n].add_card_to_hand().








Okay, again, I really appreciate your help. I may actually finish this by Friday. So I fixed all the errors, as far as when it's compiled. However, my formatting may be a bit jumbled still, especially in my header, but let me know if it works. I want to make sure everything is set up, then I can start asking specific arithmetic questions within functions, so I don't have to keep sending you all my code. Let me know how it looks.

Also, I'm still not quite sure how the Player and Hand stuff will work, because right now your outline just has the Class Player, but will we also need a Player function, like player1hand()?

card_games.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef HEADER_H
#define HEADER_H
#include <string>
#include <iostream>
#include <vector>

using namespace std;

struct Card
	{
		int value;
		char suit;
		string drawnCard;
		Card(int value, char suit) : value(value), suit(suit) {}
		void printCard() const;
	
	};

	
class Deck{

	
public:
	void createDeck();
	void shuffleDeck(Card);
	Card drawRandomCard();
	
private:
	vector<Card> deck;
};


class Hand {
	vector<Card> cards_in_hand;
	
public:
	void add_card_to_hand (const Card & card);
	void print() const;
};

class Player: public Hand
{
	int player_chips;
	bool folded;
	
public:
	Player (int starting_chips);
	bool make_bet (int chips);
};


class StudPoker: public Card{
	
public:
	
	void deal();
		
	};

#endif // HEADER_H 


functions.cpp
Still a bit confused about your statement here:
"This code really should be Deck's constructor. i.e. It populates the Deck."
What would that look like in my Deck Class?
1
2
Deck();
~Deck();

Also I noticed my 'value' in printCard() doesn't seem to be looked at as a variable in my IDE, while 'suit' is looked as a variable. So this may answer my question about testing the printCard() in main below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "card_games.h"
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

void Deck::createDeck() {
	deck.clear();
	static const char suits[] = {'C','D','H','S'};
	for (int suit=0; suit < 4; suit++)
		for (int val=1; val <=13; val++)
			deck.push_back(Card(val,suits[suit]));
}

Card Deck::drawRandomCard()
{
	int RandIndex=rand() % deck.size();
	Card drawnCard=deck[RandIndex];
	deck.erase(deck.begin()+RandIndex);
	return drawnCard;

}

void Card::printCard() const { //for debugging purposes
	cout << value << " of " << suit << endl;
}

void StudPoker::deal() {
	
}
void Hand::add_card_to_hand(const Card & card) {

}


In my main, I just wanted to test if I could print a card random card using my printCard() function. Declaring it as an object like the following wasn't working either:

1
2
Card printC;
printC.printCard();


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <iostream>
#include <vector>
#include "card_games.h"

using namespace std;

int main(int argc, char **argv)
{
	
	Card printCard();
	
}


Hopefully my next post will just be regarding a couple functions, if everything is set up. I realize, these posts are quite time consuming, so thank you!!
This looks a lot better, but you still have a lot of work to do.

card_games.h
Line 9: I would make Card a class, just for consistency. The only difference between a struct and a class is the default visibility of members.

Line 12: Get rid of this. There is no reason for it.

Line 48: I would change make_bet somewhat. It should prompt the player for his bet. No reason to pass in chips as an argument. I would return the value of the bet (instead of it being a bool).

Line 52: StudPoker should NOT inherit card. Applying the "is a" rule, a StudPoker game is NOT a Card.

functions.cpp
----------------
Regarding Deck's constructor. Deck's constructor should call createDeck() to populate the vector.

my 'value' in printCard() doesn't seem to be looked at as a variable in my IDE

Not sure why. Looks ok.

Lines 30-32: You've got this to write. I outlined what you need to do here earlier.

Lines 33-35: This is simply a push_back of card onto cards_in_hand.

In my main, I just wanted to test if I could print a card random card using my printCard() function. Declaring it as an object like the following wasn't working either:

You have no default constructor for Card, so the values in printC are garbage.

main.cpp
-----------
Line 11: This statement makes no sense. It looks like a function call, but there is no instance specified.

main() should look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char **argv)
{  StudPoker  game;
    char ans;

    do
    {	
    game.reset ();  // reset the game
    game.deal();    // deal should invoke a betting round after each round of cards dealt
    game.showdown();  // evaluate the hands and find the winner    
    //  award the pot to the winning player
    cout << "Do you want to play again?";
    cin >> ans;
    } while (ans == 'y');
}


IMO, the hardest part of this assignment is going to be the evaluation of the hands. Here's an enum to get you thinking about that:
1
2
3
4
5
6
7
8
9
10
11
enum hand_kind
    {   HIGH_CARD,
        ONE_PAIR,
        TWO_PAIR,
        THREE_OF_A_KIND,
        STRAIGHT, 
        FLUSH,
        FULL_HOUSE,
        FOUR_OF_A_KIND,
        STRAIGHT_FLUSH,        
    };

I would suggest a function in your Hand class that classifies the hand as one of the above values. Once the betting is complete and the hands are laid down (displayed), you would need to find the hand(s) with the highest value of the above enum. Keep in mind that if two hands have the same enum value, then the one with the highest rank wins. i.e. if you have two straights, the hand with the higher straight wins.




Last edited on
Sorry, I'm still a bit confused on how to use constructors, especially with functions. So you said "Deck's constructor should call createDeck() to populate the vector. "
What would this look like in the header file, as part of the class? So like:
1
2
3
4
5
6
7
8
9
class Deck {

	public:
		void createDeck();
		void shuffleDeck(Card);
		Card drawRandomCard();
		Deck(); 
		vector<Card> deck;
};


So like this wouldn't really do anything right now, right.

Then in the function itself. How do I go about sending this information to the vector?

1
2
3
4
5
6
7
void Deck::createDeck() {
	deck.clear();
	static const char suits[] = {'C','D','H','S'};
	for (int suit=0; suit < 4; suit++)
		for (int val=1; val <=13; val++)
			deck.push_back(Card(val,suits[suit]));
}


I'm going through and fixing the other stuff you mentioned, but ran into this issue I mentioned above.
Also simply passing the drawnCard around to my printCard function is troublesome. But I'll continue to work on it before asking more questions. Thanks again!!
Last edited on
What would this look like in the header file, as part of the class?

A constructor is simply a function with the name of the class that gets called when an instance of the class is instantiated. At line 7, you have a declaration for a default constructor. That's fine. Now that you have declared the constructor, you will need to implement it.
1
2
3
Deck::Deck ()    // Default constructor
{  createDeck();  // populate the vector
}


How do I go about sending this information to the vector?

Not sure I understand your question. line 2 clears the vector. Line 6 pushes a Card onto the vector (executed 52 times).

simply passing the drawnCard around to my printCard function is troublesome.

Before you try to print a Card, make sure it has valid values. Simply instantiating a Card won't ensure that it has meaningful values.







Last edited on
Okay, fixed the constructor issue then. Thanks.
What is the name of the vector? So I can output it's contents to see if it's working properly. I don't think my
 
vector<Card> deck

is what I would use to output the contents.
I think the problem is, I'm thinking, if I want to print something from a function, I would return a value, then send that value off to a print statement of some sort, or just print it within my createDeck() function, but vectors are tricky.

If I can print the values, I can ensure they are valid, then move onto the random selection and drawing one card, etc.

Thanks.
What is the name of the vector?

deck
is what I would use to output the contents

vectors don't inherently have a print function. You have to iterate through the vector and print ech element.

or just print it within my createDeck() function

You don't want to do that. createDeck has one purpose: populate the deck.

You can add a print function to your deck class:
1
2
3
4
5
6
7
void Deck::Print () const
{  Card tempcard;
    for (unsigned i=0;i<deck.size(); i++)
    { tempcard = deck[i];
       tempcard.Print();  // Assumes Card has a print function
    }
}

Okay, I think my header file may need work, because setting up the function you suggested, wouldn't really work, I'm getting no matching function for call Card::Card(). Deck was not declared in this scope. But moving it into Card kind of fixed it.
Unfortunately, this is all due tomorrow and I haven't even finished half of it, my programming skills just aren't at this level yet...

Maybe, you could help me organize the class and functions a bit, they seem a bit jumbled right now, that's probably why nothing is working. Anyway, I really appreciate all the help.

card_games.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef HEADER_H
#define HEADER_H
#include <string>
#include <iostream>
#include <vector>

using namespace std;

class Card{
	public:
		int value;
		char suit;
		string drawnCard;
		Card(int value, char suit) : value(value), suit(suit) {}
	};

class Deck {

	public:
		void createDeck();
		void shuffleDeck(Card);
		Card drawRandomCard();
		Deck(); 
		vector<Card> deck;
		void Print() const;
};

class Hand: public Deck {
	
	vector<Card> cards_in_hand;
	
	public:
		void add_card_to_hand (const Card & card);
};

class Player: public Hand {
	
	int player_chips;
	bool folded;
	
	public:
	Player (int starting_chips);
	int make_bet ();
	void player1hand();
};

class StudPoker{
	
public:
	
	void deal();
		
	};

#endif // HEADER_H


functions.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "card_games.h"
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

Deck::Deck() {
	createDeck();
}

void Deck::createDeck() {
	deck.clear();
	static const char suits[] = {'C','D','H','S'};
	for (int suit=0; suit < 4; suit++)
		for (int val=1; val <=13; val++)
			deck.push_back(Card(val,suits[suit]));
}

Card Deck::drawRandomCard()
{
	int RandIndex=rand() % deck.size();
	Card drawnCard=deck[RandIndex];
	deck.erase(deck.begin()+RandIndex);
	return drawnCard;

}

void Deck::Print () const{ //for debugging purposes
	Card tempcard;
	for (unsigned i = 0; i < deck.size(); i++)
	{
		tempcard = deck[i];
		tempcard.Print();
	}
	
}

void StudPoker::deal() {
	//pass value to add_card_to_hand
}
void Hand::add_card_to_hand(const Card & card) {

	deck.push_back(Card(val,suits[suit]));
}

void Player::player1hand() {
	string p1array[5]; 
	//somehow implement add_card_to_hand here...
	//call to make_bet();
	//simply repeat everything in here for all players, once it's done...
} 

int Player::make_bet(){
	cout << "Your chips: " << player_chips;
	cout << "Place bet (enter 0 to fold): ";
	cin >> make_bet;

}
enum hand_kind
    {   HIGH_CARD,
        ONE_PAIR,
        TWO_PAIR,
        THREE_OF_A_KIND,
        STRAIGHT, 
        FLUSH,
        FULL_HOUSE,
        FOUR_OF_A_KIND,
        STRAIGHT_FLUSH,        
    };
	
	//obviously need to use this too 


main.cpp - not really used yet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <iostream>
#include <vector>
#include "card_games.h"

using namespace std;

int main(int argc, char **argv)
{  StudPoker  game;
    char ans;

    do
    {	
    game.reset ();  // reset the game
    game.deal();    // deal should invoke a betting round after each round of cards dealt
    game.showdown();  // evaluate the hands and find the winner    
    //  award the pot to the winning player
    cout << "Do you want to play again?";
    cin >> ans;
    } while (ans == 'y');
}


Thanks again.
card_games.h
------------------
line 11-12: These should be private members of Card.

line 13: For the upteenth time, get rid of this line. It is NOT an attribute of a card.

Lne 21: Card does not belong as an argument of shuffleDeck().

Line 24: This should be a private member of Deck.

Line 28: Hand should NOT inherit Deck. A Hand is NOT a Deck.

Line 47: Your StudPoker class needs an array or vector of players. It also needs to keep track of the pot.

Line 50: StudPoker will need a default constructor in order to initialize the pot.

1
2
  vector <Player>  m_players;
  int                       m_pot;


functions.cpp
-----------------
I don't see any implementations for member functions of Card.

Line 40-42: I've given you the algorithm for this already.
Line 41: After one card is deal to each player, you need to call a function such as betting_round() which solicits a bet from each player.

Line 45: The push_back should be to cards_in_hand.

Line 58: You have no variable called make_bet.

Line 59: You need a return statement.

Lines 61-71: These are declarations. They belong in the header file before the declaration for Hand.

Lines 48-53: I still don't understand what you're trying to do with this function. I think you're confusing this function with the Hand class.

Here's an enhanced Hand class that should give you some ideas for evaluating a Hand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Hand 
{   vector<Card>    m_hand;
    hand_kind       m_kind;
    int             m_high_rank;
    
    bool is_straight_flush ();
    bool is_flush ();
    bool is_full_house();            
    bool is_same_rank (int rank);
    bool is_straight ();
    bool is_two_pair ();
    int find_high_rank () const;
    int how_many (int rank) const;
    
public:
    Hand ();
    void add_card (const Card & card);
    hand_kind classify_hand ();
    
    //  Compare two hands
    bool operator < (const Hand & rhs) const;
    
    //  Display a Hand
    friend ostream & operator << (ostream & os, const Hand & hand);
};


I'm not going to write the functions for you, but I'll give you a freebie:
1
2
3
4
5
6
bool Hand::is_straight_flush () 
{   if (! (is_flush () && is_straight()))
        return false;  // Not flush and not straight
    m_high_rank = find_high_rank();
    return true;        
}


main.cpp
-----------
line 14: You have no reset() function.

line 16: You have no showdown() function.

Last edited on
Pages: 12