Bridges-C++  3.1.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 {
10  class GameBase {
11  private:
13 
14  GameGrid gg;
15 
16  bool firsttime = true;
17 
18 
19  std::unique_ptr<SocketConnection> sockcon;
20 
21  bool bquit = false;
22 
23  protected:
24  bool debug = false;
25 
26 
30  GameBase(int assignmentID, std::string username, std::string apikey, int nbRow = 10, int nbColumn = 10)
31  : bridges(assignmentID, username, apikey), gg(nbRow, nbColumn) {
32  bridges.setServer("games");
33 
34  sockcon = std::make_unique<SocketConnection>(bridges);
35 
36  if (debug)
37  std::cerr << "nbRow: " << nbRow << " nbCol: " << nbColumn << std::endl;
38  }
39 
44  virtual void initialize () = 0;
45 
50  virtual void gameLoop () = 0;
51 
52 
53  protected:
62  sockcon->registerKeyListener(p);
63  }
64 
69  void render() {
70  if (firsttime) {
71  bridges.setVisualizeJSONFlag(debug);
72 
73  bridges.setDataStructure(&gg);
74 
75  bridges.visualize();
76 
77  firsttime = false;
78 
79  }
80 
81  sockcon->sendGameGrid(gg);
82  }
83 
84  protected:
85 
86  bool gameover() const {
87  return bquit;
88  }
89 
94  void quit() {
95  bquit = true;
96  }
97 
103  void setBGColor(int row, int col, NamedColor nc) {
104  gg.setBGColor(row, col, nc);
105  }
106 
113  void drawSymbol(int row, int col, NamedSymbol symb, NamedColor nc) {
114  gg.drawSymbol(row, col, symb, nc);
115  }
116 
120  void setTitle(std::string title) {
121  bridges.setTitle(title);
122  }
123 
127  void setDescription(std::string desc) {
128  bridges.setDescription(desc);
129  }
130 
135  NamedColor getBGColor(int row, int col) {
136  return gg[row][col].getBGColor();
137  }
138 
143  NamedSymbol getSymbol(int row, int col) {
144  return gg[row][col].getSymbol();
145  }
146 
151  NamedColor getSymbolColor(int row, int col) {
152  return gg[row][col].getFGColor();
153  }
154 
159  int const* size = gg.getDimensions();
160  return size[1];
161  }
162 
167  int const* size = gg.getDimensions();
168  return size[0];
169  }
170 
171  };
172  }
173 }
174 
175 #endif
void visualize()
Definition: Bridges.h:430
void registerKeyListener(KeypressListener *p)
register a new KeypressListener
Definition: GameBase.h:61
void setDescription(const string &descr)
Definition: Bridges.h:269
void render()
Renders the game.
Definition: GameBase.h:69
NamedSymbol getSymbol(int row, int col)
What object is in this cell?
Definition: GameBase.h:143
bool debug
Definition: GameBase.h:24
Definition: GameGrid.h:365
NamedColor getBGColor(int row, int col) const
Definition: GameGrid.h:391
void setTitle(const string &t)
Definition: Bridges.h:254
Definition: GameBase.h:10
void setServer(const string &server_type)
Definition: Bridges.h:335
NamedColor getBGColor(int row, int col)
What color is this cell?
Definition: GameBase.h:135
void drawSymbol(int row, int col, NamedSymbol symbol, NamedColor color)
Definition: GameGrid.h:431
void setDataStructure(DataStructure *ds)
Definition: Bridges.h:280
void quit()
calling this function causes the game to end.
Definition: GameBase.h:94
void setDescription(std::string desc)
Set a short description of the game.
Definition: GameBase.h:127
void drawSymbol(int row, int col, NamedSymbol symb, NamedColor nc)
Draw an object on the game.
Definition: GameBase.h:113
GameBase(int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbColumn=10)
Definition: GameBase.h:30
NamedColor getSymbolColor(int row, int col)
What color is object in this cell?
Definition: GameBase.h:151
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:103
NamedColor
Definition: GameGrid.h:21
int getBoardWidth()
How wide is the Game Board?
Definition: GameBase.h:158
bool gameover() const
Definition: GameBase.h:86
void setVisualizeJSONFlag(bool flag)
Definition: Bridges.h:149
NamedSymbol
Definition: GameGrid.h:171
NamedSymbol getSymbol(int row, int col) const
Definition: GameGrid.h:399
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:120
int getBoardHeight()
How tall is the Game Board?
Definition: GameBase.h:166
void setBGColor(int row, int col, NamedColor color)
Definition: GameGrid.h:387
int const * getDimensions()
Definition: Grid.h:131
NamedColor getFGColor(int row, int col) const
Definition: GameGrid.h:395
virtual void gameLoop()=0
This function is called once per frame of the game.
Definition: SocketConnection.h:20