0

我正在创建然后从堆栈中删除项目。在这里,我将原始牌组传递给一个方法,在该方法中我尝试确定牌组现在是否为空。如果为空,我想通过创建新牌组并将其引用回原始牌组来重置牌组。

我的逻辑有什么问题?我知道出了点问题,因为当我到达牌组的尽头,最后 if 语句开始运行时,它不会让我稍后逃脱,并且一直告诉我牌组是空的。

private static Deck checkDeck(Deck deck)
{
    if(deck.isEmpty())
    {
        System.out.print("Deck is empty! You must recreate and reshuffle deck of cards!\n\n");
        System.out.println("Creating deck...");
        deck = new Deck(DECKSIZE);
        deck.createDeck();
        System.out.println("Shuffling deck...");
        deck.shuffleDeck();
        System.out.print("\n");
    }

    return deck;
}

主类:

public static void main(String[] args) throws IOException 
{
    Deck deck = null;
    Hand playersHand = null;
    Hand dealersHand = null;

    System.out.println("Creating deck...");
    deck = new Deck(DECKSIZE);
    deck.createDeck();

    // Remove elements of deck...

    deck = checkDeck(deck);

    temp = new Card(deck.pop());
    System.out.println("Drawing players card... " + temp.toString());
    playersHand.insert(temp);
}

示例输出运行:

deal, bet, hit, stay, split, leave: deal

Drawing players card... 9 ♣
Drawing dealers card... 6 ♠
Drawing players card... 11 ♠
Drawing dealers card... 13 ♥

Dealers Hand: K_♥ 6_♠ = 16
Players Hand: J_♠ 9_♣ = 19

deal, bet, hit, stay, split, leave: deal

Deck is empty! You must recreate and reshuffle deck of cards!

Creating deck...
Shuffling deck...

Drawing players card... 13 ♥
Drawing dealers card... 6 ♣
Drawing players card... 7 ♠
Drawing dealers card... 11 ♣

Dealers Hand: J_♣ 6_♣ = 16
Players Hand: 7_♠ K_♥ = 17

deal, bet, hit, stay, split, leave: deal

Deck is empty! You must recreate and reshuffle deck of cards!

Creating deck...
Shuffling deck...

Drawing players card... 4 ♦
Drawing dealers card... 9 ♥
Drawing players card... 3 ♥
Drawing dealers card... 9 ♦

Dealers Hand: 9_♦ 9_♥ = 18
Players Hand: 3_♥ 4_♦ = 7

deal, bet, hit, stay, split, leave: 
4

2 回答 2

0

checkDeck()函数使用了一个 main 函数没有的函数,并且您没有显示 ( Deck.shuffleDeck())。鉴于您确实显示的代码,我最好的猜测是错误就在那里。

于 2012-11-23T08:57:03.893 回答
0

对于我所看到的,您的代码似乎没问题。问题应该出在您执行循环的任何地方,并向“玩家”询问动作。似乎您要么每次都创建一个新的空牌组,要么在此循环中取出剩余的牌。

于 2012-11-23T12:26:12.270 回答