This is a remake of tetris game for mbed. Please see detail here http://developer.mbed.org/users/sucrelv/notebook/tetris-game-on-mbed

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed-rtos mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Board.cpp Source File

Board.cpp

00001 #include "Board.h"
00002 
00003 /*
00004 ==================
00005 Init
00006 ==================
00007 */
00008 Board::Board (Pieces *pPieces, int pScreenHeight)
00009 {
00010     // Get the screen height
00011     mScreenHeight = pScreenHeight;
00012 
00013     // Get the pointer to the pieces class
00014     mPieces = pPieces;
00015 
00016     //Init the board blocks with free positions
00017     InitBoard();
00018 }
00019 
00020 
00021 /*
00022 ======================================                                  
00023 Init the board blocks with free positions
00024 ====================================== 
00025 */
00026 void Board::InitBoard()
00027 {
00028     for (int i = 0; i < BOARD_WIDTH; i++)
00029         for (int j = 0; j < BOARD_HEIGHT; j++)
00030             mBoard[i][j] = POS_FREE;
00031 }
00032 
00033 /* 
00034 ======================================                                  
00035 Store a piece in the board by filling the blocks
00036 
00037 Parameters:
00038 
00039 >> pX:      Horizontal position in blocks
00040 >> pY:      Vertical position in blocks
00041 >> pPiece:  Piece to draw
00042 >> pRotation:   1 of the 4 possible rotations
00043 ====================================== 
00044 */
00045 void Board::StorePiece (int pX, int pY, int pPiece, int pRotation)
00046 {
00047     // Store each block of the piece into the board
00048     for (int i1 = pX, i2 = 0; i1 < pX + PIECE_BLOCKS; i1++, i2++)
00049     {
00050         for (int j1 = pY, j2 = 0; j1 < pY + PIECE_BLOCKS; j1++, j2++)
00051         {   
00052             // Store only the blocks of the piece that are not holes
00053             if (mPieces->GetBlockType (pPiece, pRotation, j2, i2) != 0)     
00054                 mBoard[i1][j1] = POS_FILLED;    
00055         }
00056     }
00057 }
00058 
00059 
00060 /* 
00061 ======================================                                  
00062 Check if the game is over becase a piece have achived the upper position
00063 
00064 Returns true or false
00065 ====================================== 
00066 */
00067 bool Board::IsGameOver()
00068 {
00069     //If the first line has blocks, then, game over
00070     for (int i = 0; i < BOARD_WIDTH; i++)
00071     {
00072         if (mBoard[i][0] == POS_FILLED) return true;
00073     }
00074 
00075     return false;
00076 }
00077 
00078 
00079 /* 
00080 ======================================                                  
00081 Delete a line of the board by moving all above lines down
00082 
00083 Parameters:
00084 
00085 >> pY:      Vertical position in blocks of the line to delete
00086 ====================================== 
00087 */
00088 void Board::DeleteLine (int pY)
00089 {
00090     // Moves all the upper lines one row down
00091     for (int j = pY; j > 0; j--)
00092     {
00093         for (int i = 0; i < BOARD_WIDTH; i++)
00094         {
00095             mBoard[i][j] = mBoard[i][j-1];
00096         }
00097     }   
00098 }
00099 
00100 
00101 /* 
00102 ======================================                                  
00103 Delete all the lines that should be removed
00104 ====================================== 
00105 */
00106 int Board::DeletePossibleLines ()
00107 {
00108     int count =0;
00109     for (int j = 0; j < BOARD_HEIGHT; j++)
00110     {
00111         int i = 0;
00112         while (i < BOARD_WIDTH)
00113         {
00114             if (mBoard[i][j] != POS_FILLED) break;
00115             i++;
00116         }
00117 
00118         if (i == BOARD_WIDTH) 
00119         {
00120             DeleteLine (j);
00121             count++;
00122         }
00123     }
00124     return count;
00125 }
00126 
00127 
00128 /* 
00129 ======================================                                  
00130 Returns 1 (true) if the this block of the board is empty, 0 if it is filled
00131 
00132 Parameters:
00133 
00134 >> pX:      Horizontal position in blocks
00135 >> pY:      Vertical position in blocks
00136 ====================================== 
00137 */
00138 bool Board::IsFreeBlock (int pX, int pY)
00139 {
00140     if (mBoard [pX][pY] == POS_FREE) return true; else return false;
00141 }
00142 
00143 
00144 /* 
00145 ======================================                                  
00146 Returns the horizontal position (isn pixels) of the block given like parameter
00147 
00148 Parameters:
00149 
00150 >> pPos:    Horizontal position of the block in the board
00151 ====================================== 
00152 */
00153 int Board::GetXPosInPixels (int pPos)
00154 {
00155     return  ( ( BOARD_POSITION - (BLOCK_SIZE * (BOARD_WIDTH / 2)) ) + (pPos * BLOCK_SIZE) );
00156 }
00157 
00158 
00159 /* 
00160 ======================================                                  
00161 Returns the vertical position (in pixels) of the block given like parameter
00162 
00163 Parameters:
00164 
00165 >> pPos:    Horizontal position of the block in the board
00166 ====================================== 
00167 */
00168 int Board::GetYPosInPixels (int pPos)
00169 {
00170     return ( (mScreenHeight - (BLOCK_SIZE * BOARD_HEIGHT)) + (pPos * BLOCK_SIZE) );
00171 }
00172 
00173 
00174 /* 
00175 ======================================                                  
00176 Check if the piece can be stored at this position without any collision
00177 Returns true if the movement is  possible, false if it not possible
00178 
00179 Parameters:
00180 
00181 >> pX:      Horizontal position in blocks
00182 >> pY:      Vertical position in blocks
00183 >> pPiece:  Piece to draw
00184 >> pRotation:   1 of the 4 possible rotations
00185 ====================================== 
00186 */
00187 bool Board::IsPossibleMovement (int pX, int pY, int pPiece, int pRotation)
00188 {
00189     // Checks collision with pieces already stored in the board or the board limits
00190     // This is just to check the 5x5 blocks of a piece with the appropiate area in the board
00191     for (int i1 = pX, i2 = 0; i1 < pX + PIECE_BLOCKS; i1++, i2++)
00192     {
00193         for (int j1 = pY, j2 = 0; j1 < pY + PIECE_BLOCKS; j1++, j2++)
00194         {   
00195             // Check if the piece is outside the limits of the board
00196             if (    i1 < 0          || 
00197                 i1 > BOARD_WIDTH  - 1   ||
00198                 j1 > BOARD_HEIGHT - 1)
00199             {
00200                 if (mPieces->GetBlockType (pPiece, pRotation, j2, i2) != 0)
00201                     return 0;       
00202             }
00203 
00204             // Check if the piece have collisioned with a block already stored in the map
00205             if (j1 >= 0)    
00206             {
00207                 if ((mPieces->GetBlockType (pPiece, pRotation, j2, i2) != 0) &&
00208                     (!IsFreeBlock (i1, j1)) )
00209                     return false;
00210             }
00211         }
00212     }
00213 
00214     // No collision
00215     return true;
00216 }