sw-battle-test-c

Interesting test task in C instead of C++
git clone git://bsandro.tech/sw-battle-test-c
Log | Files | Refs | README

main.c (1180B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include "core/world.h"
      5 #include "core/parser.h"
      6 #include "systems/actions.h"
      7 #include "systems/death.h"
      8 
      9 int main(int argc, char **argv) {
     10     World w = {0};
     11     w.turn = 1;
     12     World_Add_System(&w, &(System){ .run = actions_run });
     13     World_Add_System(&w, &(System){ .run = death_run });
     14     //puts("world create ok");
     15 
     16     if (argc>1) {
     17         FILE *f = fopen(argv[1], "r");
     18         // @todo check if f is NULL
     19         char *line = NULL;
     20         size_t linesize = 0;
     21         ssize_t slen;
     22         while ((slen=getline(&line, &linesize, f))!=-1) {
     23             if (slen==1) continue;
     24             //printf("[%zd] %s", slen, line);
     25             if (!try_parse_call(&w, line)) {
     26                 printf("invalid command %s\n", line);
     27                 return 1;
     28             }
     29         }
     30         free(line);
     31         fclose(f);
     32     }
     33 
     34     while (World_Run_Systems(&w)) {
     35         //printf("[turn %3d] world systems run ok\n", turn++);
     36         // safeguard
     37         if (w.turn >= 1000) {
     38             puts("Safeguard: turns limit has been reached.");
     39             break;
     40         }
     41     }
     42 
     43     //puts("simulation ok");
     44     return 0;
     45 }