I dont understand class objects.. poker game

For my c++ class, i have to use write program that deals 10,000 five-card hands using the functions already written in the code and count how many hands contain a pair and how many contain a flush. But the thing is.. i just don't understand class objects. im totally dumbfounded. Can someone help me out? Heres the 4 files i have to work with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//deck.h

#ifndef _DECK_H
#define _DECK_H

#include "card.h"

const int DECKSIZE = 52;

class Deck {
private:
	Card inDeck[DECKSIZE];            // These are private data members and
	int nextCard;                     // can be used only by member functions.
public:
	Deck();                           // Initialization. Called automatically
	// when a Deck variable is declared.
	void shuffle(int);                // Exchange random pairs of cards.
	Card getCard();                   // Returns top card from the deck.
	void addCard(Card);               // Put named card in the deck.
	int totalCards();                 // Returns number of cards left in deck.
};

#endif



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
// card.h

#ifndef _CARD_H
#define _CARD_H

class Card {
public:
	// Define types for the suit and value
	enum Suit { Diamonds, Hearts, Clubs, Spades };
	enum Value { NullCard, Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
private:
	static const char *snames[4];
	static const char *vnames[14];
	Suit s;
	Value v;
public:
	// Constructors initialize a card
	Card();
	Card(Suit newSuit, Value newValue);
	Suit getSuit();                             // Returns a card's suit.
	Value getValue();                           // Returns a card's value.
	void printSuit();                           // Print a card's suit.
	void printValue();                          // Print a card's value.
	void printCard();                           // Print a card's suit and value.

};

// Return the next suit or card value in succession

Card::Suit nextSuit(Card::Suit);
Card::Value nextValue(Card::Value);

#endif 


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
// deck.cpp


#include <stdlib.h>
#include "deck.h"
#include "card.h"

Deck::Deck() {
	int i;
	Card::Suit curSuit;
	Card::Value curValue;

	nextCard = 0;
	for (i = 0, curSuit = Card::Diamonds, curValue = Card::Ace; i < DECKSIZE; i++) {
		inDeck[i] = Card(curSuit, curValue);
		curValue = nextValue(curValue);
		if (curValue == Card::Ace)
			curSuit = nextSuit(curSuit);
	}
}

void Deck::shuffle(int swaps) {
	Card temp;

	for (int i = 0; i < swaps; i++) {
		int i1 = rand() % DECKSIZE;
		int i2 = rand() % DECKSIZE;
		temp = inDeck[i1];
		inDeck[i1] = inDeck[i2];
		inDeck[i2] = temp;
	}
}

Card Deck::getCard() {
	return(nextCard < DECKSIZE) ? inDeck[nextCard++] : Card(Card::Clubs, Card::NullCard);
}

void Deck::addCard(Card newCard) {
	if (nextCard > 0)
		inDeck[--nextCard] = newCard;
}

int Deck::totalCards() {
	return DECKSIZE - nextCard;
}


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
// card.cpp


#include <iostream>
#include "card.h"

using namespace std;

const char * Card::snames[4] = { " Diamonds", "Hearts", "Clubs", "Spades" };
const char * Card::vnames[14] = { " Bad Card", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };


Card::Card() {
	v = NullCard;
}

Card::Card(Suit newSuit, Value newValue) {
	s = newSuit;
	v = newValue;
}

Card::Suit Card::getSuit() {
	return s;
}

Card::Value Card::getValue() {
	return v;
}

void Card::printSuit() {
	cout << snames[s];
}

void Card::printValue() {
	cout << vnames[v];
}

void Card::printCard() {
	printValue();
	cout << " of ";
	printSuit();
}

Card::Suit nextSuit(Card::Suit s) {
	return(s + 1 > Card::Spades) ? Card::Diamonds : (Card::Suit) (s + 1);
}

Card::Value nextValue(Card::Value v) {
	return(v + 1 > Card::King) ? Card::Ace : (Card::Value) (v + 1);
}


Heres what i written so far:
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
//game1.cpp

#include <iostream>
#include "card.h"
#include "deck.h"
#include "stdlib.h"
#include "time.h"


using namespace std;

class game1 {
public:
	Card::Value cv;
	Card::Suit cs;
};

int main() {
	srand((unsigned)time(NULL));

	Deck d;
	Card c;

	Card::Value cv = Card::Ace;
	Card::Suit cs = Card::Diamonds;

	cout << "Card Count: " << d.totalCards() << endl;

	cout << "Shuffling deck..." << endl;
	d.shuffle(100);

	cout << "Dealing out card..." << endl;
	Card(cs, cv);

	d.getCard();

	cout << "Testing - Card printed: ";
	c.printValue();
	cout << " " << endl;

	cout << "Returning card back to deck..." << endl;
	d.addCard(c);

	cout << "Card count: " << d.totalCards() << endl;

	return 0;
}


the output:
1
2
3
4
5
6
7
Card Count: 52
Shuffling deck...
Dealing out card...
Testing - Card printed:  Bad Card
Returning card back to deck...
Card count: 52
Press any key to continue . . .



i declared "c" as object of type "Cards" and i declared "d" as object of type "Deck". Used some functions and im getting "Bad Card" ? Can someone explain what im doing wrong and point me to the right direction. thanks.
Last edited on
This is what produces the "Bad Card":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "card.h"
#include "deck.h"

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

int main() {
	Card c;
	cout << "Testing - Card printed: ";
	c.printValue();
	cout << " " << endl;
	return 0;
}

Your line 35 d.getCard(), isn't that a function call and doesn't that function return something?
ahh i see. Card c = d.getCard(); did the trick. Now i just have to figure out pull out 5 cards instead of 1 and how to check for flushes and pairs. Thanks
Last edited on
Have an array for five card. Use a loop to deal cards to the array. Then check the array for pairs and flush.

Repeat that routine 10000 times, but remember to take a new shuffled deck when current deck has less than five cards left.
edit: nevermind i got it, thanks!
Last edited on
Topic archived. No new replies allowed.