0

Im trying to check to see if a hand in poker is a Royal Flush. However, when I keep checking it, it comes up as Straight Flush and not Royal Flush. I have modified it slightly but am unable to check it because I forgot to send myself one of the main programs I needed for it. But the problem still holds.

private boolean isRoyalFlush(){//check if from 10-Ace and All of same suit---
  if(isFlush()==true){
      sortByValue();
      if((cards[0].rank==10)&&(cards[1].rank==11)&&(cards[2].rank==12)&&(cards[3].rank==13)&&(cards[4].rank==14)){
          return true;
      }
      else{
          return false;
      }
  }
  else{
      return false;
  }
}

In theory the cards[i].rank gets the rank of the card, where 11==Jack, 12==Queen, etc. isFlush() checks to see if all of same suit. sortByValue(); sorts the cards into numerical value.

Other Code:

Card File

package playingcards;

public class Card {

 public final static int CLUBS = 0, DIAMONDS = 1, HEARTS = 2, SPADES = 3;
 public final static int JACK = 11, QUEEN = 12, KING = 13, ACE = 14;

 public final int suit; // 0 .. 3
 public final int rank; // 0 .. 12

 private final static String[] suits = { "Clubs", "Diamonds", "Hearts",
   "Spades" };
 private final static String[] ranks = { "2", "3", "4", "5", "6", "7", "8",
   "9", "10", "Jack", "Queen", "King", "Ace" };

/** * Create a card with value v, as ordered first by suit (Clubs, Diamonds, * Hearts, Spades, in increasing order), the by rank (aces high). For * example, the value 1 corresponds to the 2 of Clubs, 12 to the Ace of * Clubs, and 13 to the 2 of Diamonds. * * @param v * a value between 1 and 52 * @throws IllegalArgumentException * if v less than 1 or greater than 52 */

 public Card(int v) {
  // PRE:
  if (!(1 <= v && v <= 52)) {
   throw new IllegalArgumentException("Bad constructor arguments " + v);
  }
  suit = (v - 1) / 13;
  rank = (v - 1) % 13;
 }

/** * Create a card with rank r and suit s. Rank values are literal for number * ranks, 10 for Jacks, 14 for Aces, and so on. Suits are ordered from 0 to * 3, corresponding to Clubs, Diamonds, Hearts, and Spades. Note that this * class includes symbolic constants for all rank and suit values. * * @param r * the card's rank a value between 2 and Card.ACE (14) * @param s * the suit, a value between Card.CLUBS (0) and Card.SPADES (3) * * @throws IllegalArgumentException * if r is not between 2 and 14 (inclusive) or if s is not * between 0 and 3 (inclusive) */

 public Card(int r, int s) {
  // PRE: (0 <= s <= 3) AND (2 <= r <= 14)
  if (!((0 <= s && s <= 3) && (2 <= r && r <= 14))) {
   throw new IllegalArgumentException("Bad constructor arguments " + r
     + "," + s);
  }
  suit = s;
  rank = r - 2;
 }

/** * Returns a string representation of this card, including both its suit and * its value. Sample return values are: "Queen of Hearts", "10 of Diamonds", * "Ace of Spades" */

     public String toString() {
      return ranks[rank] + " of " + suits[suit];
     }

 // New things:

/** * Returns -1, 1, or 0, according to whether this Card is greater than, less * than or equal to its argument. The ordering is that of traditional draw * poker: by rank first (aces high), then by suit. * * @throws NullPointerException * if c is null */

 public int compareTo(Card c) {
  if (c.rank > this.rank || (c.rank == this.rank && c.suit > this.suit)) {
   return -1;
  } else if (this.rank > c.rank
    || (this.rank == c.rank && this.suit > c.suit)) {
   return 1;
  } else {
   return 0;
  }
 }

 public boolean equals(Card c) {
  return this.compareTo(c) == 0;
 }

} // Card
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
Jack Armstrong
  • 1,182
  • 4
  • 26
  • 59
  • 1
    Hint: Your cards can be in any order, unless you sort them first. – Robert Harvey Nov 24 '14 at 18:29
  • Post enough code for us to run this. My guess, it's your sorting or your card rank. – Elliott Frisch Nov 24 '14 at 18:30
  • 1
    @RobertHarvey thats what sortByValue does. – Jack Armstrong Nov 24 '14 at 18:31
  • @Jack In what order does `sortByValue` put your cards? Is `cards[0]` the highest or the lowest? – Sergey Kalinichenko Nov 24 '14 at 18:33
  • 1
    What does isFlush() do? Does it return true? What does sortByValue() does? What happens when you print the array of cards after sortByValue()? Have you tried using your debugger to step through the code line by line? Are you sure the rank of an Ace is 14, and not 1? – JB Nizet Nov 24 '14 at 18:34
  • sortByValue does low to high. isFLush() checks if all of same suit. it does return true because I have done it with cards from 10-Ace of same suit and got Straight Flush. I haven't tried printing. – Jack Armstrong Nov 24 '14 at 18:37
  • You set the rank by doing %13 but you test for values 13, 14. – laune Nov 24 '14 at 18:40
  • Do you have another comparator for `sortByValue`? Or are you using the above `comapreTo`? – Elliott Frisch Nov 24 '14 at 18:40
  • In the code you're showing us, Card.rank is 0..12, not 2..14. – Lee Daniel Crocker Nov 24 '14 at 18:40
  • Also, your compareTo() uses suits. Poker NEVER ranks cards by suit, except for incidental things like drawing for first deal or odd chip in a split pot. – Lee Daniel Crocker Nov 24 '14 at 18:44
  • Card itself may have changed as well. It seems like I dont have the updated version. I am not using the above compareTo method. – Jack Armstrong Nov 24 '14 at 18:44
  • Also check out: http://etceterology.com/blog/2013/5/23/representing-playing-cards-in-software Make up your mind about whether ranks are 0..12 or 2..14 and stick with it. I generally prefer the former, and I also compose them into integers with suit first instead of rank first, so I never have to divide by 13. – Lee Daniel Crocker Nov 24 '14 at 18:54
  • And while I'm here, a minor pet peeve of mine is poker programs that treat a "royal" flush as something other than just an Ace-high straight flush. It's not really its own class of hand, it just happens to have a special slang name like "broadway" or "wheel". – Lee Daniel Crocker Nov 24 '14 at 22:07
  • There are scenarios where you need to check for a royal. For example, hitting a royal within a certain time window may fire the payout of some sort of a promo. Aside from that, I'm not a big fan of the sorting of the cards prior to evaluation. It gets messy with aces swinging high and low. Is there a function analogous to isFlush() for isStraight()? If so, you could check that in there too. I'm also curious how you handle the swinging ace in your straight evaluation. It may suffer from a similar issue. – LDMJoe Jun 24 '15 at 01:16

0 Answers0