二十一点赢得计算错误:计算太多手牌

时间:2014-04-21 06:45:56

标签: java blackjack

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

我认为这是我的推送和拆分方法,但我不确定如何修复它们。目前,我的推送方法处理新手并确定手牌的获胜者,然后将推送量添加到玩家或经销商的获胜。然而,这将它视为当前手的循环中的新手,因此给了我额外的一手牌。我的分割方法以类似的方式工作。任何有关如何正确重写它们的建议都将非常感谢,谢谢。

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
}
}

0 个答案:

没有答案