Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 61 additions & 43 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,64 +56,82 @@ static void print_version() {
exit(EXIT_SUCCESS);
}


/* print_one_roll() - Prints a roll, either just the total or the
* separate dices.
*
* Parameters:
* - Dice string with which to calculate dice rolls
* - Index to know which roll is computed
* Returns: None
*/
void print_one_roll(int *dice_nums, int index){
int 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(EXIT_FAILURE);
}

temp_total = 0;
if(print_separate) printf("Roll #%d: (", index + 1);
for(j = 0; j < dice_nums[NUM_DICE]; j++) {
temp_roll[j] = rolldie(dice_nums[NUM_SIDES]);
if(print_separate) printf("%d ", temp_roll[j]);
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");

free(temp_roll);
}


/* 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;
int i;

if(dice_nums == NULL) {
fprintf(stderr, "Problems with the dice string, either malformed "
"or numbers are too large,\nso quitting!\n");
exit(EXIT_FAILURE);
}

if((temp_roll = malloc(sizeof(*temp_roll) * dice_nums[NUM_DICE])) == NULL) {
perror("rolldice");
exit(EXIT_FAILURE);
}

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 ", temp_roll[j]);
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");
print_one_roll(dice_nums, i);
}
if(!print_separate) printf("\n");
}
Expand Down
65 changes: 38 additions & 27 deletions rolldice.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@

#include "rolldice.h"


/* The following #defines give the tokens for each part of the format
* string.
*/
#define ROLL_IDENT 'x'
#define DICE_SIDES_IDENT 'd'
#define MULTI_IDENT '*'
#define MOD_PLUS_IDENT '+'
#define MOD_MINUS_IDENT '-'
#define DROP_IDENT 's'


// File pointer for random device
static FILE* ran_dev;

// Local functions
int get_num_dice(int temp_int, int default_num);
int *get_num_sides(char *dice_string, int temp_int, int *res_int);
int *get_num_drop(char *dice_string, int temp_int, int *res_int);
int get_num_dice(const int temp_int, const int default_num);
int *get_num_sides(const char const *dice_string, int temp_int, int *res_int);
int *get_num_drop(const char const *dice_string, int temp_int, int *res_int);
int *get_num_rolls(int temp_int, int *res_int);
int *get_mutiplier(char *dice_string, int temp_int, int *res_int);
int *get_plus_modifier(char *dice_string, int temp_int, int *res_int);
int *get_minus_modifier(char *dice_string, int temp_int, int *res_int);
int is_too_big(int num);
void print_parse_error(const char * label, const int too_big_error);
int *get_mutiplier(const char const *dice_string, int temp_int, int *res_int);
int *get_plus_modifier(const char const *dice_string, int temp_int, int *res_int);
int *get_minus_modifier(const char const *dice_string, int temp_int, int *res_int);
int is_too_big(const int num);
void print_parse_error(const char const * label, const int too_big_error);


