Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Poker Game Trouble using arrays to determine poker hands!

Status
Not open for further replies.

jasonlegg

Programmer
Apr 28, 2006
2
US
My college class is doing a project in which we have to write a program that generates a deck of cards, shuffles them (srand()) and deals five cards. After that we check if they have a pair, straight, flush, straight flush, three of a kind, four of a kind, or full house, or none of the above. I have a majority of the code written. I have to tweak the straight array to include an ace being used in a straight from the bottom (e.g. ace, 2, 3, 4, 5) and from the top (ace king queen jack 10) also i dont have the straight flush, 4 of a kind, or full house. Ill display the code i have so far. Any suggestions on the best way to do these things would help a lot. Thanks to anyone who'll help.

#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

int main()
{
srand(time(0));
int deck[52];
int i;
string suitnames[4]={"spades", "diamonds", "clubs", "hearts"};
string ranknames[13]={"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"};

// create a new deck, with cards in order, but unique
for(i=0; i<52; i++)
{
deck = i;
}

// shuffle the deck:
for(i=0; i<52; i++)
{
// generate a random index to swap with the card at index i.
int j = rand() % 52;
int temp = deck;
deck = deck[j];
deck[j] = temp;
}


// print all the cards
for(i=0; i<52; i++)
{
int suitnumber = deck / 13; // 0 - 3
int rank = deck % 13;
cout << ranknames[rank] << " of " << suitnames[suitnumber]<< "\n";
}

// Check first 5 cards for an ace
cout << endl;
for(i=0; i<5; i++)
{
int acerank = 0;
int currentCardRank = deck%13;
if(currentCardRank == acerank)
{
cout << "Got an ace!" << endl;
}
}

// Get the rank of the first 5 cards
int R[5]; // = {4, 7, 6, 3, 5}; // rank of the first 5 cards
int S[5];
for(i=0; i<5; i++)
{
R = deck%13;
S = deck/13;
}

// Sort the R array
bool swapped = false;
do
{
// 1 pass of the bubble sort
swapped = false;
for(int i=0; i<4; i++)
{
if(R > R[i+1])
{
int temp = R;
R = R[i+1];
R[i+1] = temp;
swapped = true;
}
}
}
while(swapped == true);

// print the sorted ranks and suits
for(i=0; i<5; i++)
{
cout << ranknames[R] << " of " << suitnames[S]<< "\n";
}

// Check for a straight:
if(R[1]==R[0]+1 && R[2]==R[1]+1 && R[3]==R[2]+1 && R[4]==R[3]+1)
{
cout << "You got a straight! (you must have cheated)" << endl;
}
else
{
cout << "No straight" << endl;
}

// Check first 5 cards for any pair
if(R[0] == R[1] || R[1]==R[2] || R[2]==R[3] || R[3]==R[4])
{
cout << "You got a pair!" << endl;
}
else
{
cout << "No pair" << endl;
}

// Check for a flush (all the same suit)
if(S[0] == S[1] && S[1]==S[2] && S[2]==S[3] && S[3]==S[4])
{
cout << "You got a flush!" << endl;
}
else
{
cout << "No flush" << endl;
}

// Check for straight flush

// Check for 4 of a kind

// Check for full house

return 0;
}
 
You forgot cases for 3 of a kind and 2 pair.

You should move the tests for 2, 3, 4 of a kind, straight & flush into their own functions:
Code:
bool HavePair( int, int );
bool Have3Kind( int, int, int );
bool Have4Kind( int, int, int, int );
bool HaveFlush( int, int, int, int, int );
bool HaveStraight( int, int, int, int, int );
For 2 pair just call the new HavePair() function 4 times (once for each pair of cards in your hand) and count how many times it returns true.
For 3 & 4 of a kind, just do what you did for a pair, but add another condition. Ex.
Code:
if ( ((R[0] == R[1]) && (R[1] == R[2])) || ... )
For Full House, check if you have a pair and 3 of a kind.

For a Straight Flush, check if you have a Flush & a Straight.

If you DO have a Straight Flush, you could also check if R[0] is an Ace && R[4] is a King, in which case you have a Royal Flush.

Also, you don't appear to be checking for the case of {10, J, Q, K, A} when you check for a Straight.
 
not realy an answer to your problem, but here is some simple (fast) code for determining hands.

i initialy wrote it for doing texas holdem hand rank calculations.. but the functionality is there.

If somethings hard to do, its not worth doing - Homer Simpson
 
I know it sounds selfish, but could somebody maybe post the finished code. I did that much but can't figure out the end. It would really help a lot. I could study it to figure out how to do it and learn. Thanks to anyone for the help.
 
Sorry, that would violate the terms of use for this site.
I'll show you the HaveFlush() function using your own code:
Code:
bool HaveFlush( int s1,
                int s2,
                int s3,
                int s4,
                int s5 )
{
    // Check for a flush (all the same suit)
    if ( s1 == s2 && s2 == s3 && s3 == s4 && s4 == s5 )
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
   ...
   if ( HaveFlush( S[0], S[1], S[2], S[3], S[4] ) == true )
   {
      cout << "You got a Flush!" << endl;
   }
   else
   {
      cout << "No flush." << endl;
   }
   ...
}
 
there is a mistake in this program. in this part of code:
// 1 pass of the bubble sort
swapped = false;
for(int i=0; i<4; i++)
{
if(R > R[i+1])
{
int temp = R;
R = R[i+1];
R[i+1] = temp;
swapped = true;
}
}

you only sort the rank of cards but not the suit. And it is possible to get two same cards. you should to swap the suit too. this code can solve the problem:
for(int i=0; i<4; i++)
{
if(R > R[i+1])
{
int temp = R;
R = R[i+1];
R[i+1] = temp;
swapped = true;

int temp2 = S;
S = S[i+1];
S[i+1] = temp2;
swapped = true;

...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top