planarbot/planarbot.c

170 lines
4.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arraylist.h"
#include "sortedmap.h"
#include "json.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 };
*/
const int NO_OWNER = -1;
typedef struct planet {
int ship_count;
float x;
float y;
int owner;
char *name;
} Planet;
typedef struct expedition {
int id;
int ship_count;
char *origin;
char *destination;
int owner;
int turns_remaining;
} Expedition;
typedef struct gamestate {
ArrayList *planets;
ArrayList *expeditions;
} Gamestate;
void * print_planet(void *acc, int index, void *item) {
Planet *planet = item;
printf(" { \"ship_count\": %d , \"x\": %f , \"y\": %f , \"owner\": %d , \"name\": \"%s\" }\n",
planet->ship_count, planet->x, planet->y, planet->owner, planet->name);
return NULL;
}
void * print_expedition(void *acc, int index, void *item) {
Expedition *expedition = item;
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);
return NULL;
}
void print_gamestate(Gamestate *this) {
printf("\n");
printf("{ \"planets\": [\n");
fold_list(this->planets, NULL, print_planet);
printf("], \"expeditions\": [\n");
fold_list(this->expeditions, NULL, print_expedition);
printf("]}\n");
printf("\n");
}
void * free_planet(void *acc, int index, void *item) {
Planet *planet = item;
free(planet->name);
free(planet);
return NULL;
}
void * free_expedition(void *acc, int index, void *item) {
Expedition *expedition = item;
free(expedition->origin);
free(expedition->destination);
free(expedition);
return NULL;
}
void free_gamestate(Gamestate *this) {
fold_list(this->planets, NULL, free_planet);
free_list(this->planets);
fold_list(this->expeditions, NULL, free_expedition);
free_list(this->expeditions);
free(this);
}
void * planet_from_json(void *acc, int index, void *item) {
ArrayList *planets = (ArrayList *) acc;
Json *json = (Json *) item;
Json *owner = (Json *) get_map(json->value.map, "owner");
char *name;
Planet *planet = malloc(sizeof(Planet));
planet->ship_count = (int) ((Json *) get_map(json->value.map, "ship_count"))->value.number;
planet->x = ((Json *) get_map(json->value.map, "x"))->value.number;
planet->y = ((Json *) get_map(json->value.map, "y"))->value.number;
planet->owner = owner->type == NIL ? NO_OWNER : owner->value.number;
name = ((Json *) get_map(json->value.map, "name"))->value.string;
planet->name = malloc(strlen(name) + 1);
strcpy(planet->name, name);
add_list(planets, planet);
return acc;
}
void * expedition_from_json(void *acc, int index, void *item) {
ArrayList *expeditions = (ArrayList *) acc;
Json *json = (Json *) item;
char *origin, *destination;
Expedition *expedition = malloc(sizeof(Expedition));
expedition->id = (int) ((Json *) get_map(json->value.map, "id"))->value.number;
expedition->ship_count = (int) ((Json *) get_map(json->value.map, "ship_count"))->value.number;
origin = ((Json *) get_map(json->value.map, "origin"))->value.string;
expedition->origin = malloc(strlen(origin) + 1);
strcpy(expedition->origin, origin);
destination = ((Json *) get_map(json->value.map, "destination"))->value.string;
expedition->destination = malloc(strlen(destination) + 1);
strcpy(expedition->destination, destination);
expedition->owner = (int) ((Json *) get_map(json->value.map, "owner"))->value.number;
expedition->turns_remaining = (int) ((Json *) get_map(json->value.map, "turns_remaining"))->value.number;
add_list(expeditions, expedition);
return acc;
}
Gamestate * parse_gamestate_json() {
Gamestate *gamestate = malloc(sizeof(Gamestate));
Json *json = json_parse(stdin);
Json *planets = get_map(json->value.map, "planets");
Json *expeditions = get_map(json->value.map, "expeditions");
json_print(json, stderr);
json_print(planets, stderr);
json_print(expeditions, stderr);
gamestate->planets = list();
fold_list(planets->value.list, gamestate->planets, planet_from_json);
gamestate->expeditions = list();
fold_list(expeditions->value.list, gamestate->expeditions, expedition_from_json);
json_free(json);
return gamestate;
}
int main(int argc, char **argv) {
Gamestate *gamestate = parse_gamestate_json();
print_gamestate(gamestate);
free_gamestate(gamestate);
/*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;
}