Tetris Base Game
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Board.cpp@0:1858f2b100fd, 20 months ago (annotated)
- Committer:
- jsanchez307
- Date:
- Wed Nov 30 21:57:25 2022 +0000
- Revision:
- 0:1858f2b100fd
Tetris Skeleton V 1.0
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jsanchez307 | 0:1858f2b100fd | 1 | #include "Board.h" |
| jsanchez307 | 0:1858f2b100fd | 2 | |
| jsanchez307 | 0:1858f2b100fd | 3 | /* |
| jsanchez307 | 0:1858f2b100fd | 4 | ================== |
| jsanchez307 | 0:1858f2b100fd | 5 | Init |
| jsanchez307 | 0:1858f2b100fd | 6 | ================== |
| jsanchez307 | 0:1858f2b100fd | 7 | */ |
| jsanchez307 | 0:1858f2b100fd | 8 | Board::Board (Pieces *pPieces, int pScreenHeight) |
| jsanchez307 | 0:1858f2b100fd | 9 | { |
| jsanchez307 | 0:1858f2b100fd | 10 | // Get the screen height |
| jsanchez307 | 0:1858f2b100fd | 11 | mScreenHeight = pScreenHeight; |
| jsanchez307 | 0:1858f2b100fd | 12 | |
| jsanchez307 | 0:1858f2b100fd | 13 | // Get the pointer to the pieces class |
| jsanchez307 | 0:1858f2b100fd | 14 | mPieces = pPieces; |
| jsanchez307 | 0:1858f2b100fd | 15 | |
| jsanchez307 | 0:1858f2b100fd | 16 | //Init the board blocks with free positions |
| jsanchez307 | 0:1858f2b100fd | 17 | InitBoard(); |
| jsanchez307 | 0:1858f2b100fd | 18 | } |
| jsanchez307 | 0:1858f2b100fd | 19 | |
| jsanchez307 | 0:1858f2b100fd | 20 | /* |
| jsanchez307 | 0:1858f2b100fd | 21 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 22 | Init the board blocks with free positions |
| jsanchez307 | 0:1858f2b100fd | 23 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 24 | */ |
| jsanchez307 | 0:1858f2b100fd | 25 | void Board::InitBoard() |
| jsanchez307 | 0:1858f2b100fd | 26 | { |
| jsanchez307 | 0:1858f2b100fd | 27 | for (int i = 0; i < BOARD_WIDTH; i++) |
| jsanchez307 | 0:1858f2b100fd | 28 | for (int j = 0; j < BOARD_HEIGHT; j++) |
| jsanchez307 | 0:1858f2b100fd | 29 | mBoard[i][j] = POS_FREE; |
| jsanchez307 | 0:1858f2b100fd | 30 | } |
| jsanchez307 | 0:1858f2b100fd | 31 | |
| jsanchez307 | 0:1858f2b100fd | 32 | /* |
| jsanchez307 | 0:1858f2b100fd | 33 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 34 | Store a piece in the board by filling the blocks |
| jsanchez307 | 0:1858f2b100fd | 35 | |
| jsanchez307 | 0:1858f2b100fd | 36 | Parameters: |
| jsanchez307 | 0:1858f2b100fd | 37 | |
| jsanchez307 | 0:1858f2b100fd | 38 | >> pX: Horizontal position in blocks |
| jsanchez307 | 0:1858f2b100fd | 39 | >> pY: Vertical position in blocks |
| jsanchez307 | 0:1858f2b100fd | 40 | >> pPiece: Piece to draw |
| jsanchez307 | 0:1858f2b100fd | 41 | >> pRotation: 1 of the 4 possible rotations |
| jsanchez307 | 0:1858f2b100fd | 42 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 43 | */ |
| jsanchez307 | 0:1858f2b100fd | 44 | void Board::StorePiece (int pX, int pY, int pPiece, int pRotation) |
| jsanchez307 | 0:1858f2b100fd | 45 | { |
| jsanchez307 | 0:1858f2b100fd | 46 | // Store each block of the piece into the board |
| jsanchez307 | 0:1858f2b100fd | 47 | for (int i1 = pX, i2 = 0; i1 < pX + PIECE_BLOCKS; i1++, i2++) |
| jsanchez307 | 0:1858f2b100fd | 48 | { |
| jsanchez307 | 0:1858f2b100fd | 49 | for (int j1 = pY, j2 = 0; j1 < pY + PIECE_BLOCKS; j1++, j2++) |
| jsanchez307 | 0:1858f2b100fd | 50 | { |
| jsanchez307 | 0:1858f2b100fd | 51 | // Store only the blocks of the piece that are not holes |
| jsanchez307 | 0:1858f2b100fd | 52 | if (mPieces->GetBlockType (pPiece, pRotation, j2, i2) != 0) |
| jsanchez307 | 0:1858f2b100fd | 53 | mBoard[i1][j1] = POS_FILLED; |
| jsanchez307 | 0:1858f2b100fd | 54 | } |
| jsanchez307 | 0:1858f2b100fd | 55 | } |
| jsanchez307 | 0:1858f2b100fd | 56 | } |
| jsanchez307 | 0:1858f2b100fd | 57 | |
| jsanchez307 | 0:1858f2b100fd | 58 | /* |
| jsanchez307 | 0:1858f2b100fd | 59 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 60 | Check if the game is over becase a piece have achived the upper position |
| jsanchez307 | 0:1858f2b100fd | 61 | |
| jsanchez307 | 0:1858f2b100fd | 62 | Returns true or false |
| jsanchez307 | 0:1858f2b100fd | 63 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 64 | */ |
| jsanchez307 | 0:1858f2b100fd | 65 | bool Board::IsGameOver() |
| jsanchez307 | 0:1858f2b100fd | 66 | { |
| jsanchez307 | 0:1858f2b100fd | 67 | //If the first line has blocks, then, game over |
| jsanchez307 | 0:1858f2b100fd | 68 | for (int i = 0; i < BOARD_WIDTH; i++) |
| jsanchez307 | 0:1858f2b100fd | 69 | { |
| jsanchez307 | 0:1858f2b100fd | 70 | if (mBoard[i][0] == POS_FILLED) return true; |
| jsanchez307 | 0:1858f2b100fd | 71 | } |
| jsanchez307 | 0:1858f2b100fd | 72 | |
| jsanchez307 | 0:1858f2b100fd | 73 | return false; |
| jsanchez307 | 0:1858f2b100fd | 74 | } |
| jsanchez307 | 0:1858f2b100fd | 75 | |
| jsanchez307 | 0:1858f2b100fd | 76 | /* |
| jsanchez307 | 0:1858f2b100fd | 77 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 78 | Delete a line of the board by moving all above lines down |
| jsanchez307 | 0:1858f2b100fd | 79 | |
| jsanchez307 | 0:1858f2b100fd | 80 | Parameters: |
| jsanchez307 | 0:1858f2b100fd | 81 | |
| jsanchez307 | 0:1858f2b100fd | 82 | >> pY: Vertical position in blocks of the line to delete |
| jsanchez307 | 0:1858f2b100fd | 83 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 84 | */ |
| jsanchez307 | 0:1858f2b100fd | 85 | void Board::DeleteLine (int pY) |
| jsanchez307 | 0:1858f2b100fd | 86 | { |
| jsanchez307 | 0:1858f2b100fd | 87 | // Moves all the upper lines one row down |
| jsanchez307 | 0:1858f2b100fd | 88 | for (int j = pY; j > 0; j--) |
| jsanchez307 | 0:1858f2b100fd | 89 | { |
| jsanchez307 | 0:1858f2b100fd | 90 | for (int i = 0; i < BOARD_WIDTH; i++) |
| jsanchez307 | 0:1858f2b100fd | 91 | { |
| jsanchez307 | 0:1858f2b100fd | 92 | mBoard[i][j] = mBoard[i][j-1]; |
| jsanchez307 | 0:1858f2b100fd | 93 | } |
| jsanchez307 | 0:1858f2b100fd | 94 | } |
| jsanchez307 | 0:1858f2b100fd | 95 | } |
| jsanchez307 | 0:1858f2b100fd | 96 | |
| jsanchez307 | 0:1858f2b100fd | 97 | /* |
| jsanchez307 | 0:1858f2b100fd | 98 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 99 | Delete all the lines that should be removed |
| jsanchez307 | 0:1858f2b100fd | 100 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 101 | */ |
| jsanchez307 | 0:1858f2b100fd | 102 | int Board::DeletePossibleLines () |
| jsanchez307 | 0:1858f2b100fd | 103 | { |
| jsanchez307 | 0:1858f2b100fd | 104 | int count =0; |
| jsanchez307 | 0:1858f2b100fd | 105 | for (int j = 0; j < BOARD_HEIGHT; j++) |
| jsanchez307 | 0:1858f2b100fd | 106 | { |
| jsanchez307 | 0:1858f2b100fd | 107 | int i = 0; |
| jsanchez307 | 0:1858f2b100fd | 108 | while (i < BOARD_WIDTH) |
| jsanchez307 | 0:1858f2b100fd | 109 | { |
| jsanchez307 | 0:1858f2b100fd | 110 | if (mBoard[i][j] != POS_FILLED) break; |
| jsanchez307 | 0:1858f2b100fd | 111 | i++; |
| jsanchez307 | 0:1858f2b100fd | 112 | } |
| jsanchez307 | 0:1858f2b100fd | 113 | |
| jsanchez307 | 0:1858f2b100fd | 114 | if (i == BOARD_WIDTH) |
| jsanchez307 | 0:1858f2b100fd | 115 | { |
| jsanchez307 | 0:1858f2b100fd | 116 | DeleteLine (j); |
| jsanchez307 | 0:1858f2b100fd | 117 | count++; |
| jsanchez307 | 0:1858f2b100fd | 118 | } |
| jsanchez307 | 0:1858f2b100fd | 119 | } |
| jsanchez307 | 0:1858f2b100fd | 120 | return count; |
| jsanchez307 | 0:1858f2b100fd | 121 | } |
| jsanchez307 | 0:1858f2b100fd | 122 | |
| jsanchez307 | 0:1858f2b100fd | 123 | /* |
| jsanchez307 | 0:1858f2b100fd | 124 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 125 | Returns 1 (true) if the this block of the board is empty, 0 if it is filled |
| jsanchez307 | 0:1858f2b100fd | 126 | |
| jsanchez307 | 0:1858f2b100fd | 127 | Parameters: |
| jsanchez307 | 0:1858f2b100fd | 128 | |
| jsanchez307 | 0:1858f2b100fd | 129 | >> pX: Horizontal position in blocks |
| jsanchez307 | 0:1858f2b100fd | 130 | >> pY: Vertical position in blocks |
| jsanchez307 | 0:1858f2b100fd | 131 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 132 | */ |
| jsanchez307 | 0:1858f2b100fd | 133 | bool Board::IsFreeBlock (int pX, int pY) |
| jsanchez307 | 0:1858f2b100fd | 134 | { |
| jsanchez307 | 0:1858f2b100fd | 135 | if (mBoard [pX][pY] == POS_FREE) return true; else return false; |
| jsanchez307 | 0:1858f2b100fd | 136 | } |
| jsanchez307 | 0:1858f2b100fd | 137 | |
| jsanchez307 | 0:1858f2b100fd | 138 | /* |
| jsanchez307 | 0:1858f2b100fd | 139 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 140 | Returns the horizontal position (isn pixels) of the block given like parameter |
| jsanchez307 | 0:1858f2b100fd | 141 | |
| jsanchez307 | 0:1858f2b100fd | 142 | Parameters: |
| jsanchez307 | 0:1858f2b100fd | 143 | |
| jsanchez307 | 0:1858f2b100fd | 144 | >> pPos: Horizontal position of the block in the board |
| jsanchez307 | 0:1858f2b100fd | 145 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 146 | */ |
| jsanchez307 | 0:1858f2b100fd | 147 | int Board::GetXPosInPixels (int pPos) |
| jsanchez307 | 0:1858f2b100fd | 148 | { |
| jsanchez307 | 0:1858f2b100fd | 149 | return ( ( BOARD_POSITION - (BLOCK_SIZE * (BOARD_WIDTH / 2)) ) + (pPos * BLOCK_SIZE) ); |
| jsanchez307 | 0:1858f2b100fd | 150 | } |
| jsanchez307 | 0:1858f2b100fd | 151 | |
| jsanchez307 | 0:1858f2b100fd | 152 | /* |
| jsanchez307 | 0:1858f2b100fd | 153 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 154 | Returns the vertical position (in pixels) of the block given like parameter |
| jsanchez307 | 0:1858f2b100fd | 155 | |
| jsanchez307 | 0:1858f2b100fd | 156 | Parameters: |
| jsanchez307 | 0:1858f2b100fd | 157 | |
| jsanchez307 | 0:1858f2b100fd | 158 | >> pPos: Horizontal position of the block in the board |
| jsanchez307 | 0:1858f2b100fd | 159 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 160 | */ |
| jsanchez307 | 0:1858f2b100fd | 161 | int Board::GetYPosInPixels (int pPos) |
| jsanchez307 | 0:1858f2b100fd | 162 | { |
| jsanchez307 | 0:1858f2b100fd | 163 | return ( (mScreenHeight - (BLOCK_SIZE * BOARD_HEIGHT)) + (pPos * BLOCK_SIZE) ); |
| jsanchez307 | 0:1858f2b100fd | 164 | } |
| jsanchez307 | 0:1858f2b100fd | 165 | |
| jsanchez307 | 0:1858f2b100fd | 166 | /* |
| jsanchez307 | 0:1858f2b100fd | 167 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 168 | Check if the piece can be stored at this position without any collision |
| jsanchez307 | 0:1858f2b100fd | 169 | Returns true if the movement is possible, false if it not possible |
| jsanchez307 | 0:1858f2b100fd | 170 | |
| jsanchez307 | 0:1858f2b100fd | 171 | Parameters: |
| jsanchez307 | 0:1858f2b100fd | 172 | |
| jsanchez307 | 0:1858f2b100fd | 173 | >> pX: Horizontal position in blocks |
| jsanchez307 | 0:1858f2b100fd | 174 | >> pY: Vertical position in blocks |
| jsanchez307 | 0:1858f2b100fd | 175 | >> pPiece: Piece to draw |
| jsanchez307 | 0:1858f2b100fd | 176 | >> pRotation: 1 of the 4 possible rotations |
| jsanchez307 | 0:1858f2b100fd | 177 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 178 | */ |
| jsanchez307 | 0:1858f2b100fd | 179 | bool Board::IsPossibleMovement (int pX, int pY, int pPiece, int pRotation) |
| jsanchez307 | 0:1858f2b100fd | 180 | { |
| jsanchez307 | 0:1858f2b100fd | 181 | // Checks collision with pieces already stored in the board or the board limits |
| jsanchez307 | 0:1858f2b100fd | 182 | // This is just to check the 5x5 blocks of a piece with the appropiate area in the board |
| jsanchez307 | 0:1858f2b100fd | 183 | for (int i1 = pX, i2 = 0; i1 < pX + PIECE_BLOCKS; i1++, i2++) |
| jsanchez307 | 0:1858f2b100fd | 184 | { |
| jsanchez307 | 0:1858f2b100fd | 185 | for (int j1 = pY, j2 = 0; j1 < pY + PIECE_BLOCKS; j1++, j2++) |
| jsanchez307 | 0:1858f2b100fd | 186 | { |
| jsanchez307 | 0:1858f2b100fd | 187 | // Check if the piece is outside the limits of the board |
| jsanchez307 | 0:1858f2b100fd | 188 | if ( i1 < 0 || |
| jsanchez307 | 0:1858f2b100fd | 189 | i1 > BOARD_WIDTH - 1 || |
| jsanchez307 | 0:1858f2b100fd | 190 | j1 > BOARD_HEIGHT - 1) |
| jsanchez307 | 0:1858f2b100fd | 191 | { |
| jsanchez307 | 0:1858f2b100fd | 192 | if (mPieces->GetBlockType (pPiece, pRotation, j2, i2) != 0) |
| jsanchez307 | 0:1858f2b100fd | 193 | return 0; |
| jsanchez307 | 0:1858f2b100fd | 194 | } |
| jsanchez307 | 0:1858f2b100fd | 195 | |
| jsanchez307 | 0:1858f2b100fd | 196 | // Check if the piece have collisioned with a block already stored in the map |
| jsanchez307 | 0:1858f2b100fd | 197 | if (j1 >= 0) |
| jsanchez307 | 0:1858f2b100fd | 198 | { |
| jsanchez307 | 0:1858f2b100fd | 199 | if ((mPieces->GetBlockType (pPiece, pRotation, j2, i2) != 0) && |
| jsanchez307 | 0:1858f2b100fd | 200 | (!IsFreeBlock (i1, j1)) ) |
| jsanchez307 | 0:1858f2b100fd | 201 | return false; |
| jsanchez307 | 0:1858f2b100fd | 202 | } |
| jsanchez307 | 0:1858f2b100fd | 203 | } |
| jsanchez307 | 0:1858f2b100fd | 204 | } |
| jsanchez307 | 0:1858f2b100fd | 205 | |
| jsanchez307 | 0:1858f2b100fd | 206 | // No collision |
| jsanchez307 | 0:1858f2b100fd | 207 | return true; |
| jsanchez307 | 0:1858f2b100fd | 208 | } |