Game Development Reference
In-Depth Information
void Swap(int m0, int n0, int m1, int n1)
{
auto tmp = std::move(m_items[m0][n0]);
m_items[m0][n0] = std::move(m_items[m1][n1]);
m_items[m1][n1] = std::move(tmp);
}
Of course, this means we have to be very careful when constructing the grid. We do not
keep any objects we add into the grid, these will be left in an indeterminate state by the
move operation.
Having defined the representation of a slot of a grid, we should revisit the grid class de-
claration.
template <int m, int n, typename T>
class grid
{
public:
T& operator ()(int i, int j) { return m_items[i][j]; }
int Rows() const { return m; }
int Columns() const { return n; }
protected:
slot_item m_items[m][n];
};
Notice how by using the template parameters m and n , we declare a static two dimensional
array of slot_item objects and thus avoided the need to dynamically allocate memory to
hold the items. Furthermore, we can create some preset grid types through the typedef
keyword, this simplifies the syntax when we create grid objects later on.
typedef ui::grid_game<4,4,ui::game_item> grid4x4;
We have two more core grid operations left to perform, we need the ability to add items
into the grid, and to remove them.
Adding an item into the grid may mean different things, first, if the slot is empty, adding is
straightforward.
Next, if the slot is not empty and the item within it is of the same type we are adding. In
this case, we will increment the count of the item within that slot, if there is not enough
space, we will add as many as we can and return the remainder count to the user. If there is
not enough space available, we will just return false to indicate that the add operation did
not succeed.
Search WWH ::




Custom Search