ブラック ジャック シミュレータ プログラムをコーディングしようとしています。使用するカードのデッキ数とシミュレートするハンドの数、および各ハンドの結果を表示するかどうかをユーザーに尋ねます。次に、ブラック ジャック戦略に基づいてプログラムを実行し、正確な勝敗率を出力することになっています。しかし、実行すると、プレイヤーの勝利数/ディーラーの勝利数が、プレイしたかったハンドの数よりも多くなっています。
それは私のプッシュとスプリットの方法だと思いますが、それらを修正する方法がわかりません。現在、私の push メソッドは新しいハンドを配り、そのハンドの勝者を決定してから、プレーヤーまたはディーラーの勝利にプッシュ量を追加します。ただし、これは現在のハンドのループ内の新しいハンドとして扱われるため、余分なハンドが得られます。私の分割方法も同様の方法で機能します。それらを正しく書き直す方法についての提案は大歓迎です、ありがとう。
import java.util.*;
import java.text.*;
public class Game
{
int decks = 4;
int hands = 1;
int myCardsSum = 0;
int dealerCardsSum = 0;
final int DECK_SIZE = 52;
final int BLACKJACK = 21;
String display;
boolean displayData = true;
boolean myHardHand = true;
boolean dealerHardHand = true;
boolean doubleAllowed = true;
boolean splitAllowed = true;
boolean surrenderAllowed = true;
Deck myDecks;
int myWins = 0;
int dealerWins = 0;
int push = 0;
Card myFirst;
Card mySecond;
Card dealerFirst;
Card dealerSecond;
// Input prompt removed
// asks user for decks, hands, and display information
public void play()
{
// A deck of cards made of all the decks selected by the user
myDecks = new Deck();
myDecks.setCardsInDeck(4 * decks);
for (int i = hands; i > 0; i--)
{
deal();
}
double winPercent = (double) myWins / hands;
double losePercent = (double) dealerWins / hands;
// Output removed
}
public void deal()
{
myHardHand = true;
dealerHardHand = true;
splitAllowed = true;
doubleAllowed = true;
surrenderAllowed = true;
myDecks.shuffle(decks); // shuffles if the deck is half empty or less
myFirst = myDecks.drawCard();
mySecond = myDecks.drawCard();
dealerFirst = myDecks.drawCard();
dealerSecond = myDecks.drawCard();
myCardsSum = myFirst.getCardVal() + mySecond.getCardVal();
dealerCardsSum = dealerFirst.getCardVal() + dealerSecond.getCardVal();
// Check for aces and assign either 1 or 11 as its value for the round
if (myCardsSum <= 10 &&
(myFirst.getCardName().equalsIgnoreCase("ace") ||
mySecond.getCardName().equalsIgnoreCase("ace")))
{
myCardsSum+= 10;
myHardHand = false;
}
if (dealerCardsSum <= 10 &&
(dealerFirst.getCardName().equalsIgnoreCase("ace") ||
dealerSecond.getCardName().equalsIgnoreCase("ace")))
{
dealerCardsSum+= 10;
dealerHardHand = false;
}
if (myCardsSum == BLACKJACK && dealerCardsSum != BLACKJACK)
{
myWins++;
// output removed
}
else if (myCardsSum > BLACKJACK)
{
dealerWins++;
// output removed
}
else if (myCardsSum == dealerCardsSum)
{
// output removed
push();
}
else
{
// output removed
chooseMove(dealerFirst, myFirst, mySecond, myCardsSum);
}
}
public void hit()
{
doubleAllowed = false;
splitAllowed = false;
surrenderAllowed = false;
Card draw = myDecks.drawCard();
myCardsSum += draw.getCardVal();
if (myCardsSum <= 10 &&
draw.getCardName().equalsIgnoreCase("ace"))
{
myCardsSum+= 10;
myHardHand = false;
}
else if (myCardsSum > 21 &&
(myFirst.getCardName().equalsIgnoreCase("ace") ||
mySecond.getCardName().equalsIgnoreCase("ace")))
{
myCardsSum-= 10;
myHardHand = true;
}
// output removed
if (myCardsSum == BLACKJACK && dealerCardsSum != BLACKJACK)
{
myWins++;
// output removed
}
else if (myCardsSum > BLACKJACK)
{
dealerWins++;
// output removed
}
else if (myCardsSum == dealerCardsSum)
{
// output removed
push();
}
else
{
chooseMove(dealerFirst, myFirst, mySecond, myCardsSum);
}
}
public void stand()
{
// output removed
while (dealerCardsSum < 17 || (dealerCardsSum == 17 && dealerHardHand == false))
{
Card draw = myDecks.drawCard();
dealerCardsSum += draw.getCardVal();
if (dealerCardsSum <= 10 &&
draw.getCardName().equalsIgnoreCase("ace"))
{
dealerCardsSum+= 10;
dealerHardHand = false;
}
else if (dealerCardsSum > 21 &&
(dealerFirst.getCardName().equalsIgnoreCase("ace") ||
dealerSecond.getCardName().equalsIgnoreCase("ace")))
{
dealerCardsSum-= 10;
dealerHardHand = true;
}
//output removed
} // End loop
if (myCardsSum == dealerCardsSum)
{
// output removed
push();
}
else if (dealerCardsSum > BLACKJACK)
{
// output removed
myWins++;
}
else if (dealerCardsSum > myCardsSum && dealerCardsSum < BLACKJACK)
{
// output removed
dealerWins++;
}
}
public void push()
{
push++;
int score = myWins;
deal();
if (score < myWins) // if the player won the last round
{
myWins += push;
}
else
{
dealerWins += push;
}
push = 0;
}
public void surrender() // program does not keep track of bets; no half loss
{
// output removed
dealerWins++;
}
public void doubleDown() // program does not keep track of bets; no double gain
{
doubleAllowed = false;
splitAllowed = false;
surrenderAllowed = false;
Card draw = myDecks.drawCard();
myCardsSum += draw.getCardVal();
if (myCardsSum <= 10 &&
draw.getCardName().equalsIgnoreCase("ace"))
{
myCardsSum+= 10;
myHardHand = false;
}
else if (myCardsSum > 21 &&
(myFirst.getCardName().equalsIgnoreCase("ace") ||
mySecond.getCardName().equalsIgnoreCase("ace")))
{
myCardsSum-= 10;
myHardHand = true;
}
// output removed
if (myCardsSum == BLACKJACK && dealerCardsSum != BLACKJACK)
{
myWins++;
// output removed
}
else if (myCardsSum > BLACKJACK)
{
dealerWins++;
// output removed
}
else if (myCardsSum == dealerCardsSum)
{
// output removed
push();
}
else if (myCardsSum < dealerCardsSum)
{
dealerWins++;
// output removed
}
else
{
myWins++;
// output removed
}
}
public void split()
{
Card split1 = myDecks.drawCard();
myCardsSum = myFirst.getCardVal() + split1.getCardVal();
if (myCardsSum <= 10 &&
split1.getCardName().equalsIgnoreCase("ace"))
{
myCardsSum+= 10;
myHardHand = false;
}
else if (myCardsSum > 21 &&
(myFirst.getCardName().equalsIgnoreCase("ace") ||
split1.getCardName().equalsIgnoreCase("ace")))
{
myCardsSum-= 10;
myHardHand = true;
}
// output removed
if (myCardsSum == BLACKJACK && dealerCardsSum != BLACKJACK)
{
myWins++;
// output removed
}
else if (myCardsSum > BLACKJACK)
{
dealerWins++;
// output removed
}
else if (myCardsSum == dealerCardsSum)
{
// output removed
push();
}
else
{
chooseMove(dealerFirst, myFirst, split1, myCardsSum);
}
Card split2 = myDecks.drawCard();
myCardsSum = mySecond.getCardVal() + split2.getCardVal();
if (myCardsSum <= 10 &&
split2.getCardName().equalsIgnoreCase("ace"))
{
myCardsSum+= 10;
myHardHand = false;
}
else if (myCardsSum > 21 &&
(mySecond.getCardName().equalsIgnoreCase("ace") ||
split2.getCardName().equalsIgnoreCase("ace")))
{
myCardsSum-= 10;
myHardHand = true;
}
// output removed
if (myCardsSum == BLACKJACK && dealerCardsSum != BLACKJACK)
{
// output removed
}
else if (myCardsSum > BLACKJACK)
{
dealerWins++;
// output removed
}
else if (myCardsSum == dealerCardsSum)
{
// output removed
push();
}
else
{
chooseMove(dealerFirst, mySecond, split2, myCardsSum);
}
}
public void chooseMove(Card dealers, Card first, Card second, int mySum)
{
// switch determining which moves to make removed
}
}