Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17ajf by
Grid/Grid.h@41:91b0c73b9e02, 2019-05-09 (annotated)
- Committer:
- el17ajf
- Date:
- Thu May 09 12:10:29 2019 +0000
- Revision:
- 41:91b0c73b9e02
- Parent:
- 36:c854f1f51f43
Commented remaining parts, added unit tests
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17ajf | 18:24ce897024d0 | 1 | #ifndef GRID_H |
| el17ajf | 18:24ce897024d0 | 2 | #define GRID_H |
| el17ajf | 18:24ce897024d0 | 3 | |
| el17ajf | 4:aa433f9865a6 | 4 | #include "Tetromino.h" |
| el17ajf | 3:a1b058ff66c2 | 5 | |
| el17ajf | 36:c854f1f51f43 | 6 | /** |
| el17ajf | 36:c854f1f51f43 | 7 | * Grid class |
| el17ajf | 36:c854f1f51f43 | 8 | * @brief Abstracts away access to a height x width 2d array, used to store |
| el17ajf | 36:c854f1f51f43 | 9 | * the 'landed' tetrominos. |
| el17ajf | 36:c854f1f51f43 | 10 | */ |
| el17ajf | 3:a1b058ff66c2 | 11 | class Grid { |
| el17ajf | 3:a1b058ff66c2 | 12 | public: |
| el17ajf | 36:c854f1f51f43 | 13 | /** |
| el17ajf | 36:c854f1f51f43 | 14 | * @param tetromino A Tetromino to place on this grid |
| el17ajf | 36:c854f1f51f43 | 15 | * @returns A boolean value representing if the tetromino |
| el17ajf | 36:c854f1f51f43 | 16 | * could fit in the grid |
| el17ajf | 36:c854f1f51f43 | 17 | * @see Tetromino |
| el17ajf | 36:c854f1f51f43 | 18 | */ |
| el17ajf | 3:a1b058ff66c2 | 19 | bool isSpaceForTetromino(Tetromino tetromino); |
| el17ajf | 36:c854f1f51f43 | 20 | |
| el17ajf | 36:c854f1f51f43 | 21 | /** |
| el17ajf | 36:c854f1f51f43 | 22 | * @brief Commit a tetromino into the grid, setting values to true where |
| el17ajf | 36:c854f1f51f43 | 23 | * it overlaps the grid, updating the grid and clearing rows if needed |
| el17ajf | 36:c854f1f51f43 | 24 | * @param tetromino The tetromino to place |
| el17ajf | 36:c854f1f51f43 | 25 | * @returns The number of rows cleared by placing the tetromino on the grid |
| el17ajf | 36:c854f1f51f43 | 26 | * @see Tetromino |
| el17ajf | 36:c854f1f51f43 | 27 | */ |
| el17ajf | 19:370d83a8dc33 | 28 | int placeTetromino(Tetromino tetromino); |
| el17ajf | 36:c854f1f51f43 | 29 | |
| el17ajf | 36:c854f1f51f43 | 30 | /** |
| el17ajf | 36:c854f1f51f43 | 31 | * @brief Use the Graphics functions to draw a block for every value |
| el17ajf | 36:c854f1f51f43 | 32 | * in the grid |
| el17ajf | 36:c854f1f51f43 | 33 | * @see Graphics::Game::drawBlock |
| el17ajf | 36:c854f1f51f43 | 34 | */ |
| el17ajf | 4:aa433f9865a6 | 35 | void draw(); |
| el17ajf | 36:c854f1f51f43 | 36 | |
| el17ajf | 36:c854f1f51f43 | 37 | /** |
| el17ajf | 36:c854f1f51f43 | 38 | * @brief Create an empty grid |
| el17ajf | 36:c854f1f51f43 | 39 | */ |
| el17ajf | 8:5066ce13a430 | 40 | Grid(); |
| el17ajf | 36:c854f1f51f43 | 41 | |
| el17ajf | 36:c854f1f51f43 | 42 | /** |
| el17ajf | 36:c854f1f51f43 | 43 | * The amount of space for hidden blocks at the top of the screen |
| el17ajf | 36:c854f1f51f43 | 44 | */ |
| el17ajf | 29:d59fbe128d1f | 45 | const static int HIDDEN_HEIGHT = 4; |
| el17ajf | 36:c854f1f51f43 | 46 | |
| el17ajf | 36:c854f1f51f43 | 47 | /** |
| el17ajf | 36:c854f1f51f43 | 48 | * The full height of the grid in blocks, including the hidden area |
| el17ajf | 36:c854f1f51f43 | 49 | */ |
| el17ajf | 29:d59fbe128d1f | 50 | const static int GRID_HEIGHT = 24; |
| el17ajf | 36:c854f1f51f43 | 51 | |
| el17ajf | 36:c854f1f51f43 | 52 | /** |
| el17ajf | 36:c854f1f51f43 | 53 | * The width of the grid in blocks |
| el17ajf | 36:c854f1f51f43 | 54 | */ |
| el17ajf | 15:afeefa3ceb61 | 55 | const static int GRID_WIDTH = 10; |
| el17ajf | 7:2e37bad816cb | 56 | |
| el17ajf | 3:a1b058ff66c2 | 57 | private: |
| el17ajf | 41:91b0c73b9e02 | 58 | /** |
| el17ajf | 41:91b0c73b9e02 | 59 | * @brief Checks for full rows and moves the blocks down if required, |
| el17ajf | 41:91b0c73b9e02 | 60 | * "clearing" rows on the grid. |
| el17ajf | 41:91b0c73b9e02 | 61 | * @returns The number of lines that have been cleared |
| el17ajf | 41:91b0c73b9e02 | 62 | */ |
| el17ajf | 19:370d83a8dc33 | 63 | int checkForLines(); |
| el17ajf | 41:91b0c73b9e02 | 64 | |
| el17ajf | 41:91b0c73b9e02 | 65 | /** |
| el17ajf | 41:91b0c73b9e02 | 66 | * @brief checks all rows and shifts them down if required |
| el17ajf | 41:91b0c73b9e02 | 67 | */ |
| el17ajf | 4:aa433f9865a6 | 68 | void moveDownIfNeeded(); |
| el17ajf | 41:91b0c73b9e02 | 69 | |
| el17ajf | 41:91b0c73b9e02 | 70 | /** |
| el17ajf | 41:91b0c73b9e02 | 71 | * @brief Moves the rows from row to row + 1, 'shifting' them down. |
| el17ajf | 41:91b0c73b9e02 | 72 | * @param row The row to start shifting down from. All rows below this |
| el17ajf | 41:91b0c73b9e02 | 73 | * will be translated down. |
| el17ajf | 41:91b0c73b9e02 | 74 | */ |
| el17ajf | 4:aa433f9865a6 | 75 | void shiftDownFrom(int row); |
| el17ajf | 41:91b0c73b9e02 | 76 | |
| el17ajf | 41:91b0c73b9e02 | 77 | /** |
| el17ajf | 41:91b0c73b9e02 | 78 | * The grid as an array of booleans, true where there's a block |
| el17ajf | 41:91b0c73b9e02 | 79 | * in the space, false if it's empty |
| el17ajf | 41:91b0c73b9e02 | 80 | */ |
| el17ajf | 29:d59fbe128d1f | 81 | bool grid[GRID_WIDTH][GRID_HEIGHT]; |
| el17ajf | 41:91b0c73b9e02 | 82 | |
| el17ajf | 41:91b0c73b9e02 | 83 | /** |
| el17ajf | 41:91b0c73b9e02 | 84 | * @brief Checks if a block fits on the grid. |
| el17ajf | 41:91b0c73b9e02 | 85 | * @param block The block to check |
| el17ajf | 41:91b0c73b9e02 | 86 | * @returns True if the given block can fit in the grid, else false |
| el17ajf | 41:91b0c73b9e02 | 87 | */ |
| el17ajf | 4:aa433f9865a6 | 88 | bool isSpaceForBlock(Block block); |
| el17ajf | 18:24ce897024d0 | 89 | }; |
| el17ajf | 18:24ce897024d0 | 90 | #endif |
