スタックからアイテムを作成してから削除しています。ここでは、元のデッキをメソッドに渡し、デッキが空かどうかを判断しようとしています。が空の場合、新しいデッキを作成し、元のデッキを参照してデッキをリセットしたいと考えています。
私のロジックの何が問題になっていますか? デックの最後に到達し、最終的に 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: