-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsed_state.hpp
169 lines (150 loc) · 4.16 KB
/
parsed_state.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma once
#include <iostream>
#include "nlohmann/json.hpp"
static const int C_EMPTY = 0;
static const int C_SELF = 7;
static const int C_TAIL_OFS = 8;
static const int C_SELF_TAIL = C_SELF + C_TAIL_OFS;
static const int C_NITRO = C_SELF_TAIL + 2;
static const int C_SLOW = C_SELF_TAIL + 3;
static const int C_SAW = C_SELF_TAIL + 4;
class TParsedState
{
public:
using TJson = nlohmann::json;
bool Parse (const std::string& stateStr)
{
auto jstate = TJson::parse (stateStr);
const std::string type = jstate["type"];
if (type == "tick") {
ParseTick (jstate["params"]);
} else {
CurrentTick = 0;
if (type == "start_game") {
ParseStart (jstate["params"]);
} else if (type == "end_game") {
//
}
}
return CurrentTick > 0;
}
void PrintKeys (const std::string& what, const TJson& json)
{
std::cerr << what;
for (const auto& it : json.items ()) {
std::cerr << it.key () << "\t";
}
std::cerr << "\n";
}
void ParseTick (const TJson& jparams)
{
CurrentTick = jparams["tick_num"];
for (auto& player : Players) {
player.Alive = false;
}
for (const auto& it : jparams["players"].items ()) {
const int id = (it.key () == "i") ? C_SELF : std::stoi (it.key ());
ParsePlayer (id, it.value ());
}
for (const auto& jbonus : jparams["bonuses"]) {
auto& bonus = Bonuses.emplace_back ();
bonus.Point.Parse (jbonus["position"]);
Point2Cell (bonus.Point, bonus.Cell);
bonus.Type = jbonus["type"];
if (bonus.Type == "n") {
bonus.TypeCode = C_NITRO;
} else if (bonus.Type == "s") {
bonus.TypeCode = C_SLOW;
} else if (bonus.Type == "saw") {
bonus.TypeCode = C_SAW;
} else {
std::cerr << jbonus << "\n";
}
}
}
void ParsePlayer (int id, const TJson& jplayer)
{
auto& player = Players.at (id);
player.Alive = true;
player.Score = jplayer["score"];
for (const auto& jbonus : jplayer["bonuses"]) {
const int ticks = jbonus["ticks"];
const std::string type = jbonus["type"];
(void)ticks;
(void)type;
}
player.Point.Parse (jplayer["position"]);
const auto& jdir = jplayer["direction"];
if (jdir.is_string ()) {
player.Direction = jdir;
} else {
player.Direction.clear ();
}
ParseCoords (jplayer["lines"], player.Lines, player.LineCells);
ParseCoords (jplayer["territory"], player.Territory, player.TerrCells);
Point2Cell (player.Point, player.Cell);
}
void ParseStart (const TJson& jparams)
{
CellSize = jparams["width"];
BaseSpeed = jparams["speed"];
FieldWidth = jparams["x_cells_count"];
FieldHeight = jparams["y_cells_count"];
for (auto& player : Players) {
player.Lines.reserve (FieldWidth * FieldHeight);
player.Territory.reserve (FieldWidth * FieldHeight);
player.LineCells.reserve (FieldWidth * FieldHeight);
player.TerrCells.reserve (FieldWidth * FieldHeight);
}
}
struct TCoords {
int X;
int Y;
void Parse (const TJson& jcoords)
{
auto it = jcoords.cbegin ();
X = *it++;
Y = *it;
}
};
void Point2Cell (const TCoords& pt, TCoords& cell)
{
cell.X = (pt.X - CellSize / 2) / CellSize;
cell.Y = (pt.Y - CellSize / 2) / CellSize;
}
void ParseCoords (const TJson& jpoints, std::vector<TCoords>& points, std::vector<TCoords>& cells)
{
points.clear ();
for (const auto& jpoint : jpoints) {
points.emplace_back ().Parse (jpoint);
}
cells.resize (points.size ());
for (size_t i = 0; i < points.size (); ++i) {
Point2Cell (points[i], cells[i]);
}
}
struct TBonus {
TCoords Point;
TCoords Cell;
std::string Type;
char TypeCode; // 17=n 18=s 19=saw
};
struct TPlayer {
int Score;
std::string Direction;
TCoords Point;
TCoords Cell;
std::vector<TCoords> Lines;
std::vector<TCoords> Territory;
std::vector<TCoords> LineCells;
std::vector<TCoords> TerrCells;
bool Alive;
};
int CurrentTick;
int CellSize;
int BaseSpeed;
int FieldWidth;
int FieldHeight;
std::vector<TBonus> Bonuses;
std::array<TPlayer, 8> Players;
};