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.
if card == card == card == card == card
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.
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
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".
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
for (i=0 ; i<5 ; i++)
for (j=0 ; j<5 ; j++)
if (cards[i]==cards[j] && i != j)
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 matches cards, 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:
before your program tells the player which hand they have.