在C ++中使用单臂强盗游戏时出现问题

时间:2013-10-21 16:15:28

标签: c++

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

  • 用户应该能够将钱存入机器,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;
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

正如其他人所说,问题在于return来自你的main终止该计划。我的建议是:

  1. 将您的游戏逻辑移至单独的方法
  2. 将逻辑调用置于main中的while循环内,以检查玩家是否想再玩一次
  3. 仅在玩家想要停止时从main返回
  4. 示例部分代码(您应该明白这一点):

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

答案 1 :(得分:1)

您的控制台正在关闭,因为当主要功能点击返回时程序终止,在每次返回之前放置一个cin。这将在应用程序终止之前“暂停”它,让你看到你的踪迹。

答案 2 :(得分:0)

自己运行程序而不是计算机,你会发现为什么它的关闭速度如此之快。
请记住return中的main()表示“现在关闭程序”。