0

我正在尝试解决一个问题,即我在抽完所有牌后尝试重置我的牌组。当我到达牌组的尽头时,我确实应该收到消息Deck is empty! You must recreate and reshuffle deck of cards!,但是一旦牌组被重新创建并重新洗牌,它仍然会不断显示此消息。

所以我将 Card 对象传递给以堆栈方式存储在数组中的 Deck 类。

为什么我会在确实重置纸牌的地方得到这样的输出?

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

Drawing Player's card... 9 ♥
Drawing Dealer's card... A ♣
Drawing Player's card... 7 ♦
Drawing Dealer's card... K ♦

Dealers Hand: K_♦ A_♣ = 21
Players Hand: 7_♦ 9_♥ = 16

Dealer has BLACKJACK!

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

Deck is empty! You must recreate and reshuffle deck of cards!
Creating deck...
Shuffling deck...
Drawing Player's card... 7 ♥
Drawing Dealer's card... 5 ♥
Drawing Player's card... K ♠
Drawing Dealer's card... 6 ♦

Dealers Hand: 6_♦ 5_♥ = 11
Players Hand: K_♠ 7_♥ = 17

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

Deck is empty! You must recreate and reshuffle deck of cards!
Creating deck...
Shuffling deck...
Drawing Player's card... Q ♦
Drawing Dealer's card... 9 ♥
Drawing Player's card... 8 ♥
Drawing Dealer's card... 10 ♠

Dealers Hand: 10_♠ 9_♥ = 19
Players Hand: 8_♥ Q_♦ = 18

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

Deck is empty! You must recreate and reshuffle deck of cards!
Creating deck...
Shuffling deck...
Drawing Player's card... 3 ♣
Drawing Dealer's card... 6 ♠
Drawing Player's card... 4 ♠
Drawing Dealer's card... K ♠

Dealers Hand: K_♠ 6_♠ = 16
Players Hand: 4_♠ 3_♣ = 7

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

这就是我重置牌组的方式:

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

    System.out.println("Shuffling deck...");
    deck.shuffleDeck();

    return deck;
}

这就是我检查甲板是否为空的方式:

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

我每次抽牌前都会检查牌组:

public static void drawFromDeck(Deck deck, Hand hand)
{
    deck = checkDeck(deck);

    Card temp = new Card(deck.pop());
    System.out.println("Drawing " + hand.getName() + "'s card... " + temp.toString());
    hand.insert(temp);

    System.out.print("\n");
}

初始绘制()

private static void initialDraw(Deck deck, Hand player, Hand dealer)
{
    deck = checkDeck(deck);
    drawFromDeck(deck, player); // drawing players card
    deck = checkDeck(deck);
    drawFromDeck(deck, dealer); // drawing dealers card
    deck = checkDeck(deck);
    drawFromDeck(deck, player); // drawing players card
    deck = checkDeck(deck);
    drawFromDeck(deck, dealer); // drawing dealers card

    System.out.print("Dealers Hand:");
    dealer.displayHand();
    System.out.print(" = " + dealer.getHandTotal() + "\n");
    System.out.print("Players Hand:");
    player.displayHand();
    System.out.print(" = " + player.getHandTotal() + "\n");

    checkInitialDraw(player, dealer);
}
4

2 回答 2

2

有几个范围问题(对于 Deck 变量)。

总而言之,这是错误的类型:

void createObj(MyObject objt)
{
    objt = new MyObject(); //Local Scope
}
//Caller
MyObject obj = null;
createObj(obj); // <-- New object will be created only within the function, obj will remain unaffected.
// obj is still null;

我建议进行这些更改(wrt http://ideone.com/S4Yv3l)以使代码正常工作,但是,您可以改进您的代码。

i)更改 createDeck 的签名。

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

        System.out.println("Shuffling deck...");
        deck.shuffleDeck();

        return deck;
    }

ii)检查甲板

    private static Deck checkDeck(Deck deck)
    {
        if(deck == null)
            return createDeck();

        if(deck.isEmpty())
        {
            System.out.print("Deck is empty! You must recreate and reshuffle deck of cards!\n");
            return createDeck();
        }

        return deck;
    }

iii) InitialDraw

private static Deck initialDraw(Deck deck, Hand player, Hand dealer)
{
    deck = checkDeck(deck);
    drawFromDeck(deck, player); // drawing players card
    deck = checkDeck(deck);
    drawFromDeck(deck, dealer); // drawing dealers card
    deck = checkDeck(deck);
    drawFromDeck(deck, player); // drawing players card
    deck = checkDeck(deck);
    drawFromDeck(deck, dealer); // drawing dealers card

    System.out.print("Dealers Hand:");
    dealer.displayHand();
    System.out.print(" = " + dealer.getHandTotal() + "\n");
    System.out.print("Players Hand:");
    player.displayHand();
    System.out.print(" = " + player.getHandTotal() + "\n");

    checkInitialDraw(player, dealer);
    return deck;
}

iv)在主要:

deck = initialDraw(deck, playersHand, dealersHand);

我建议只针对一组事物进行更改,您可以使用相同的原理来组织其他功能。

于 2012-11-27T11:43:22.833 回答
0
 if(deck == null)
    return createDeck(deck);
else if(deck.isEmpty())
{
    deck = new Deck(DECKSIZE);
    System.out.print("Deck is empty! You must recreate and reshuffle deck of cards!\n");
    return createDeck(deck);
}
else
    return deck;

}

试试这个

于 2012-11-27T10:47:47.303 回答