Checking results of poker hands.

So after I got the dealing and sorting working right, I want to check the results of poker hands. I'm trying to get it to check that if there is 3 of a kind for example since my professor gave us some code to help us with that. The problem I'm having is that there is no underlining of code to give me any clues of what I did wrong. Here is all my cpp and header files.

Cards.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
#include <iostream>

using namespace std;

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

#include "Card.h"

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

void InitDeck (Card Deck [])
	{
	int		i;
	for (i = 0; i < 52; i++)
		{
		Deck [i].Suit	= (Suits) ((i / 13) + Hearts);
		Deck [i].Value	= (Values) (i % 13);
		}
	srand (time (0));
	}

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

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

	for (i = 0; i < 52; i++)
		Show (Deck [i]);
	}

void ShuffleDeck (Card Deck [])
	{
	int		i;
	bool	Picked [52];
	Card	TempDeck	[52];
	int		Which;
	int		Counter;

	memcpy (TempDeck, Deck, 52 * sizeof (Card));
	memset (Picked, false, 52 * sizeof (bool));		// no cards have been picked yet
	for (i = 0; i < 52; i++)
		{
		Counter = 0;
		do	{
			Which = rand () % 52;
			Counter++;
			} while (Picked [Which]);
		Picked [Which]	= true;
		Deck [i]		= TempDeck [Which];
		}
	}

void Deal(Card Hands[][handlength], Card Deck [])
{
	int i;
	for (i = 0; i < handlength; i++)
	{
	Hands[0][i] = Deck[i];
	Hands[1][i] = Deck[i + 5];
	Hands[2][i] = Deck[i + 10];
	Hands[3][i] = Deck[i + 15];
	}

	cout << "Hand 1" << endl;
	for (i = 0; i < handlength; i++)
	{
	Show (Hands [0][i]);
	}

	cout << "Hand 2" << endl;
	for (i = 0; i < handlength; i++)
	{
	Show (Hands [1][i]);
	}

	cout << "Hand 3" << endl;
	for (i = 0; i < handlength; i++)
	{
	Show (Hands [2][i]);
	}
	cout << "Hand 4" << endl;
	for (i = 0; i < handlength; i++)
	{
	Show (Hands [3][i]);
	}
}

void BubbleSort(Card Hands[][handlength])
{
	
	
	for (int j = 0; j < 4; j++)
	{
	int	i;
	bool	Sorted;
	Card	Temp;
	int Sorthand;
	Sorthand = handlength - 1;
	do	{
		Sorted = true;
		for (i = 0; i < handlength - 1; i++)
			if (Hands[j][i].Value > Hands[j][i + 1].Value)
					{
					Temp	= Hands[j][i];
					Hands[j][i] = Hands[j][i + 1];
					Hands[j][i + 1] = Temp;
					Sorted			= false;
					}
				else;
		Sorthand--;
		} while (!Sorted);
		
	}

	int i;
	
	cout << "New Sorted Hands!" << endl;
	cout << "Hand 1" << endl;
	for (i = 0; i < handlength; i++)
	{
	Show (Hands [0][i]);
	}

	cout << "Hand 2" << endl;
	for (i = 0; i < handlength; i++)
	{
	Show (Hands [1][i]);
	}

	cout << "Hand 3" << endl;
	for (i = 0; i < handlength; i++)
	{
	Show (Hands [2][i]);
	}
	cout << "Hand 4" << endl;
	for (i = 0; i < handlength; i++)
	{
	Show (Hands [3][i]);
	}
	
		
}
/*
bool IsThreeOfAKind (int check, Card Hands [players][handlength])
	{
	return	((Hands[check][0].Value == Hands [check][2].Value) ||
			 (Hands[check][1].Value == Hands [check][3].Value) ||
			 (Hands[check][2].Value == Hands [check][4].Value));
	}
Something is wrong with this check.
*/


Cards.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
#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};

struct Card
	{
	Suits	Suit;
	int		Value;
	};

const long handlength(5);
const long players(4);

void InitDeck		(Card []);
void Show			(const Card &);		// default method of passing a struct as a parameter is call by value
										// for efficiency, a struct is usually done as call by reference
										//		with const if we don't want it to change
void ShowDeck		(const Card []);
void ShuffleDeck	(Card []);
void Deal (Card [][handlength], Card[]);
void BubbleSort(Card[][handlength]);
//bool IsThreeOfAKind (int, Card [players][handlength]) <- I think something is also wrong with a 2D array

