How to instantiate an array of a deck of cards and compare the values of each playing card?

Kira :

I'm trying to develop a Java program which is a game that uses enumerated types/classes for representing playing cards. I want to use enumerated types to represent possible suits and possible ranks. The game randomly draws two cards from a deck of cards and compares them.

I have 4 classes namely SimpleCardGame, Card, Rank and Suit. Currently, I'm having issues with SimpleCardGame and Card.

For SimpleCardGame, my only issue is instantiating the deck of cards. What did I do wrong? Once I'm able to solve that, the whole part below the "YOU DO NOT HAVE TO MODIFY ANYTHING BELOW" will be working perfectly.

For Card, there's a method called public int compareTo(Card c) that returns

  • a. -1 if the object calling this method is smaller than c

  • b. 0 if the object calling this method is the same as c

  • c. 1 if the object calling this method is larger than c

Card c1 is smaller than c2 if the rank of c1 is smaller than c2, or if the ranks of c1 and c2 are the same, the suit of c1 is smaller than c2. Order of Suits: Spade > Heart > Club > Diamond Order of Ranks: A > K > Q > J > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3 > 2

So my issue for this is how do I compare the card values? I am thinking of using the ordinal value method, but I'm not quite sure how to use it.

public class SimpleCardGame {

  public static void main(String[] args) {

    Card cards[];  // an array of Cards, representing a deck of cards

    // put a copy of each card into cards
    // The order of the cards in the array is: 
    // S2, S3, ..., SK, H2, ..., HK, C2, ..., CK, D2, ..., DK

    int i = 0;
    for (Suit s : Suit.values()) {
      for (Rank r : Rank.values()) {
        cards[i]= new Card(s,r);
        i++;
      }
    }

    }

    //
    // YOU DO NOT HAVE TO MODIFY ANYTHING BELOW
    //
    // print out the whole deck once
    System.out.println("The whole deck is:");
    for (Card card : cards)
      System.out.print(card + " ");
    System.out.println();

    // randomly draw two cards 
    MyRandom rnd = new MyRandom();
    Card c1 = cards[rnd.nextInt(cards.length)-1];
    Card c2 = cards[rnd.nextInt(cards.length)-1];

    System.out.println("Two cards are drawn:");
    System.out.println("c1 = " + c1 + " and c2 = " + c2);

    // compare c1 and c2
    if (c1.compareTo(c2) < 0) {
      System.out.println(c1 + " is smaller than " + c2);
    }
    else if (c1.compareTo(c2) == 0) { 
      System.out.println(c1 + " is the same as " + c2); 
    }
    else {
      System.out.println(c1 + " is larger than " + c2);
    }
  }
}


public class Card {
    private Rank rank;
    private Suit suit;

    public Card (Suit suit, Rank rank) {
        this.rank = rank;
        this.suit = suit;
    }

    public String toString() {
        return suit + "" + rank;
    }

    public int compareTo(Card c) {
        int diffRank = rank - c.rank;
        if (diffRank < 0) {
            return -1;
        }
        else if (diffRank > 0) {
            return 1;
        }
        else if (diffRank == 0) {
            int diffSuit = suit - c.suit;
            if (diffSuit < 0) {
                return -1;
            }
            else if (diffSuit > 0) {
                return 1;
            } 
        }
        return 0;
    }
}

public enum Rank {
  TWO("2"), 
  THREE("3"), 
  FOUR("4"), 
  FIVE("5"), 
  SIX("6"), 
  SEVEN("7"), 
  EIGHT("8"),
  NINE("9"), 
  TEN("10"), 
  JACK("J"), 
  QUEEN("Q"), 
  KING("K"),
  ACE("A"); 

  private String rank;

  // Constructor
  Rank (String r) {
    rank = r;
  }

  public String toString() {
    return rank;
  }
}
public enum Suit {
    SPADE("S"), 
    HEART("H"), 
    CLUB("C"), 
    DIAMOND("D"); 

    private String suit;

    Suit (String s) {
      suit = s;
    }

    public String toString() {
      return suit;
    }
}

Below is the supposed sample output of the program.

The whole deck is:
S2 S3 S4 S5 S6 S7 S8 S9 S10 SJ SQ SK SA H2 H3 H4 H5 H6 H7 H8 H9 H10 HJ HQ HK HA C2 C3 C4 C5 C6 C7 C8 C9 C10 CJ CQ CK CA D2 D3 D4 D5 D6 D7 D8 D9 D10 DJ DQ DK DA 
Two cards are drawn:
c1 = S7 and c2 = HQ
S7 is smaller than HQ

Joshgun :

For SimpleCardGame, my only issue is instantiating the deck of cards. What did I do wrong?

Yes, your array instantiation is not proper. You must specify the array size when you declare it. So in the current situation, the proper way may be:

Card cards[] = new Card[Suit.values().length * Rank.values().length];

P.S.: I've not fully analyzed the whole problem and code. So, if it solves your problem at some point we can go forward. (also there are some formatting and compilation issues in your code)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=416329&siteId=1