示例#1
0
int rolldice(int sides, int times)
{
 int sum = 0;
 int x;
  for(x=0;x<times;x++)
  {
   sum += rolldie(sides);
  }
 return sum;
}
int rolldice(int dice, int sides)
{
	// roll multiple n-sided dice and return outcome

	int total = 0;

	for (int i = 0; i < dice; i++)
		total += rolldie(sides);

	return total;
}
示例#3
0
文件: main.c 项目: 5n8ke/rolldice
/* print_rolls() - Prints the rolls, either just the totals or the
 *                 separate rolls, also.
 * 
 * Parameters: Dice string with which to calculate dice rolls
 * Returns: None
 */
void print_rolls(int *dice_nums) {
    int i, j, k, temp_int, temp_index, temp_total;
    int* temp_roll;
     
    if((temp_roll = malloc(sizeof(*temp_roll) * dice_nums[NUM_DICE])) == NULL) {
        perror("rolldice");
    	exit(EX_OSERR);
    }
    
    for(i = 0; i < dice_nums[NUM_ROLLS]; i++) {
	temp_total = 0;
	if(print_separate) printf("Roll #%d: (", i+1);
	for(j = 0; j < dice_nums[NUM_DICE]; j++) {
	    temp_roll[j] = rolldie(dice_nums[NUM_SIDES]);
	    if(print_separate) printf("%d%s", temp_roll[j], (j < dice_nums[NUM_DICE] - 1) ? " " : "");
	    temp_total += temp_roll[j];
	}
	for(j = 0; j < dice_nums[NUM_DROP]; j++) {
	    temp_int = SHRT_MAX;
	    for(k = 0; k < dice_nums[NUM_DICE]; k++)
		if(temp_int > temp_roll[k]) {
		    temp_int = temp_roll[k];
		    temp_index = k;
		}
	    if(print_separate) printf("- %d ", temp_int);
	    temp_total -= temp_int;
	    temp_roll[temp_index] = SHRT_MAX;
	}
	if(print_separate) printf(") ");
	if(dice_nums[MULTIPLIER] != 1) {
	    if(print_separate) printf("* %d ", dice_nums[MULTIPLIER]);
	    temp_total *= dice_nums[MULTIPLIER];
	}
	if(dice_nums[MODIFIER]) {
	    if(print_separate){
            if (dice_nums[MODIFIER] > 0)
                printf("+ %d ", dice_nums[MODIFIER]);
            else
                printf("- %d ", abs(dice_nums[MODIFIER]));

        }
	    temp_total += dice_nums[MODIFIER];
	}
	if(print_separate) printf("= ");
	printf("%d ", temp_total);
	if(print_separate) printf("\n");
    }
    if(!print_separate) printf("\n");
}