我目前正在玩一个扑克模拟器,我在创建一副牌时遇到了麻烦。
public Deck() {
int index = 0;
cards = new Card[52];
for(int cardValue = 1; cardValue <= 13; cardValue++) {
for(int suitType = 0; suitType <= 3; suitType++) {
cards[index] = new Card(cardValue, suitType);
index++;
}
}
}
无论如何,我可以让它看起来像上面那样?
此处的代码是我从另一个类使用引用
/* Strings for use in toString method and also for identifying card
* images */
private final static String[] suitNames = {"s", "h", "c", "d"};
private final static String[] valueNames = {"Unused", "A", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "J", "Q", "K"};
/**
* Standard constructor.
* @param value 1 through 13; 1 represents Ace, 2 through 10 for numerical
* cards, 11 is Jack, 12 is Queen, 13 is King
* @param suit 0 through 3; represents Spades, Hearts, Clubs, or Diamonds
*/
public Card(int value, int suit) {
if (value < 1 || value > 13) {
throw new RuntimeException("Illegal card value attempted. The " +
"acceptible range is 1 to 13. You tried " + value);
}
if (suit < 0 || suit > 3) {
throw new RuntimeException("Illegal suit attempted. The " +
"acceptible range is 0 to 3. You tried " + suit);
}
this.suit = suit;
this.value = value;
}
答案 0 :(得分:5)
只需交换你的for循环就可以为每件套装创造13张牌而不是每张牌套装4张:
public Deck() {
int index = 0;
cards = new Card[52];
for(int suitType = 0; suitType <= 3; suitType++) {
for(int cardValue = 1; cardValue <= 13; cardValue++) {
cards[index] = new Card(cardValue, suitType);
index++;
}
}
}