Bridges-C++  3.1.1
Bridges(C++API)
GameGrid.h
Go to the documentation of this file.
1 
2 #include <string>
3 using 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 
18 namespace bridges {
19  namespace game {
20 
21  enum class NamedColor : unsigned char {
22  aliceblue,
24  aqua,
25  aquamarine,
26  azure,
27  beige,
28  bisque,
29  black,
31  blue,
32  blueviolet,
33  brown,
34  burlywood,
35  cadetblue,
36  chartreuse,
37  chocolate,
38  coral,
40  cornsilk,
41  crimson,
42  cyan,
43  darkblue,
44  darkcyan,
46  darkgray,
47  darkgreen,
48  darkgrey,
49  darkkhaki,
52  darkorange,
53  darkorchid,
54  darkred,
55  darksalmon,
61  darkviolet,
62  deeppink,
64  dimgray,
65  dimgrey,
66  dodgerblue,
67  firebrick,
70  fuchsia,
71  gainsboro,
72  ghostwhite,
73  gold,
74  goldenrod,
75  gray,
76  grey,
77  green,
79  honeydew,
80  hotpink,
81  indianred,
82  indigo,
83  ivory,
84  khaki,
85  lavender,
87  lawngreen,
89  lightblue,
90  lightcoral,
91  lightcyan,
93  lightgray,
94  lightgreen,
95  lightgrey,
96  lightpink,
103  lightyellow,
104  lime,
105  limegreen,
106  linen,
107  magenta,
108  maroon,
110  mediumblue,
111  mediumorchid,
112  mediumpurple,
118  midnightblue,
119  mintcream,
120  mistyrose,
121  moccasin,
122  navajowhite,
123  navy,
124  oldlace,
125  olive,
126  olivedrab,
127  orange,
128  orangered,
129  orchid,
131  palegreen,
134  papayawhip,
135  peachpuff,
136  peru,
137  pink,
138  plum,
139  powderblue,
140  purple,
141  red,
142  rosybrown,
143  royalblue,
144  saddlebrown,
145  salmon,
146  sandybrown,
147  seagreen,
148  seashell,
149  sienna,
150  silver,
151  skyblue,
152  slateblue,
153  slategray,
154  slategrey,
155  snow,
156  springgreen,
157  steelblue,
158  tan,
159  teal,
160  thistle,
161  tomato,
162  turquoise,
163  violet,
164  wheat,
165  white,
166  whitesmoke,
167  yellow,
169  };
170 
171  enum class NamedSymbol : unsigned char {
172  none,
173  A,
174  B,
175  C,
176  D,
177  E,
178  F,
179  G,
180  H,
181  I,
182  J,
183  K,
184  L,
185  M,
186  N,
187  O,
188  P,
189  Q,
190  R,
191  S,
192  T,
193  U,
194  V,
195  W,
196  X,
197  Y,
198  Z,
199  a,
200  b,
201  c,
202  d,
203  e,
204  f,
205  g,
206  h,
207  i,
208  j,
209  k,
210  l,
211  m,
212  n,
213  o,
214  p,
215  q,
216  r,
217  s,
218  t,
219  u,
220  v,
221  w,
222  x,
223  y,
224  z,
225  zero,
226  one,
227  two,
228  three,
229  four,
230  five,
231  six,
232  seven,
233  eight,
234  nine,
235  empty0,
236  circle,
237  square,
238  diamond,
240  triangle_up,
243  man,
244  woman,
245  cat,
246  pawn,
247  knight,
248  bishop,
249  rook,
250  queen,
251  king,
252  arrow_right,
253  arrow_left,
254  arrow_up,
255  arrow_down,
256  star,
257  droplet,
258  heart,
259  lightning,
261  speechbubble,
262  flower,
264  circle_x,
265  circle_x2,
267  empty1,
268  sword,
269  horn_helmet,
270  astro_helmet,
271  target,
272  empty2,
273  waves,
274  rain,
275  drink,
276  paperclip,
277  elephant,
278  cowboyhat,
279  ballcap,
280  flag,
281  bomb,
282  bear,
283  giraffe,
284  bug1,
285  bug2,
286  bug3,
288  fishes,
289  duck,
290  sloth,
291  bird,
292  apple,
293  carrot,
294  lemon,
295  pepper,
296  onion,
297  potion1,
298  potion2,
300  campfire,
301  donut,
302  monitor
303  };
304 
305 
306  class GameCell {
307  NamedColor bg;
308  NamedColor fg;
309  NamedSymbol symbol;
310 
311  public:
313  bg = NamedColor::black;
314  fg = NamedColor::white;
315  symbol = NamedSymbol::none;
316  }
317 
319  this->bg = bg;
320  this->fg = fg;
321  this->symbol = symbol;
322  }
323 
329  this->bg = bg;
330  }
331 
337  this->fg = fg;
338  }
339 
345  this->symbol = s;
346  }
347 
349  return bg;
350  }
351 
353  return fg;
354  }
355 
357  return symbol;
358  }
359 
360  };
361 
362 
363 
364  //TODO: implement RLE encoding
365  class GameGrid : public Grid<GameCell> {
366  private:
367  std::string encoding = "raw";
368 
369  void initializeGrid() {
370  for (int i = 0; i < gridSize[0]; i++) {
371  for (int j = 0; j < gridSize[1]; j++) {
372  setBGColor(i, j, NamedColor::black);
373  setFGColor(i, j, NamedColor::black);
374  setSymbol(i, j, NamedSymbol::none);
375  }
376  }
377  }
378  public:
379 
380 
387  void setBGColor(int row, int col, NamedColor color) {
388  (*this)[row][col].setBGColor(color);
389  }
390 
391  NamedColor getBGColor(int row, int col) const {
392  return (*this)[row][col].getBGColor();
393  }
394 
395  NamedColor getFGColor(int row, int col) const {
396  return (*this)[row][col].getFGColor();
397  }
398 
399  NamedSymbol getSymbol(int row, int col) const {
400  return (*this)[row][col].getSymbol();
401  }
402 
403 
410  void setFGColor(int row, int col, NamedColor color) {
411  (*this)[row][col].setFGColor(color);
412  }
413 
420  void setSymbol(int row, int col, NamedSymbol symbol) {
421  (*this)[row][col].setSymbol(symbol);
422  }
423 
424 
431  void drawSymbol(int row, int col,
432  NamedSymbol symbol, NamedColor color) {
433  setFGColor(row, col, color);
434  setSymbol(row, col, symbol);
435  }
436 
437 
438  virtual const string getDStype() const override {
439  return "GameGrid";
440  }
441 
446  GameGrid (int nbrow = 10, int nbcol = 10) : Grid<GameCell> (nbrow, nbcol) {
447  initializeGrid();
448  }
449 
450 
451  private:
452 
453  //public:
454 
455  string getRAWRepresentation() const {
457 
458  std::vector<unsigned char> bgbuf;
459  std::vector<unsigned char> fgbuf;
460  std::vector<unsigned char> symbolbuf;
461 
462  for (int i = 0; i < gridSize[0]; i++) {
463  for (int j = 0; j < gridSize[1]; j++) {
464  bgbuf.push_back(static_cast<unsigned char>(getBGColor(i, j)));
465  fgbuf.push_back(static_cast<unsigned char>(getFGColor(i, j)));
466  symbolbuf.push_back(static_cast<unsigned char>(getSymbol(i, j)));
467  }
468  }
469 
470  std::string bgstr = base64::encode (&(bgbuf[0]), bgbuf.size());
471  std::string fgstr = base64::encode (&(fgbuf[0]), fgbuf.size());
472  std::string symbolstr = base64::encode (&(symbolbuf[0]), symbolbuf.size());
473 
474 
475  std::string ret;
476  // Add the representation of the gamegrid
477  ret += QUOTE + "bg" + QUOTE + COLON + QUOTE + bgstr + QUOTE + COMMA;
478  ret += QUOTE + "fg" + QUOTE + COLON + QUOTE + fgstr + QUOTE + COMMA;
479  ret += QUOTE + "symbols" + QUOTE + COLON + QUOTE + symbolstr + QUOTE;
480 
481  return ret;
482  }
483 
484  public:
490  virtual const string getDataStructureRepresentation () const override {
491  std::string json_str;
492  // specify the encoding
493  json_str = QUOTE + "encoding" + QUOTE + COLON + QUOTE + encoding + QUOTE + COMMA;
494 
495  // specify the dimensions of the gamegrid
496  json_str += QUOTE + "dimensions" + QUOTE + COLON +
497  OPEN_BOX + std::to_string(gridSize[0]) + "," + std::to_string(gridSize[1]) + CLOSE_BOX + COMMA;
498 
499 
500  json_str += getRAWRepresentation();
501 
502  return json_str + CLOSE_CURLY;
503  }
504 
505 
506  };
507  }
508 } // end namespace bridges
509 
510 #endif
virtual const string getDStype() const override
Definition: GameGrid.h:438
void setSymbol(int row, int col, NamedSymbol symbol)
Definition: GameGrid.h:420
Definition: GameGrid.h:365
void setFGColor(NamedColor fg)
Definition: GameGrid.h:336
void setBGColor(NamedColor bg)
Definition: GameGrid.h:328
NamedColor getBGColor(int row, int col) const
Definition: GameGrid.h:391
string encode(BYTE const *buf, unsigned int bufLen)
Definition: base64.h:59
const string COLON
Definition: DataStructure.h:51
STL namespace.
void drawSymbol(int row, int col, NamedSymbol symbol, NamedColor color)
Definition: GameGrid.h:431
const string OPEN_BOX
Definition: DataStructure.h:54
const string CLOSE_CURLY
Definition: DataStructure.h:53
virtual const string getDataStructureRepresentation() const override
Definition: GameGrid.h:490
This is a class in BRIDGES for representing an (n x n) grid.
Definition: Grid.h:31
void setFGColor(int row, int col, NamedColor color)
Definition: GameGrid.h:410
NamedSymbol getSymbol() const
Definition: GameGrid.h:356
NamedColor getFGColor() const
Definition: GameGrid.h:352
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
GameCell()
Definition: GameGrid.h:312
const string CLOSE_BOX
Definition: DataStructure.h:55
NamedColor
Definition: GameGrid.h:21
void setSymbol(NamedSymbol s)
Definition: GameGrid.h:344
NamedSymbol
Definition: GameGrid.h:171
NamedSymbol getSymbol(int row, int col) const
Definition: GameGrid.h:399
NamedColor getBGColor() const
Definition: GameGrid.h:348
Definition: GameGrid.h:306
GameCell(NamedColor bg, NamedColor fg, NamedSymbol symbol)
Definition: GameGrid.h:318
const string COMMA
Definition: DataStructure.h:50
void setBGColor(int row, int col, NamedColor color)
Definition: GameGrid.h:387
NamedColor getFGColor(int row, int col) const
Definition: GameGrid.h:395
const string QUOTE
Definition: DataStructure.h:49
GameGrid(int nbrow=10, int nbcol=10)
Definition: GameGrid.h:446
std::string JSONencode(const T &d)
Definition: JSONutil.h:37