initial commit
This commit is contained in:
commit
563b1b0c5a
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.o
|
||||
planarbot
|
43
arraylist.c
Normal file
43
arraylist.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include "arraylist.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
14
arraylist.h
Normal file
14
arraylist.h
Normal file
@ -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
|
12
makefile
Normal file
12
makefile
Normal file
@ -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
|
125
planarbot.c
Normal file
125
planarbot.c
Normal file
@ -0,0 +1,125 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
123
sortedmap.c
Normal file
123
sortedmap.c
Normal file
@ -0,0 +1,123 @@
|
||||
#include "sortedmap.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
16
sortedmap.h
Normal file
16
sortedmap.h
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user