#### Archived

This topic is now archived and is closed to further replies.

# Help with a basic C++ Poker game

This topic is 6036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey I am in highschool right now and for my final project in C++ I chose to make a poker game. I have some of it down, but the biggest problem right now is doing a card check function type thing. If you know what to do and you wanna help me, just post up and i''ll email you what i have so far. thanks!

##### Share on other sites
I assume you mean to see whether the player has a a flush/straight/etc. Why don''t you try sorting the hand and comparing it to winning patterns? You could go the simple route and use strings. One for suit, one for number. So if the player had King of Diamonds, 2 of Spades, King of Hearts, 2 of Clubs and 2 of Hearts. you would sort it into some order, where it goes from Ace down to 2 (AKQJT98765432), and for suit Hearts, Diamond, Clubs, Spades (HDCS). That would make the order of those cards
KD,KH,2H,2C,2S
then look for patterns, first in a string of just numbers, without suits, and you get:
KK222, which could easily match a xxyyy (full house) pattern.
Create a buffer list of patterns and their point values if matched, and then declare the highest valued pattern found as the one the user has.

Hope that came out making as much sense as it does IN my head

We''''re sorry, but you don''''t have the clearance to read this post. Please exit your browser at this time. (Code 23)

##### Share on other sites
I kind of get what you mean, but could you give me an example of how you would right it out?

##### Share on other sites
oops, looks like someone wants other people to
write his homework for him....

[Hugo Ferreira][Positronic Dreams][]
"Research is what I''m doing when I don''t know what I''m doing."
- Wernher Von Braun (1912-1977)

##### Share on other sites
no way!

i wrote the whole friggin thing by myself, i just need some help doing one more function and i''ll be set. but the whole thing was written by me.

##### Share on other sites
go to that website i linked to earlier and write down all the winning hands and their ranks.
now, start at the highest one (royal flush i think, i''m not checking because it is you who should care). check if the player has that (all cards are same suit && all cards are consecutive && last card == ace). if so they have a straight flush. if not, check for 4 of a kind. if not, check for full house. so on and so on.
i mean you could work out some elegant and efficient way of doing this, but if you are asking then you probably just want it to work; you can be clever about it when you have it going.

--- krez (krezisback@aol.com)

##### Share on other sites
I made a poker game on a TI-83 a while back, and ran into the same problem. Here is how I solved it.
Assuming that you have an array 5 indexes in length representing card values (not suits), first sort the list (lowest to highest) with numbers. Now, if you were to check for a three of a kind, you know that after the list is sorted, there must be three consecutive values in a row somewhere in the array. Take this:
[ 2, 7, 7, 7, 9 ]
all you have to do is run a search, which i won''t do for you. Hope this pointed you into the right direction.

##### Share on other sites
First, make a function to sort them (find a simple little sort algorithm, even just bubble sort, its only 5 cards)
You could make a function to check for a flush (all 5 cards have same suit - simple task)
Make another to see if they have a straight (it could go something like this: please excuse me if I don''t do source right, this is my first time, and I read about how to do it from an unreliable source)
  for (int i = 0; i < cardsHeld; i++) { /*cardsHeld being the number of cards the person can hold*/ if (cardsInHand[i] == cardsInHand[i+1} && i < cardsHeld - 1) numberInARow++;}if (numberInARow == cardsHeld) {//person has a straight; if function then return 1 for yes!}

(Did my source work ok?????)
Make another function to check how many of the same cards are equal (it could return a class/structure with the following information: How many pairs he has (to check for 2-pair or Full house), which numbers make up the pair (if two people both have a pair, check who''s is higher), how many of each card (to check whether they have a full house of a 2-pair), and anything else that seems useful.

You could start it out using your flush function and your straight function for the full house(after all, isn''t it just a mixture of the two?)

  if(straight(hand[]) && flush(hand[]) && hand[1] == ACE) /*You''d declare ACE using enum or #define, and this is assuming ace is the first one (because you sorted it, you could have ace = 14 though)*///Put stuph here

##### Share on other sites
it was supposed to be
  cardsInHand[i+1]

and not
  cardsInHand[i+1}

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×