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!
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.
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.