Joh*_*rry -1 java poker constructor
我目前正在研究扑克模拟器,但无法创建一副扑克牌。
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++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以使它看起来像上面吗?
这也是我从另一个类引用的代码
/* 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;
}
Run Code Online (Sandbox Code Playgroud)
只需交换您的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++;
}
}
}
Run Code Online (Sandbox Code Playgroud)