how to pass object to member function? - poker game

Hi im a beginner at c++ and i need help passing an object to a member function. For the poker game assignment, the first part was to write a program using the 4 included files (deck.h, card.h, deck.cpp, card.cpp) to deal out 5 card hands, check for pairs/flushes, do it 10000 times for 10 trials. I was able to do that, but the second part of the assignment was to now make a "hand" class to deal out the 5 card hands, do everything else, and run it in a new file called "game2.cpp". For this assignment, how would i pass a "Deck" object to member function of class "Hand" to deal out the 5 cards?

Here are the included headers for the assignment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//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
//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 


What i written for the first part of the assignment:
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//game1.cpp

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


using namespace std;

int main() {
	int trial = 0;
	int pair = 0;
	int flush = 0;
	int hands = 0;
	int trialpair[10];
	int trialflush[10];
	double percFlush[10];
	double percPair[10];
	double avgFlush;
	double avgPair;

	srand((unsigned)time(NULL));

	Deck d;
	Card c[5];

	Card::Value cardValue[5];
	Card::Suit cardSuit[5];

	for (int t = 0; t < 10; ++t) {
		++trial;

		for (int i = 0; i < 10000; ++i) {
			d.shuffle(100);

			for (int i = 0; i < 5; ++i) {
				c[i] = d.getCard();

				cardValue[i] = c[i].getValue();
				cardSuit[i] = c[i].getSuit();
			}

			++hands;

			if (cardValue[0] == cardValue[1] || cardValue[1] == cardValue[2] || cardValue[2] == cardValue[3] || cardValue[3] == cardValue[4]) {
				++pair;
			}

			if (cardSuit[0] == cardSuit[1] && cardSuit[1] == cardSuit[2] && cardSuit[2] == cardSuit[3] && cardSuit[3] == cardSuit[4]){
				++flush;
			}

			for (int i = 0; i < 5; i++) {
				d.addCard(c[i]);
			}

		}

		trialflush[t] = flush;
		trialpair[t] = pair;

		percFlush[t] = ((trialflush[t]) / (10000.00)) * 100;
		percPair[t] = ((trialpair[t]) / (10000.00)) * 100;

		cout << "[Trial]: " << trial << endl;
		cout << "Hands: " << hands << endl;
		cout << "flush: " << trialflush[t] << endl;
		cout << "pair: " << trialpair[t] << endl;

		cout << " " << endl;

		cout << "flush %: " << setprecision(2) << percFlush[t] << endl;
		cout << "pair %: " << setprecision(2) << percPair[t] << endl;
		cout << "-----------------------------------------------" << endl;

		hands = 0;
		flush = 0;
		pair = 0;
	}

	avgFlush = (percFlush[0] + percFlush[1] + percFlush[2] + percFlush[3] + percFlush[4] + percFlush[5] + percFlush[6] + percFlush[7] + percFlush[8] + percFlush[9]) / (10.00);
	avgPair = (percPair[0] + percPair[1] + percPair[2] + percPair[3] + percPair[4] + percPair[5] + percPair[6] + percPair[7] + percPair[8] + percPair[9]) / (10.00);

	cout << "Average Flush %: " << avgFlush << endl;
	cout << "Average Pair %: " << avgPair << endl;

	return 0;
}


How would i translate that to a working hand class and run the code in main() called game2.cpp?

im not sure what i have written so far for second part is correct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//hand.h
#ifndef _HAND_H
#define _Hand_H

#include "card.h"
#include "deck.h"


class Hand {
public:
	Deck dealHand(Deck);
private:
	Card c[5];
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Hand.cpp
#include <iostream>
#include "card.h"
#include "deck.h"
#include "hand.h"

using namespace std;

Deck Hand::dealHand(Deck d) {
	d.shuffle(100);

	for (int i = 0; i < 5; i++) {
		c[i] = d.getCard();

		cardValue[i] = c[i].getValue();
		cardSuit[i] = c[i].getSuit();
	}
	return d;
}


then for main (), if i want to deal 10000 hands do i do something like
1
2
3
4
5
6
7
8
int main () {
Deck d;
Hand h[10000];
for ( int i = 0; i <10000; i++)
h[i] = Hand::dealHand(d);
{
return 0
}

Last edited on
First of all, you should pass by reference if you want changes inside function to affect original argument.

Second: Is dealHand should be a member function of Hand? Isn't it more suited to Deck to deal cards?
i think i got it working by reference. but when i check for pairs and flushes, sometimes it counts it wrong? what did i do wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//hand.h
#ifndef _HAND_H
#define _Hand_H

#include "card.h"
#include "deck.h"


class Hand {
public:
	void dealCards(Deck& mydeck);

	void print();

	Card::Value cardValue[5];
	Card::Suit cardSuit[5];
	int pair = 0;
	int flush = 0;
	int hands = 0;
private:
	Card c[5];
};

#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
46
47
48
//hand.cpp
#include <iostream>
#include "card.h"
#include "deck.h"
#include "hand.h"
#include <iomanip> 

using namespace std;


void Hand::dealCards(Deck& d) {
	for (int i = 0; i < 5; i++) { // deals out 5 cards
		c[i] = d.getCard(); //returns a card to card object array

		cardValue[i] = c[i].getValue(); //returns value to cardvalue array
		cardSuit[i] = c[i].getSuit(); //returns suit to cardsyut array

	}
	cout << "Card: " << cardValue[0] << " of " << cardSuit[0] << endl; //testing
	cout << "Card: " << cardValue[1] << " of " << cardSuit[1] << endl;
	cout << "Card: " << cardValue[2] << " of " << cardSuit[2] << endl;
	cout << "Card: " << cardValue[3] << " of " << cardSuit[3] << endl;
	cout << "Card: " << cardValue[4] << " of " << cardSuit[4] << endl;

	//finding aleast a pair
	if (cardValue[0] == cardValue[1] || cardValue[1] == cardValue[2] || cardValue[2] == cardValue[3] || cardValue[3] == cardValue[4]) {
		++pair;
	}

	//finding a flush
	if (cardSuit[0] == cardSuit[1] && cardSuit[1] == cardSuit[2] && cardSuit[2] == cardSuit[3] && cardSuit[3] == cardSuit[4]){
		++flush;
	}

	print();

	for (int i = 0; i < 5; i++) { //adds card objects back to deck
		d.addCard(c[i]);
	}
}


void Hand::print(){
	cout << "Flush: " << flush << endl;
	cout << "Pair: " << pair << endl;
	cout << " " << endl;

}


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
//game2.cpp
#include <iostream>
#include "card.h"
#include "deck.h"
#include "hand.h"
#include "time.h"
#include "stdlib.h"

using namespace std;

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

	Hand h[10];
	Deck d;
	int trial = 0;

	for (int t = 0; t < 2; t++) { //2 trials
		trial++;
		cout << "Trial: " << trial << endl;

		for (int i = 0; i < 2; i++) { // draw 2 hands
			d.shuffle(100); //shuffle deck

			h[i].dealCards(d); //deal 5 cards
		}
	}

	return 0;
}


The output:

Trial: 1
Card: 4 of 0
Card: 6 of 3
Card: 7 of 3
Card: 9 of 0
Card: 3 of 2
Flush: 0
Pair: 0

Card: 4 of 0
Card: 12 of 1
Card: 11 of 0
Card: 11 of 2
Card: 6 of 1
Flush: 0
Pair: 1

Trial: 2
Card: 5 of 0
Card: 7 of 3
Card: 2 of 3
Card: 7 of 1
Card: 1 of 1
Flush: 0
Pair: 0

Card: 8 of 0
Card: 1 of 0
Card: 5 of 0
Card: 12 of 0
Card: 12 of 3
Flush: 0
Pair: 2

Press any key to continue . . .


like in this output, it showing 2 pairs for the last hand when there is only 1 pair
Last edited on
What if first and fourth card are pair? Will your condition catch it?
I agree with MiiNiPaa it makes much more sense to have the Deal() function in Deck, and pass a Hand object to it. It may also be beneficial to have individual functions for testing pairs, etc within Hand instead of just lumping it all into one bit of code.

Create Hand
Deal Hand
Check Hand for pairs
Check hand for 3-of-a-kind
.. and so on

May make things less confusing to sort through.
Last edited on
I cant make any changes to the deck and card files.

Heres the full instructions for part 2 of the assignment:
Create a new program, game2.h and game2.cpp, using the Card and Deck classes outlined above ( again without making any changes in either of these two classes.) Instead of placing all of the code for a poker hand in game1() as you did in the first part, the code for a poker hand will now reside within its own class, Hand.

All of the code that you wrote to manipulate the poker hand (check for pair, check for flush, deal hand, return cards to the deck at the end of each hand, ect) will now become the member functions of this new Hand Class. The private data members of the Hand class will contain the cards in the hand (i.e., an array of Card objects). This new class should reside in its own sources files, Hand.h and hand.cpp.

Keep in mind that as you write the member functions of the Hand class, you will also need to rewrite main() to accommodate these changes. You should find that there is much less code in game2.cpp than game 1.cpp

Hint: There is no need for declaration of a Deck in the Hand class. However, a Deck object could be passed to a member function of the Hand class.
Last edited on
if you can't change the classes then what you have is fine. as MiiNiPaa pointed out you aren't comparing each card in the hand so your pair counts will not be right. Maybe a loop such as this?:

1
2
3
for(int i = 0; i < 5; ++i)
  for(int i2 = i+1; i2 < 5; ++i2)
    if(c[i].getValue() == c[i2].getValue()) ++pair;


Ahh, i get it now Texan40. Thanks. However, ended up turning in the assignment with the wrong algorithm to check for pairs lol. Yours makes so much sense.

1
2
3
if (cardValue[0] == cardValue[1] || cardValue[1] == cardValue[2] || cardValue[2] == cardValue[3] || cardValue[3] == cardValue[4] || cardValue[4] == cardValue[0] || cardValue[0] == cardValue[2] || cardValue[0] == cardValue[3] || cardValue[1] == cardValue[3] || cardValue[1] == cardValue[4] || cardValue[2] == cardValue[4]) {
				++pair;
}
Last edited on
Topic archived. No new replies allowed.