figuring out a 5 card hand

stevf

Senior member
Jan 26, 2005
290
0
0
Hello all, got an assignment in my C++ class to deal a 5 card hand and determine it the hand is a pair, 3 of a kind, flush, etc. I have been thinking about it and with sorting the hand I can write a bunch of if statements with multiple && and || to figure this out but that is a lot of code.

Does anyone have any thoughts or tips on a more elegant solution other than this mass of if statements? I dont want code or anything like that, just a nudge in the right direction


Thanks
 

lousydood

Member
Aug 1, 2005
158
0
0
Bucket-sorting by rank will make it pretty easy to test those properties.

For example, detecting a pair is simple this way: only 4 buckets will be filled, one bucket with 2 cards.
 

stevf

Senior member
Jan 26, 2005
290
0
0
thanks - I will look into that - that sounds much better than the ifs and all the and/ors required for pairs
 

stevf

Senior member
Jan 26, 2005
290
0
0
Thanks - that looks interesting, will have to study it closer the next couple of days
 

txrandom

Diamond Member
Aug 15, 2004
3,773
0
71
This may help for finding a straight or straight flush:

Assign numeric values to cards, I'm guessing you're already doing that. Sort them. Repeat this four times increasing i, array['i'] - array[i+1] = 1. If true continue, else it's not a straight.

Then use the bucket for storing my suit. If one bucket has 5 cards, it's a straight flush. There may be an easier way, but this should work without too much "brute"
forcing.

'i' = i . It was making my post italicized.
 

stevf

Senior member
Jan 26, 2005
290
0
0
Thanks all - that was just the push I needed. The bucket sort worked well and made pair, 2 pair, 3 of a kind, full house, and 4 of a kind a breeze.

Straight wasnt too bad using a small loop to check if 5 were adjacent. did add an extra check for the Ace since it can fill two different straights

I was given the basics of a card program that just shuffled then dealt all the cards into two hands and with the way it was written, it was easy to seperate the suits and then that was easy at that point to check for the flush.

The best part was seeing all the different sorting algorithms out there as I was researching this.

Thanks again