卡片对象Java

时间:2015-04-17 14:54:28

标签: java

对于我的APCS课程,我们必须使用他们给我们的方法创建一副牌,然后将其洗牌并用它进行游戏。但是,我似乎无法弄清楚第一步:创建甲板。这是他们给我们的代码:

/**
 * Creates a new <code>Deck</code> instance.<BR>
 * It pairs each element of ranks with each element of suits,
 * and produces one of the corresponding card.
 * @param ranks is an array containing all of the card ranks.
 * @param suits is an array containing all of the card suits.
 * @param values is an array containing all of the card point values.
 */
public Deck(String[] ranks, String[] suits, int[] values) {
    cards = new ArrayList<Card>();
    for (int j = 0; j < ranks.length; j++) {
        for (String suitString : suits) {
            cards.add(new Card(ranks[j], suitString, values[j]));
        }
    }
    size = cards.size();
    shuffle();
}

这是应该创建卡片组的主要方法的一部分。包括他们给我们的样品,我认为,应该在他们指定的两件套装中创造6张牌,杰克,女王和国王 - 红色和蓝色。但是,即使这会产生重复的卡片。

    //String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"};
    String[] ranks = {"jack", "queen", "king"};
    //String[] suits = {"spades", "diamonds", "clubs", "hearts"};
    String[] suits = {"blue", "red"};
    //int[] pointValues = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};
    int[] pointValues = {11, 12, 13};
    Deck d = new Deck(ranks, suits, pointValues);

非评论的东西是他们给我们的东西,评论的东西是我认为会起作用的。但是,当我尝试运行我的代码时,它会创建148个Card对象而不是52个。我似乎无法弄清楚原因。有什么帮助吗?

2 个答案:

答案 0 :(得分:3)

我尝试你的代码,没关系。看来你在另一部分有一个错误

class Main {

    public static void main(String[] args) {
        String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                "jack", "queen", "king", "ace" };
        String[] suits = { "spades", "diamonds", "clubs", "hearts" };
        int[] pointValues = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 };
        deck(ranks, suits, pointValues);
    }

    public static void deck(String[] ranks, String[] suits, int[] values) {
        List<Card> cards = new ArrayList<Card>();
        for (int j = 0; j < ranks.length; j++) {
            for (String suitString : suits) {
                cards.add(new Card(ranks[j], suitString, values[j]));
            }
        }
        System.out.println(cards.size());
    }
}

class Card {

    String rank;
    String suit;
    int value;

    public Card(String rank, String suit, int value) {
        this.rank = rank;
        this.suit = suit;
        this.value = value;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

}

它打印52,这是正确的。

你是如何改变甲板的?您使用Collections.shuffle(卡片)还是自定义方法?

答案 1 :(得分:2)

你的逻辑被转换..

for (int j = 0; j < ranks.length; j++) {
    for (String suitString : suits) {
        cards.add(new Card(ranks[j], suitString, values[j]));
    }
}

应该是

for (String suitString : suits) {
  for (int j = 0; j < ranks.length; j++) {
    cards.add(new Card(ranks[j], suitString, values[j]));
  }
}

我认为你想首先循环套装,然后是这些套装中的牌。