-5

嘿,我正在尝试制作一副牌

一副牌应该包含 52 张牌,

ofc 与铁锹、钻石、心、带有等级的俱乐部,

这是我的代码,到目前为止我还没有得到更多提前感谢您的帮助。

让牌组洗牌,创建一个代表包含 52 张牌的牌组的类,当创建此类的新对象时,牌组将使用它将包含的牌进行初始化。

public class Card {
    int[] deck = new int[52];

    String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

    Card() {
       for (int i = 0; i < deck.length; i++) {
           deck[i] = i;
       }
    }
}
4

3 回答 3

2

卡片不是一副纸牌卡片suit具有类似于和的属性(即成员变量)rank(尽管为了比较,通常使用整数或枚举来表示排名,然后更改它们的显示方式)。

一副扑克牌是很多张牌;通常在创建套牌时建立限制。在这种情况下,suits[]andranks[]可用于在套牌中构建许多卡片(在创建时为每张卡片提供特定的花色和等级)。在 Java 中执行此操作的最简单方法是使用嵌套循环 - 每个维度一次。

每张牌的“颜色”来源于花色,不需要存储,因为它可以根据花色确定。如果使用枚举,则只能将其分配为属性。

按照上述逻辑,这是一组示例类:

class Card {
   final public String suit;
   final public String rank;
   public Card(String suit, String rank) {
      // Assign suit/rank from arguments
   }
   String getColor() {
      // Return the color based on suit (effectively "for free" if
      // using an enumeration)
   }
}

class DeckOfCards {
    String[] suits = {..};
    String[] ranks = {..};

    // Arrays are icky to deal with; favor Lists/Collections.
    List<Card> cards = new ArrayList<Card>();

    public DeckOfCards () {
       // For each suit/rank pair, create a Card and add it
       // to the cards collection.
       // You'll want nested loops, as shown by another answer.
    }

    public List<Card> getCards () {
        // Return cards, perhaps
    }

    // Other methods relating to the Deck of Cards
    public void shuffle () {
    }
}

虽然我建议查看enums,但上面应该指出两个不同的“事物”之间的区别 - 卡片和卡片的甲板(或集合)。另一个答案包含显示如何从两个输入数组的叉积生成卡片的代码。

于 2013-09-29T20:55:38.383 回答
0

首先,将suits[] 和ranks[] 制作成int 数组。将甲板制作成一组卡片,并使甲板和您正在使用的构造函数在不同的cals中(称为deck)将构造函数更改为Card(int suit,int name)只需这样做:

for(int i = 0; i < 4; i++) for(int j = 0; j < 13; j++){
   Cards[13*i + j] = new Card(i, j); 
}
于 2013-09-29T20:46:26.640 回答
0

创建一个Card具有西装和价值属性的对象。

创建一个方法来创建所有 52 张卡片的列表。

对于你的牌组,我建议使用Deque,通常发音为“牌组”,并且与一副牌很相似。

然后创建洗牌套牌

//Create a List of cards
List<Card> deckList = ... //A method to create your 52 cards
Collections.shuffle(deckList);
Deque<Card> deck = new ArrayDeque<Card>(deckList);
于 2013-09-29T20:52:48.697 回答