Scoring Deck of Cards

I know this is an old problem, but cant seem to find the help needed as most people are using vectors, in which our professor hasn't taught. I have accomplished #'s 1-3, and am trying to finish up with 4 and the bonus. I have written some additional code to sort the cards into their ranks, but haven't gotten it work so its commented out at the bottom of main. Any help to finish this problem and the bonus(needed lol) is appreciated.

*Edit*
Just need help with the scoring now, i have so far completed step #4


Write a program that will operate on a deck of 52 cards. Your program will:
1. Deal them into four random hands of 5 cards each
a. The four hands need to be kept in an array for later use.
2. Sort each hand so that it shows the cards in sequence from two as the lowest to ace as the highest.
3. Display the cards in each hand using the card face (2, 10, King, etc.) and the suit (Spades, Hearts, etc.)
4. Display what you determined the hand to be. A ranking of poker hands is

BONUS: Determine which is the winning hand. A tie is possible. (25 points)


Card.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
#ifndef CARD_H
#define CARD_H

enum Suits { Hearts = 3, Diamonds, Clubs, Spades };
enum Values { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace };

const int NumCardsInDeck(52);

struct Card
{
	Suits	Suit;
	Values	Value;
};

void Display(const Card &);	
void InitDeck(Card[]);
void SortHand(Card[]);
void ShowDeck(const Card[]);
void ShowHand(Card[]);
void Shuffle(Card[]);
void Deal(Card[]);
bool HighestCard(Card[]);
bool OnePair(Card[]);
bool IsThreeOfAKind(Card[]);
bool IsTwoPair(Card[]);
bool IsFourOfAKind(Card Hand[]);
bool IsFullHouse(Card Hand[]);




#endif  


Card.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <iostream>

using namespace std;

#include <stdlib.h>
#include <time.h>
#include <string.h>

#include "Card .h"

char * ValueNames[13] = {
	"Two",
	"Three",
	"Four",
	"Five",
	"Six",
	"Seven",
	"Eight",
	"Nine",
	"Ten",
	"Jack",
	"Queen",
	"King",
	"Ace"
};

void Display(const Card & C)
{
	cout << (char)C.Suit << ' ' << ValueNames[C.Value] << endl;
}

void InitDeck(Card Deck[])
{
	int		i;
	Suits	S;
	Values	V;

	srand(time(0));	
	i = 0;
	for (S = Hearts; S <= Spades; S = (Suits)(S + 1))
		for (V = Two; V <= Ace; V = (Values)(V + 1))
		{
			Deck[i].Suit = S;
			Deck[i].Value = V;
			i++;
		}
}

void SortHand(Card Hand[])
{
	int		i;
	int		j;
	int		CardOne;
	int		CardTwo;
	Card	Temp;



	for (j = 0; j < 5; j++)
	{
		CardOne = 0;
		CardTwo = 1;
		for (i = 0; i <= 4; i++)
		{
			if (Hand[CardOne].Value > Hand[CardTwo].Value)
			{
				Temp = Hand[CardOne];
				Hand[CardOne] = Hand[CardTwo];
				Hand[CardTwo] = Temp;
			}
			else;

			CardOne++;
			CardTwo++;
			i++;
		}
	}
	if (Hand[3].Value > Hand[4].Value)
	{
		Temp = Hand[3];
		Hand[3] = Hand[4];
		Hand[4] = Temp;
	}
	if (Hand[2].Value > Hand[3].Value)
	{
		Temp = Hand[2];
		Hand[2] = Hand[3];
		Hand[3] = Temp;
	}

	if (Hand[1].Value > Hand[2].Value)
	{
		Temp = Hand[1];
		Hand[1] = Hand[2];
		Hand[2] = Temp;
	}
	if (Hand[0].Value > Hand[1].Value)
	{
		Temp = Hand[0];
		Hand[0] = Hand[1];
		Hand[1] = Temp;
	}

}

void ShowDeck(const Card Deck[])
{
	int		i;

	for (i = 0; i < NumCardsInDeck; i++)
		Display(Deck[i]);
}

void ShowHand(Card Deck[])
{
	int		i;
	for (i = 0; i < 5; i++)
		Display(Deck[i]);
}

void Shuffle(Card Deck[])
{
	int		CardOne;
	int		CardTwo;
	Card	Temp;

	for (CardOne = 0; CardOne <NumCardsInDeck; CardOne++)
	{
		CardTwo = rand() % NumCardsInDeck;
		Temp = Deck[CardOne];
		Deck[CardOne] = Deck[CardTwo];
		Deck[CardTwo] = Temp;
	}
}

