feat: defined dice logic and types
This commit is contained in:
		
							parent
							
								
									ee8fc5391b
								
							
						
					
					
						commit
						827b96b0a3
					
				
							
								
								
									
										44
									
								
								src/dice.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								src/dice.c
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,44 @@
 | 
				
			||||||
 | 
					#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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										33
									
								
								src/dice.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/dice.h
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,33 @@
 | 
				
			||||||
 | 
					#ifndef DICE_H
 | 
				
			||||||
 | 
					#define DICE_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef MAX_ACTIVE_DICE
 | 
				
			||||||
 | 
					#define MAX_ACTIVE_DICE 5
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					enum die_type {
 | 
				
			||||||
 | 
						COIN = 2,
 | 
				
			||||||
 | 
						D4 = 4,
 | 
				
			||||||
 | 
						D8 = 8,
 | 
				
			||||||
 | 
						D10 = 10,
 | 
				
			||||||
 | 
						D12 = 12,
 | 
				
			||||||
 | 
						D20 = 20,
 | 
				
			||||||
 | 
						D100 = 100
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct roll_result_type {
 | 
				
			||||||
 | 
						size_t individual_result_count;
 | 
				
			||||||
 | 
						int individual_results[MAX_ACTIVE_DICE];
 | 
				
			||||||
 | 
						int total;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extern int roll_die(enum die_type die);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extern enum die_type const *get_active_dice_set(size_t *out_length);
 | 
				
			||||||
 | 
					extern size_t add_die_to_active(enum die_type die);
 | 
				
			||||||
 | 
					extern void remove_die_from_active(size_t index);
 | 
				
			||||||
 | 
					extern struct roll_result_type roll_active_dice_set(enum die_type die);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // !DICE_H
 | 
				
			||||||
		Loading…
	
		Reference in a new issue