二十一点游戏

C++ Blackjack Gameplay

本文关键字:游戏 十一点 二十一      更新时间:2023-10-16

好吧,我对编程真的很陌生,我正试图为我正在上的课制作一款21点游戏。到目前为止,我已经掌握了所有内容的要点,但当涉及到实际的游戏内容部分时,我遇到了麻烦。当我看着自己的代码时,我不知道为什么我的游戏不能正常运行。我希望这是一个比我看到的更容易解决的问题,因为我经常错过一些小事情。

for(;;)
{
    cout << "1)Bet, 2)Hit or 3)Stay?n";
    cin >> ans;
    if(ans == 1)
    {
    }
    if(ans == 2)        // Hit
    {
        Console::Clear();
        cout << "Your Handn";
        Player.AddCard(Deck.Draw());
        cout << "nHand Value: " << Player.HandValue() << "n";
        Player.Show();
        if(Player.HandValue() > 21)
        {
            cout << "You Busted man...Sorry :/nn"
                //<< "Dealer's Handnn";
                //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! :Dn"
                    << "nDealer's Handn";
                    Dealer.Show();
            }
            else if(Dealer.HandValue() == 21)
            {
                cout << "Dealer Wins...Your Soul! >:Dn"
            }
        }
    if(Player.HandValue() > Dealer.HandValue())
    {
        cout << "Your Hand Beats Dealer's...You Win!! :Dn"
    }
    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 Handn";
    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! :Dn";
        cout << "nn";
        cout << "Dealer's Handn";
        Dealer.Show();
    }
    while(Dealer.HandValue() < 17)
    {
        Dealer.AddCard(Deck.Draw());
        Dealer.Show(1);
    if(Dealer.HandValue() > 21)
    {
        cout << "Dealer Busted man...You Win! :Dn"
    }
    else if(Dealer.HandValue() == 21)
    {
        cout << "Dealer Wins...Your Soul! >:Dn"
    }
    }
    if(Player.HandValue() > Dealer.HandValue())
    {
        cout << "Your Hand Beats Dealer's...You Win!! :Dn"
    }
    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),我把它保存在我的投注代码中。这段代码在很大程度上做了我需要它做的事情。我还没有查a的东西。我们有一个用于牌组的堆栈,这就是Draw()和AddCard()提取的内容。任何帮助是非常感激的,并与评论温柔,我是新的。:)

您的问题很可能源于没有处理某些情况。如果players.HandValue() == dealers.HandValue(),您的代码将通过测试。同时也不能保证玩家拥有<17(你在你的测试中依赖)。

我建议加入else{}子句,打印玩家和发牌手值和调试信息,看看你错过了什么条件,然后你可以尝试找出原因。

if(Player.HandValue() > Dealer.HandValue())
{
    cout << "Your Hand Beats Dealer's...You Win!! :Dn";
}
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