Bridges-C++  3.4.1
Bridges(C++API)
GameBase.h
Go to the documentation of this file.
1 #ifndef GAME_BASE_H
2 #define GAME_BASE_H
3 
4 #include <SocketConnection.h>
5 
6 
7 
8 namespace bridges {
9  namespace game {
25  class GameBase {
26  private:
28 
29  GameGrid gg;
30 
31  bool firsttime = true;
32 
33 
34 
35  bool bquit = false;
36  std::unique_ptr<SocketConnection> sockcon;
37 
38  protected:
39  bool debug = false;
40 
41 
49  GameBase(int assignmentID, std::string username,
50  std::string apikey, int nbRow = 10, int nbColumn = 10)
51  : bridges(assignmentID, username, apikey), gg(nbRow,
52  nbColumn) {
53  bridges.setServer("games");
54 
55  sockcon = std::make_unique<SocketConnection>(bridges);
56 
57  if (debug)
58  std::cerr << "nbRow: " << nbRow << " nbCol: " <<
59  nbColumn << std::endl;
60  }
61 
62  virtual ~GameBase() =default;
63 
68  virtual void initialize () = 0;
69 
74  virtual void gameLoop () = 0;
75 
76 
77  protected:
86  sockcon->registerKeyListener(p);
87  }
88 
93  void render() {
94  if (firsttime) {
95  bridges.setJSONFlag(debug);
96 
97  bridges.setDataStructure(&gg);
98 
99  bridges.visualize();
100 
101  firsttime = false;
102 
103  }
104 
105  sockcon->sendGameGrid(gg);
106  }
107 
108  protected:
109 
110  bool gameover() const {
111  return bquit;
112  }
113 
118  void quit() {
119  bquit = true;
120  }
121 
127  void setBGColor(int row, int col, NamedColor nc) {
128  gg.setBGColor(row, col, nc);
129  }
130 
137  void drawSymbol(int row, int col, NamedSymbol symb,
138  NamedColor nc) {
139  gg.drawSymbol(row, col, symb, nc);
140  }
141 
145  void setTitle(std::string title) {
146  bridges.setTitle(title);
147  }
148 
152  void setDescription(std::string desc) {
153  bridges.setDescription(desc);
154  }
155 
160  NamedColor getBGColor(int row, int col) {
161  return gg[row][col].getBGColor();
162  }
163 
168  NamedSymbol getSymbol(int row, int col) {
169  return gg[row][col].getSymbol();
170  }
171 
176  NamedColor getSymbolColor(int row, int col) {
177  return gg[row][col].getFGColor();
178  }
179 
184  int const* size = gg.getDimensions();
185  return size[1];
186  }
187 
192  int const* size = gg.getDimensions();
193  return size[0];
194  }
195 
196  };
197  }
198 }
199 
200 #endif
void registerKeyListener(KeypressListener *p)
register a new KeypressListener
Definition: GameBase.h:85
void render()
Renders the game.
Definition: GameBase.h:93
NamedSymbol getSymbol(int row, int col)
What object is in this cell?
Definition: GameBase.h:168
bool debug
Definition: GameBase.h:39
This is a class in BRIDGES for representing an (n x n)game grid.
Definition: GameGrid.h:400
NamedColor getBGColor(int row, int col) const
Definition: GameGrid.h:426
The base class building and using the BRIDGES based games.
Definition: GameBase.h:25
NamedColor getBGColor(int row, int col)
What color is this cell?
Definition: GameBase.h:160
void drawSymbol(int row, int col, NamedSymbol symbol, NamedColor color)
Definition: GameGrid.h:466
void quit()
calling this function causes the game to end.
Definition: GameBase.h:118
void setDescription(std::string desc)
Set a short description of the game.
Definition: GameBase.h:152
void drawSymbol(int row, int col, NamedSymbol symb, NamedColor nc)
Draw an object on the game.
Definition: GameBase.h:137
GameBase(int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbColumn=10)
Protected constructed prevens direct creation.
Definition: GameBase.h:49
NamedColor getSymbolColor(int row, int col)
What color is object in this cell?
Definition: GameBase.h:176
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:42
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
void setBGColor(int row, int col, NamedColor nc)
Change the background color of a cell.
Definition: GameBase.h:127
NamedColor
Definition: GameGrid.h:14
int getBoardWidth()
How wide is the Game Board?
Definition: GameBase.h:183
bool gameover() const
Definition: GameBase.h:110
NamedSymbol
Definition: GameGrid.h:164
NamedSymbol getSymbol(int row, int col) const
Definition: GameGrid.h:434
virtual void initialize()=0
This function is called once when the game starts.
virtual ~GameBase()=default
void setTitle(std::string title)
Set the title of the game.
Definition: GameBase.h:145
int getBoardHeight()
How tall is the Game Board?
Definition: GameBase.h:191
void setBGColor(int row, int col, NamedColor color)
Definition: GameGrid.h:422
int const * getDimensions()
Get dimenions of the grid.
Definition: Grid.h:168
NamedColor getFGColor(int row, int col) const
Definition: GameGrid.h:430
virtual void gameLoop()=0
This function is called once per frame of the game.
This is meant to be an internal class, not something that the library user will use....
Definition: SocketConnection.h:34