Bridges-C++  3.4.5-dev1-6-g935685a
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 namespace bridges {
7  namespace game {
23  class GameBase {
24  private:
26 
27  GameGrid gg;
28 
29  bool firsttime = true;
30 
31  bool bquit = false;
32  std::unique_ptr<SocketConnection> sockcon;
33 
34  protected:
35  bool debug = false;
36 
44  GameBase(int assignmentID, std::string username,
45  std::string apikey, int nbRow = 10, int nbColumn = 10)
46  : bridges(assignmentID, username, apikey), gg(nbRow,
47  nbColumn) {
48  bridges.setServer("games");
49 
50  sockcon = std::make_unique<SocketConnection>(bridges);
51 
52  if (debug)
53  std::cerr << "nbRow: " << nbRow << " nbCol: " <<
54  nbColumn << std::endl;
55  }
56 
57  virtual ~GameBase() = default;
58 
63  virtual void initialize () = 0;
64 
69  virtual void gameLoop () = 0;
70 
71  protected:
80  sockcon->registerKeyListener(p);
81  }
82 
87  void render() {
88  if (firsttime) {
89  bridges.setJSONFlag(debug);
90 
91  bridges.setDataStructure(&gg);
92 
93  bridges.visualize();
94 
95  firsttime = false;
96 
97  }
98 
99  sockcon->sendDataToServer(gg);
100  }
101 
102  protected:
103 
104  bool gameover() const {
105  return bquit;
106  }
107 
112  void quit() {
113  bquit = true;
114  }
115 
121  void setBGColor(int row, int col, NamedColor nc) {
122  gg.setBGColor(row, col, nc);
123  }
124 
131  void drawSymbol(int row, int col, NamedSymbol symb,
132  NamedColor nc) {
133  gg.drawSymbol(row, col, symb, nc);
134  }
135 
139  void setTitle(std::string title) {
140  bridges.setTitle(title);
141  }
142 
146  void setDescription(std::string desc) {
147  bridges.setDescription(desc);
148  }
149 
154  NamedColor getBGColor(int row, int col) {
155  return gg[row][col].getBGColor();
156  }
157 
162  NamedSymbol getSymbol(int row, int col) {
163  return gg[row][col].getSymbol();
164  }
165 
170  NamedColor getSymbolColor(int row, int col) {
171  return gg[row][col].getFGColor();
172  }
173 
178  int const* size = gg.getDimensions();
179  return size[1];
180  }
181 
186  int const* size = gg.getDimensions();
187  return size[0];
188  }
189 
190  };
191  }
192 }
193 
194 #endif
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:50
int const * getDimensions()
Get dimenions of the grid.
Definition: Grid.h:164
The base class building and using the BRIDGES based games.
Definition: GameBase.h:23
NamedSymbol getSymbol(int row, int col)
What object is in this cell?
Definition: GameBase.h:162
void setTitle(std::string title)
Set the title of the game.
Definition: GameBase.h:139
int getBoardHeight()
How tall is the Game Board?
Definition: GameBase.h:185
virtual void gameLoop()=0
This function is called once per frame of the game.
NamedColor getSymbolColor(int row, int col)
What color is object in this cell?
Definition: GameBase.h:170
void drawSymbol(int row, int col, NamedSymbol symb, NamedColor nc)
Draw an object on the game.
Definition: GameBase.h:131
void quit()
calling this function causes the game to end.
Definition: GameBase.h:112
NamedColor getBGColor(int row, int col)
What color is this cell?
Definition: GameBase.h:154
void registerKeyListener(KeypressListener *p)
register a new KeypressListener
Definition: GameBase.h:79
virtual void initialize()=0
This function is called once when the game starts.
virtual ~GameBase()=default
void setDescription(std::string desc)
Set a short description of the game.
Definition: GameBase.h:146
void setBGColor(int row, int col, NamedColor nc)
Change the background color of a cell.
Definition: GameBase.h:121
GameBase(int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbColumn=10)
Protected constructed prevens direct creation.
Definition: GameBase.h:44
void render()
Renders the game.
Definition: GameBase.h:87
bool debug
Definition: GameBase.h:35
int getBoardWidth()
How wide is the Game Board?
Definition: GameBase.h:177
bool gameover() const
Definition: GameBase.h:104
This is a class in BRIDGES for representing an (n x n)game grid.
Definition: GameGrid.h:398
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
NamedColor getFGColor(int row, int col) const
Definition: GameGrid.h:427
NamedSymbol getSymbol(int row, int col) const
Definition: GameGrid.h:431
NamedColor getBGColor(int row, int col) const
Definition: GameGrid.h:423
This is meant to be an internal class, not something that the library user will use....
Definition: SocketConnection.h:34
NamedSymbol
Definition: GameGrid.h:164
NamedColor
Definition: GameGrid.h:14
Support for drawing Bar charts.
Definition: alltypes.h:4