0

我正在尝试编写一个 Black Jack 模拟器程序。它询问用户要使用多少副牌,要模拟多少手牌,以及是否显示每手牌的结果。然后它运行基于 Black Jack 策略的程序,并且应该输出准确的赢/输百分比。但是,当我运行它时,我的玩家获胜次数/庄家获胜次数大于我想要玩的手数。

我认为这是我的推送和拆分方法,但我不确定如何解决它们。目前,我的 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
}
}
4

0 回答 0