45 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include "dice.h"
 | 
						|
#include <stdlib.h>
 | 
						|
#include <memory.h>
 | 
						|
 | 
						|
int roll_die(enum die_type die) {
 | 
						|
	int const max = die - 1;
 | 
						|
	return (rand() % max) + 1;
 | 
						|
}
 | 
						|
 | 
						|
static int current_active_count = 0;
 | 
						|
static enum die_type active_dice_set[MAX_ACTIVE_DICE];
 | 
						|
 | 
						|
enum die_type const *get_active_dice_set(size_t *out_length) {
 | 
						|
	if (out_length != nullptr) {
 | 
						|
		*out_length = current_active_count;
 | 
						|
	}
 | 
						|
	return active_dice_set;
 | 
						|
}
 | 
						|
 | 
						|
size_t add_die_to_active(enum die_type die) {
 | 
						|
	if (current_active_count >= MAX_ACTIVE_DICE) {
 | 
						|
		return MAX_ACTIVE_DICE;
 | 
						|
	}
 | 
						|
	active_dice_set[current_active_count] = die;
 | 
						|
	return current_active_count++;
 | 
						|
}
 | 
						|
 | 
						|
void remove_die_from_active(size_t index) {
 | 
						|
	memmove(active_dice_set + index + 1, active_dice_set + index, MAX_ACTIVE_DICE - index - 1);
 | 
						|
}
 | 
						|
 | 
						|
struct roll_result_type roll_active_dice_set(enum die_type die) {
 | 
						|
	struct roll_result_type results = {
 | 
						|
		.individual_result_count = MAX_ACTIVE_DICE,
 | 
						|
		.total = 0
 | 
						|
	};
 | 
						|
	for (size_t i = 0; i < current_active_count; ++i) {
 | 
						|
		results.individual_results[i] = roll_die(active_dice_set[i]);
 | 
						|
		results.total += results.individual_results[i];
 | 
						|
	}
 | 
						|
 | 
						|
	return results;
 | 
						|
}
 | 
						|
 |