C ++ Blackjack游戏

时间:2011-12-08 02:18:59

标签: c++

好吧,所以我真的很喜欢编程,而且我正在尝试为我正在上课的二十一点游戏。到目前为止,我已经掌握了所有内容的要点,但是当谈到实际的游戏时,我遇到了麻烦。当我查看我的代码时,我无法理解为什么我的游戏无法正常运行。我希望它比我看到的更容易解决,因为我通常会想念小事。

for(;;)
{
    cout << "1)Bet, 2)Hit or 3)Stay?\n";
    cin >> ans;

    if(ans == 1)
    {

    }

    if(ans == 2)        // Hit
    {

        Console::Clear();
        cout << "Your Hand\n";
        Player.AddCard(Deck.Draw());
        cout << "\nHand Value: " << Player.HandValue() << "\n";
        Player.Show();

        if(Player.HandValue() > 21)
        {
            cout << "You Busted man...Sorry :/\n\n"
                //<< "Dealer's Hand\n\n";
                //Dealer.Show();

        }

            else if(Player.HandValue() == 21)
            {
                cout << "You've Won Your Freedom! :D";
            }

        while( Dealer.HandValue() <= 17)
        {
            Dealer.AddCard(Deck.Draw());
            Dealer.Show(1);

            if(Dealer.HandValue() > 21)
            {
               cout << "Dealer Busted man...You Win! :D\n"
                    << "\nDealer's Hand\n";
                    Dealer.Show();
            }

            else if(Dealer.HandValue() == 21)
            {
                cout << "Dealer Wins...Your Soul! >:D\n"

            }

        }

    if(Player.HandValue() > Dealer.HandValue())
    {
        cout << "Your Hand Beats Dealer's...You Win!! :D\n"

    }

    else if(Player.HandValue() < Dealer.HandValue() && Dealer.HandValue() > 17 && Player.HandValue() > 17)
    {
        cout << "Dealer's Hand Wins...Sorry :/\n"

    }
    cin.ignore(INT_MAX, '\n');

    cin.clear();
    cin.ignore(INT_MAX, '\n');
}
    break;
if(ans == 3)        // Stay
{

    Console::Clear();
    cout << "Your Hand\n";
    cout << "\nHand Value: " << Player.HandValue() << "\n";
    Player.Show();

    if(Player.HandValue() > 21)
    {
        cout << "You Busted man...Sorry :/\n"

    }
    else if(Player.HandValue() == 21)
    {
        cout << "You've Won Your Freedom! :D\n";
        cout << "\n\n";
        cout << "Dealer's Hand\n";
        Dealer.Show();
    }
    while(Dealer.HandValue() < 17)
    {
        Dealer.AddCard(Deck.Draw());
        Dealer.Show(1);

    if(Dealer.HandValue() > 21)
    {
        cout << "Dealer Busted man...You Win! :D\n"

    }
    else if(Dealer.HandValue() == 21)
    {
        cout << "Dealer Wins...Your Soul! >:D\n"

    }
    }
    if(Player.HandValue() > Dealer.HandValue())
    {
        cout << "Your Hand Beats Dealer's...You Win!! :D\n"

    }
    else if(Player.HandValue() < Dealer.HandValue() && Dealer.HandValue() > 17 && Player.HandValue() > 17)
    {
        cout << "Dealer's Hand Wins...Sorry :/\n;
        cin.ignore(INT_MAX, '\n');
    }
    cin.clear();
    cin.ignore(INT_MAX, '\n');
}

if(ans == 4)
{
    exit(0);
}
system("pause");
}

不介意if(ans == 1),我将其保存为我的投注代码。在大多数情况下,此代码完成了我所需要的操作。我还没有东西来检查Aces。我们有一个堆栈,这是Draw()和AddCard()从中拉出来的。非常感谢任何帮助,并对评论保持温和,我是新人。 :)

1 个答案:

答案 0 :(得分:1)

你的问题很可能源于不处理某些案件。如果players.HandValue()== dealer.HandValue(),您的代码将通过测试。此外,不保证玩家有&lt; 17(你在测试中依赖它)。

我建议输入一个else {}子句并打印玩家和经销商的手工价值和调试信息以查看您缺少的条件,然后您可以尝试找出原因。

if(Player.HandValue() > Dealer.HandValue())
{
    cout << "Your Hand Beats Dealer's...You Win!! :D\n";
}
else if(Player.HandValue() < Dealer.HandValue() && Dealer.HandValue() > 17 && Player.HandValue() > 17)
{
    cout << "Dealer's Hand Wins...Sorry :/\n";
}
else
{
    cout << "Unhandled case! Player handvalue: " << Player.HandValue() 
         << " dealer handvalue: " << Dealer.HandValue() << '\n';
}

HTH