创建一副纸牌?
Creating a Deck of Cards?
我目前正在开发扑克模拟器,但在创建一副纸牌时遇到了问题。
我当前的代码创建了套牌
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++;
}
}
}
我需要让它看起来像:
我能让它看起来像上面那样吗?
这也是我从另一个 class 用于推荐的代码
/* 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;
}
只需交换您的 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++;
}
}
}
我目前正在开发扑克模拟器,但在创建一副纸牌时遇到了问题。
我当前的代码创建了套牌
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++;
}
}
}
我需要让它看起来像:
我能让它看起来像上面那样吗?
这也是我从另一个 class 用于推荐的代码
/* 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;
}
只需交换您的 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++;
}
}
}