#endif


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
#include <iostream>

using namespace std;

#include "Card.h"

void main ()
	{
	Card	Deck [52];
	InitDeck (Deck);
	ShuffleDeck (Deck);

	Card Hands[4][5];

	cout << "Here Are The Hands!" << endl;
	Deal(Hands,Deck);

	BubbleSort(Hands);
	/*
	int check;

	for (check = 0; check < 4; check++)
	{
	if (IsThreeOfAKind(check,Hands) == 1)
	cout << "Player " << check <<" has a three of a kind" << endl;
	}


	*/
	}



This is the assignment

ASSIGNMENT:
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 at http://www.pagat.com/vying/pokerrank.html
a. Straight flush
b. Four of a kind
c. Full House
d. Flush
e. Straight
f. Three of a kind
g. Two pair
h. One pair
i. Highest card
5. Each time the program is run, a different set of hands is to be dealt.
BONUS: Determine which is the winning hand. A tie is possible. (25 points)
NOTES:
1. Use a structure to hold the face value and suit of each card.
2. Use enumerations for the values of Jack, Queen, King, and Ace and to identify the suits (Diamonds, Clubs, etc.).

Don't worry too much about the bonus, I just want to get the checking working. I know it will be tedious writing all the checks, but I just want help on this one check so I can do the rest.

The check is supposed to be a for loop to check each player's hand, and to go through each condition to see which hand they have. If anyone knows what is wrong, I would appreciate it. Also, if anyone has any idea on how to make the checks shorter, that would be amazing.

I could probably use help on doing the straights after this problem is fixed. My idea would be to have

Hands[player][4].Value == Hands[player][3].Value + 1 == Hands[player][2].Value + 2 == Hands[player][1].Value + 3 == Hands[player][0].Value + 4

Something like this, but I just want to get this initial check working so I can do the others easier.

My professor gave me this part of the function to help me check the three of a kind.
1
2
3
4
5
6
bool IsThreeOfAKind (Card Hand [])
	{
	return	((Hand [0].Value == Hand [2].Value) ||
			 (Hand [1].Value == Hand [3].Value) ||
			 (Hand [2].Value == Hand [4].Value));
	}


but I can't get it to work with my code.
Last edited on
Wow there are some big problems in your code!!! What compiler is letting you compile this?!
I'm using Visual Studio 2012.

Likewise, I only got the program to deal cards to each player and sort them. Everything else before that the professor gave us.

Only focus on the bottom part the professor gave us if you know how to change it with my code. The bottom is currently not in my code, and I commented out the problem. The part dealing with straights you can ignore for now until you can get the IsThreeOfAKind working. If I'm not writing the code well, tell me so I could fix stuff up for you to read it easier.
Read the 1st reply: http://www.cplusplus.com/forum/beginner/19979/

And read this: http://stackoverflow.com/questions/4872373/why-is-including-using-namespace-into-a-header-file-a-bad-idea-in-c

On your problem:

I don't know what your professor wants, and someone may reply with a better way than this, but if I were checking for those winning hands I would do this (though not necessarily in this order).

i: High card
-Place the numerical value of the first code into a temporary holder e.g. int temphigh;
-Check each of the other 4 cards in turn to see if they are higher than temphigh.
-If yes, replace temphigh with the new value
-You could also modify this to break immediately if an ace is found

h: Pair
-Look at the value of card 1 and compare the other 4 cards in turn to see if they match its numerical value, if any do, store them as a pair
-Do the same for card 2
-Then the same for card 3 etc...

g: Two pair

-Same as above but only return true if two pairs are found

f: ToaK

-Look at the value of card 1 and compare each of the other 4 cards in turn. If two others match, return true.
-Otherwise, do the same for cards 2 & 3.

e: straight
You already bubblesorted your cards right? So you can simply look if they increment by 1 each time.

d: flush
Look at suit of card 1. Check each other card in turn to see if the suits match. If ALL of them do, return true.

c: full house
-If Toak returned true
-Check the other two cards and see if they are a pair

b: 4 of a kind
-Same method as Toak but you must find 4 matching cards.

a: straight flush
If flush & straight both returned true...

Hopefully that helps.
Last edited on
Topic archived. No new replies allowed.