Help with arrays

Hello! I have to write a simple poker program using arrays. By simple I mean no face cards are needed only 2-9. I need to ask the user to input 5 cards and the program will tell them if they have a pair, three of kind, flush...etc. So my question is how do I write the program to know if there is a pair? I think I'm supposed to use bool, but I'm unsure how. Any tips will be much appreciated!

This is what I have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main ()

{

const int hand = 5 ;
int cards[hand];

cout << "Enter " << hand << " numeric cards, no face cards\n";
cin >> cards[0];
cin >> cards[1];
cin >> cards[2];
cin >> cards[3];
cin >> cards[4];

return 0; 

}


I got some hints that it might look something like this, but obviously this won't work...any insight?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main ()

{

const int hand = 5 ;
int cards[hand];

cout << "Enter " << hand << " numeric cards, no face cards\n";
cin >> cards[0];
cin >> cards[1];
cin >> cards[2];
cin >> cards[3];
cin >> cards[4];

if (containsAPair(hand)) {
cout << "contains a pair" << endl;
}

return 0; 

}
You have 5 cards in your hand... you're using the variable cards for the array, meaning you need to work on the array.

To find out if you have duplicates, you need to scan each card in your hand to find out if anything matches.

for example:
if card[0] == card[1] == card[2] == card[3] == card[4]
then you have 5 of a kind (someone's cheating)

you dont even really need to do this as a function (though for good quality code you should) but you need to iterate through your array (cards[]) and find out how many cards match, then based on this, determine your output.
The simplest way is to sort your array and then you can easily scan to see if you have a pair, three of a kind and whatnot. If you don't sort then you'll end up with some unwieldy if checks.

If you're just starting off learning how to program look at bubble sort since it's the simplest and most people figure it out on their own anyways without any formal algorithm teaching.
Last edited on
Thanks for your help! Another question though this is what I did:

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
//Soung Khammaniphanh
//CS110B
//Poker Hands

#include <iostream>
using namespace std;

int main ()

{

const int hand = 5 ;
int cards[hand];

cout << "Enter " << hand << " numeric cards, no face cards\n";
cin >> cards[0];
cin >> cards[1];
cin >> cards[2];
cin >> cards[3];
cin >> cards[4];

if (cards[0]==cards[1]) {

cout << "Two Pair!" << endl;

}

if (cards[0]==cards[2]) {

cout << "Two Pair!" << endl;

}

if (cards[0]==cards[3]) {

cout << "Two Pair!" << endl;

}

if (cards[0]==cards[4]) {

cout << "Two Pair!" << endl;

}


return 0; 

}


And it works, but I'm wonder if there is any easier way to do this? I've tried

if (cards[0]==cards[1,2,3,4]) but it didn't work. I'm just hoping there is an easier way I am overlooking lol.

edit: I firgured it out...I used "||" idk why I didn't of it earlier lol. Brain fart!
Last edited on
You could also do it with a for loop...

You see above that you've only got 4 checks, to find a pair match, but thats only for the 1st card in your hand... but what if card 2 and 4 match? or 5 and 3?

what if i have 3 of a kind?

How many hands must be shook to shake everyone's hand, and to know if i have pairs, 3 of a kind, or 4 of a kind?

Convert the 5 cards to letters for simplicity sake:
A B C D E
A,B
A,C
A,D
A,E
B,C
B,D
B,E
C,D
C,E
D,E
A,B,C
A,B,D
A,B,E
A,C,D
A,C,E
A,D,E
B,C,D
B,C,E
B,D,E
A,B,C,D
A,B,D,E
A,C,D,E
B,C,D,E

assuming i've not missed any permutations, thats 23 separate tests that you have to run (i think i missed one or two).

You could have a tonne of or statements, which you've alluded to, but thats an awfully long line.

In addition, having 3 of a kind, also matches having a pair, so are you going to pay out for a two of a kind, at the same time as a three of a kind? a four of a kind, is 2 pairs and a three of a kind at the same time, so you pay out 4 times?
Quick poker note: two of the same card is just a "pair", not "two pair".

Moving along.....

You could also maybe set up a for loop to test certain conditions so you don't have to run each individual check. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool pair=false;

for (i=0 ; i<5 ; i++)
{
   for (j=0 ; j<5 ; j++)
   {
      if (cards[i]==cards[j] && i != j)
           pair=true;
   }
}


if (pair==true)
   cout << "You've got a pair!";



i think it's better to save the result of the check as a bool rather than just cout'ing, because if cards[1] matches cards[3], it would print the pair statement twice. Mind you, this doesn't solve the fact that you need to make the programs specify which hand is better. For instance, the above check works for pairs, but if you had 3 of a kind you wouldn't want it going on about having two matching cards.

Without giving it a whole lot of consideration, you could simply say:

1
2
if (threeOfAKind==true)
   pair=false;


before your program tells the player which hand they have.


edit: silly mistake in the original code
Last edited on
You can use STL set and some STL algorithms...

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
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
    const int HAND = 5 ;
    int cards[HAND];

    cout << "Enter " << HAND << " numeric cards, no face cards\n";
    for (int i = 0; i < HAND; i++)
    {
        cin >> cards[i];
    }

    //check poker rank
    set<int> cardSet(cards, cards+HAND);
    set<int>::iterator it = cardSet.begin();
    int freq1 = (int)count(cards, cards+HAND, *it++);
    int freq2 = (int)count(cards, cards+HAND, *it);
    int maxVal = *max_element(cards, cards+HAND);
    int minVal = *min_element(cards, cards+HAND);

    if (cardSet.size() == 4)
    {
        cout << "One Pair" << endl;
    }
    else if (cardSet.size() == 3)  //113 131 311 122 212 221
    {
        if (freq1%2 && freq2%2)   //if there is no "2" or even value freq
            cout << "Three of a Kind" << endl;
        else
            cout << "Two Pairs" << endl;
    }
    else if (cardSet.size() == 2)  //14 41 23 32
    {
        if (freq1 == 2 || freq1 == 3)
            cout << "Full House" << endl;
        else
            cout << "Four of a Kind" << endl;
    }
    else  //five distinct cards
    {
        if (maxVal - minVal == 4)
        {
            //Straight
            //StraightFlush
            cout << "either Straight or Straight Flush" << endl;
        }
        else
        {
            //Flush
            //Nothing
            cout << "either Flush or Nothing" << endl;
        }
    }

    return 0;
}


I'm not sure how can you check for card suit from integer numbers 2-9.
Last edited on
Topic archived. No new replies allowed.