我一直试图让这个方法用于套牌处理,但是一旦它处理了52张牌就会进入无限循环。
我知道这是因为我要生成一个随机数,直到它获得一张尚未设置的卡,但是一旦设置了所有卡,条件将永远不会成立,因此无限循环。
即使我知道我的问题,我也不知道如何解决它。我一直在努力工作几个小时。一旦达到52张牌,我想要豁免,但是一旦进入无限循环,它就永远不会达到if语句。
public PlayingCard deal() {
Random swift = new Random();
int index = swift.nextInt(DECK_SIZE);
cardsInDeck = DECK_SIZE;
int i = 51;
while (this.deck[index] == false||i==cardsInDeck) {index = swift.nextInt(DECK_SIZE);}
if(i==cardsInDeck) { throw new RuntimeException("Empty Deck");}
PlayingCard.CardRank[] Ranking = PlayingCard.CardRank.values();
PlayingCard.CardSuit[] Suiting = PlayingCard.CardSuit.values();
PlayingCard.CardRank Rank = Ranking[index % 13];
PlayingCard.CardSuit Suit = Suiting[index % 4];
PlayingCard selected = new PlayingCard(Suit, Rank);
this.deck[index] = false;
i++;
cardsInDeck--;
return selected;
}
=============================================== ==============================
整个代码
import java.util.Random;
公共课DeckOfCards {
public static final int DECK_SIZE = 52;
//Instance Variables
private boolean[] deck; //An implicit set of 52 Playing-Cards
private int cardsInDeck;//Number of cards currently in the deck
private Random dealer; //Used to rendomly select a card to be dealt
//Constructor
public DeckOfCards() {
this.deck = new boolean[DECK_SIZE];
int index = 0;
for (PlayingCard.CardSuit Suit : PlayingCard.CardSuit.values()) {
for (PlayingCard.CardRank Rank : PlayingCard.CardRank.values()) {
PlayingCard card = new PlayingCard(Suit, Rank);
deck[index] = true;
index++;
}
}
}
//Collect all 52 Playing-Cards into the deck
public void shuffle() {
/*Random shuffle = new Random();
for (int j = 0; j < this.deck.length; j++) {
int k = shuffle.nextInt(this.deck.length);
boolean temp = this.deck[j];
this.deck[j] = this.deck[k];
this.deck[k] = temp;*/
int index = 0;
for (PlayingCard.CardSuit Suit : PlayingCard.CardSuit.values()) {
for (PlayingCard.CardRank Rank : PlayingCard.CardRank.values()) {
PlayingCard card = new PlayingCard(Suit, Rank);
deck[index] = true;
index++;
}
}
}
//Simulate dealing a randomly selected card from the deck
//Dealing from an empty deck results in a RuntimeException
public PlayingCard deal() {
Random swift = new Random();
int index = swift.nextInt(DECK_SIZE);
cardsInDeck = DECK_SIZE;
int i = 0;
while (this.deck[index] == false&&i>0) {index = swift.nextInt(DECK_SIZE);}
if(i>cardsInDeck) { throw new RuntimeException("Empty Deck");}
PlayingCard.CardRank[] Ranking = PlayingCard.CardRank.values();
PlayingCard.CardSuit[] Suiting = PlayingCard.CardSuit.values();
PlayingCard.CardRank Rank = Ranking[index % 13];
PlayingCard.CardSuit Suit = Suiting[index % 4];
PlayingCard selected = new PlayingCard(Suit, Rank);
this.deck[index] = false;
i++;
cardsInDeck--;
return selected;
}
=============================================== ===================
public static void main(String[] args) {
DeckOfCards myDeck = new DeckOfCards();
myDeck.shuffle();
for (int p = 1; p <= 4; p++) {
for (int c = 1; c <= 13; c++) {
System.out.print(myDeck.deal() + " ");
}
System.out.println();
}
try {
System.out.println(myDeck.deal());
} catch (RuntimeException rte) {
System.out.println(rte.getMessage());
}
}
}
答案 0 :(得分:4)
我一直试图让这个方法用于套牌处理,但是一旦它处理了52张牌就会进入无限循环。
处理一副牌的简单方法是:
ArrayList
。Collections.shuffle()
; 代码将比您现在的代码简单得多,并且更容易调试。
答案 1 :(得分:1)
你有没有尝试过类似的东西
while ((this.deck[index] == false||i==cardsInDeck)&&i<52)
答案 2 :(得分:1)
一旦开始挑选随机索引,就太晚了。你需要先测试牌组是否有牌。
if (cardsInDeck <= 0) ...throw an exception?...
while (this.deck[index] == false) {index = swift.nextInt(DECK_SIZE);}
您需要正确初始化cardsInDeck
(设置为DECK_SIZE
不在deal
但在shuffle
和构造函数中。您希望cardsInDeck
等于deck[index]==true
的索引数的不变量。