File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Card class straight Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Card class straight" Watch "Card class straight" New topic

Card class straight

Joe Wilkes

Joined: Oct 29, 2005
Posts: 7
Hey guys, maybe someone can help me, I am working on a problem for class and it is from the Deitel java book. Chapter 7 problem 31, It has to do with calling a straight function. I have it calling the full house, pair, two pair, and flush. How do I see if there is striaght? My cards are strings numbered Ace, Duece, Three... Not ints 1, 2, 3... So how can I determine if Duece+1 == Three? Can anyone help me out on this???
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

I don't have that book so I can't speak to the specifics of your problem although I once wrote a program that did poker calculations from scratch. I would suggest creating a Card Class (if you don't have one all ready). Then you can add a property to the class such as card.getNumericValue().

For a straight, you just take the card with the lowest numeric value (cost of O(n) to find min card) then determine if remaining cards have values x+1, x+2, x+3 etc (Cost of O(5*n) total where n is 5 for 5-card poker, so very fast technique). You do need to put some special logic in their for aces but the case of aces happens so rarely that its not a difficult specific case to write. Probably could be handled with one if statement such as, if the lowest card value is 2, check if there exists an ACE (with value 14) then reset that as the lowest card and look for 2 though 5.

[OCA 8 Book] [Blog]
I agree. Here's the link:
subject: Card class straight
jQuery in Action, 3rd edition