Jordan Sanchez / Mbed 2 deprecated Tetris

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