"Color Game" - 从数组 C++ 调用随机

"Color Game" - calling random from array C++

本文关键字:C++ 调用 随机 数组 Color Game      更新时间:2023-10-16

大家好,所以我们在C 类中进行项目。它被称为"彩色游戏",有一个想象的模具,有6个侧面和差异颜色。我不知道如何随机调用字符串数组。例如,我声明:

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

我希望来自数组中的颜色的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;
}

,当我们运行程序时,两种颜色是从数组中随机挑选的。

这只是输出应该如何的示例。请帮助我随机呼叫阵列。谢谢!

我已经更改了您的程序,并添加了一些更改以使游戏循环继续,该游戏循环仍在剩下的颜色或用户输入'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

祝你好运!