first
This commit is contained in:
13
CMakeLists.txt
Normal file
13
CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
cmake_minimum_required(VERSION 3.30.0)
|
||||
|
||||
project(DiceRoller VERSION 0.1)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
set(CMAKE_COLOR_MAKEFILE ON)
|
||||
set(CMAKE_COLOR_DIAGNOSTICS ON)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
add_subdirectory(source)
|
||||
add_subdirectory(external)
|
||||
6
source/CMakeLists.txt
Normal file
6
source/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
# ADD SOURCES HERE
|
||||
set(SOURCE_FILES
|
||||
main.c
|
||||
)
|
||||
|
||||
add_executable(roll ${SOURCE_FILES})
|
||||
62
source/main.c
Normal file
62
source/main.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/* #############################################################################
|
||||
* # DiceRoller
|
||||
* This Program will alow you to role a dice or die using the scmatics such
|
||||
* as 1d6, 5d20, 1d100, d20
|
||||
*
|
||||
* AUTHER: PreacherDHM
|
||||
* DATE: 06/02/25
|
||||
* #############################################################################
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
/* GetDice(char* format)
|
||||
*
|
||||
* This will return the value of the rolled dice.
|
||||
*/
|
||||
int GetDice(const char* format) {
|
||||
int size = strlen(format);
|
||||
int seperator = 0;
|
||||
int resolt = 0;
|
||||
unsigned int seed = time(0);
|
||||
char c_amount[30] = { '\0' };
|
||||
char c_dice[30] = { '\0' };
|
||||
|
||||
for(int i = 0; i < size; i++) {
|
||||
if(format[i] != 'd')
|
||||
continue;
|
||||
seperator = i;
|
||||
break;
|
||||
}
|
||||
if(seperator == 0)
|
||||
return -1;
|
||||
|
||||
strncpy(c_amount, format,seperator);
|
||||
strcpy(c_dice, &format[seperator + 1]);
|
||||
|
||||
int amount = atoi(c_amount);
|
||||
int dice = atoi(c_dice);
|
||||
int r = 0;
|
||||
for (int i = 0; i < amount; i++) {
|
||||
r = rand_r(&seed) % (dice - 1 + 1) + 1;
|
||||
resolt += r;
|
||||
}
|
||||
|
||||
return resolt;
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
int roled;
|
||||
for(int i = 1; i < argc; i++) {
|
||||
roled = GetDice(argv[i]);
|
||||
if(roled <= 0) {
|
||||
printf("FORMAT ERROR\n"); return -1;
|
||||
}
|
||||
printf("%s: Roled a %d\n",argv[i], GetDice(argv[i]));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user