# C++ Newbie Poker Program

• 05-19-2005
Denied88
C++ Newbie Poker Program
Hello everyone, I currently have created a poker program and just recently due to some help from everyone stopped repeating cards. It deals five cards to you, and after you discard a maximum of three cards shows you the dealers cards. I still need the program to "weigh" each hand and see which hand will win. Any suggestions?

-Denied
• 05-19-2005
quzah
I'd say looking at the cards in each hand would be a good start. How do you figure out who wins when you play cards? I bet you compare them with known winning combinations...

Quzah.
• 05-19-2005
Darryl
I just completed a poker framework. What it does is first ranks each hand
Code:

```/* ranks a hand based on Poker ranking         1 - High Card         2 - 1 Pair         3 - 2 Pair         4 - 3 of a kind         5 - Straight         6 - Flush         7 - Full House         8 - 4 of a kind         9 - Straight Flush         10 - Royal flush         */```
If two hands have same rank, then it must do a "tiebreaker" compare to see for example who has highest pair if both hands have 1 pair.

I can provide actually code if you'd like to see it.
• 05-20-2005
Denied88
The only way I can think to give the cards values like that is to compare the cards with eachother. I. E.

Code:

```if (card 1 = card 2)  {   Then convert to a pair value } if (card 1 = card 3)  {   Then convert to a pair value } and so on..```
Does anyone know of an easier way?
• 05-20-2005
Denied88
So far, I've tried comparing the cards individually. Unfortunately I've discovered that it would probably take somewhere around 10,000 lines of code to write.

I think, due to the fact that you can't have multiple conditions inside an if statement I would need 3 nested if then statements each containing 10 possible card pair combinations.

A loop would be nice, but I can't use a loop in this format.

Code:

```if (card1 = card2)                                                pair = true; }                                                 if (card1 = card3)        {                                                         pair = true; } if (card1 = card4)        {                                                         pair = true; } if (card1 = card5)        {                                                         pair = true; }                                                 if (card2 = card3)        {                                                         pair = true; } if (card2 = card4)        {                                                         pair = true; } if (card2 = card5)        {                                                         pair = true; } if (card3 = card4)        {                                                         pair = true; } if (card3 = card5)        {                                                         pair = true; } if (card4 = card5)        {                                                         pair = true; }```
this is the code I have been toying around with so far, and unless i'm mistaken it would take about 10,000 copies of these inside of eachother to work properly. Any thoughts?

-Denied
• 05-20-2005
JoshR
well you should make sure your program has two cards each with its own value, then lets say you have cardA and then you have cardB you only need to compare them once. btw dont forget the second '='

Code:

```if (cardA == cardB)   pair = true;```
so if you wanted to, look through a hand, you should rotate through using a for statement and compare.

Code:

```for (int count = 0; count < 5; count++) {   for (int inner = 0; inner < 5; inner++)       if (cardinmyhand[count] == cardinmyhand[inner])         pair = true; }   /* [....etc....]*/```
it might look close to bubble sort.
• 05-20-2005
Darryl
Insert the cards by value(not suit, ie. 3 of hearts value is 3) into a container such as set that doesn't allow duplicates. Once you have put all 5 cards from your hand in, these will be true.

if set.size() = 5 you have no pairs but you might have a straight or a flush
if set.size() = 4 you have 1 pair
if set.size() = 3 you have 2 pair or 3 of a kind
if set.size() = 2 you have either a full-house or 4 of a kind

now if you understand that, instead of using a set, use a map with the card value as the key and an int count as the value so that you are counting duplicates now as opposed to just eliminating them as above, then you can differentiate between full-house and 4 of a kind and between 3 of a kind and 2 pair :

here some actual code from my program, hope it helps
Code:

```std::map<Card::Value, int> CardSorter;         for (unsigned int i = 0; i < hand.size(); ++i)         {                 //first test if map contains key already                 if (CardSorter.find(hand[i].getValue())==CardSorter.end())                         //It doesn't so insert it.                         CardSorter[hand[i].getValue()]=1;                 else                       //It does so increment what we have                         CardSorter[hand[i].getValue()]++;         }```
if map.size = 2 you have either a full-house or 4 of a kind
iterate through the 2 map elements
if either map element value is 4, then it's 4 of a kind else its full house

if map.size = 3 you have either 2 pair or 3 of a kind
iterate through the 3 map elements
if any map element value is 3, then it's 3 of a kind else its two pair

