• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

A little logic problem

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Cluster

Member
Joined
Dec 5, 2001
Location
Canuckistan
I'm making a game of poker in pascal and i'm stuck on a logic problem.

I need to find a way to check the cards to find out if the cards fit into one of 8 winning categories. But with 7 cards there's alot of winning combinations just starting at the first with a pair. The eight winning combinations are pair (10,J,Q,K,A only), 2 pair, flush, straight, 3ofakind, full house, 4 of a kind, royal flush.

My idea was to setup an array and a bunch of if statements to check the cards to see if there are any card combinations matching the winning combinations. But soon realized that there are 1000's. I need an easier way to do this. Any help would be greatly appreciated.
 

JigPu

Inactive Pokémon Moderator
Joined
Jun 20, 2001
Location
Vancouver, WA
Well... Let me have a try...

You could keep an array containing the number of each kind of card that each player has. Then, you could consult the array to check for a pair (possibly 3 of a kind... or a straight... I don't play poker, so I don't know).

You could even have two arrays, one to do what I just suggested with card values (2 - Ace), and the other keeping track of the number of cards with a cirtian symbol (heart, club, ect.).

Hope it helps with a few of them...
JigPu
 

vandersl

Member
Joined
Apr 23, 2001
Never thought about this before, but this approach might be worth a look...

Define 3 criteria arrays:

(1) The first represents the number of each card value in the hand. That is, a 13-element array, where the value in each location is the number of cards of that value in the hand (2 thru Ace). This array is useful for checking for pairs, 3-of-a-kind, or 4-of-a-kind.

(2) The second represents whether the hand contains one or more cards of each value. This is really the same as the first array. It it useful for checking for straights (i.e. 5 locations in a row each with a value of '1').

(3) The third represents the number of cards of each house in the hand. That is, a 4 element array representing the number of clubs, hearts, etc. This one is useful for checking for flushes.

Populate the arrays for the hand, then perform the scan (in order of highest possible hand on down):
- you have a flush if one of the arrays in (3) has 5 cards in it
- you have a straight if the array in (2) has 5 non-zero values in a row
- you have 4-of-a-kind if one of the locations in (1) is 4
- you have full house if one of the locations in (1) is 3 and another is 2
- etc

One good thing about this approach is you don't need to break the 7 cards into all possible 5-card hands.
 
OP
C

Cluster

Member
Joined
Dec 5, 2001
Location
Canuckistan
I missed the straight flush. Thanks.

Right now i got 2 arrays, a 13 value containing the face values. And a 4 value array with the suit.

Could you possibly write a piece of code, preferably in a procedure. I would want to call the procedure at the end of the hand to check for winners. I'd just like to see what it looks like. I haven't really dived that deep into arrays yet, but i'm getting there.

Thanks for the help guys.
 

Istari1

Member
Joined
Mar 12, 2002
Location
Raleigh NC
Chris said:
I missed the straight flush. Thanks.

Right now i got 2 arrays, a 13 value containing the face values. And a 4 value array with the suit.

Could you possibly write a piece of code, preferably in a procedure. I would want to call the procedure at the end of the hand to check for winners. I'd just like to see what it looks like. I haven't really dived that deep into arrays yet, but i'm getting there.

Thanks for the help guys.

Just to give ya some push in the right direction, first sort the hand by face value. That will make your checking procedures easier. When you do this notice the combinations you get and you might see how you should now check for winning hands. If not I'll look back here and give ya another clue. (I dont like just giving answers, you learn more if ya figure it out yourself).

Istari
 

Gresyth

Computer Abuser
Joined
May 7, 2001
This might help. I've learned to set up my data structure first and then figure out how to process the data afterwards.


deck
array(1..52)


player1
array(1..5)


player2
array(1..5)



player3
array(1..5)


player4
array(1..5)


player5
array(1..5)


the deck array for example:

elements 1 ,13 = clubs in ascending order
elements 14,26 = diamonds in ascending order
elements 27,39 = hearts in ascending order
elements 40,52 = spades in ascending order

player1 example

element 1 = numeric value of card
ie. 3 of diamonds = 15

element 2 = numeric value of card
ie. jack of spades = 49

element 3 = numeric value of card
ie. jack of clubs = 10

element 4 = numeric value of card
ie. ace of hearts = 39

element 5 = numeric value of card
ie ace of clubs = 13


write a sorting algorithim to look for multiplies of 13, 10, 3, 7, etc. to check for pairs, three of a kind, four of a kind.

I posted a bubble sort program and code in this thread

this ought to get you started.