Creating something that Checks if its a Royal Flush

2.5k views Asked by At

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
0

There are 0 answers