Rolling seven dice over and over is how I passed time “playing” D&D. But I also played a game with the dice, one that I introduced in last week’s Lesson. That lesson’s code got things started. This Lesson finishes the project.
The goal of the game is to roll the seven dice until each maxes out its value. For example, keep rolling a D20 until the value 20 (“nat 20!”) appears. Then the D20 is eliminated and the rest of the dice are rolled over and over until each one maxes out. Yes, it’s a stupid way to pass the time, but it was less distracting for the other players than had I started reading a book or humming loudly.
The updates to last week’s code include a new array, done[]
. This array tracks when each die has maxed is value. It’s a simple, binary array with zeros set for each element representing each die in the set.
I’ve also added int variable count
to monitor the total number of throws. Variable tally
determines when all the dice have maxed out.
An endless while loop keeps “throwing” the dice until the game is over. For each roll, a if test determines whether a dice has maxed out. If true, the die’s corresponding element in the done[]
array is set to one. A second check is made to monitor the done[]
array to check when all the dice are maxed out. Otherwise, the while loop keeps spinning.
Here is the full code:
2025_08_02-Lesson.c
#include <stdio.h> #include <stdlib.h> #include <time.h> /* rolling an 's' sided die */ int roll(int s) { return( rand() % s + 1 ); } int main() { const int die = 6; /* d4, d6, d8, d12, d20, d100 */ const int sides[] = { 4, 6, 8, 12, 20, 100 }; int d[die],done[die]; /* six different die */ int x,count,tally; /* seed the randomizer */ srand( (unsigned)time(NULL) ); /* initialize the done[] array */ for( x=0; x<die; x++ ) done[x] = 0; /* show results */ printf("%6s%5s%5s%5s%5s%5s%5s\n", " Count","D4","D6","D8","D12","D20","D100" ); /* throw dem bones! */ count = 1; while(1) { printf("%5d:",count); for( x=0; x<die; x++ ) { d[x] = roll( sides[x] ); if( d[x]==sides[x] ) done[x] = 1; printf("%5d", done[x] ? sides[x] : d[x] ); } putchar('\n'); /* test for completion */ tally = 0; for( x=0; x<die; x++ ) tally += done[x]; if( tally==die ) break; /* end the loop */ count++; } /* summary */ printf("It took %d throws for each die to max out\n",count); return 0; }
In the code, I modified the initial printf() statement adjusting output to account for a running throw total.
The first for loop in the endless while loop rolls each die: d[x] = roll( sides[x] );
The if test checks to see whether the dice has maxed out:
if( d[x]==sides[x] )
done[x] = 1;
When a die is maxed out, its value is equal to the number of sides (represented in the sides[]
array). At this point, its corresponding element in the done[]
array is set to one.
The final statement in the first for loop outputs either the die’s current value or, when the die’s corresponding done[]
element is set, the max value is output:
printf("%5d", done[x] ? sides[x] : d[x] );
Yes, this evaluation means that the program continues to roll each die, but the output shows only that a die is maxed.
The second for loop tests to see whether all the dice have maxed out. The total of all elements in the done[]
array is saved in variable tally
. When tally is equal to the number of dice (variable die
), the endless while loop breaks. The game is over and the total number of rolls is output.
Here’s a sample run:
Count D4 D6 D8 D12 D20 D100
1: 2 4 2 12 8 61
2: 1 6 7 12 2 64
3: 1 6 2 12 14 10
4: 3 6 4 12 3 96
5: 4 6 2 12 8 70
6: 4 6 5 12 16 68
7: 4 6 7 12 7 44
8: 4 6 3 12 18 40
9: 4 6 3 12 8 57
10: 4 6 5 12 6 96
11: 4 6 8 12 20 19
12: 4 6 8 12 20 60
13: 4 6 8 12 20 95
14: 4 6 8 12 20 78
15: 4 6 8 12 20 10
16: 4 6 8 12 20 100
It took 16 throws for each die to max out
It was fortunate that the program took only 16 rolls to max out each die for the sample run. Other sample runs ran into the multiple hundreds of throws. But this is the nature of this silly game, basically to waste time while others were enjoying themselves playing a game I just don’t get.