How to resolve the algorithm RPG attributes generator step by step in the C programming language
How to resolve the algorithm RPG attributes generator step by step in the C programming language
Table of Contents
Problem Statement
RPG = Role Playing Game.
You're running a tabletop RPG, and your players are creating characters. Each character has six core attributes: strength, dexterity, constitution, intelligence, wisdom, and charisma. One way of generating values for these attributes is to roll four, 6-sided dice (d6) and sum the three highest rolls, discarding the lowest roll. Some players like to assign values to their attributes in the order they're rolled. To ensure generated characters don't put players at a disadvantage, the following requirements must be satisfied: However, this can require a lot of manual dice rolling. A programatic solution would be much faster.
Write a program that:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm RPG attributes generator step by step in the C programming language
This C code snippet generates 6 random numbers in the range 1 to 6, calculates their sum and counts how many of them are greater than or equal to 15. The code uses the rand() function to generate random numbers and the qsort() function to sort the numbers in ascending order. If the sum of the numbers is less than 75 or fewer than 2 of the numbers are greater than or equal to 15, the code generates a new set of numbers. Once a valid set of numbers is generated, the code prints them to the console along with their sum and the number of numbers that are greater than or equal to 15.
Here is a detailed explanation of the code:
- The
#includedirectives include the necessary header files for the program. - The
compareInts()function is a comparison function used by theqsort()function to sort the numbers in ascending order. It takes two pointers to integers and returns the difference between the two integers. - The
main()function is the entry point of the program. - The
srand()function is used to initialize the random number generator with a seed based on the current time. This ensures that the program generates different random numbers each time it is run. - The
forloop that begins on line 11 is the main loop of the program. It continues to generate sets of 6 random numbers until a valid set is found. - The
forloop that begins on line 12 generates 4 random numbers in the range 1 to 6 and stores them in thenumbersarray. - The
qsort()function is used to sort the numbers in ascending order. - The
nsumvariable stores the sum of the 3 middle numbers in thenumbersarray. - The
values[i]variable stores thensumvalue for the current set of numbers. - The
vsumvariable stores the sum of all 6nsumvalues. - If the
vsumvalue is less than 75, thecontinuestatement is executed, which causes the program to skip the rest of the loop and start the next iteration. - The
vcountvariable stores the number ofnsumvalues that are greater than or equal to 15. - If the
vcountvalue is less than 2, thecontinuestatement is executed, which causes the program to skip the rest of the loop and start the next iteration. - If a valid set of numbers is found, the program prints them to the console along with their sum and the number of numbers that are greater than or equal to 15.
- The
breakstatement is used to exit theforloop. - The
return 0statement is used to indicate that the program has terminated successfully.
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int compareInts(const void *i1, const void *i2) {
int a = *((int *)i1);
int b = *((int *)i2);
return a - b;
}
int main() {
int i, j, nsum, vsum, vcount, values[6], numbers[4];
srand(time(NULL));
for (;;) {
vsum = 0;
for (i = 0; i < 6; ++i) {
for (j = 0; j < 4; ++j) {
numbers[j] = 1 + rand() % 6;
}
qsort(numbers, 4, sizeof(int), compareInts);
nsum = 0;
for (j = 1; j < 4; ++j) {
nsum += numbers[j];
}
values[i] = nsum;
vsum += values[i];
}
if (vsum < 75) continue;
vcount = 0;
for (j = 0; j < 6; ++j) {
if (values[j] >= 15) vcount++;
}
if (vcount < 2) continue;
printf("The 6 random numbers generated are:\n");
printf("[");
for (j = 0; j < 6; ++j) printf("%d ", values[j]);
printf("\b]\n");
printf("\nTheir sum is %d and %d of them are >= 15\n", vsum, vcount);
break;
}
return 0;
}
You may also check:How to resolve the algorithm Equilibrium index step by step in the C programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the C programming language
You may also check:How to resolve the algorithm I before E except after C step by step in the C programming language
You may also check:How to resolve the algorithm Empty directory step by step in the C programming language
You may also check:How to resolve the algorithm Bitcoin/address validation step by step in the C programming language