2

我一直在尝试用这种方法让套牌发牌,但是一旦发牌了 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;

}

==================================================== ============================

整个代码

导入 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());
    }
}

}

4

3 回答 3

4

我一直在尝试用这种方法让套牌发牌,但是一旦发牌了 52 张牌,它就会进入无限循环。

处理一副纸牌的简单方法是:

  1. 将所有卡片放在一个ArrayList.
  2. 调用Collections.shuffle()该列表;
  3. 按照它们在洗牌列表中出现的顺序发牌。

代码将比您现在拥有的代码简单得多,并且更容易调试。

于 2012-05-19T06:33:31.430 回答
1

你有没有尝试过这样的事情

    while ((this.deck[index] == false||i==cardsInDeck)&&i<52)

?

于 2012-05-19T05:01:05.867 回答
1

一旦开始选择随机索引,就为时已晚。您需要先测试甲板是否有任何卡片。

if (cardsInDeck <= 0) ...throw an exception?...
while (this.deck[index] == false) {index = swift.nextInt(DECK_SIZE);}

您需要cardsInDeck正确初始化(设置为DECK_SIZEnot in dealbut inshuffle和构造函数)。您希望不变量cardsInDeck等于deck[index]==true.

于 2012-05-19T06:12:29.727 回答