This assignment is due at midnight. i have been working on it for the better part of a week and I still can't figure out how to analyze poker hands for what kind of hand they are. I have looked around the internet for ideas to no avail. I am supposed to do this without sorting because we don't know how to do that yet. My code only reveals that I have NO idea what I'm doing. Most of it (besides what's in the functions) is what my teacher gave me to start off with. Please help. Here's my code:

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>

using namespace std;

string suits[] = {"Hearts", "Spades", "Diamonds", "Clubs"};
string values[] = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
//function for each type of hand Flushes, straights, etc.

vector<int> highs;

int getSuit(int card){
return card / 13;   
}

int getValue(int card){
    return card % 13;
}

void print(int d[], int length){
for(int i = 0; i < length; i++){
    cout << values[ getValue(d[i]) ]<< " of " << suits[ getSuit(d[i]) ]<< endl;
}
}

void shuffle(int d[], int length){
for(int i = 0; i < length; i++){
    int other = rand() % length;
    int temp = d[i];
    d[i] = d[other];
    d[other] = temp;

}
}

//example function given by teacher (not needed for assignment)
/*bool royalty(int h[], int length){
int num_kings = 0;
int num_queens = 0;

for(int i = 0; i < length; i++){
    if(getValue(h[i]) == 11){ //king
        num_kings++;
    }
    if(getValue(h[i]) == 10){ //queen
        num_queens++;
    }
}
return num_kings > 0 && num_queens > 0;
}*/
int averageHigh(vector<int> array){
    int sum = 0;
for(int i = 0; i < array.size(); i++){
    sum += array[i];
}
return sum/array.size();
}

int highCard(int h[], int length){
    //stuff

int tempHigh = getValue(h[0]);
for(int i = 0; i < length; i++){
    if(getValue(h[i]) > tempHigh){
        tempHigh = getValue(h[i]);
    }
}
highs.push_back(tempHigh);
return tempHigh;
}

bool straight(int h[], int length){
//scan till you find next highest etc then return false
//for(int i = 0; i < length; i++){
    //if(/*confusion*/){
        /*more confusion*/
    //}else{
    //  return false; //??
//}
}

bool flush(int h[], int length){
//stuff
if(getSuit(h[0]) == getSuit(h[1]) && getSuit(h[1]) == getSuit(h[2]) && getSuit(h[2]) == getSuit(h[3]) && getSuit(h[3]) == getSuit(h[4])){
    return true;
}else{
    return false;
}
/*for(int i = 0; i < length; i++){
    if(getSuit(h[i]) == getSuit(h[i + 1])){
        flush++;
    }else{
        return 0;
    }
}*/
}

bool straightFlush(int h[], int length){
//stuff
}

bool royalFlush(int h[], int length){
//stuff

}

/*int strength(int h[], int length){
    //stuff
if(royalFlush(h[], length)){
    return 9;
}else if(straightFlush(int h[], length)){
    return 8;
}else if(flush(int h[], length)){
    return 7;
}else if(straight(int h[], length)){
    return 6,
}else{
    return 5;
}
}*/

int main(){

srand(time(0));
int deck[52]; // contains garbage numbers.

float times = 0;

cout << "How many games do you want to simulate?" << endl;
cin >> times;

for(int i = 0; i < 52; i++){
    deck[i] = i;
}

int straight_count = 0;
int flush_count = 0;
int straightFlush_count = 0;
int royalFlush_count = 0;

for(int i = 0; i < times; i++){
    shuffle(deck,52);
    int hand1[5];
    int hand2[5];
    //print(deck, 52);

    for(int i = 0; i < 5; i++){
        hand1[i] = deck[i];
        hand2[i] = deck[i + 5];
    }

    /*cout << "Hand 1:" << endl;
    print(hand1, 5);
    cout << endl;
    cout << "Hand 2:" << endl;
    print(hand2, 5);
    cout << endl;
    //cout << royalty(hand1, 5) << endl;
    //cout << royalty(hand2, 5) << endl; */

    if(straight(hand1, 5)){
        straight_count++;
    }
    if(straight(hand2, 5)){
        straight_count++;
    }
    if(flush(hand1, 5)){
        flush_count++;
    }
    if(flush(hand2, 5)){
        flush_count++;
    }
    if(straightFlush(hand1, 5)){
        straightFlush_count++;
    }
    if(straightFlush(hand2, 5)){
        straightFlush_count++;
    }
    if(royalFlush(hand1, 5)){
        royalFlush_count++;
    }
    if(royalFlush(hand2, 5)){
        royalFlush_count++;
    }

    highCard(hand1, 5);
    highCard(hand2, 5);
    straight(hand1, 5);
    straight(hand2, 5);
    flush(hand1, 5);
    flush(hand2, 5);
}

cout << "Average High Card Value: " << averageHigh(highs) << endl;
cout << "Percent of Straight Hands: " << straight_count/times * 100 << "%" << endl;
cout << "Percent of Flush Hands: " << flush_count/times * 100 << "%" << endl;
cout << "Percent of Straight Flush Hands: " << straightFlush_count/times * 100 << "%" << endl;
cout << "Percent of Royal Flush Hands: " << royalFlush_count/times * 100 << "%" << endl;

Here's the teacher's specific instructions on how it's graded:
10% - Programs follows style guidelines and has header information.
10% - Filename is pokerHands.cpp.
10% - Produces random decks of cards.
10% - Is able to detect a Royal Flush and correct percentage.
10% - Is able to detect a Straight Flush and correct percentage.
10% - Is able to detect a Flush and correct percentage.
10% - Is able to detect a Straight and correct percentage.
10% - Correctly reports the high card (value) and correct percentage.
10% - Overall correct structure of program; accepts # rounds, etc.
10% - Correct reported percentages of tie.

Recommended Answers

All 3 Replies

Updated code (which may be worse than the original):

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>

using namespace std;

string suits[] = {"Hearts", "Spades", "Diamonds", "Clubs"};
string values[] = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
//function for each type of hand Flushes, straights, etc.

vector<int> highs;

int getSuit(int card){
    return card / 13;   
}

int getValue(int card){
    return card % 13;
}

void print(int d[], int length){
    for(int i = 0; i < length; i++){
    cout << values[ getValue(d[i]) ]<< " of " << suits[ getSuit(d[i]) ]<< endl;
}
}

void shuffle(int d[], int length){
for(int i = 0; i < length; i++){
    int other = rand() % length;
    int temp = d[i];
    d[i] = d[other];
    d[other] = temp;

}
}

//example strength function
/*bool royalty(int h[], int length){
    int num_kings = 0;
int num_queens = 0;

for(int i = 0; i < length; i++){
    if(getValue(h[i]) == 11){ //king
        num_kings++;
    }
    if(getValue(h[i]) == 10){ //queen
        num_queens++;
    }
}
return num_kings > 0 && num_queens > 0;
}*/
int averageHigh(vector<int> array){
int sum = 0;
for(int i = 0; i < array.size(); i++){
    sum += array[i];
}
return sum/array.size();
}

int highCard(int h[], int length){
//stuff

int tempHigh = getValue(h[0]);
for(int i = 0; i < length; i++){
    if(getValue(h[i]) > tempHigh){
        tempHigh = getValue(h[i]);
    }
}
highs.push_back(tempHigh);
return tempHigh;
}

bool straight(int h[], int length){
    //scan till you find next highest etc then return false
double temp;
for (int n = 0; n < length; n++)
{
    for (int i = 0; i < length - 1; i++)
    {
        if(h[i] < h[n])
           {
               temp = h[n];
               h[n] = h[i];
               h[i] = temp;
           }
    }
}
if(getValue(h[0]) == getValue(h[1] + 1) && getValue(h[1]) == getValue(h[2] + 1) && getValue(h[2]) == getValue(h[3] +1) && getValue(h[3]) == getValue(h[4] + 1)){
    return true;
}else{
    return false;
}
}

bool flush(int h[], int length){
//stuff
if(getSuit(h[0]) == getSuit(h[1]) && getSuit(h[1]) == getSuit(h[2]) && getSuit(h[2]) == getSuit(h[3]) && getSuit(h[3]) == getSuit(h[4])){
    return true;
}else{
    return false;
}
/*for(int i = 0; i < length; i++){
    if(getSuit(h[i]) == getSuit(h[i + 1])){
        flush++;
    }else{
        return 0;
    }
}*/
}

bool straightFlush(int h[], int length){
//stuff
if(straight(h, length) && flush(h, length)){
    return true;
}
}

bool royalFlush(int h[], int length){
//stuff
if(straight(h, length) && flush(h, length) && getValue(h[4]) == 12){
    return true;
}

}

int strength(int h[], int length){
//stuff
if(royalFlush(h, length)){
    return 9;
}else if(straightFlush(h, length)){
    return 8;
}else if(flush(h, length)){
    return 7;
}else if(straight(h, length)){
    return 6;
}else{
    return 5;
}
}

int main(){

srand(time(0));
int deck[52]; // contains garbage numbers.

float times = 0;

cout << "How many games do you want to simulate?" << endl;
cin >> times;

for(int i = 0; i < 52; i++){
    deck[i] = i;
}

int straight_count = 0;
int flush_count = 0;
int straightFlush_count = 0;
int royalFlush_count = 0;
int tie_count = 0;

for(int i = 0; i < times; i++){
    shuffle(deck,52);
    int hand1[5];
    int hand2[5];
    //print(deck, 52);

    for(int i = 0; i < 5; i++){
        hand1[i] = deck[i];
        hand2[i] = deck[i + 5];
    }

    /*cout << "Hand 1:" << endl;
    print(hand1, 5);
    cout << endl;
    cout << "Hand 2:" << endl;
    print(hand2, 5);
    cout << endl;
    //cout << royalty(hand1, 5) << endl;
    //cout << royalty(hand2, 5) << endl; */

    if(straight(hand1, 5)){
        straight_count++;
    }
    if(straight(hand2, 5)){
        straight_count++;
    }
    if(flush(hand1, 5)){
        flush_count++;
    }
    if(flush(hand2, 5)){
        flush_count++;
    }
    if(straightFlush(hand1, 5)){
        straightFlush_count++;
    }
    if(straightFlush(hand2, 5)){
        straightFlush_count++;
    }
    if(royalFlush(hand1, 5)){
        royalFlush_count++;
    }
    if(royalFlush(hand2, 5)){
        royalFlush_count++;
    }

    highCard(hand1, 5);
    highCard(hand2, 5);
    straight(hand1, 5);
    straight(hand2, 5);
    flush(hand1, 5);
    flush(hand2, 5);
    straightFlush(hand1, 5);
    straightFlush(hand2, 5);
    royalFlush(hand1, 5);
    royalFlush(hand2, 5);
    strength(hand1, 5);
    strength(hand2, 5);
    if(strength(hand1, 5) == strength(hand2, 5)){
        tie_count++;
    }
}

cout << "Average High Card Value: " << averageHigh(highs) << endl;
cout << "Percent of Straight Hands: " << straight_count/times * 100 << "%" << endl;
cout << "Percent of Flush Hands: " << flush_count/times * 100 << "%" << endl;
cout << "Percent of Straight Flush Hands: " << straightFlush_count/times * 100 << "%" << endl;
cout << "Percent of Royal Flush Hands: " << royalFlush_count/times * 100 << "%" << endl;
cout << "Number of Ties: " << tie_count/times * 100 << "%" << endl;

}

The problem is now only with the Straight function (and consequently, all functions that call straight.)

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>

using namespace std;

string suits[] = {"Hearts", "Spades", "Diamonds", "Clubs"};
string values[] = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
//function for each type of hand Flushes, straights, etc.

vector<int> highs;

int getSuit(int card){
    return card / 13;   
}

int getValue(int card){
    return card % 13;
}

void print(int d[], int length){
    for(int i = 0; i < length; i++){
        cout << values[ getValue(d[i]) ]<< " of " << suits[ getSuit(d[i]) ]<< endl;
    }
}

void shuffle(int d[], int length){
    for(int i = 0; i < length; i++){
        int other = rand() % length;
        int temp = d[i];
        d[i] = d[other];
        d[other] = temp;

    }
}

    //example strength function
/*bool royalty(int h[], int length){
    int num_kings = 0;
    int num_queens = 0;

    for(int i = 0; i < length; i++){
        if(getValue(h[i]) == 11){ //king
            num_kings++;
        }
        if(getValue(h[i]) == 10){ //queen
            num_queens++;
        }
    }
    return num_kings > 0 && num_queens > 0;
}*/
int averageHigh(vector<int> array){
    int sum = 0;
    for(int i = 0; i < array.size(); i++){
        sum += array[i];
    }
    return sum/array.size();
}

int highCard(int h[], int length){
    //stuff

    int tempHigh = getValue(h[0]);
    for(int i = 0; i < length; i++){
        if(getValue(h[i]) > tempHigh){
            tempHigh = getValue(h[i]);
        }
    }
    highs.push_back(tempHigh);
    return tempHigh;
}

bool straight(int h[], int length){
    //scan till you find next highest etc then return false
    //not positive on this one, and therefore, it will affect the other two functions straight flush and royal flush
    //because I call straight inside them. Therefore, if straight is incorrect, so is str flush and roy flush :c
    int temp;
    //bubble sort, I think. Couldn't understand how to do it otherwise. Not sure it works.
    for (int n = 0; n <= (length - 1); n++){
        for (int i = 0; i < (length - 1); i++){
            if(h[i] > h[i + 1]){
                   temp = h[i];
                   h[i] = h[i + 1];
                   h[i + 1] = temp;
            }
        }
    }
    if((getValue(h[0]) + 1) == getValue(h[1]) && (getValue(h[1]) + 1) == getValue(h[2]) && (getValue(h[2]) + 1) == getValue(h[3]) && (getValue(h[3]) + 1) == getValue(h[4])){
        return true;
    }else{
        return false;
    }
}

bool flush(int h[], int length){
    //stuff
/*  if(getSuit(h[0]) == getSuit(h[1]) && getSuit(h[1]) == getSuit(h[2]) && getSuit(h[2]) == getSuit(h[3]) && getSuit(h[3]) == getSuit(h[4])){
        return true;
    }else{
        return false;
    }*/
    for(int i = 0; i < (length - 1); i++){
        if(getSuit(h[i]) == getSuit(h[i + 1])){
            //keepgoing
        }else{
            return false;
        }
    }
    return true;
}

bool straightFlush(int h[], int length){
    //stuff
    if(straight(h, length) && flush(h, length)){
        return true;
    }else{
        return false;
    }
}

bool royalFlush(int h[], int length){
    //stuff
    if(straight(h, length) && flush(h, length) && getValue(h[4]) == 12){
        return true;
    }else{
        return false;
    }

}

int strength(int h[], int length){
    //stuff
    if(royalFlush(h, length)){
        return 9;
    }else if(straightFlush(h, length)){
        return 8;
    }else if(flush(h, length)){
        return 7;
    }else if(straight(h, length)){
        return 6;
    }else{
        return highCard(h, length);
    }
}

int main(){

    srand(time(0));
    int deck[52]; // contains garbage numbers.

    float times = 0;

    cout << "How many games do you want to simulate?" << endl;
    cin >> times;

    for(int i = 0; i < 52; i++){
        deck[i] = i;
    }

    int straight_count = 0;
    int flush_count = 0;
    int straightFlush_count = 0;
    int royalFlush_count = 0;
    int tie_count = 0;

    for(int i = 0; i < times; i++){
        shuffle(deck,52);
        int hand1[5];
        int hand2[5];
        //print(deck, 52);

        for(int i = 0; i < 5; i++){
            hand1[i] = deck[i];
            hand2[i] = deck[i + 5];
        }

        /*cout << "Hand 1:" << endl;
        print(hand1, 5);
        cout << endl;
        cout << "Hand 2:" << endl;
        print(hand2, 5);
        cout << endl;
        //cout << royalty(hand1, 5) << endl;
        //cout << royalty(hand2, 5) << endl; */

        if(straight(hand1, 5)){
            straight_count++;
        }
        if(straight(hand2, 5)){
            straight_count++;
        }
        if(flush(hand1, 5)){
            flush_count++;
        }
        if(flush(hand2, 5)){
            flush_count++;
        }
        if(straightFlush(hand1, 5)){
            straightFlush_count++;
        }
        if(straightFlush(hand2, 5)){
            straightFlush_count++;
        }
        if(royalFlush(hand1, 5)){
            royalFlush_count++;
        }
        if(royalFlush(hand2, 5)){
            royalFlush_count++;
        }

        highCard(hand1, 5);
        highCard(hand2, 5);
        straight(hand1, 5);
        straight(hand2, 5);
        flush(hand1, 5);
        flush(hand2, 5);
        straightFlush(hand1, 5);
        straightFlush(hand2, 5);
        royalFlush(hand1, 5);
        royalFlush(hand2, 5);
        strength(hand1, 5);
        strength(hand2, 5);
        if(strength(hand1, 5) == strength(hand2, 5)){
            tie_count++;
        }
    }

    cout << "Average High Card Value: " << averageHigh(highs) << endl;
    cout << "Percent of Straight Hands: " << straight_count/times * 100 << "%" << endl;
    cout << "Percent of Flush Hands: " << flush_count/times * 100 << "%" << endl;
    cout << "Percent of Straight Flush Hands: " << straightFlush_count/times * 100 << "%" << endl;
    cout << "Percent of Royal Flush Hands: " << royalFlush_count/times * 100 << "%" << endl;
    cout << "Number of Ties: " << tie_count/times * 100 << "%" << endl;

}

Regarding the due at midnight part of your post, I won't give you the "You should have started sooner" lecture. We've all been there. That said, forums usually don't work in a way that allows that. People respond in their own time and it may not be before midnight. For example, I'm going to weigh in really quick, then I have plans for the rest of the day and won't be available for follow-up till tomorrow at the earliest. Which is too late.

To your actual problem, as far as sorting goes, it would of course help if you could do that. If you're not allowed to do that, then you're not allowed to do that.

I can't follow your code as far as what you are trying to do, but you say you'e not allowed to sort, but then it appears that you do sort? I guess that's because you are stuck?

So I assume the question is "How do I write the straght procedure without sorting? I'm not sure whether you are stuck with the function declaration as it is where you return a bool, but I'll assume so. So you have a function that takes an array and you need to know whether it is a straight. First off, your getValue function seems off. How can I have a "value" of 0? King whould have value 13, right? Ace should be 1 or 14, right? That's always the way it was when I played cards.

As far your straight function, I see it takes a length, ie the number of cards. However, in your function, you seem to be assuming that you have five cards, so length is always 5? I'll assume that length might not be 5. Otherwise why bother with the length parameter.

Again, sorting makes things way easier. But if you can't sort, you can't sort. Now it appears that you have values from 0 to 12, with Ace always being 12, unlike in most games where you can make Ace the low card for straights. OK.

A straight is where all the cards are in order with no gaps when sorted, and no pairs, So the trivial examples are if length is less than 1, there's no straight because you have no cards. If length is 1, you have a straight, and if length is over 13, you don't because you have at least one pair. Code those cases if you want.

You should probably write a function to check if you have a pair. Call that function. If it returns true, you don't have a straight, so return false.

Frankly, since you have to turn this in at midnight, I would just code it so that you are allowed to sort. The algorithms are too hard to explain for me to explain without them. I'm sure I could think of one, but it would be a ridiculous algorithm. Sort it. It's real easy. Ready?

// note 3 and 4 of a kind will also return true here.  Improve if needed.
// Does not confirm that length >= 2
bool pair(int h[], int length)
{
    // sort h[]
    for(int i = 0; i < length - 1; i++)
    {
        if(getValue(h[i] == getValue(h[i+1]))
            return true;
    }
    return false;
}

// check length if needed
// Adjust if Ace low card is allowed
bool straight(int h[], int length)
{
    // sort h[]
    if(pair(h, length))
        return false;
    if(getValue(h[length-1]) - getValue(h[0]) == length - 1)
        return true;
    else
        return false;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.