void Board::slideLeft() { for (int i = 0; i < 2; i++) { Tile boardCopy[boardWidth][boardHeight]; std::copy(&board[0][0], &board[0][0] + boardWidth*boardHeight, &boardCopy[0][0]); for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { if (boardCopy[x][y].value == 0) { for (int k = y+1; k < 4; k++) { if (boardCopy[x][k].value != 0) { boardCopy[x][y] = boardCopy[x][k]; boardCopy[x][y].xPos = x; boardCopy[x][y].yPos = y; boardCopy[x][k] = Tile(Tile::TileColor::EMPTY, x, k, 0); break; } } } } } std::copy(&boardCopy[0][0], &boardCopy[0][0] + boardWidth*boardHeight, &board[0][0]); if (i == 0) { mergeLeft(); } } }
bool mergeMoveLeft(struct tile **board, int size, int pos_rel, int multi) { //move so that no space between tiles bool move = moveTileLeft(board, size, pos_rel); //plus bool plus = mergeLeft(board, size, pos_rel, multi); //move again to make sure no space moveTileLeft(board, size, pos_rel); // Play sound when cannot move. // if (!move && !plus){ // moving_sound(0); // } return move || plus; }
/** * Splits block b across constraint c into two new blocks, l and r (c's left * and right sides respectively) */ void Blocks::split(Block *b, Block *&l, Block *&r, Constraint *c) { b->split(l, r, c); #ifdef RECTANGLE_OVERLAP_LOGGING ofstream f(LOGFILE, ios::app); f << "Split left: " << *l << endl; f << "Split right: " << *r << endl; #endif r->posn = b->posn; r->wposn = r->posn * r->weight; mergeLeft(l); // r may have been merged! r = c->right->block; r->wposn = r->desiredWeightedPosition(); r->posn = r->wposn / r->weight; mergeRight(r); removeBlock(b); insert(l); insert(r); }
int main(void) { int addScore, position, pauseSel; // Initializations PLL_Init(); // Clock set at 80 MHz LCD_Init(); Board_Init(); Input_Init(); DAC_Init(); Random_Init(NVIC_ST_CURRENT_R); Timer2_Init(80000000); // time interrupt Timer1_Init(2000); // sound interrupt EnableInterrupts(); generateRandomTile(); drawAllTiles(); writeScore(0); writeHighscore(0); writeTime(0); displayHighestTile(); while(1) { // draw arrow if ready if (arrowReady == 1) { // acknowledge flag arrowReady = 0; // draw arrow drawArrow(); } // write time if ready if (timeReady == 1) { // acknowledge flag timeReady = 0; // write time writeTime(elapsedTime); } // Play mode and button1 is pushed if(Button1 && !pauseMode && !gameOver) { // Play sound playSound = 1; // shift and merge tiles towards arrow position = getSliderPosition(); if (position == 1) { shiftLeft(); addScore = mergeLeft(); shiftLeft(); } else if (position == 2) { shiftUp(); addScore = mergeUp(); shiftUp(); } else if (position == 3) { shiftRight(); addScore = mergeRight(); shiftRight(); } else { shiftDown(); addScore = mergeDown(); shiftDown(); } eraseBoard(); drawAllTiles(); // update score score += addScore; addScore = 0; writeScore(score); displayHighestTile(); // delay before adding new tile delay(200); // create new tile if (countEmptyTiles() != 0) { generateRandomTile(); } drawAllTiles(); // update highest tile image displayHighestTile(); // check if game over if (checkGameOver() == 1) { gameOver = 1; } // unset flag Button1 = 0; } // button 2 is pause else if (Button2 && !gameOver && !pauseMode) { pauseMode = 1; pauseSel = 0; // disable arrow and timer NVIC_ST_CTRL_R = 0; TIMER2_CTL_R = 0x00000000; LCD_DrawFilledRect(prevX,prevY,20,20,BLACK); // draw pause mode screen drawPauseMode(); // acknowledge button Button2 = 0; // wait until button is pushed while (pauseMode) { // Button 1 selects current pause selection button if (Button1) { // acknowledge button Button1 = 0; Button2 = 0; pauseMode = 0; // if pause selection = "continue" (pauseSel = 0), continue with game if (pauseSel == 0) { // redraw screen eraseBoard(); drawAllTiles(); // enable gameplay NVIC_ST_CTRL_R = 0x07; TIMER2_CTL_R = 0x00000001; } // if pause selection = "restart" (pauseSel = 0), end game else if (pauseSel == 1) { if (score > highscore) { writeHighscore(score); } score = 0; eraseScore(); writeScore(0); clearBoard(); eraseBoard(); pauseMode = 0; generateRandomTile(); drawAllTiles(); elapsedTime = 0; eraseTime(); writeTime(0); displayHighestTile(); NVIC_ST_CTRL_R = 0x07; TIMER2_CTL_R = 0x00000001; } } // Button 2 changes pause selection else if (Button2) { Button2 = 0; if (pauseSel == 0) { pauseSel = 1; LCD_DrawRect(144,112,58,16,BLACK); LCD_DrawRect(222,112,51,16,WHITE); } else if (pauseSel == 1) { pauseSel = 0; LCD_DrawRect(222,112,51,16,BLACK); LCD_DrawRect(144,112,58,16,WHITE); } } } } // game over if (gameOver == 1) { NVIC_ST_CTRL_R = 0; TIMER2_CTL_R = 0x00000000; LCD_DrawFilledRect(156,38,100,20,BLACK); LCD_SetTextColor(255,255,240); LCD_Goto(30,5); printf("GAME OVER"); while (Button1 == 0 && Button2 == 0) {} Button1 = 0; Button2 = 0; LCD_DrawFilledRect(prevX,prevY,20,20,BLACK); drawGameOver(score, elapsedTime); if (score > highscore) { writeHighscore(score); gameOverHighscore(score); } // wait til button is pushed while (Button1 == 0 && Button2 == 0) {} // acknowledge buttons Button1 = 0; Button2 = 0; // start new game score = 0; eraseScore(); writeScore(0); clearBoard(); eraseBoard(); pauseMode = 0; generateRandomTile(); drawAllTiles(); elapsedTime = 0; eraseTime(); writeTime(0); displayHighestTile(); NVIC_ST_CTRL_R = 0x07; TIMER2_CTL_R = 0x00000001; // finish game over mode gameOver = 0; } } }