如何实例化一副纸牌的数组并比较每张纸牌的价值?

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

我正在尝试开发一个 Java 程序,该程序使用枚举 types/classes 来表示扑克牌。我想使用枚举类型来表示可能的花色和可能的等级。游戏从一副牌中随机抽取两张牌进行比较

我有 4 个 类 即 SimpleCardGame、Card、Rank 和 Suit。目前,我在使用 SimpleCardGame 和 Card 时遇到问题。

对于 SimpleCardGame,我唯一的问题是实例化一副纸牌。我做错了什么?一旦我能够解决这个问题,"YOU DO NOT HAVE TO MODIFY ANYTHING BELOW" 下面的整个部分将完美地工作。

对于 Card,有一个名为 public int compareTo(Card c) 的方法 returns

如果c1的点数小于c2,或者c1和c2的点数相同,则c1的花色小于c2,则c1小于c2。 花色顺序:黑桃>红心>梅花>方块 排名顺序:A > K > Q > J > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3 > 2

所以我的问题是如何比较卡值?我正在考虑使用序数值法,但我不太确定如何使用它。

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;
    }
}

下面是程序的假设示例输出。

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

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

是的,您的数组实例化不正确。声明时必须指定数组大小。所以在目前的情况下,正确的做法可能是:

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

P.S.: 我没有完全分析整个问题和代码。因此,如果它在某个时候解决了您的问题,我们就可以继续进行。 (您的代码中还存在一些格式和编译问题)