void Board::slideUp() { 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 y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (boardCopy[x][y].value == 0) { for (int k = x + 1; k < 4; k++) { if (boardCopy[k][y].value != 0) { boardCopy[x][y] = boardCopy[k][y]; boardCopy[x][y].xPos = x; boardCopy[x][y].yPos = y; boardCopy[k][y] = Tile(Tile::TileColor::EMPTY, k, y, 0); break; } } } } } std::copy(&boardCopy[0][0], &boardCopy[0][0] + boardWidth*boardHeight, &board[0][0]); if (i == 0) { mergeUp(); } } }
// Entering function we know that child has no split of its own void HDCluster::condenseTooSmall() { mergeUp(); #ifdef DEBUG if (lambda_death == INFINITY) throw Rcpp::exception("Infinity is too small."); #endif left->left = nullptr; left->right = nullptr; delete left; left = nullptr; }
bool mergeMoveUp(struct tile **board, int size, int pos_rel, int multi) { bool move = moveTileUp(board, size, pos_rel); bool plus = mergeUp(board, size, pos_rel, multi); moveTileUp(board, size, pos_rel); // Play sound when cannot move. // if (!move && !plus){ // moving_sound(0); // } return move || plus; }
// Entering function we know that child has a split of its own void HDCluster::condenseSingleton() { mergeUp(); lambda_death = max(lambda_death, left->lambda_death); #ifdef DEBUG if (lambda_death == INFINITY) throw Rcpp::exception("max infinity"); #endif right = left->right; left->right = nullptr; HDCluster* keep = left; left = keep->left; keep->left = nullptr; delete keep; }
GameAlgorithm::Point2DArrPair GameAlgorithm::updateBoard(GameAlgorithm::Move move) { GameAlgorithm::Point2DArrPair result; for (int row = 0; row < m_nSideLen; row++) { for (int col = 0; col < m_nSideLen; col++) { m_board[row][col].m = false; m_board[row][col].n = false; m_board[row][col].ids.clear(); if (m_board[row][col] != ZERO) m_board[row][col].ids.push_back(row * m_nSideLen + col); } } for (int row = 0; row < m_nSideLen; row++) { for (int col = 0; col < m_nSideLen; col++) { Point2D p2d(row, col, m_board[row][col]); result.first.push_back(p2d); } } switch (move) { case UP: mergeUp(); moveUp(); break; case DOWN: mergeDown(); moveDown(); break; case LEFT: mergerLeft(); moveLeft(); break; case RIGHT: mergeRight(); moveRight(); break; } Point2DArr freeSlots; for (int row = 0; row < m_nSideLen; row++) { for (int col = 0; col < m_nSideLen; col++) { if (m_board[row][col] == NULL) { Point2D p2d(row, col, NULL); freeSlots.push_back(p2d); } } } putNextNumbersOnBoard(1 * (m_nSideLen / 4) * (m_nSideLen / 4), freeSlots); for (int row = 0; row < m_nSideLen; row++) { for (int col = 0; col < m_nSideLen; col++) { Point2D p2d(row, col, m_board[row][col]); result.second.push_back(p2d); } } return result; }
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; } } }