Odd little project I am working on. Before you answer, yes, I know that vbscript is probably the worst language to use for this.
I need help determining what each player has. Each card has a unique number (which I ‘translate’ into it’s poker value with a ♥♦♣♠ next to it). For example:
A♥ = 0
2♥ = 1
3♥ = 2
...
and so on. I need help determining what hand I have. I have thought of a few ways. The first is using the delta between each card value. For example, a straight would be:
n
n +/- (1+ (13 * (0 or 1 or 2 or 3)))
n +/- (2 + (13 * (0 or 1 or 2 or 3 )))
...
and so on. For example cards 3, 3+1+0, 3+2+13, 3+3+(13*3), 3+4+(13*2)
would give me:
4♥ 5♥ 6♦ 7♠ 8♣
My questions is, should I attempt to use regex for this? What is the best way to tell the computer what hand he has without hardcoding every hand?
EDIT: FULL CODE HERE: https://codereview.stackexchange.com/questions/21338/how-to-tell-the-npc-what-hand-it-has
Poker hands all depend on the relative ranks and/or suits of cards.
I suggest writing some utility functions, starting with determining a rank and suit.
So a card in your representation is an
intfrom 0..51. Here are some useful functions (pseudo-code):Now that you can obtain the rank and suit of a set of hands you can write some utilities to work with those.
You will need some more utility functions, but with these you can now look for a flush or straight:
And so on.
In general, you will need to check each hand against the possible poker hands, starting with the best, working down to high card. In practice you will need more than that to differentiate ties (two players have a fullhouse, the winner is the player with the higher ranked three of a kind making their fullhouse). So you need to store a bit more information for ranking two hands against one another, such as kickers.
That would be my general approach. There is a wealth of information on poker hand ranking algorithms out there. You might enjoy the Unit 1: Winning Poker Hands from Peter Norvig’s Udacity course Design of Computer Programs