• Post Reply Bookmark Topic Watch Topic
  • New Topic

Advice on string comparison  RSS feed

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm building this poker game app and so far its quite ok: i can deal 5 cards, change some, get new ones.
Now i need advice.
What i want it to check for flush, full house, straight, and so on.
I'm using a class which constructor looks like this:

My cards looks like this: QueenClubs.gif.
I wonder how can i check for those cases (two, three or four of a kind), full hand and so on.
Beeing more concrete: how can i write logic to detect a full hand, or a straight?
I'd apreciate any hints or ideas or links to help me start this out
Thanks in advance
hmm, after i reread this i wonder if regex is the solution...
[ April 15, 2004: Message edited by: miguel lisboa ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd make a Card class to hold some information about the card like suit (naipe?), ordinal number (0-12) and image name if you need pictures. Those will be a lot easier to work with in the long run than your strings like QueenClubs.
I'd also make a class for each type of hand, and test them in order from most valuable hand to least.

There might be a RoyalFlushChecker and a FourOfAKindChecker and a TwoPairChecker. Each one would see if the hand meets some set of rules and return true or false. It would also know how much that kind of hand is worth. You could score each player's hand and see who won.
This is kinda cool ... you could have different sets of checkers for different varieties of poker.
Any of that make sense? It's a leap into some kind of advanced ideas. Lemme know if you want to pursue it further.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey! thanks for your post!
here's my original idea:
i'd like to add a bonus point award mechanism.
For example: 200 pts for a poker, 1000 pts for a Royal Flush and so on.
I need a way to read the hands.
Here's what i have (in pseudo code):
I have my cards of type
2Hearts.gif
AceDiamonds.gif
and so on.
So, i'd put all 5 cards in an ArrayList (for example) and parse the array searching for "A" (Aces) and everytime i find one i add that card to another arrayList called Ace.
At the end, if that arrayList size is =2, the i've a pair, if = 4 i've got a poker and so on.
Incidentally if i've two arrays with size >=2 i've either two pairs or a full house...
As for flushes (5 of the same suit), i run the original arrayList searching for H(hearts) and if matched i add it to another arrayList called Hearts...etc etc the very same to every other suit.
Finally, to check for Straights i had to order cards saying ace bigger then king, which is bigger then queen etc etc.
Now, i parse the arrayList comparing the first with next: if they related, get them into another arrayList called Straight, and so on.
So i hope you'r getting my idea.
I think if i study regex i can use it to parse the arrayList against the various restrictions i want.
(Well, i'm sitll not sure how i can establish the order of cards...)
As to your idea its great!Still, i have some doubts:
I'd make a Card class to hold some information about the card like suit (naipe?), ordinal number (0-12)

yes - suit == naipe
how to do it? perhaps establishng a correspondence between the string name ( e.g. JackSpades.gif) and some internal card order, lets say the 4th of Spades? How to do it?
As to your handCheckers i guess that's the pseudo code i showed above, but not sure how to actually write it...
Lemme know if you want to pursue it further.

Absolutely!!
thanks a lot for your reply!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!