#include "Grid.hpp" //==================== Constructor and Destructor ==================== //Constructor Grid::Grid(): m_size(4), m_grid(4){ //Init all cells for(int i=0;i Grid::rightDefragment(std::vector line){ for(int j=0; j Grid::leftDefragment(std::vector line){ std::vector reversedLine= this->reverseLine(line); return this->reverseLine(this->rightDefragment(reversedLine)); } //Merge line to the right std::vector Grid::rightMerge(std::vector line){ for(int i=0; i< m_size-1;i++){ int val1=line.at(i); int val2=line.at(i+1); if(val1==val2){ line.at(i)=0; line.at(i+1)=val1*2; m_lastMoveScore+=val1*2; i++; } } return line; } //Merge line to the left std::vector Grid::leftMerge(std::vector line){ for(int i=m_size-1; i>0;i--){ int val1=line.at(i); int val2=line.at(i-1); if(val1==val2){ line.at(i)=0; line.at(i-1)=val1*2; m_lastMoveScore+=val1*2; i--; } } return line; } //==================== Swipe methods ==================== //Swipe to the right bool Grid::swipeRight(){ m_lastMoveScore=0; bool moveDone=false; for(int i=0; i swipedLine(this->rightDefragment(this->leftMerge(this->rightDefragment(m_grid.at(i))))); if(!this->compareLines(m_grid.at(i), swipedLine)){ moveDone=true; m_grid.at(i)=swipedLine; } } return moveDone; } //Swipe to the left bool Grid::swipeLeft(){ m_lastMoveScore=0; bool moveDone=false; for(int i=0; i swipedLine(this->leftDefragment(this->rightMerge(this->leftDefragment(m_grid.at(i))))); if(!this->compareLines(m_grid.at(i), swipedLine)){ moveDone=true; m_grid.at(i)=swipedLine; } } return moveDone; } //Swipe to the top bool Grid::swipeUp(){ m_lastMoveScore=0; bool moveDone=false; for(int i=0; i colVect=this->getCol(i); std::vector swipedLine(this->leftDefragment(this->rightMerge(this->leftDefragment(colVect)))); if(!this->compareLines(colVect, swipedLine)){ moveDone=true; this->setCol(i,swipedLine); } } return moveDone; } //Swipe to the bottom bool Grid::swipeDown(){ m_lastMoveScore=0; bool moveDone=false; for(int i=0; i colVect=this->getCol(i); std::vector swipedLine(this->rightDefragment(this->leftMerge(this->rightDefragment(colVect)))); if(!this->compareLines(colVect, swipedLine)){ moveDone=true; this->setCol(i,swipedLine); } } return moveDone; } //==================== Helpers ==================== //Get the max len of a string of the numbers in the grid : // Exemple: // - 1 return 1 // - 22 return 2 // - 120 return 3 // - 1000 return 4 //This method help to calculate the columns size int Grid::maxStrLenInGrid(){ int max=0; for(int i=0;i max) max=number.size(); } } return max; } //Test if the cell at (i,j) is empty bool Grid::isEmpty(int i, int j){ if(m_grid.at(i).at(j) == 0) return true; return false; } //Return a tuple that contain a random empty cell std::tuple Grid::getRandomEmptyCellCoord(){ //Init list of candidate std::vector > candidates; //Construct list of candidates for(int i=0;iisEmpty(i,j)){ std::tuple currentCandidate(i,j); candidates.push_back(currentCandidate); } } } //If no candidate available if(candidates.size() == 0) return std::tuple(-1, -1); //Select the candidates int winnerIs(rand() % candidates.size()); //Return the candidate return candidates.at(winnerIs); } //Reverse a line std::vector Grid::reverseLine(std::vector line){ std::vector reversedLine; for(int j=m_size-1; j>=0;j--){ reversedLine.push_back(line.at(j)); } return reversedLine; } //Return true if the grid is full. False else. bool Grid::isFull(){ for(int i=0;iisFull()) return false; for(int i=0;i colVect(this->getCol(i)); for(int j=0;j line1, std::vector line2){ for(int i=0;imaxStrLenInGrid(); //Start to write description std::stringstream gridBorder; for(int i=0;i<(maxStrLen+1)*4+1;i++){ gridBorder<<"-"; } description << std::endl << gridBorder.str() << std::endl; for(int i=0;i coord, int value){ int i=std::get<0>(coord); int j=std::get<1>(coord); if(i>=0 && i=0 && j coord(i,j); return this->setCell(coord, value); } //Assign a vector to a column. void Grid::setCol(int col, std::vector colVect){ for(int i=0;i Grid::getCol(int col){ std::vector colVect; for(int i=0;i > Grid::getGrid(){ return m_grid; }