I just popped this open and had a look at the code. Very nice use of flattened arrays and bitfields
@Smart Kitty.
The row major board array is an especially nice touch.
For anyone interested, what she's done is an old school technique called
array flattening. It's when you have a multi-dimensional data set - in this case the two-dimensional board, and you use math to flatten it into a single array of integer values. Each slot represents one cell, and the value stored in that slot tells the game what occupies it.
Both row and column numbers start at zero. For a board 10 cells wide, finding the contents of row 4, column 3 looks like this:
C:
BW = 10; // Board width.
x = 3; // Column.
y = 4; // Row.
index = y * BW + x; // 4 * 10 + 3 = 43.
Multiplying y by the board width skips over four complete rows of 10 cells, putting us at index 40. Adding x moves three cells into that row, giving us index 43.
Here is how row 4 maps to positions in the array:
| Column (x) | 0 | 1 | 2 | 3 | 4 | … | 9 |
|---|
| Array index | 40 | 41 | 42 | 43 | 44 | … | 49 |
The game can then retrieve that cell's contents:
C:
value = get(board, index);
In this code,
-1 means the cell is empty. Occupied cells contain a value identifying the block.
So the entire board lives in one indexed array, while simple arithmetic lets the game address it as a grid. It's very compact - 2.6 KiB in memory - and
very fast - as fast as you're going to get for a data set like this. As I mentioned to
@O Ilusionista, you can run thousands of these per frame and the engine won't even notice. Very nice work indeed
@Smart Kitty!
DC