initial commit
This commit is contained in:
commit
563b1b0c5a
7 changed files with 335 additions and 0 deletions
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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue