Compare commits

..

3 commits
legacy ... main

Author SHA1 Message Date
Sara dc30a2f183 feat: added TODO.txt 2025-09-14 23:14:51 +02:00
Sara e619d7e271 feat: created new project 2025-09-14 23:14:07 +02:00
Sara 859eb3b9d6 feat: removed legacy code 2025-09-14 22:16:39 +02:00
28 changed files with 126 additions and 294 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
bin/
build/

4
TODO.txt Normal file
View file

@ -0,0 +1,4 @@
Configure a build system
Implement parse
Implement help
Implement interactive mode

BIN
a.out

Binary file not shown.

View file

@ -1,53 +0,0 @@
#include "string.h"
#include "stdlib.h"
#include "ctype.h"
#include "stdio.h"
#include "futil.h"
char* g_attribs=(char*)0;
const char** g_attrib_index=(const char**)0;
int g_attribs_count=0;
int g_attribs_len=0;
/*
* load and index an attributes file
*/
int load_attrib_file(const char* file) {
FILE* stream=fopen(file,"r");
g_attribs_len=file_len(stream);
g_attribs_count=fcountc(stream,',')+1;
g_attribs=calloc(g_attribs_len,sizeof(char)+1);
g_attrib_index=calloc(sizeof(char*),g_attribs_count);
const char** ind_writer = g_attrib_index;
int n='\0';
char* writer=g_attribs;
*ind_writer=writer;
++ind_writer;
while((n=fgetc(stream))!=EOF) {
char c=(char)n;
printf("%c",c);
switch(c) {
case ',':
*writer='\0';
if(isspace((char)fpeek(stream)))
*ind_writer=writer+2;
else
*ind_writer=writer+1;
++ind_writer;
break;
case '\t':
case '\n':
*writer=' ';
break;
default:
*writer=c;
break;
}
++writer;
}
*writer='\0';
for(int i=0;i<g_attribs_count;++i) {
printf("%d) %s\n",i+1,g_attrib_index[i]);
}
return 0;
}

View file

@ -1,11 +0,0 @@
#ifndef _attrib_h
#define _attrib_h
extern char* g_attribs;
extern int g_attribs_count;
extern int g_attribs_len;
int load_attribs(const char*);
int load_attrib_file(const char*);
#endif /* _attrib_h */

View file

@ -1,2 +0,0 @@
#!/bin/bash
gcc *.c -o dice --std=c99 -g

BIN
dice

Binary file not shown.

59
dice.c
View file

@ -1,59 +0,0 @@
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "memory.h"
int rolld(int d) {
return rand() % d + 1;
}
int rollnd(int n, int d, int* i) {
int total=0;
while(n-->0) {
int roll = rolld(d);
total += roll;
if(i!=(void*)0)
*(i++) = roll;
}
return total;
}
int process_dice_format(const char* format,int* num,int* die) {
int dummy; // dummy int for if either of the out adresses is 0
if(num==(void*)0)num=&dummy;
if(die==(void*)0)die=&dummy;
char buf[10];
memset(buf,0,10); // clear buffer
char* bufw=buf;
const char* next=format;
while(*next!='\0') {
if(*next=='d') { // argument is d in <num>d<die> format
if(next==format) // if there is no die count given, assume 1
*num=1;
else
*num=atoi(buf); // store buffered string as number
memset(buf,0,10); // clear buffer
bufw=buf; // reset buffer walker
next++; // skip
}
*bufw=*next; // buffer next character
bufw++;next++; // increment walker and next character
}
*die=atoi(buf);
return 0;
}
void roll_and_print(int n, int d) {
int* out_rolls=malloc(sizeof(int)*n);
int result=rollnd(n,d,out_rolls);
printf(" %d (%dd%d) ",result,n,d);
// only print inbetween steps if there are any
if(n>1) {
for(int i=0;i<n;++i) {
printf("%d%s",out_rolls[i],i==n-1?"\n":"+");
}
} else {
printf("\n");
}
free(out_rolls);
}

9
dice.h
View file

@ -1,9 +0,0 @@
#ifndef _dice_h
#define _dice_h
extern int rolld(int d);
extern int rollnd(int n,int d,int* i);
extern int process_dice_format(const char* format,int* num,int* die);
extern void roll_and_print(int n, int d);
#endif /* _dice_h */

28
flags.c
View file

@ -1,28 +0,0 @@
#include "memory.h"
#include "ctype.h"
#include "dice.h"
#include "attrib.h"
int f_interactive=0;
int f_num=0;
int f_die=0;
int g_argc=0;
const char** g_argv=(void*)0;
int process_arg(int argind) {
if(argind>=g_argc)return 0;
const char* arg=g_argv[argind];
char buf[10];
memset(buf,0,10);
if(strcmp(arg,"-i")==0) {
f_interactive=1;
return process_arg(argind+1);
} else if(strcmp(arg, "-a")==0) {
load_attrib_file(g_argv[argind+1]);
return process_arg(argind+2);
} else if(isdigit(arg[0])||arg[0]=='d') {
process_dice_format(arg,&f_num,&f_die);
return process_arg(argind+1);
}
return 0;
}

12
flags.h
View file

@ -1,12 +0,0 @@
#ifndef _flags_h
#define _flags_h
extern int f_interactive;
extern int f_num;
extern int f_die;
extern int g_argc;
extern const char** g_argv;
extern int process_arg(int argind);
#endif /* _flags_h */

32
futil.c
View file

@ -1,32 +0,0 @@
#include "stdlib.h"
#include "stdio.h"
long file_len(FILE* file) {
fpos_t pos;
fgetpos(file,&pos);
fseek(file,0,SEEK_END);
long len=ftell(file);
fsetpos(file,&pos);
return len;
}
long fcountc(FILE* file, char c) {
int i = 0;
long t = 0;
fpos_t strt;
fgetpos(file,&strt);
while((i=fgetc(file))!=EOF) {
if((char)i==c)
++t;
}
fsetpos(file, &strt);
return t;
}
int fpeek(FILE* file) {
fpos_t p;
fgetpos(file,&p);
int v = fgetc(file);
fsetpos(file,&p);
return v;
}

22
futil.h
View file

@ -1,22 +0,0 @@
#ifndef _futils_h
#define _futils_h
#include "stdio.h"
/*
* return the length of a text file
*/
long file_len(FILE* file);
/*
* count the number of occurences of c in file
* starting from the current stream position
*/
long fcountc(FILE* file, char c);
/*
* peek the next value of file
*/
int fpeek(FILE* file);
#endif /* _futils_h */

View file

@ -1,19 +0,0 @@
#include "stdio.h"
#include "dice.h"
#include "string.h"
#include "flags.h"
int run_interactive() {
int running = 1;
char buf[24];
while(running) {
printf("> ");
memset(buf,0,10);
scanf("%s",buf);
if(strcmp(buf,"quit")==0||strcmp(buf,"exit")==0||strcmp(buf,"q")==0) return 0;
process_dice_format(buf,&f_num,&f_die);
if(f_num!=0&&f_die!=0||buf[0]=='\0')
roll_and_print(f_num, f_die);
}
return 0;
}

View file

@ -1,6 +0,0 @@
#ifndef _interactive_h
#define _interactive_h
extern int run_interactive();
#endif /* _interactive_h */

19
main.c
View file

@ -1,19 +0,0 @@
#include "time.h"
#include "stdlib.h"
#include "dice.h"
#include "flags.h"
#include "interactive.h"
int main(int argc,const char* argv[]) {
srand(time(NULL));
g_argv=argv;
g_argc=argc;
process_arg(1);
// if there are valid dice to roll
if(f_die>0&&f_num>0) {
roll_and_print(f_num,f_die);
}
if(f_interactive==1||argc==1) {
return run_interactive();
}
}

