简体   繁体   中英

C++ Dice simulation problem. Im stuck on looping with 2 variables and how this should be formatted

Write a function that simulates two six-sided dice. Roll both dice and count the number of rolls it takes to roll a double6 ten times. Output this message when each double6 is rolled. double six rolled: roll #xxxxx Roll the dice for a maximum of 200000 tries. For every 10000 roll, a double6 must be rolled. Display if the double6 was forced. Stop when double6 is rolled ten times. Display the number of rolls it took or state that we did not get 10 double6s if we exceed the maximum rolls.

Below are some of my attempts.







void rollDice(int numDice = 0)
{
    srand(time(0));
    int die1, die2, maxRoll,doubleSix;
    //die1 = rand() % 6 + 1;
  //  die2 = rand() % 6 + 1;
  // doubleSix = die1 == 6 && die2 == 6; // need a count function for that


   if (die1 == 6 && die2 == 6)
       for (int doubleSix = 0; doubleSix >=10; doubleSix++)
       {
           die1 = rand() % 6 + 1;
           die2 = rand() % 6 + 1;


       }


 //attempt 1
while ( maxRoll <= 200000 || doubleSix == 10)
    if (numDice > 0)
        for (int i = 0; i < numDice; i++) {
            cout << "\nDice roll1" << i + 1 << ": " << die1<<endl;
            cout << "\nDice roll2" << i + 1 << ": " <<die2<<endl;
        }

    else
        cout << "\nExiting.  Next time please enter a value greater than zero and less than 10.\n";

}


 //attempt 2
int i, maxRoll;

maxRoll = 0;
while ( maxRoll < 200000 )
{
maxRoll++;
continue;
cout << "Hello\n";
if ( doubleSix == 10)
break;
}
return 0;

Lets go in tiny steps...

Write a function that simulates two six-sided dice.

std::pair<int,int> rollDice() {
     return { rand() % 6 + 1, rand() % 6 + 1 };
}

Thats what you need to get started. Not more.

Roll both dice and count the number of rolls it takes to roll a double6 ten times

int rolls_till_2six() {
     int counter = 0;
     while (true) {
         ++counter;
         auto roll = rollDice();
         if (roll.first == 6 && roll.second == 6) return counter;
     }
}

Output this message when each double6 is rolled. double six rolled: roll #xxxxx

int main() {
     srand(time(0));
     int n = rolls_till_2six();
     std::cout << "double six rolled: roll #" << n << "\n";
}

Roll the dice for a maximum of 200000 tries

Now we are going to modify the function above:

 std::pair<int,bool> rolls_till_2six(int max_rolls) {
     int counter = 0;
     while (true) {
         ++counter;
         auto roll = rollDice();
         if (roll.first == 6 && roll.second == 6) return {counter,true};
         if (counter == max_rolls) return {counter,false};
     }
}

It now returns an int and a bool . The bool indicates whether the double 6 was rolled and the int is the number of trials.

For every 10000 roll, a double6 must be rolled. Display if the double6 was forced.

Ok, so we roll for a maximum of 10000 and if not we "force" a double six:

int main() {
     int total = 0;
     int total_max = 200000;
     int n_force = 10000;
     while (total < total_max) {
         auto res = rolls_till_2six(n_force);
         total += total.first;
         if (res.second) {
             // there was a double 6
         } else {
            // there was no double 6
            // ... force it
         }
     }
}

I'll stop here. Nothing of this is tested and the last snippet is incomplete. Actually only after writing the answer I realized that I ignored the "to roll a double6 ten times" part of the assignment. However, it wasn't my intention to give away a full solution anyhow. The take away message is: Do things in small steps. Sometimes small steps are still to big, then take tiny steps. Add one thing to your code at a time. And after each tiny step compile the code and perform tests. Do not add more code before you know that the code you already wrote compiles and does the right thing.

Start small, add complications one by one.

First, roll a die.

int roll() { return rand() % 6 + 1; }

To roll two dice, call it twice.

roll();
roll();

Count the number of double-sixes.

int double6 = 0;
if (roll() == 6 && roll() == 6)
{
    double6 += 1;
    std::cout << "Double six!" << std::endl;
}

Repeat counting until ten double-sixes.

int double6 = 0;
while (double6 < 10)
{
    if (roll() == 6 && roll() == 6)
    {
        double6 += 1;
        std::cout << "Double six!" << std::endl;
    }
}

Repeat until 200000 rolls or ten double-sixes.

int rolls = 0;
int double6 = 0;
while (rolls < 200000 && double6 < 10)
{
    rolls += 1;
    if (roll() == 6 && roll() == 6)
    {
        double6 += 1;
        std::cout << "Double six!" << std::endl;
    }
}

Force a double-six every 10000 rolls unless one has occurred.

int rolls = 0;
int double6 = 0;
bool got_one = false;
while (rolls < 200000 && double6 < 10)
{
    rolls += 1;
    if (roll() == 6 && roll() == 6)
    {
        double6 += 1;
        std::cout << "Double six!" << std::endl;
        got_one = true;
    }
    if (rolls % 10000 == 0)
    {
        if (!got_one)
        {
            double6 += 1;
            std::cout << "Forced a double six." << std::endl;
        }
        got_one = false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM