Bridges-C++  3.2.0
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  std::unique_ptr<SocketConnection> sockcon;
35 
36  bool bquit = false;
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 
66  virtual void initialize () = 0;
67 
72  virtual void gameLoop () = 0;
73 
74 
75  protected:
84  sockcon->registerKeyListener(p);
85  }
86 
91  void render() {
92  if (firsttime) {
93  bridges.setVisualizeJSONFlag(debug);
94 
95  bridges.setDataStructure(&gg);
96 
97  bridges.visualize();
98 
99  firsttime = false;
100 
101  }
102 
103  sockcon->sendGameGrid(gg);
104  }
105 
106  protected:
107 
108  bool gameover() const {
109  return bquit;
110  }
111 
116  void quit() {
117  bquit = true;
118  }
119 
125  void setBGColor(int row, int col, NamedColor nc) {
126  gg.setBGColor(row, col, nc);
127  }
128 
135  void drawSymbol(int row, int col, NamedSymbol symb,
136  NamedColor nc) {
137  gg.drawSymbol(row, col, symb, nc);
138  }
139 
143  void setTitle(std::string title) {
144  bridges.setTitle(title);
145  }
146 
150  void setDescription(std::string desc) {
151  bridges.setDescription(desc);
152  }
153 
158  NamedColor getBGColor(int row, int col) {
159  return gg[row][col].getBGColor();
160  }
161 
166  NamedSymbol getSymbol(int row, int col) {
167  return gg[row][col].getSymbol();
168  }
169 
174  NamedColor getSymbolColor(int row, int col) {
175  return gg[row][col].getFGColor();
176  }
177 
182  int const* size = gg.getDimensions();
183  return size[1];
184  }
185 
190  int const* size = gg.getDimensions();
191  return size[0];
192  }
193 
194  };
195  }
196 }
197 
198 #endif
void visualize()
Definition: Bridges.h:430
void registerKeyListener(KeypressListener *p)
register a new KeypressListener
Definition: GameBase.h:83
void setDescription(const string &descr)
Definition: Bridges.h:269
void render()
Renders the game.
Definition: GameBase.h:91
NamedSymbol getSymbol(int row, int col)
What object is in this cell?
Definition: GameBase.h:166
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
void setTitle(const string &t)
Definition: Bridges.h:254
The base class building and using the BRIDGES based games.
Definition: GameBase.h:25
void setServer(const string &server_type)
Definition: Bridges.h:335
NamedColor getBGColor(int row, int col)
What color is this cell?
Definition: GameBase.h:158
void drawSymbol(int row, int col, NamedSymbol symbol, NamedColor color)
Definition: GameGrid.h:466
void setDataStructure(DataStructure *ds)
Definition: Bridges.h:280
void quit()
calling this function causes the game to end.
Definition: GameBase.h:116
void setDescription(std::string desc)
Set a short description of the game.
Definition: GameBase.h:150
void drawSymbol(int row, int col, NamedSymbol symb, NamedColor nc)
Draw an object on the game.
Definition: GameBase.h:135
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:174
This class contains methods to connect and transmit a user&#39;s data structure representation to the Bri...
Definition: Bridges.h:39
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:125
NamedColor
Definition: GameGrid.h:14
int getBoardWidth()
How wide is the Game Board?
Definition: GameBase.h:181
bool gameover() const
Definition: GameBase.h:108
void setVisualizeJSONFlag(bool flag)
Definition: Bridges.h:149
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.
void setTitle(std::string title)
Set the title of the game.
Definition: GameBase.h:143
int getBoardHeight()
How tall is the Game Board?
Definition: GameBase.h:189
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. Provides support for input handling for the Game API.
Definition: SocketConnection.h:23