&引用;彩色游戏从数组C+调用随机数+; 嘿,伙计们,我们正在C++类中做我们的项目。这被称为“颜色游戏”,有一个6面不同颜色的假想模具。我不知道如何随机调用字符串数组。例如,我宣布: int color[] = {"Blue", "Red", "Green", "White", "Orange", "Yellow"};

&引用;彩色游戏从数组C+调用随机数+; 嘿,伙计们,我们正在C++类中做我们的项目。这被称为“颜色游戏”,有一个6面不同颜色的假想模具。我不知道如何随机调用字符串数组。例如,我宣布: int color[] = {"Blue", "Red", "Green", "White", "Orange", "Yellow"};,c++,random,C++,Random,我希望数组中的颜色的1个随机名称作为输出。我只是不知道我会用什么代码。我只是一个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 i

我希望数组中的颜色的1个随机名称作为输出。我只是不知道我会用什么代码。我只是一个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;
}
char答案;

cout我已经更改了您的程序,并添加了一些更改,以使游戏循环在仍有颜色或用户输入“y”时继续。我用的是STL集装箱,很舒服<代码>标准::向量
用于初始化和保存一些静态数据。所有颜色都将随机排列并生成<代码>标准::堆栈保存混洗颜色并逐个弹出,它通过后进先出(后进先出)工作:


祝你好运

欢迎来到堆栈溢出!这是一个供开发者互相帮助解决常见问题的问答网站,不存在让陌生人在互联网上为你做作业的情况。也就是说,我认为你自己可能解决这个问题。想想你们在课堂上学到的关于数组的知识,你们有并没有学到如何从给定索引的数组中获取元素?你能想出一种生成随机索引的方法吗?为什么没有人提到这个
intcolor[]{“蓝”、“红”、“绿”、“白”、“橙”、“黄”}这在哪个世界起作用?了解
=
=
之间的区别。嘿,伙计们。我只是一个C++的业余爱好者。我知道你们在编码方面都是教授。好冷。这不是我将包含的最终代码。这只是一个样本,天哪。我只是在DeV C++上复制粘贴了代码,不工作
#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