8
pc.c
View file

@ -1,8 +0,0 @@
#include "pc.h"
#include "memory.h"
int new_character(pc_t* pc, const char* name) {
memset(pc,0,sizeof(pc_t));
strncpy(pc->name,name,64);
return 0;
}

13
pc.h
View file

@ -1,13 +0,0 @@
#ifndef _pc_h
#define _pc_h
typedef struct pc_t {
char name[64];
int dexterity,strength,constitution,intelligence,wisdom;
const char* spells[64];
const char* traits[64];
} pc_t;
int new_character(pc_t* pc, const char* name);
#endif /* _pc_h */

8
src/dice.c Normal file
View file

@ -0,0 +1,8 @@
#include "dice.h"
#include <stdlib.h>
int roll_die(struct roll_data data) {
int min = data.count;
int max = data.count * data.die - data.count;
return min + (rand() % max);
}

22
src/dice.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef DICE_H
#define DICE_H
enum die {
D4 = 4,
D6 = 6,
D8 = 8,
D10 = 10,
D12 = 12,
D20 = 20,
D100 = 100,
COIN = 2,
};
struct roll_data {
int count;
enum die die;
};
int roll_die(struct roll_data data);
#endif // !DICE_H

6
src/interactive.c Normal file
View file

@ -0,0 +1,6 @@
#include "interactive.h"
#include <stdio.h>
void interactive() {
fprintf(stderr, "interactive mode not implemented\n");
}

6
src/interactive.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef DICE_INTERACTIVE_H
#define DICE_INTERACTIVE_H
void interactive();
#endif // !DICE_INTERACTIVE_H

23
src/main.c Normal file
View file

@ -0,0 +1,23 @@
#include "dice.h"
#include "interactive.h"
#include "output.h"
#include "parse.h"
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]) {
srand(time(NULL));
int mod;
struct roll_data roll_data = { 1, D4 };
if (argc == 1) {
interactive();
return 0;
} else if (parse(argv[argc-1], &roll_data, &mod)) {
print_roll(roll_data, mod, roll_die(roll_data));
return 0;
} else {
print_help();
return 0;
}
}

27
src/output.c Normal file
View file

@ -0,0 +1,27 @@
#include "output.h"
#include <stdio.h>
#include <stdlib.h>
static void print_mod(int mod) {
if (mod > 0) {
printf("+%d", abs(mod));
} else if (mod < 0) {
printf("%d", abs(mod));
}
}
void print_roll(struct roll_data data, int mod, int roll) {
printf("rolled: %dd%d", data.count, data.die);
print_mod(mod);
printf(" (%d", roll);
print_mod(mod);
printf(")");
}
void print_error(char const *error) {
fprintf(stderr, "format incorrect: %s\n", error);
}
void print_help() {
fprintf(stderr, "help not implemented\n");
}

10
src/output.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef DICE_OUTPUT_H
#define DICE_OUTPUT_H
#include "dice.h"
extern void print_roll(struct roll_data data, int mod, int roll);
extern void print_format_error(char const *error);
extern void print_help();
#endif // !DICE_OUTPUT_H

8
src/parse.c Normal file
View file

@ -0,0 +1,8 @@
#include "parse.h"
#include <stdbool.h>
#include <stdio.h>
bool parse(char *die_arg, struct roll_data* o_data, int *o_mod) {
fprintf(stderr, "parse not implemented\n");
return false;
}

10
src/parse.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef DICE_PARSE_H
#define DICE_PARSE_H
#include <stdbool.h>
struct roll_data;
extern bool parse(char *die_arg, struct roll_data* o_data, int *o_mod);
#endif // !DICE_PARSE_H

View file

@ -1 +0,0 @@
firebolt, fireball, mage hand, magic shield
1 firebolt fireball mage hand magic shield