Ok, I'll leave you with that to get you started....
• 05-20-2005
As a newbie to C++ you may not be aware of sets/maps as described by Darryl, but I'm sure you can see part of their usefulness. If you aren't familiar with these containers you can work from the other end up. Look at each card compared with another, and if of equal value increment a counter. At end of comparison if counter for a given card is higher than current max counter change max counter. At the end of the comparisons, if max counter is 1 then you have no matches but you could have a flush or a straight. If max counter is 4 then you have four of a kind. If max counter is 3 then you have three of a kind or a full house. If max counter is 2 then you have one or two pairs.
Code:

```struct Card {   int value;          //etc. };   struct Hand {   Card cards[5];   int value;   void valueHand();   //etc. };   void Hand::valueHand() {   int counter;   int maxCounter = 1;   int i, j;   for(i = 0; i < 5; ++i)   {         counter = 0;         for(j = i; j < 5; ++j)         {         if(cards[i].value == cards[j].value)                 ++counter;         }         if(counter > maxCounter)                 maxCounter = counter;   }     switch(maxCounter)   {         case 1:                 cout << "look for straight or flush";                 //etc.                 break;         case 2:                 cout << "one or two pairs";                 //etc.                 break;         case 3:                 cout << "three of a kind or full house";                 //etc.                 break;         case 4:                 cout << "four of a kind";                 //etc.                 break;         default:                 cout << "deadman's hand or wild cards";                 break;   } }```
• 05-23-2005
Denied88
Yeah, I didn't exactly know what darryl was talking about, but I got the general idea. Thanks for the help guys i've got some experimenting to do.
• 05-24-2005
Denied88
Well guys, I made a multi-dimensional deck array instead of a card array. My deck array consists of a possible 13 face values and a possible 4 suit values. Because I used a multi-dimensional array I think it would be extremely difficult to use the exact same method as you guys. However, I have made a variation of it by saying:

Code:

```                                                if (card == 1)                                                         card1 = wFace[column];                                                 if (card == 2)                                                         card2 = wFace[column];                                                 if (card == 3)                                                         card3 = wFace[column];                                                 if (card == 4)                                                         card4 = wFace[column];                                                 if (card == 5)                                                         card5 = wFace[column];```
this is supposed to take the face value from the deck array and place it into the card's own variable value. It seems to compile properly, but gets an error when executing and when I debug saying "expression cannot be evaluated"

my wFace is the pointer I used to get the card's face values, and the column is the 13 possible array in the array "deck."

For some reason it made me initialize card1, card2... as const char * so that they could be compared to the wFace[column] value. If they have the same variable type, why can't they compare?
• 05-24-2005
Darryl
Sorry if I went over your head last time . . . sometimes I do that to encourage programmers to "reach" a bit.
Quote:

For some reason it made me initialize card1, card2... as const char * so that they could be compared to the wFace[column] value. If they have the same variable type, why can't they compare?
How did you define them? How did you compare them. Let's see some code.
• 05-25-2005
Denied88
Code:

```int main() {         const char *suit[ 4 ] =         { "Hearts" , "Diamonds" , "Clubs" , "Spades" };         const char *face[ 13 ] =                 {"Ace", "Deuce" , "Three", "Four",                 "Five", "Six", "Seven", "Eight",                 "Nine", "Ten", "Jack", "Queen", "King" };         int deck [ 4 ] [ 13 ] = { 0 };```
This is where I initialize my const char *face. So naturally, I wanted to initialize the card1, card2... etc. in the same way so that they wouldn't have a compatability issue. So I initialized card1, and so on like:

Code:

```        const char *card1;         const char *card2;         const char *card3;         const char *card4;         const char *card5;```
and it still says that it has an error when trying to compare them like so:

Code:

`card1 = wFace[column];`
Any ideas?
• 05-25-2005
Darryl
Quote:

Originally Posted by Denied88
Code:

```int main() {         const char *suit[ 4 ] =         { "Hearts" , "Diamonds" , "Clubs" , "Spades" };         const char *face[ 13 ] =                 {"Ace", "Deuce" , "Three", "Four",                 "Five", "Six", "Seven", "Eight",                 "Nine", "Ten", "Jack", "Queen", "King" };         int deck [ 4 ] [ 13 ] = { 0 };```
This is where I initialize my const char *face. So naturally, I wanted to initialize the card1, card2... etc. in the same way so that they wouldn't have a compatability issue. So I initialized card1, and so on like:

Code:

```        const char *card1;         const char *card2;         const char *card3;         const char *card4;         const char *card5;```
and it still says that it has an error when trying to compare them like so:

Code:

`card1 = wFace[column];`
Any ideas?

1. = is assignment, == is compare
2. you can't compare c-strings(char arrays) using ==, you have to use strcmp()
3. what is wFace? you only gave definition for const char *face[ 13 ]