“彩色游戏” - 从数组C ++中随机调用

时间:2018-04-26 16:25:37

标签: c++ random

嘿伙计们,所以我们在c ++课上做我们的项目。它被称为“彩色游戏”,有一个假想的模具,有6个侧面和差异颜色。我不知道如何随机调用一个字符串数组。所以我举例说:

int color[] = {"Blue", "Red", "Green", "White", "Orange", "Yellow"};

我希望从数组中选择一个颜色的随机名称作为输出。我只是不知道我将使用什么代码。我只是一个c ++的业余爱好者。

此外,我们需要显示不同的颜色名称,例如:

char answer;

cout<<"The first color is: " <<color1;
cout<<"want to play again (y/n)? ";
cin>>answer;

if (answer = 'y')
{
cout<<"The second color is: " <<color2;
}

当我们运行程序时,从阵列中随机挑选出两种颜色。

这只是输出应该如何的样本。请帮我随机调用数组。谢谢!

1 个答案:

答案 0 :(得分:0)

我已经更改了你的程序,并添加了一些更改以使游戏循环继续,同时仍有剩余颜色或用户输入'y'。我用的是STL容器,非常舒服。 std::vector用于初始化并保存一些静态数据。所有颜色都将随机排列并生成。 std::stack持有随机播放的颜色并逐个弹出,它由LIFO(后进先出)起作用:

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <time.h>
#include <random>

bool IsWin ( const std::string& color )
{
    //Add here your logic
    return false;
}

int main(int argc, const char * argv[])
{
    // All your colors. You can add new or remove some old
    std::vector<std::string> AllPossibleColors {"Blue", "Red", "Green", "White", "Orange", "Yellow"};
    // Numerations of step, you can add some more there
    std::vector<std::string> ColorsOutputNumerations {"first", "second", "third", "fourth", "fifth", "sixth", "seventh"};

    //Shuffle colors order randomly
    unsigned long seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::shuffle (AllPossibleColors.begin(), AllPossibleColors.end(), std::default_random_engine(seed));

    //Stack will hold our shuffled colors
    std::stack<std::string> RandomColors;
    std::for_each(AllPossibleColors.begin(), AllPossibleColors.end(), [ &RandomColors ]( const std::string& Color  )
        {
            RandomColors.push(Color);
        });

    //Counter just to print current step if there are more steps then given in ColorsOutputNumerations
    size_t CurrentStep = 0;

    //Loop to output all colors
    while ( RandomColors.size() > 0 )
    {
        std::string CurColor = RandomColors.top();
        RandomColors.pop();

        char answer;

        std::cout << "The "<< ( (ColorsOutputNumerations.size() > CurrentStep) ? ColorsOutputNumerations[CurrentStep] : std::to_string(CurrentStep+1) ) <<" color is: " << CurColor << std::endl;

        // You can add win logic in this method IsWin(). Don't know what do you need
        if ( IsWin(CurColor) )
        {
            std::cout << "You have won!" << std::endl;
        }

        if (RandomColors.size() > 0 ) //If there are no more colors then gane ends
        {
            std::cout << "want to play again (y/n)? ";
            std::cin >> answer;
            if ( answer != 'y')
            {
                break;
            }
        }
        else
        {
            std::cout << "There are no more colors anymore" << std::endl;
        }

        CurrentStep++;
    }

    return 0;
}

测试播放输出:

The first color is: Red
want to play again (y/n)? y
The second color is: White
want to play again (y/n)? y
The third color is: Blue
want to play again (y/n)? y
The fourth color is: Green
want to play again (y/n)? y
The fifth color is: Orange
want to play again (y/n)? y
The sixth color is: Yellow
There are no more colors anymore
Program ended with exit code: 0
祝你好运!