Raylib Bubbles
C++11 Raylib bubble shooter game.
board.hpp
1 #ifndef BOARD_HPP_
2 #define BOARD_HPP_
3 
4 #include <forward_list>
5 #include <vector>
6 #include <unordered_set>
7 #include <array>
8 #include <stdexcept>
9 #include <algorithm>
10 
11 #include "util.hpp"
12 
47 class GameBoard {
48 public:
61  struct BubbleCell {
62  size_t hue;
63  size_t neighbors;
64 
73  BubbleCell(size_t hue = 0, size_t neighbors = 0)
74  : hue(hue), neighbors(neighbors) {}
75 
78 
79  bool operator==(const BubbleCell& other) const {
80  return hue == other.hue;
81  }
82 
83  bool operator!=(const BubbleCell& other) const {
84  return hue != other.hue;
85  }
86 
87  void operator=(const size_t hue) {
88  this->hue = hue;
89  }
90 
92  };
93 
107 
113  struct BubbleEvent {
114 
115  const size_t row;
116  const size_t col;
117  const size_t hue;
118 
125  BubbleEvent(size_t row, size_t col, size_t hue)
126  : row(row), col(col), hue(hue) {}
127  };
128 
133  enum class EventType {
134  ATTACH,
135  POP,
136  DROP
137  };
138 
139  std::vector<BubbleEvent> events;
140  const EventType type;
141 
147  BubbleEventVector(const std::vector<BubbleEvent>& events, const EventType type)
148  : events(events), type(type) {}
149 
150  BubbleEventVector(std::vector<BubbleEvent>&& events, const EventType type)
151  : events(std::move(events)), type(type) {}
152  };
153 
165  GameBoard(const size_t rows, const size_t cols)
166  : rows(rows),
167  cols(cols),
168  MATCHES_TO_POP(GameUtils::getCfgSizeT("*.Board", "MATCHES_TO_POP")),
169  board(getGridSize(rows)) {};
170 
173 
178  size_t getRows() const;
179 
185  size_t getCols() const;
186 
200  size_t getColAlign(const size_t row) const;
201 
210  size_t getGridSize(const size_t nRows) const;
211 
213 
216 
225  size_t get(const size_t row, const size_t col) const;
226 
235  size_t getNbrs(const size_t row, const size_t col) const;
236 
253  void set(const size_t row, const size_t col, const size_t hue);
254 
256 
259 
264  size_t count() const;
265 
273  bool oob(const size_t row, const size_t col) const;
274 
276 
279 
288  bool canAttach(const size_t row, const size_t col) const;
289 
304  bool attach(const size_t row, const size_t col, const size_t hue);
305 
324  bool pop(const size_t row, const size_t col, const size_t matches = 0);
325 
333  void dropFloating();
334 
339  bool reachedBottom() const;
340 
361  bool update(const size_t row, const size_t col, const size_t hue, const size_t matches = 0);
362 
367  void fill(const size_t hue);
368 
370 
373 
378  bool hasEvents() const;
379 
386  BubbleEventVector popEventVec();
387 
389 
390 private:
391  const size_t rows;
392  const size_t cols;
393  const size_t MATCHES_TO_POP;
394 
395  std::vector<BubbleCell> board;
396 
397  std::forward_list<BubbleEventVector> boardEvents;
398 
399  size_t at(const size_t row, const size_t col) const;
400  void applyNbrs(std::vector<BubbleCell>& b, const size_t row, const size_t col);
401 
402  std::array<std::pair<int, int>, 6> getOffsets(const size_t row) const;
403 };
404 
405 #endif
Represents the game board and provides public methods to interact with it.
Definition: board.hpp:47
size_t getGridSize(const size_t nRows) const
Returns the total number of cells of the game board.
Definition: board.cpp:17
size_t getColAlign(const size_t row) const
Returns the number of columns in the game board on the requested row.
Definition: board.cpp:13
bool pop(const size_t row, const size_t col, const size_t matches=0)
Pops same-colored bubbles starting from the specified cell on the board.
Definition: board.cpp:65
bool update(const size_t row, const size_t col, const size_t hue, const size_t matches=0)
Applies all transformations for a game loop iteration, while allowing for a new bubble to be attached...
Definition: board.cpp:149
void set(const size_t row, const size_t col, const size_t hue)
Sets the hue of a bubble at the specified location on the board.
Definition: board.cpp:29
size_t getCols() const
Getter for the maximum number of columns in the game board (column amount alternates on row index).
Definition: board.cpp:9
BubbleEventVector popEventVec()
Pops and returns the first event vector from the board event list.
Definition: board.cpp:173
size_t getNbrs(const size_t row, const size_t col) const
Getter for the number of neighbors of a bubble on the game board.
Definition: board.cpp:25
size_t count() const
Returns the number of non-empty board cells.
Definition: board.cpp:38
size_t getRows() const
Getter for the number of rows in the game board.
Definition: board.cpp:5
void dropFloating()
Drops floating bubbles on the board.
Definition: board.cpp:104
void fill(const size_t hue)
Fills the game board data structure.
Definition: board.cpp:159
bool canAttach(const size_t row, const size_t col) const
Checks if a bubble can be attached to a specific location on the board.
Definition: board.cpp:48
bool reachedBottom() const
Checks if the board has a bubble in its lowest row.
Definition: board.cpp:141
size_t get(const size_t row, const size_t col) const
Getter for the hue of a bubble on the game board.
Definition: board.cpp:21
GameBoard(const size_t rows, const size_t cols)
Constructor for the GameBoard class.
Definition: board.hpp:165
bool attach(const size_t row, const size_t col, const size_t hue)
Attaches a bubble to a specific location on the board.
Definition: board.cpp:52
bool hasEvents() const
Checks if the board has any event vectors that other modules might want to process.
Definition: board.cpp:169
bool oob(const size_t row, const size_t col) const
Checks if a specific row and column are out of bounds.
Definition: board.cpp:44
Provides utility functions for the game.
Definition: util.hpp:26
Represents a bubble cell on the game board.
Definition: board.hpp:61
BubbleCell(size_t hue=0, size_t neighbors=0)
Default constructor for the BubbleCell struct.
Definition: board.hpp:73
Wraps the row and column of a bubble on the board with its hue.
Definition: board.hpp:113
BubbleEvent(size_t row, size_t col, size_t hue)
Constructs a BubbleEvent instance with the given hue, row, and column.
Definition: board.hpp:125
Holds a vector of bubble events and the type of event that occurred.
Definition: board.hpp:106
EventType
Determines the type of event that occurred with the bubble.
Definition: board.hpp:133
BubbleEventVector(const std::vector< BubbleEvent > &events, const EventType type)
Constructs a BubbleEventVector instance with the given events and type.
Definition: board.hpp:147