如何正确创建“牌”类,然后正确创建一副牌

时间:2019-04-20 15:20:08

标签: java

我需要创建一个“卡”类,在该类中,我分别为每个卡分配一个值(A,2、3,...,K),一个符号(心形,菱形..)和卡的值。以磅为单位。然后我必须创建一个牌组,还有一个类似的问题,但是他/她是以这种方式创建牌组(Java - Creating a Card Deck Class):

  public Deck()
  {
  deck = new String [] {"Ace Of Hearts" , "2 Of Hearts" , "3 Of Hearts" , "4 
  Of Hearts" , "5 Of Hearts" , "6 Of Hearts" , "7 Of Hearts" , 
  "8 Of Hearts" , "9 Of Hearts" , "10 Of Hearts" , "Jack Of Hearts" , "Queen 
  Of Hearts" , "King Of Hearts" ,
 "Ace Of Diamonds" , "2 Of Diamonds" , "3 Of Diamonds" , "4 Of Diamonds" , 
 "5 Of Diamonds" , "6 Of Diamonds" ,
 "7 Of Diamonds" , "8 Of Diamonds" , "9 Of Diamonds" , "10 Of Diamonds" , 
 "Jack Of Diamonds" , "Queen Of Diamonds" ,
 "King Of Diamonds" , "Ace Of Clovers" , "2 Of Clovers" , "3 Of Clovers" , 
 "4 Of Clovers" , "5 Of Clovers" ,
 "6 Of Clovers" , "7 Of Clovers" , "8 Of Clovers" , "9 Of Clovers" , "10 Of 
 Clovers" , "Jack Of Clovers" ,
 "Queen Of Clovers" , "King Of Clovers" , "Ace Of Spades" , "2 Of Spades" , 
 "3 Of Spades" , "4 Of Spades" , "5 Of Spades" ,
 "6 Of Spades" , "7 Of Spades" , "8 Of Spades" , "9 Of Spades" , "10 Of 
 Spades" , "Jack Of Spades" ,
 "Queen Of Spades" , "King Of Spades"};

我不是很喜欢。

我不知道该怎么做,非常感谢您的帮助。 我有一些代码,但是结构不好。

1 个答案:

答案 0 :(得分:1)

这是我为我所教的编程课程编写的一个类,该类构建一个由52个Card对象组成的Deck对象:

Deck.java

import java.util.HashMap;
import java.util.Map;

public class Deck {

    // The four possible suits
    enum Suit {
        HEARTS,
        DIAMONDS,
        CLUBS,
        SPADES
    }

    // HashMap that maps Suit enum values to Strings
    private static Map<Suit, String> suitToString = new HashMap<>();
    static {
        suitToString.put(Suit.HEARTS, "Hearts");
        suitToString.put(Suit.DIAMONDS, "Diamonds");
        suitToString.put(Suit.SPADES, "Spades");
        suitToString.put(Suit.CLUBS, "Clubs");
    }

    // HashMap that maps numeric rank values to non-numeric rank names
    private static Map<Integer, String> rankToString = new HashMap<>();
    static {
        rankToString.put(1, "Ace");
        rankToString.put(11, "Jack");
        rankToString.put(12, "Queen");
        rankToString.put(13, "King");
    }

    static class Card {

        private Suit suit;
        private int rank;

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

        public String toString() {
            // Create a string representation of the card using the maps defined above
            String rankStr;
            if (rankToString.containsKey(rank))
                rankStr = rankToString.get(rank);
            else
                rankStr = String.valueOf(rank);
            String suitStr = suitToString.get(suit);
            return String.format("%s of %s", rankStr, suitStr);
        }
    }

    // Storage for the cards in the deck
    private Card[] cards = new Card[52];

    // Construct a deck of cards, creating 52 Card objects by iterating over each suit, and
    // then for each suit, iterating over the 13 card ranks.
    public Deck() {
        for (Suit suit : Suit.values())
            for (int rank = 0; rank < 13; rank++)
                cards[suit.ordinal() *  13 + rank] = new Deck.Card(suit, rank + 1);
    }

    // Return all the cards in the deck
    public Card[] getCards() {
        return cards;
    }
}

DeckTest.java

public class DeckTest {
    public static void main(String... args) {
        // Create a deck of cards
        Deck deck = new Deck();

        // Get the cards from the deck and print each of them
        for (Deck.Card card: deck.getCards())
            System.out.println(card);
    }
}

结果

Ace of Hearts
2 of Hearts
3 of Hearts
4 of Hearts
...
8 of Spades
9 of Spades
10 of Spades
Jack of Spades
Queen of Spades
King of Spades