void init_random(rand_type rand_file) {
void init_random(const rand_type rand_file) {
if(rand_file == RANDOM) {
if((ran_dev = fopen("/dev/random", "r")) == NULL) {
fprintf(stderr, "Error in opening /dev/random!\n");
Expand All @@ -38,7 +50,7 @@ void init_random(rand_type rand_file) {
}
}

static int get_random(int sides) {
static int get_random(const int sides) {
unsigned int ret_value;

if(!(fread(&ret_value, sizeof(unsigned int), 1, ran_dev) == 1)) {
Expand All @@ -53,8 +65,7 @@ static int get_random(int sides) {
* Parameters: int num_sides - number of sides of the die to roll
* Returns: int - the result of the roll
*/
int rolldie ( int num_sides ) {

int rolldie (const int num_sides) {
return (1 + get_random(num_sides));
}

Expand All @@ -64,7 +75,7 @@ int rolldie ( int num_sides ) {
* Returns: int * - array of nums describing the different aspects of the
* dice to be rolled
*/
int *parse_string(char *dice_string) {
int *parse_string(const char const *dice_string) {
int temp_int = -1, *dice_nums, *res_int;
const int DEFAULT_NUM_DICE = 1;

Expand All @@ -87,7 +98,7 @@ int *parse_string(char *dice_string) {
}
else {
switch(*dice_string) {
case 'd':
case DICE_SIDES_IDENT:
dice_nums[NUM_DICE] = get_num_dice(temp_int, DEFAULT_NUM_DICE);
dice_string++;
res_int = get_num_sides(dice_string, temp_int, res_int);
Expand All @@ -98,7 +109,7 @@ int *parse_string(char *dice_string) {
dice_nums[NUM_SIDES] = *res_int;
}
break;
case 's':
case DROP_IDENT:
dice_string++;
res_int = get_num_drop(dice_string, temp_int, res_int);
if (res_int == NULL){
Expand All @@ -108,7 +119,7 @@ int *parse_string(char *dice_string) {
dice_nums[NUM_DROP] = *res_int;
}
break;
case 'x':
case ROLL_IDENT:
dice_string++;
res_int = get_num_rolls(temp_int, res_int);
if (res_int == NULL){
Expand All @@ -118,7 +129,7 @@ int *parse_string(char *dice_string) {
dice_nums[NUM_ROLLS] = *res_int;
}
break;
case '*':
case MULTI_IDENT:
dice_string++;
res_int = get_mutiplier(dice_string, temp_int, res_int);
if (res_int == NULL){
Expand All @@ -128,7 +139,7 @@ int *parse_string(char *dice_string) {
dice_nums[MULTIPLIER] = *res_int;
}
break;
case '+':
case MOD_PLUS_IDENT:
dice_string++;
res_int = get_plus_modifier(dice_string, temp_int, res_int);
if (res_int == NULL){
Expand All @@ -138,7 +149,7 @@ int *parse_string(char *dice_string) {
dice_nums[MODIFIER] = *res_int;
}
break;
case '-':
case MOD_MINUS_IDENT:
dice_string++;
res_int = get_minus_modifier(dice_string, temp_int, res_int);
if (res_int == NULL){
Expand All @@ -159,18 +170,18 @@ int *parse_string(char *dice_string) {
return dice_nums;
}

int get_num_dice(int temp_int, int default_num){
int get_num_dice(const int temp_int, const int default_num){
if( (temp_int <= 0 ) || is_too_big(temp_int) )
return default_num;
else
return temp_int;
}

int is_too_big(int num){
int is_too_big(const int num){
return num >= SHRT_MAX;
}

void print_parse_error(const char * label, const int too_big_error){
void print_parse_error(const char const * label, const int too_big_error){
if (too_big_error){
fprintf(stderr, "rolldice: Requested %s is too large\n", label);
}
Expand All @@ -180,7 +191,7 @@ void print_parse_error(const char * label, const int too_big_error){

}

int *get_num_sides(char *dice_string, int temp_int, int *res_int){
int *get_num_sides(const char const *dice_string, int temp_int, int *res_int){
const char *PERCENT = "%";
if(strncmp(dice_string, PERCENT, 1) == 0){
temp_int = 100;
Expand All @@ -197,7 +208,7 @@ int *get_num_sides(char *dice_string, int temp_int, int *res_int){
}
}

int *get_num_drop(char *dice_string, int temp_int, int *res_int){
int *get_num_drop(const char const *dice_string, int temp_int, int *res_int){
if( (sscanf(dice_string, "%d", &temp_int) < 1) ||
(temp_int < 0) || is_too_big(temp_int) ) {
print_parse_error("number of dropped dice", is_too_big(temp_int));
Expand All @@ -218,7 +229,7 @@ int *get_num_rolls(int temp_int, int *res_int){
}
}

int *get_mutiplier(char *dice_string, int temp_int, int *res_int){
int *get_mutiplier(const char const *dice_string, int temp_int, int *res_int){
if( (sscanf(dice_string, "%d", &temp_int) < 1) ||
(temp_int < 0) || is_too_big(temp_int) ) {
print_parse_error("multiplier", is_too_big(temp_int));
Expand All @@ -229,7 +240,7 @@ int *get_mutiplier(char *dice_string, int temp_int, int *res_int){
}
}

int *get_plus_modifier(char *dice_string, int temp_int, int *res_int){
int *get_plus_modifier(const char const *dice_string, int temp_int, int *res_int){
if( (sscanf(dice_string, "%d", &temp_int) < 1) ||
(temp_int < 0) || is_too_big(temp_int) ) {
print_parse_error("add modifier", is_too_big(temp_int));
Expand All @@ -240,7 +251,7 @@ int *get_plus_modifier(char *dice_string, int temp_int, int *res_int){
}
}

int *get_minus_modifier(char *dice_string, int temp_int, int *res_int){
int *get_minus_modifier(const char const *dice_string, int temp_int, int *res_int){
if( (sscanf(dice_string, "%d", &temp_int) < 1) ||
(temp_int < 0) || is_too_big(temp_int) ) {
print_parse_error("minus modifier", is_too_big(temp_int));
Expand Down
23 changes: 3 additions & 20 deletions rolldice.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
/* For the rand() and srand() functions */
#include <math.h>

/* For the strstr() function */
#include <string.h>

/* For some bounds */
#include <limits.h>

Expand All @@ -43,25 +40,11 @@
#define NUM_DROP 5
#define DICE_ARRAY_SIZE 6

/* The following #defines give the tokens for each part of the format
* string. Perhaps eventually I'll change parse_string to use strtok()
* instead of strstr() :)
*/
#define ROLL_IDENT "x"
#define DICE_SIDES_IDENT "d"
#define MULTI_IDENT "*"
#define MOD_PLUS_IDENT "+"
#define MOD_MINUS_IDENT "-"
#define DROP_IDENT "s"

// Defines values for true and false, just for testing stuff boolean-wise :)
#define TRUE_VAL 1
#define FALSE_VAL 0

// Defines values for the random number file to use
typedef enum {UNDEF, URANDOM, RANDOM} rand_type;

// External function declarations for using rolldice() and kin.
extern int *parse_string(char *dice_string);
extern int rolldie(int num_sides);
extern void init_random(rand_type rand_file);
extern int *parse_string(const char const *dice_string);
extern int rolldie(const int num_sides);
extern void init_random(const rand_type rand_file);