创建一个Yahtzee游戏,并遇到Re-Roller(C++)的问题
Creating a Yahtzee Game and have trouble with Re-Roller(C++)
我目前很难让我的滚筒100%正确工作。有时它的行为是正确的,只会改变我的选择,但有时它似乎有自己的想法。以下是我的代码片段。
Int main()
...
while (counter<13){
cout<< playName << " please roll the dice."<<endl;
system("pause");
roll();
reRoll();
reRoll();
score();}
void reRoll(){
cout<< playName << " please select which dice you would like to re-roll by entering
a y or n."<<endl;
cout<< "Would you like to re-roll die 1?";
cin>>dieOne;
cout<< "Would you like to re-roll die 2?";
cin>>dieTwo;
cout<< "Would you like to re-roll die 3?";
cin>>dieThree;
cout<< "Would you like to re-roll die 4?";
cin>>dieFour;
cout<< "Would you like to re-roll die 5?";
cin>>dieFive;
srand(static_cast<unsigned int>(time(0)));
const int dice = 6;
int die[6] = {1, 2, 3, 4, 5, 6};
if (dieOne=="y"){
int dice1Roll = (rand() % dice);
currentDice[0] = die[dice1Roll];}
if (dieOne!="y"){}
if (dieTwo=="y"){
int dice2Roll = (rand() % dice);
currentDice[1] = die[dice2Roll];}
if (dieTwo!="y"){}
if (dieThree=="y"){
int dice3Roll = (rand() % dice);
currentDice[2] = die[dice3Roll];}
if (dieThree!="y"){}
if (dieFour=="y"){
int dice4Roll = (rand() % dice);
currentDice[3] = die[dice4Roll];}
if (dieFour!="y"){}
if (dieFive=="y"){
int dice5Roll = (rand() % dice);
currentDice[4] = die[dice5Roll];}
if (dieFive!="y"){}
cout<<playName<<"'s die are now "<<currentDice[0]<<" "<<currentDice[1]<<" "
<<currentDice[2]<<" "<<currentDice[3]<<" "<<currentDice[4]<<endl;}
在使用rand函数之前,不必每次都调用srand。无论你在做什么,只需在主目录中调用一次就足够了:
#include <cstdlib>
int main(void) {
srand(static_cast<unsigned int>(time(NULL)));
while(...) {}
return 0
}
此外,如果你试图滚动1到6之间的数字,你的滚轮应该是
int dice1Roll = (rand() % dice) + 1;
相关文章:
- 编译包含字符串的代码时遇到问题
- 遇到新行时,有没有办法停止istream_iterator
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 在 for 循环中查找问题时遇到困难
- 创建结构的数组时遇到分段错误
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 为什么我遇到分段错误?
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- 为什么我在尝试模板时遇到视觉工作室C++错误
- 如何从文本文件中读取数值,直到遇到字符类型?
- 从其他类访问类时遇到问题
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 打印问题.我在数组中打印值时遇到一些问题
- 使用静态成员声明类时遇到问题
- 创建 Spdlog 异步文件记录器时遇到困难
- C++ 填充数组时遇到问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 创建一个Yahtzee游戏,并遇到Re-Roller(C++)的问题