Jay Balar / Mbed 2 deprecated 4180_Project_3

Dependencies:   4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed

Committer:
jstephens78
Date:
Thu Dec 01 01:46:22 2022 +0000
Revision:
18:cf74968078ea
Parent:
15:e9f3b72b7486
merge

Who changed what in which revision?

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