创建一个Yahtzee游戏,并遇到Re-Roller(C++)的问题

Creating a Yahtzee Game and have trouble with Re-Roller(C++)

本文关键字:Re-Roller 遇到 C++ 问题 游戏 一个 Yahtzee 创建      更新时间:2023-10-16

我目前很难让我的滚筒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;