From 563b1b0c5ad1a12572b1fce65b3e6581f1c7a429 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Tue, 1 Nov 2022 22:09:32 +0100 Subject: [PATCH] initial commit --- .gitignore | 2 + arraylist.c | 43 ++++++++++++++++++ arraylist.h | 14 ++++++ makefile | 12 +++++ planarbot.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++ sortedmap.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++ sortedmap.h | 16 +++++++ 7 files changed, 335 insertions(+) create mode 100644 .gitignore create mode 100644 arraylist.c create mode 100644 arraylist.h create mode 100644 makefile create mode 100644 planarbot.c create mode 100644 sortedmap.c create mode 100644 sortedmap.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1a5cdd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +planarbot diff --git a/arraylist.c b/arraylist.c new file mode 100644 index 0000000..da84d5e --- /dev/null +++ b/arraylist.c @@ -0,0 +1,43 @@ +#include "arraylist.h" +#include +#include + +ArrayList * list() { + ArrayList *this = malloc(sizeof(ArrayList)); + this->capacity = 4; + this->size = 0; + this->items = malloc(this->capacity * sizeof(void *)); + return this; +} + +void * get_list(ArrayList *this, int index) { + if (index < 0 || this->size < index) { + fprintf(stderr, "reading outside the bounds of an arraylist, index=%d, size=%d\n", index, this->size); + exit(1); + } + return this->items[index]; +} + +void add_list(ArrayList *this, void *item) { + if (this->size == this->capacity) { + this->capacity *= 2; + this->items = realloc(this->items, this->capacity * sizeof(void *)); + } + this->items[this->size++] = item; +} + +void free_listitems(ArrayList *this) { + int i; + for(i = 0; i < this->size; i++) { + free(this->items[i]); + } +} + +void free_list(ArrayList *this) { + int i; + for (i = 0; i < this->size; i++) { + free(this->items[i]); + } + free(this->items); + free(this); +} diff --git a/arraylist.h b/arraylist.h new file mode 100644 index 0000000..f0ff6a3 --- /dev/null +++ b/arraylist.h @@ -0,0 +1,14 @@ +#ifndef ARRAY_LIST +#define ARRAY_LIST +typedef struct arraylist { + int capacity; + int size; + void **items; +} ArrayList; + +ArrayList * list(); +void * get_list(ArrayList *this, int index); +void add_list(ArrayList *this, void *item); +void free_listitems(ArrayList *this); +void free_list(ArrayList *this); +#endif diff --git a/makefile b/makefile new file mode 100644 index 0000000..8567105 --- /dev/null +++ b/makefile @@ -0,0 +1,12 @@ +CFLAGS += -g -Wall -Werror -pedantic -ansi + +all: planarbot inputfile + valgrind ./planarbot < inputfile + +planarbot: planarbot.o arraylist.o sortedmap.o +planarbot.o: arraylist.h +arraylist.o: arraylist.h +sortedmap.o: sortedmap.h + +clean: + rm -f *.o planarbot diff --git a/planarbot.c b/planarbot.c new file mode 100644 index 0000000..a752384 --- /dev/null +++ b/planarbot.c @@ -0,0 +1,125 @@ +#include +#include +#include + +#include "arraylist.h" +#include "sortedmap.h" + +#define BUF 20 + +struct LIST { + ArrayList * (*init)(); + void (*add)(ArrayList *, void *); + void * (*get)(ArrayList *, int); + void (*freeitems)(ArrayList *); + void (*free)(ArrayList *); +} LIST = { list, add_list, get_list, free_listitems, free_list }; + +typedef struct planet { + int ship_count; + float x; + float y; + int owner; + char name[BUF]; +} Planet; + +typedef struct expedition { + int id; + int ship_count; + char origin[BUF]; + char destination[BUF]; + int owner; + int turns_remaining; +} Expedition; + +typedef struct gamestate { + ArrayList *planets; + ArrayList *expeditions; +} Gamestate; + +void print_planet(Planet *this) { + printf("{ \"ship_count\" : %d , \"x\" : %f , \"y\" : %f , \"owner\" : %d , \"name\" : %s }\n", + this->ship_count, this->x, this->y, this->owner, this->name); +} + +int read_planet(ArrayList *planets) { + char owner[BUF] = {0}; + Planet *planet = calloc(1, sizeof(Planet)); + if (scanf( + " { \"ship_count\" : %d , \"x\" : %f , \"y\" : %f , \"owner\" : %19[0-9a-z] , \"name\" : \"%19[^\"]\" } ", + &planet->ship_count, &planet->x, &planet->y, owner, planet->name + ) != 5) { + free(planet); + return 1; + } + scanf(" , "); + if (owner[0] == 'n') { + planet->owner = 0; + } else { + sscanf(owner, "%d", &planet->owner); + } + print_planet(planet); + LIST.add(planets, planet); + return 0; +} + +void print_expedition(Expedition *expedition) { + printf("{ \"id\" : %d , \"ship_count\" : %d , \"origin\" : %s , \"destination\" : %s , \"owner\" : %d , \"turns_remaining\" : %d }\n", + expedition->id, + expedition->ship_count, + expedition->origin, + expedition->destination, + expedition->owner, + expedition->turns_remaining); +} + +int read_expedition(ArrayList *expeditions) { + Expedition *expedition = calloc(1, sizeof(Expedition)); + if (scanf( + " { \"id\" : %d , \"ship_count\" : %d , \"origin\" : \"%19[^\"]\" , \"destination\" : \"%19[^\"]\" , \"owner\" : %d , \"turns_remaining\" : %d } ", + &expedition->id, &expedition->ship_count, expedition->origin, expedition->destination, &expedition->owner, &expedition->turns_remaining + ) != 6) { + free(expedition); + return 1; + } + scanf(" , "); + + print_expedition(expedition); + add_list(expeditions, expedition); + return 0; +} + +Gamestate *read_gamestate() { + Gamestate *gamestate = malloc(sizeof(Gamestate)); + gamestate->planets = list(); + gamestate->expeditions = list(); + scanf(" { \"planets\": [ "); + while (read_planet(gamestate->planets) == 0); + scanf(" ] , \"expeditions\" : [ "); + while (read_expedition(gamestate->expeditions) == 0); + return gamestate; +} + +void free_gamestate(Gamestate *this) { + free_list(this->planets); + free_list(this->expeditions); + free(this); +} + +int main(int argc, char **argv) { + Gamestate *gamestate; + SortedMap *test; + + gamestate = read_gamestate(); + free_gamestate(gamestate); + test = map(); + test = put_map(test, "asdf", "value"); + test = put_map(test, "asdf", "asdf"); + test = put_map(test, "qwer", "asdf"); + test = put_map(test, "qwer", "qwer"); + test = put_map(test, "awer", "qwer"); + printf("%s\n", (char *) get_map(test, "awer")); + print_map(test); + free_map(test); + return 0; +} diff --git a/sortedmap.c b/sortedmap.c new file mode 100644 index 0000000..fd86fd1 --- /dev/null +++ b/sortedmap.c @@ -0,0 +1,123 @@ +#include "sortedmap.h" +#include +#include +#include + +struct intern { + enum nodetype type; + char * key; + struct node * left; + struct node * right; +}; + +struct leaf { + enum nodetype type; + char * key; + void * value; +}; + +SortedMap * map() { + return NULL; +} + +void * get_map(SortedMap *this, char *key) { + if (this == NULL) return NULL; + if (this->type == LEAF) { + if (strcmp(key, this->key) == 0) return ((struct leaf *) this)->value; + return NULL; + } else { + if (strcmp(key, this->key) <= 0) { + return get_map(((struct intern *) this)->left, key); + } else { + return get_map(((struct intern *) this)->right, key); + } + } +} + +SortedMap * put_map(SortedMap *this, char *key, void *value) { + struct leaf *leaf, *newleaf; + struct intern *intern; + int cmp; + + if (this == NULL) { + struct leaf *leaf = malloc(sizeof(struct leaf)); + leaf->type = LEAF; + leaf->key = key; + leaf->value = value; + return (SortedMap *) leaf; + } else { + cmp = strcmp(key, this->key); + if (this->type == LEAF) { + leaf = (struct leaf*) this; + if (cmp == 0) { + leaf->value = value; + return (SortedMap *) leaf; + } + newleaf = malloc(sizeof(struct leaf)); + newleaf->type = LEAF; + newleaf->key = key; + newleaf->value = value; + intern = malloc(sizeof(struct intern)); + intern->type = INTERN; + intern->key = leaf->key; + intern->left = (struct node*) (cmp < 0 ? newleaf : leaf); + intern->right = (struct node*) (cmp < 0 ? leaf : newleaf); + return (SortedMap *) intern; + } else { + intern = (struct intern*) this; + if (cmp <= 0) { + intern->left = put_map(intern->left, key, value); + return (SortedMap *) intern; + } else { + intern->right = put_map(intern->right, key, value); + return (SortedMap *) intern; + } + } + } +} + +void print_indent_(int indent) { + int i; + for(i = 0; i < indent; i++) printf(" "); +} + +void print_map_(SortedMap *this, int indent) { + if (this->type == LEAF) { + struct leaf *leaf = (struct leaf*) this; + print_indent_(indent); + printf("%s: %s\n", leaf->key, (char *) leaf->value); + } else { + struct intern *intern = (struct intern*) this; + print_map_(intern->left, indent + 1); + print_indent_(indent); + printf("%s\n", intern->key); + print_map_(intern->right, indent + 1); + } +} + +void print_map(SortedMap *this) { + if (this == NULL) printf("empty\n"); + else print_map_(this, 0); +} + +void free_mapvalues(SortedMap *this) { + if (this == NULL) return; + else if (this->type == LEAF) free(((struct leaf*) this)->value); + else { + struct intern *intern = (struct intern*) this; + free_mapvalues(intern->left); + free_mapvalues(intern->right); + } +} + +void free_map(SortedMap *this) { + struct intern *intern; + if (this == NULL) return; + else if (this->type == LEAF) free(this); + else { + intern = (struct intern*) this; + free_map(intern->left); + free_map(intern->right); + free(this); + } +} diff --git a/sortedmap.h b/sortedmap.h new file mode 100644 index 0000000..2644cea --- /dev/null +++ b/sortedmap.h @@ -0,0 +1,16 @@ +#ifndef SORTED_MAP +#define SORTED_MAP +enum nodetype { LEAF, INTERN }; + +typedef struct node { + enum nodetype type; + char * key; +} SortedMap; + +SortedMap * map(); +void * get_map(SortedMap *this, char *key); +SortedMap * put_map(SortedMap *this, char *key, void *value); +void print_map(SortedMap *this); +void free_mapvalues(SortedMap *this); +void free_map(SortedMap *this); +#endif