gpt4 book ai didi

java - 我对输出感到困惑,因为它不允许我重新创建对象

转载 作者:行者123 更新时间:2023-12-02 07:25:54 29 4
gpt4 key购买 nike

我正在尝试解决一个问题,即在抽完所有牌后尝试重置我的牌组。当我到达甲板的末尾时,我确实应该收到消息甲板是空的!您必须重新创建并重新洗牌一副牌!但是一旦重新创建并重新洗牌一副牌,它仍然会不断显示此消息。

所以我将 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);
}

最佳答案

存在几个范围问题(对于 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)CheckDeck

    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)初始绘图

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);

我仅建议更改一组内容,您可以使用相同的原则来组织其他功能。

关于java - 我对输出感到困惑,因为它不允许我重新创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13582359/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com