Search code examples
c++arraysprobabilitydice

Dice Roll Simulator C++


In class we received an assignment to make a C++ program that simulates rolling a die (or multiple). Simple enough, I did that really easy, even got it to roll more than 1 die at once and to count how many times a number was rolled (2-12, as you'll see in the code at the bottom). But then the teacher gave us another assignment. Making the program roll how ever many dice the user inputs, how many sides the dice has based on user input, how many times the user wants the dice to be rolled, and being able to "hold" dice, like in the game Yahtzee.

My classmates and I are really confused at how to do this, the hint the teacher gave us for creating the "hold" function involves Arrays. We're quite confused as the only Array example we have is the example we did in class, the original Dice simulator we made as an example and rolling two dice, etc.

Below is my code for what I have so far. I'm aware not all my variables have a purpose, and I tried replicating the example below my code with Arrays, and I know I didn't do it right, so please go easy on me. I'm not asking for you to do the assignment for me, I just need some guidance on what to do next and what I'm doing wrong. The code with the comment tags are the original Dice Simulator I mentioned, and above is what I'm working on. Any help is appreciated.

srand (time(NULL));
int dicenumber,diceroll,sides,rolls,rolling;
//Be able to hold, ask how many dice to roll, how many sides per die, and how many rolls
cout<<"\033[1;36m How many dice would you like to roll? \033[0m \n";
cin>>dicenumber;
cout<<"\033[1;36m How many sides per die? \033[0m \n";
cin>>sides;
cout<<"\033[1;36m How many times do you want to roll? \033[0m \n";
cin>>rolls;

//Sets total rolls to 0
for (rolling=0;rolling<rolls;rolling++)
    diceroll=rand()%sides+1;
    cout<<diceroll;




//Initialize variables and randomness
//srand (time(NULL));
//int COUNTER,ROLLS,TOTALS[13];

        //Set totals to 0
//for (COUNTER=0;COUNTER<13;COUNTER++)
//TOTALS[COUNTER]=0;

        //Simulate 1,000,000 dice rolls
//for (ROLLS=0;ROLLS<1000;ROLLS++)
    //TOTALS[rand()%6+1+rand()%6+1]++;

        //Output the totals
//for (COUNTER=1;COUNTER<13;COUNTER++)
    //cout<<COUNTER<<" = \033[1;36m"<<TOTALS[COUNTER]<<"\033[0m \n";

Solution

  • If you have no upper limit on the number of dice that a user can choose (or rather no 'assumed' upper limit) then in order to store the results you'll need to allocate the array according to the user's input, which can be accomplished like so:

    int *results = new int[dicenumber];
    for(int i = 0; i < dicenumber; i++){
        // rolling the dice and storing the values...
        *(results+i) = rand() % sides + 1;
    }// end for loop
    
    // viewing the results...
    for(int i = 0; i < dicenumber; i++){
        cout << *(results+i) << endl;
    }
    ...
    // end of program; free the memory when you're done
    delete[] results;
    

    In the above case, you're using a pointer *results to point to a specific address in memory, where using the new keyword allocated space for us to store the results of the die rolls. In this case, we've allocated a space equal to the size of an int times the number of dice. With each iteration of the for loop(s) we are incrementing the pointer, and thereby indexing through our array.

    However if you're just being introduced to arrays I find it highly unlikely your professor had pointer arithmetic in mind. Storing the results in a static array is considerably easier (or at least more intuitive); below is an example:

    int results[32]; // allocated an array of size sizeof(int)*32, so space for 32 ints!
    for(int i = 0; i < dicenumber; i++){
        // indexing through the array and assigning our roll...
        results[i] = rand() % sides + 1;
    }// end for loop
    
    // viewing the results...
    for(int i = 0; i < dicenumber; i++){
        cout << results[i] << endl;
    }// end for loop
    

    Either way, yes, an array is a simple way to store the results of your program. As the other answer suggested you could indeed use a std::vector<int> or a vector of some other custom class/struct to store the results, but frankly anything beyond the static array is overkill (at least for a programming course...)