Bridges-C++ 3.5.0-dev2-1-ge3e57bf
Bridges(C++ API)
GameGrid.h
Go to the documentation of this file.
1
2#include <string>
3using namespace std;
4
5#ifndef GAME_GRID_H
6#define GAME_GRID_H 1
7
8#include "Grid.h"
9#include "Color.h"
10#include "base64.h"
11
12namespace bridges {
13 namespace game {
14 enum class NamedColor : unsigned char {
17 aqua,
19 azure,
20 beige,
21 bisque,
22 black,
24 blue,
26 brown,
31 coral,
34 crimson,
35 cyan,
47 darkred,
57 dimgray,
58 dimgrey,
63 fuchsia,
66 gold,
68 gray,
69 grey,
70 green,
73 hotpink,
75 indigo,
76 ivory,
77 khaki,
97 lime,
99 linen,
100 magenta,
101 maroon,
112 mintcream,
113 mistyrose,
114 moccasin,
116 navy,
117 oldlace,
118 olive,
119 olivedrab,
120 orange,
121 orangered,
122 orchid,
124 palegreen,
128 peachpuff,
129 peru,
130 pink,
131 plum,
133 purple,
134 red,
135 rosybrown,
136 royalblue,
138 salmon,
140 seagreen,
141 seashell,
142 sienna,
143 silver,
144 skyblue,
145 slateblue,
146 slategray,
147 slategrey,
148 snow,
150 steelblue,
151 tan,
152 teal,
153 thistle,
154 tomato,
155 turquoise,
156 violet,
157 wheat,
158 white,
160 yellow,
162 };
163
164 enum class NamedSymbol : unsigned char {
165 none,
166 A,
167 B,
168 C,
169 D,
170 E,
171 F,
172 G,
173 H,
174 I,
175 J,
176 K,
177 L,
178 M,
179 N,
180 O,
181 P,
182 Q,
183 R,
184 S,
185 T,
186 U,
187 V,
188 W,
189 X,
190 Y,
191 Z,
192 a,
193 b,
194 c,
195 d,
196 e,
197 f,
198 g,
199 h,
200 i,
201 j,
202 k,
203 l,
204 m,
205 n,
206 o,
207 p,
208 q,
209 r,
210 s,
211 t,
212 u,
213 v,
214 w,
215 x,
216 y,
217 z,
218 zero,
219 one,
220 two,
221 three,
222 four,
223 five,
224 six,
225 seven,
226 eight,
227 nine,
228 empty0,
229 circle,
230 square,
231 diamond,
236 man,
237 woman,
238 cat,
239 pawn,
240 knight,
241 bishop,
242 rook,
243 queen,
244 king,
247 arrow_up,
249 star,
250 droplet,
251 heart,
252 lightning,
255 flower,
257 circle_x,
258 circle_x2,
260 empty1,
261 sword,
264 target,
265 empty2,
266 waves,
267 rain,
268 drink,
269 paperclip,
270 elephant,
271 cowboyhat,
272 ballcap,
273 flag,
274 bomb,
275 bear,
276 giraffe,
277 bug1,
278 bug2,
279 bug3,
281 fishes,
282 duck,
283 sloth,
284 bird,
285 apple,
286 carrot,
287 lemon,
288 pepper,
289 onion,
290 potion1,
291 potion2,
293 campfire,
294 donut,
295 monitor
296 };
297
312 class GameCell {
313 NamedColor bg;
314 NamedColor fg;
315 NamedSymbol symbol;
316
317 public:
322 symbol = NamedSymbol::none;
323 }
324
330 this->bg = bg;
331 this->fg = fg;
332 this->symbol = symbol;
333 }
334
340 this->bg = bg;
341 }
342
348 this->fg = fg;
349 }
350
356 this->symbol = s;
357 }
358
364 return bg;
365 }
366
372 return fg;
373 }
374
380 return symbol;
381 }
382
383 };
384
385 //TODO: implement RLE encoding
398 class GameGrid : public Grid<GameCell> {
399 private:
400 std::string encoding = "raw";
401
402 void initializeGrid() {
403 for (int i = 0; i < gridSize[0]; i++) {
404 for (int j = 0; j < gridSize[1]; j++) {
408 }
409 }
410 }
411 public:
412
419 void setBGColor(int row, int col, NamedColor color) {
420 (*this)[row][col].setBGColor(color);
421 }
422
423 NamedColor getBGColor(int row, int col) const {
424 return (*this)[row][col].getBGColor();
425 }
426
427 NamedColor getFGColor(int row, int col) const {
428 return (*this)[row][col].getFGColor();
429 }
430
431 NamedSymbol getSymbol(int row, int col) const {
432 return (*this)[row][col].getSymbol();
433 }
434
441 void setFGColor(int row, int col, NamedColor color) {
442 (*this)[row][col].setFGColor(color);
443 }
444
451 void setSymbol(int row, int col, NamedSymbol symbol) {
452 (*this)[row][col].setSymbol(symbol);
453 }
454
461 void drawSymbol(int row, int col,
462 NamedSymbol symbol, NamedColor color) {
463 setFGColor(row, col, color);
464 setSymbol(row, col, symbol);
465 }
466
467 virtual const string getDStype() const override {
468 return "GameGrid";
469 }
470
475 GameGrid (int nbrow = 10, int nbcol = 10)
476 : Grid<GameCell> (nbrow, nbcol) {
477 initializeGrid();
478 }
479
480 GameGrid (const GameGrid &) = delete;
481
482 virtual ~GameGrid() = default;
483
484 private:
485
486 //public:
487
488 string getRAWRepresentation() const {
490
491 std::vector<unsigned char> bgbuf;
492 std::vector<unsigned char> fgbuf;
493 std::vector<unsigned char> symbolbuf;
494
495 for (int i = 0; i < gridSize[0]; i++) {
496 for (int j = 0; j < gridSize[1]; j++) {
497 bgbuf.push_back(static_cast<unsigned char>(getBGColor(i, j)));
498 fgbuf.push_back(static_cast<unsigned char>(getFGColor(i, j)));
499 symbolbuf.push_back(static_cast<unsigned char>(getSymbol(i, j)));
500 }
501 }
502
503 std::string bgstr = base64::encode (&(bgbuf[0]), bgbuf.size());
504 std::string fgstr = base64::encode (&(fgbuf[0]), fgbuf.size());
505 std::string symbolstr = base64::encode (&(symbolbuf[0]), symbolbuf.size());
506
507 std::string ret;
508 // Add the representation of the gamegrid
509 ret += QUOTE + "bg" + QUOTE + COLON + QUOTE + bgstr + QUOTE + COMMA;
510 ret += QUOTE + "fg" + QUOTE + COLON + QUOTE + fgstr + QUOTE + COMMA;
511 ret += QUOTE + "symbols" + QUOTE + COLON + QUOTE + symbolstr + QUOTE;
512
513 return ret;
514 }
515
516 public:
522 virtual const string getDataStructureRepresentation () const override {
523 std::string json_str;
524 // specify the encoding
525 json_str = QUOTE + "encoding" + QUOTE + COLON + QUOTE + encoding + QUOTE + COMMA;
526
527 // specify the dimensions of the gamegrid
528 json_str += QUOTE + "dimensions" + QUOTE + COLON +
529 OPEN_BOX + std::to_string(gridSize[0]) + "," + std::to_string(gridSize[1]) + CLOSE_BOX + COMMA;
530
531 json_str += getRAWRepresentation();
532
533 return json_str + CLOSE_CURLY;
534 }
535
536 };
537 }
538} // end namespace bridges
539
540#endif
This is a class in BRIDGES for representing an (n x n) grid.
Definition: Grid.h:30
int gridSize[2]
Definition: Grid.h:61
This class represents a single cell of the Game Grid.
Definition: GameGrid.h:312
NamedSymbol getSymbol() const
Get cell symbol.
Definition: GameGrid.h:379
GameCell(NamedColor bg, NamedColor fg, NamedSymbol symbol)
Definition: GameGrid.h:329
GameCell()
constructor
Definition: GameGrid.h:319
void setFGColor(NamedColor fg)
Set foreground color using NamedColor Enum argument.
Definition: GameGrid.h:347
void setSymbol(NamedSymbol s)
Set symbol using int argument.
Definition: GameGrid.h:355
NamedColor getBGColor() const
Get background color of cell.
Definition: GameGrid.h:363
void setBGColor(NamedColor bg)
Set background color using NamedColor Enum argument.
Definition: GameGrid.h:339
NamedColor getFGColor() const
Get foreground color of cell.
Definition: GameGrid.h:371
This is a class in BRIDGES for representing an (n x n)game grid.
Definition: GameGrid.h:398
GameGrid(int nbrow=10, int nbcol=10)
Definition: GameGrid.h:475
virtual const string getDStype() const override
Return the data structure type.
Definition: GameGrid.h:467
void setBGColor(int row, int col, NamedColor color)
Definition: GameGrid.h:419
void drawSymbol(int row, int col, NamedSymbol symbol, NamedColor color)
Definition: GameGrid.h:461
GameGrid(const GameGrid &)=delete
NamedColor getFGColor(int row, int col) const
Definition: GameGrid.h:427
virtual ~GameGrid()=default
NamedSymbol getSymbol(int row, int col) const
Definition: GameGrid.h:431
void setSymbol(int row, int col, NamedSymbol symbol)
Definition: GameGrid.h:451
void setFGColor(int row, int col, NamedColor color)
Definition: GameGrid.h:441
NamedColor getBGColor(int row, int col) const
Definition: GameGrid.h:423
virtual const string getDataStructureRepresentation() const override
Definition: GameGrid.h:522
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
string encode(BYTE const *buf, unsigned int bufLen)
Definition: base64.h:56
NamedSymbol
Definition: GameGrid.h:164
NamedColor
Definition: GameGrid.h:14
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:52
const string OPEN_BOX
Definition: DataStructure.h:55
const string COMMA
Definition: DataStructure.h:51
const string CLOSE_BOX
Definition: DataStructure.h:56
const string CLOSE_CURLY
Definition: DataStructure.h:54
const string QUOTE
Definition: DataStructure.h:50