Browse Source

Strategická: c++: Dokončení vstupu a výstupu

master
Jiří Kalvoda 2 years ago
parent
commit
601fa275bc
  1. 53
      klient/client.cpp

53
klient/client.cpp

@ -96,6 +96,7 @@ struct State
State(char * input_str) State(char * input_str)
{ {
// Ano, vím, že je to ošklivé, ale prostě parsovat JSON v C/C++ je za trest
int r; int r;
jsmn_parser p; jsmn_parser p;
jsmntok_t t[10000]; /* We expect no more than this count of tokens (jsmn limitation)*/ jsmntok_t t[10000]; /* We expect no more than this count of tokens (jsmn limitation)*/
@ -126,11 +127,9 @@ struct State
; ;
} }
assert(state_tok); assert(state_tok);
printf("%d", teams_count);
for(int i=0; i<teams_count; i++) for(int i=0; i<teams_count; i++)
{
teams.push_back(new Team(i, i==team_id)); teams.push_back(new Team(i, i==team_id));
} my_team = teams[team_id];
for (int i = state_tok+1, c=0; c < t[state_tok].size;i = js_skip(t, i), c++) for (int i = state_tok+1, c=0; c < t[state_tok].size;i = js_skip(t, i), c++)
{ {
if (jsoneq(input_str, &t[i], "world") == 0) if (jsoneq(input_str, &t[i], "world") == 0)
@ -151,13 +150,23 @@ struct State
current_field->hill = input_str[t[k+1].start] == 't'; current_field->hill = input_str[t[k+1].start] == 't';
if (jsoneq(input_str, &t[k], "home_for_team") == 0) if (jsoneq(input_str, &t[k], "home_for_team") == 0)
if(input_str[t[k+1].start] != 'n') 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 = 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 (jsoneq(input_str, &t[k], "occupied_by_team") == 0)
if(input_str[t[k+1].start] != 'n') 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 = 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 (jsoneq(input_str, &t[k], "protected_for_team") == 0)
if(input_str[t[k+1].start] != 'n') 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 = 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) if (jsoneq(input_str, &t[k], "members") == 0)
members_tok = k+1; members_tok = k+1;
} }
@ -172,6 +181,7 @@ struct State
if (jsoneq(input_str, &t[l], "team") == 0) if (jsoneq(input_str, &t[l], "team") == 0)
m->team = teams[atoi(input_str+t[l+1].start)]; m->team = teams[atoi(input_str+t[l+1].start)];
} }
m->team->members.push_back(m);
current_field->members.push_back(m); current_field->members.push_back(m);
} }
current_row.push_back(current_field); current_row.push_back(current_field);
@ -179,6 +189,32 @@ struct State
world.push_back(current_row); 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; State* state;
@ -189,15 +225,16 @@ State* state;
// main (zde doplňte kód) // main (zde doplňte kód)
void load() int main()
{ {
char * input_str; char * input_str;
scanf("%m[^\n]", &input_str); scanf("%m[^\n]", &input_str);
state = new State(input_str); state = new State(input_str);
}
int main() // TODO sem patří herní logika
{ for(Member *m: state->my_team->members)
load(); m->action = UP;
state->print_turn();
return 0; return 0;
} }

Loading…
Cancel
Save