-2

我目前正在制作一个单臂强盗游戏,这就是它应该做的:

  • 用户应该能够将钱放入机器中,50、100 或 500
  • 用户应该能够为每场比赛下注
  • 用户下注后,货币符号应随机出现在如下字段中:

[ o ] [ p ] [ x ]

[ x ] [ x ] [ x ]

[ x ] [ o ] [ x ]

(1 列/行赢 2x 钱,2 列/行赢 4x 钱等)

这个字段每次都应该用字母随机化,也应该用一个二维数组来表示游戏区域。

我遇到了一些问题,基本上是在我赌了一些钱之后游戏关闭并且我看不到任何错误等,任何澄清这个问题的帮助将不胜感激,以及我可以对我的代码做的任何改进等。

这是我的代码:

#include <iostream>
#include <ctime>

using namespace std;


int main(int args, const char* argv[])
{

int svar = 0;
int bokstav = 0;
int insert;
int build;
int bet;
int credit;
int array;
int jackpot;
int tal;
int spin_1x = 0;
int spin_1y = 0;
int spin_2x = 0;
int spin_2y = 0;


cout << "Welcome to the one-armed bandit!" << endl;
cout << endl;
cout << "To play start by betting some money!" << endl;
cout << endl;
cout << "Insert 50/ 100/ 500 kr to your account!" << endl;
cout << endl;
cin >> insert;

while (insert < 50 || insert > 500)
    {
        cout << "You either inserted to little or to much" << endl;
        cin >> insert;
    }
    cout << "You inserted" << insert;
    cout << endl;
    while (insert > 50)
    {
        cout << "Bet some money to play!" << endl;
        cout << endl;
        cin >> bet;
        cout << endl;
        while (bet !=50 && bet !=100 && bet !=500)
        {
            if (bet == 50) return 1;
            {
                cout << "You have betted 50 kr!" << endl;
            }
            {
                if (bet == 100) return 2;
                {
                    cout << "You have betted 100 kr!" << endl;
                }
                {
                    if (bet == 500) 
                    {
                        cout << "You have betted 500 kr!" << endl;
                    }
                    char a = '£';
                    char b = '$';
                    char c = '*';

                    char build [4][4] = { 
                        { ' ', 'A', 'B', 'C',},
                        { '1', ' ', ' ', ' ',},
                        { '2', ' ', ' ', ' ',},
                        { '3', ' ', ' ', ' ',},
                    };

                    int array();

                    cout << build[0][1] << "    " << build[0][2] <<"    "<< build[0][3] <<" " << endl;
                        cout << build[1][0] <<"|_|" <<"|_|" << "|_|" << endl
                        << build[2][0] <<"|_|" <<"|_|" << "|_|" << endl
                        << build[3][0] <<"|_|" <<"|_|" << "|_|" << endl;
                        return 0;

                    srand(time(0));
                    spin_1x=rand() % 4 + 1;
                    spin_1y=rand() % 4 + 1;
                    spin_2x=rand() % 4 + 1;
                    spin_2y=rand() % 4 + 1;

                    int y = 0;
                    int x = 0;

                    if (x == spin_1x && y == spin_1y)
                    {
                        build[x][y]='0';
                        cout << "Congrats you won!" << endl;
                    }
                    else if (x == spin_2x && y == spin_2y)
                        cout << "Congrats you won!" << endl;
                    cout << endl;
                    cout << "JACKPOT!" << endl;
                    {
                        if (insert <= 0)
                        {
                            cout << "You dont have any money left! Game's over!" << endl;
                        }

                        int insert;

                        if ((a == 7 && b == 7 && c == 7))
                        {
                            cout << "You have won x2 your bet!($" << ( bet*2) << ")" << endl;
                            credit = credit + (bet*2);
                        }
                        else if ((a == b == c) && ! (a == 7 && b == 7 && c == 7))
                        {
                            cout << "You have won x4 your bet!($" << (bet*4) << ")" << endl;
                            credit = credit + (bet*4);
                        }
                        else if ((a == b || a == c || b == c) && !(a == b == c) && !(a == 7 && b == 7 && c == 7))
                        {
                            credit = credit + (bet*8);
                            cout << "You have won x8 your bet!($" << (bet*8) << ")" << endl;
                        }
                        else if ((a == b || a == c || b == c) && ! (a == b == c) && ! (a == 7 && b == 7 && c == 7))
                        {
                            credit = credit + (bet*16);
                            cout << "You have won x16 your bet!($" << (bet*16) << ")" << endl;
                        }
                        else if (( a == b || a == c || b == c) && ! (a == b == c) && ! (a == 7 && b == 7 && c == 7))
                        {
                            credit = credit + (bet*128);
                            cout << "You have won x128 your bet!($" << (bet*128) << ")" << endl;
                        }
                        else
                        {
                                cout << "You have lost your betting!($-" << bet << ")" << endl;
                                credit = credit - bet;
                        }
                        return credit;
                    }
                }
                return 0;
            }
        }
    }
}
4

3 回答 3

2

正如其他人所说,问题是您正在return终止main程序。我的建议:

  1. 将您的游戏逻辑移动到单独的方法中
  2. 将逻辑调用放在一个while循环中main,检查玩家是否想再次玩
  3. main在玩家想要停止时返回

示例部分代码(你应该明白):

void printWelcome( )
{
    std::cout << "Welcome to the One-Armed Bandit!" << std::endl;
    // etc.
}

int getBet( )
{
    int betAmount;

    std::cout << "To play start by betting some money!\n"
              << "Insert 50/ 100/ 500 kr to your account!" << std::endl;

    std::cin >> betAmount;

    return betAmount;
}

void playGame( )
{
    // All of your game logic in here
}

bool wantsToPlay( )
{
    std::cout << "Do you want to play again? (y/n)" << std::endl;

    char response;

    std::cin >> response;

    if( response == 'y' )
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main( int argc, char** argv )
{
    int betAmount = 0;

    printWelcome( );

    do
    {
        betAmount = getBet( );

        if( betAmount != 50 && betAmount != 100 && betAmount != 150 )
        {
            // Restart the game loop (ask for bet again)
            continue;
        }

        playGame( );

    } while( wantsToPlay( ) );

    std::cout << "Thank you for playing!" << std::endl;

    return 0;
}
于 2013-10-21T16:55:06.017 回答
1

您的控制台正在关闭,因为当 main 函数遇到 return 时程序终止,在每次 return 之前放置一个 cin 。这将在应用程序终止之前“暂停”应用程序,让您看到您的痕迹。

于 2013-10-21T16:20:25.140 回答
0

自己运行程序而不是计算机,你会明白为什么它关闭得这么快。
请记住,returninmain()表示“立即关闭程序”。

于 2013-10-21T16:39:20.657 回答