简体   繁体   中英

Deck of Cards Objects Java

For my APCS class, we have to create a deck of cards using the method they have given us, and then shuffle it and play a game with it. However, I can not seem to figure out the first step: creating the deck. This is the code they've given us:

/**
 * 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();
}

Here's the portion of the main method that is supposed to create the deck of cards. Included is a sample that they've given us that is, I assume, supposed to create 6 cards, jack, queen, and king, in the two suits they've specified - red and blue. However, even this creates duplicate cards.

    //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);

The non-commented stuff is what they've given us, the commented stuff is what I thought would work. However, when I try to run my code, it creates 148 Card objects instead of just 52. I can't seem to figure out why. Any help?

I try your code and it's okay. It seems that you have a bug in another part

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

}

It prints 52, that it's correct.

How are you shuffling the Deck ? You use Collections.shuffle(cards) or a custom method ?

Your logic is transposed..

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

should be

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

I think you want to loop the suits first, then the cards within those suits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM