I don’t understand why I’m getting such output. The output tries to recreate and reshuffle cards every time I draw initial card, but why? Since I’m checking if deck of cards is empty or is null and if it is, it is being created.
Why wouldn’t my code work?
Output:
######### BLACK ### JACK #########
deal, hit, stay, exit: deal
Creating deck...
Shuffling deck...
Drawing Player's card... A_♦
Creating deck...
Shuffling deck...
Drawing Dealer's card... X_X
Creating deck...
Shuffling deck...
Drawing Player's card... 6_♥
Creating deck...
Shuffling deck...
Drawing Dealer's card... X_X
Dealers Hand: A_♦ X_X = 11
Players Hand: 6_♥ A_♦ = 17
Initial Draw method:
private static Deck initialDraw(Deck deck, Hand player, Hand dealer)
{
drawFromDeck(deck, player);
drawFromDeck(deck, dealer);
drawFromDeck(deck, player);
drawFromDeck(deck, dealer);
System.out.print("\n");
showHands(player, dealer);
compareHands(player, dealer);
return deck;
}
I don’t get this output when I use following code, how is it different? I’m still referencing the same array and passing it back, no ?
private static Deck initialDraw(Deck deck, Hand player, Hand dealer)
{
deck = checkDeck(deck);
drawFromDeck(deck, player);
drawFromDeck(deck, dealer);
drawFromDeck(deck, player);
drawFromDeck(deck, dealer);
System.out.print("\n");
showHands(player, dealer);
compareHands(player, dealer);
return deck;
}
Quick solution:
Explanation:
Inside
drawFromDeckyou are creating/updating the deck and returning this, but not updating the variable as seen ininitialDraw. This may not be such an issue (since Objects are passed by reference, but test before relying on it!), but I assume you are passingnullintoinitialDraw, and sonullis being passed to eachdrawFromDeckcall, and so a newDeckis created each time.For your Edit where you code works
In that case, you are ensuring that
deckis not anullreference, but a reference to an actualDeckobject, and so you will be passing a reference to aDeckobject to eachdrawFromDeckcall, rather than passing anullreference each time.