planarbot/planarbot.c
2023-10-30 22:54:47 +01:00

129 lines
3.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 };
*/
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(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);
}
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);
}
void free_gamestate(Gamestate *this) {
fold_list(this->planets, NULL, free_listitem);
free_list(this->planets);
fold_list(this->expeditions, NULL, free_listitem);
free_list(this->expeditions);
free(this);
}
void * planet_from_json(void *acc, int index, void *item) {
ArrayList *planets = (ArrayList *) acc;
Json *json = (Json *) item;
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 = (int) ((Json *) get_map(json->value.map, "owner"))->value.number;
planet->name = ((Json *) get_map(json->value.map, "name"))->value.string;
add_list(planets, planet);
return acc;
}
void * expedition_from_json(void *acc, int index, void *item) {
ArrayList *expeditions = (ArrayList *) acc;
Json *json = (Json *) item;
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;
expedition->origin = ((Json *) get_map(json->value.map, "origin"))->value.string;
expedition->destination = ((Json *) get_map(json->value.map, "destination"))->value.string;
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();
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;
}