"Color Game" - 从数组 C++ 调用随机
"Color Game" - calling random from array C++
大家好,所以我们在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
祝你好运!
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 什么时候调用析构函数
- 如何用参数值调用函数(仅在运行时已知)
- std::cout.imbue()多重调用
- 函数何时会在c++中包含stack_Unwind_Resume调用