Numerical sorting?

Blitz3D Forums/Blitz3D Beginners Area/Numerical sorting?

I'm doing a bit of Blitz in my spare time, and have decided to write a Video Poker game.

When it comes to calculating whether you have a winning hand, and what that hand is, I'm a little confused.

Obviously I could go and write code for each individual possible hand, but that is far from efficient.

Say for instance I have - at the end of your hand being dealt - 5 strings, each of which contains the value of the card:

card_1_value = 12
card_2_value = 5
card_3_value = 14
card_4_value = 7
card_5_value = 14

The values I am using are as follows:

2 to 10 - self explanatory
Jack - 11
Queen - 12
King - 13
Ace - 14

How can I check these values and determine what poker hand I have? One pair, Two Pairs, Three of a kind, Flush, Straight, Full House, Straight Flush, Royal Flush are what I would like to check for.

If anyone can advise me, or point me in the right direction, I would be very grateful :)

Thanks!

One (part) solution that immediately comes to mind is to have an array that represents the count for each value of card (i.e. the 2-14 that you have, although it would be 0-12 because of where element numbering starts) and a second for the suits (just 0-3).

Then you could determine a pair by there being one or more card values with a count of 2. Three of a kind would mean one having 3.

A flush would be one suit having a count of however many make a flush up (this probably depends on the type of Poker you're playing).

A straight would entale consecutive card values having a count of 1. A straight flush would also have this along with a count of the total number of cards in a hand in one suit.

After the card value and suit count-up, you'd make the above checks in the highest winning hand down to the lowest. Alright, I've not included all the possibilities here, but I hope it's enough to get you thinking in one possible direction.

Possibly an array of types?

Type cards
Field value
field suit
end type

dim deck(51).cards
dim hands(4,4).cards

for t = 0 to 51
 deck(t).cards = new cards
 deck(t).value = t mod 13 + 2
 deck(t).suit = Floor(t / 13)
next

cardcount = 0


after that you would do something to shuffle the deck, then deal the cards using something like
for t = 0 to 4
 for j = 0 to 4
  hands(t,j).cards = new cards
  hands(t,j)/value = deck(cardcount)/value
  hands(t.j)/suit = deck(cardcount)/suit
  cardcount = cardcount + 1
 next
next


basically, t is the player (0-4 so 5 players total) and j is the card (5 cards).
Then after the player chooses which cards to discard, you deal the player some more always updating cardcount each time one is dealt. Also some betting mechanism and so forth. Then when it's time to check the hands:

for t = 0 to 4
 suit = hands(t,0)/suit
 flush = true
 for j = 1 to 4; check for flush
  if (hands(t,j)/suit <> suit then flush = false
 next
 
 low = 13 ;check for straight
 high = 0
 for j = 0 to 4
  if hands(t,j) < low then low = hands(t,j)
  if hands(t,j) > high then high = hands(t,j)
 next
 if (high - low) = 4; straight 
  straight = true
 else
  straight = false
 end if
 ... check for other hands. 
next

Now this method assumes that ace is high. If you're playing Ace as either high or low, then you need to make provisions for that when you check for the straight.

Edited because I realized my code example would check for a flush and straight flush, but not a straight. :-)