From 601fa275bc0b391d630f053d2bab8427913e0b77 Mon Sep 17 00:00:00 2001 From: Jiri Kalvoda Date: Fri, 23 Sep 2022 02:34:05 +0200 Subject: [PATCH] =?UTF-8?q?Strategick=C3=A1:=20c++:=20Dokon=C4=8Den=C3=AD?= =?UTF-8?q?=20vstupu=20a=20v=C3=BDstupu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- klient/client.cpp | 53 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/klient/client.cpp b/klient/client.cpp index ccea599..6fa245f 100644 --- a/klient/client.cpp +++ b/klient/client.cpp @@ -96,6 +96,7 @@ struct State State(char * input_str) { + // Ano, vím, že je to ošklivé, ale prostě parsovat JSON v C/C++ je za trest int r; jsmn_parser p; jsmntok_t t[10000]; /* We expect no more than this count of tokens (jsmn limitation)*/ @@ -126,11 +127,9 @@ struct State ; } assert(state_tok); - printf("%d", teams_count); for(int i=0; ihill = input_str[t[k+1].start] == 't'; if (jsoneq(input_str, &t[k], "home_for_team") == 0) if(input_str[t[k+1].start] != 'n') + { current_field->home_for_team = teams[atoi(input_str+t[k+1].start)]; + current_field->home_for_team->home = current_field; + } if (jsoneq(input_str, &t[k], "occupied_by_team") == 0) if(input_str[t[k+1].start] != 'n') + { current_field->occupied_by_team = teams[atoi(input_str+t[k+1].start)]; + current_field->occupied_by_team->occupied.push_back(current_field); + } + if (jsoneq(input_str, &t[k], "protected_for_team") == 0) if(input_str[t[k+1].start] != 'n') + { current_field->protected_for_team = teams[atoi(input_str+t[k+1].start)]; + current_field->protected_for_team->protected_fields.push_back(current_field); + } if (jsoneq(input_str, &t[k], "members") == 0) members_tok = k+1; } @@ -172,6 +181,7 @@ struct State if (jsoneq(input_str, &t[l], "team") == 0) m->team = teams[atoi(input_str+t[l+1].start)]; } + m->team->members.push_back(m); current_field->members.push_back(m); } current_row.push_back(current_field); @@ -179,6 +189,32 @@ struct State world.push_back(current_row); } } + + void print_turn() + { + for(Team *t: teams) + if(!t->is_me) + for(Member *m: t->members) + if(m->action != STAY) + { + fprintf(stderr, "Voják cizího týmu má přiřazenou akci."); + exit(1); + } + bool first = true; + printf("{\"members\": ["); + for(Member *m: my_team->members) + { + if(!first) printf(", "); + const char * action = "stay"; + if(m->action == UP) action = "up"; + if(m->action == LEFT) action = "left"; + if(m->action == DOWN) action = "down"; + if(m->action == RIGHT) action = "right"; + printf("{\"id\": %d, \"action\": \"%s\"}", m->id, action); + first = false; + } + printf("]}"); + } }; State* state; @@ -189,15 +225,16 @@ State* state; // main (zde doplňte kód) -void load() +int main() { char * input_str; scanf("%m[^\n]", &input_str); state = new State(input_str); -} -int main() -{ - load(); + // TODO sem patří herní logika + for(Member *m: state->my_team->members) + m->action = UP; + + state->print_turn(); return 0; }