void Deal(Card Deck[])
{
	int		CardOne;
	int		CardTwo;
	Card	Temp;

	for (CardOne = 0; CardOne < 5; CardOne++)
	{
		CardTwo = rand() % 5;
		Temp = Deck[CardOne];
		Deck[CardOne] = Deck[CardTwo];
		Deck[CardTwo] = Temp;
	}
}

bool	HighestCard(Card Hand[])
{
	if (Hand[0].Value != Hand[1].Value && Hand[0].Value != Hand[2].Value && Hand[0].Value != Hand[3].Value
		&& Hand[0].Value != Hand[4].Value && Hand[1].Value != Hand[2].Value && Hand[1].Value != Hand[3].Value &&
		Hand[1].Value != Hand[4].Value && Hand[2].Value != Hand[3].Value && Hand[2].Value != Hand[4].Value && Hand[3].Value != Hand[4].Value)
	{
		return true;
		cout << "The High Card " << endl;
	}
	else
		return false;
}

bool	OnePair(Card Hand[])
{
	if ((Hand[0].Value == Hand[1].Value && Hand[0].Value != Hand[2].Value) ||
		(Hand[1].Value == Hand[2].Value && Hand[1].Value != Hand[3].Value) ||
		(Hand[2].Value == Hand[3].Value && Hand[2].Value != Hand[4].Value) ||
		Hand[3].Value == Hand[4].Value)
	{
		return true;
	}
	else
		return false;
}

bool IsTwoPair(Card Hand[])
{
	if ((Hand[0].Value == Hand[1].Value && Hand[2].Value == Hand[3].Value) || (Hand[1].Value == Hand[2].Value && Hand[3].Value == Hand[4].Value) || (Hand[0].Value == Hand[1].Value && Hand[3].Value == Hand[4].Value))
	{
		return true;
		cout << "Two Pair" << endl;
	}
	else
		return false;
}

bool IsThreeOfAKind(Card Hand[])
{
	if ((Hand[0].Value == Hand[2].Value) || (Hand[1].Value == Hand[3].Value) || (Hand[2].Value == Hand[4].Value))
	{
		return true;
		cout << "Three of a Kind" << endl;
	}
	else
		return false;
}

bool IsFourOfAKind(Card Hand[])
{
	if ((Hand[0].Value == Hand[1].Value && Hand[0].Value == Hand[2].Value && Hand[0].Value == Hand[3].Value) || (Hand[1].Value == Hand[2].Value && Hand[1].Value == Hand[3].Value && Hand[1].Value == Hand[4].Value))
	{
		return true;
		cout << "Four of a Kind" << endl;
	}
	else
		return false;
}

bool IsFullHouse(Card Hand[])
{
	if ((Hand[0].Value == Hand[1].Value && Hand[0].Value == Hand[2].Value && Hand[3].Value == Hand[4].Value) || (Hand[0].Value == Hand[1].Value && Hand[2].Value == Hand[3].Value && Hand[2].Value == Hand[4].Value))

	{
		return true;
		cout << "Full House" << endl;
	}
	else
		return false;
}

Last edited on
Main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>

using namespace std;

#include "Card .h"

void main()
{
	Card	Deck[52];
	Card	Hands[4][5];
	int		i, j;
	const int StartingRank = 2;
	const int NumRanks = 13;


	InitDeck(Deck);
	cout << "\t The deck starts as " << endl;
	ShowDeck(Deck);
	Shuffle(Deck);
	cout << "\t After shuffling it is" << endl;
	ShowDeck(Deck);

	
	j = 0;

	for (i = 0; i < 5; i++)
	{
		Hands[0][i] = Deck[j];
		j++;
	}
	SortHand(Hands[0]);
	cout << "Hand 1 is: " << endl;
	if (HighestCard(Hands[0]))
		cout << "\tHighest Card" << endl;
	if (OnePair(Hands[0]))
		cout << "\tOne Pair" << endl;
	if (IsThreeOfAKind(Hands[0]))
		cout << "\tThree of a kind" << endl;
	if (IsTwoPair(Hands[0]))
		cout << "\tTwo Pair" << endl;
	if (IsFourOfAKind(Hands[0]))
		cout << "\tFour of a Kind" << endl;
	if (IsFullHouse(Hands[0]))
		cout << "\tFull House" << endl;

		for (i = 0; i < 5; i++)
	{
		Display(Hands[0][i]);
	}



	for (i = 0; i < 5; i++)
	{
		Hands[1][i] = Deck[j];
		j++;
	}
	SortHand(Hands[1]);
	cout << "Hand 2 is: " << endl;
	if (HighestCard(Hands[1]))
		cout << "\tHighest Card" << endl;
	if (OnePair(Hands[1]))
		cout << "\tOne Pair" << endl;
	if (IsThreeOfAKind(Hands[1]))
		cout << "\tThree of a kind" << endl;
	if (IsTwoPair(Hands[1]))
		cout << "\tTwo Pair" << endl;
	if (IsFourOfAKind(Hands[1]))
		cout << "\tFour of a Kind" << endl;
	if (IsFullHouse(Hands[1]))
		cout << "\tFull House" << endl;
	for (i = 0; i < 5; i++)
	{
		Display(Hands[1][i]);
	}

	for (i = 0; i < 5; i++)
	{
		Hands[2][i] = Deck[j];
		j++;
	}
	SortHand(Hands[2]);
	cout << "Hand 3 is: " << endl;
	if (HighestCard(Hands[2]))
		cout << "\tHighest Card" << endl;
	if (OnePair(Hands[2]))
		cout << "\tOne Pair" << endl;
	if (IsThreeOfAKind(Hands[2]))
		cout << "\tThree of a kind" << endl;
	if (IsTwoPair(Hands[2]))
		cout << "\tTwo Pair" << endl;
	if (IsFourOfAKind(Hands[2]))
		cout << "\tFour of a Kind" << endl;
	if (IsFullHouse(Hands[2]))
		cout << "\tFull House" << endl;
	for (i = 0; i < 5; i++)
	{
		Display(Hands[2][i]);
	}


	for (i = 0; i < 5; i++)
	{
		Hands[3][i] = Deck[j];
		j++;
	}
	SortHand(Hands[3]);
	cout << "Hand 4 is: " << endl;
	if (HighestCard(Hands[3]))
		cout << "\tHighest Card" << endl;
	if (OnePair(Hands[3]))
		cout << "\tOne Pair" << endl;
	if (IsThreeOfAKind(Hands[3]))
		cout << "\tThree of a kind" << endl;
	if (IsTwoPair(Hands[3]))
		cout << "\tTwo Pair" << endl;
	if (IsFourOfAKind(Hands[3]))
		cout << "\tFour of a Kind" << endl;
	if (IsFullHouse(Hands[3]))
		cout << "\tFull House" << endl;
	for (i = 0; i < 5; i++)
	{
		Display(Hands[3][i]);
	}

}
Last edited on
Please edit your posts and fix your code tags. There is no / on the opening code tag.
My apologies , it has been fixed.
I'm examining your code to try to help with the bonus, but it's difficult because the suit symbols appear as question mark boxes.
You have the right idea with bool functions for each possible hand. What you need is a function to assign a ranking to each 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
enum hand_rank
{   HIGH_CARD = 1, 
    ONE_PAIR = 2, 
    TWO_PAIR = 3,
    THREE_OF_A_KIND = 4,
    STRAIGHT = 5, 
    FLUSH = 6, 
    FULL_HOUSE = 7, 
    FOUR_OF_A_KIND = 8,
    STRAIGHT_FLUSH = 9
};

struct Card
{   Suits	Suit;
	Values	Value;
};

//  To simplify things, I've created a struct for a single hand
struct Hand
{   Card    card[5];
};

Hand    hands[4]; 

//  Must check in descending order
hand_rank classify_hand (Hand & hand)
{   if (IsStraightFlush(hand))
        return STRAIGHT_FLUSH;
    if (IsFourOfAKind(hand)) 
		return FOUR_OF_A_KIND;        
    if (IsFullHouse(hand))  
        return FULL_HOUSE;      
    if (IsFlush(hand))
        return FLUSH;        
    if (IsStraight(hand))
        return STRAIGHT;        
    if (IsThreeOfAKind(hand))
        return THREE_OF_A_KIND;        
    if (IsTwoPair(hand)) 
        return TWO_PAIR;
    if (IsOnePair(hand)) 
        return ONE_PAIR;
    return HIGH_CARD;	    
} 

Call classify_hand for each hand. Now, finding the highest hand becomes a simple matter of finding the hand with the highest enum value. Don't forget that when comparing multiple hands of the same rank, you also need to find the highest of the hands. Simple case: if two hands have two pair, you need to identify the hand with the highest pair.

card.cpp line 30: Do not call srand() within a loop or a random number function. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

main.cpp When you have repeated code, that's a good clue that you need a function.

p.s. Are you writing this in C or C++? Your use of cout implies C++, but your headers and coding style imply C.
Last edited on
Topic archived. No new replies allowed.