From af7f2fc8700df64f245caf7b864e777831867c3f Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sun, 3 May 2015 09:44:41 +0200 Subject: Organize Grid cpp code --- src/Model/Grid.cpp | 279 +++++++++++++++++++++++++++-------------------------- 1 file changed, 143 insertions(+), 136 deletions(-) (limited to 'src/Model') diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index ed8686f..cc0fe60 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -1,5 +1,7 @@ #include "Grid.hpp" +//==================== Constructor and Destructor ==================== + //Constructor Grid::Grid(): m_size(4), m_grid(4){ @@ -16,111 +18,9 @@ Grid::~Grid(){ } -std::string Grid::description(){ - - //Init stringstream description - std::stringstream description; - - //Get max str len of the grid - int maxStrLen=this->maxStrLenInGrid(); - - //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 max) - max=number.size(); - } - } - return max; -} - -bool Grid::isEmpty(int i, int j){ - if(m_grid.at(i).at(j) == 0) - return true; - return false; -} - -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); - -} - - - -//Change value of cell -bool Grid::setCell(std::tuple 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); -} - +//==================== Merge and defragment methods ==================== +//Defragment the line to the right std::vector Grid::rightDefragment(std::vector line){ for(int j=0; j Grid::rightDefragment(std::vector line){ return line; } +//Defragment the line to the left using right defragmentation std::vector Grid::leftDefragment(std::vector line){ - //for(int j=0; j0;i--){ - //int val1=line.at(i); - //int val2=line.at(i-1); - - //if(val1 != 0 && val2 == 0){ - //line.at(i)=0; - //line.at(i-1)=val1; - //} - //} - //} - 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); @@ -169,6 +60,7 @@ std::vector Grid::rightMerge(std::vector line){ 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); @@ -183,22 +75,12 @@ std::vector Grid::leftMerge(std::vector line){ } return line; } -std::vector Grid::swipeLine(std::vector line){ - //Swipe line is : - //- A defragmentation - //- A merging - //- Another defragmentation - line=this->rightDefragment(line); - line=this->rightMerge(line); - line=this->rightDefragment(line); - //Return swiped line - return line; -} +//==================== Swipe methods ==================== -//Swipe to right +//Swipe to the right bool Grid::swipeRight(){ m_lastMoveScore=0; @@ -214,7 +96,7 @@ bool Grid::swipeRight(){ return moveDone; } -//Swipe to right +//Swipe to the left bool Grid::swipeLeft(){ m_lastMoveScore=0; bool moveDone=false; @@ -229,6 +111,7 @@ bool Grid::swipeLeft(){ } +//Swipe to the top bool Grid::swipeUp(){ m_lastMoveScore=0; bool moveDone=false; @@ -243,6 +126,7 @@ bool Grid::swipeUp(){ } return moveDone; } +//Swipe to the bottom bool Grid::swipeDown(){ m_lastMoveScore=0; bool moveDone=false; @@ -259,23 +143,63 @@ bool Grid::swipeDown(){ return moveDone; } -void Grid::setCol(int col, std::vector colVect){ +//==================== 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; } -std::vector Grid::getCol(int col){ +//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; +} - std::vector colVect; +//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); + } + } } - return colVect; + //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; @@ -286,7 +210,7 @@ std::vector Grid::reverseLine(std::vector line){ return reversedLine; } - +//Return true if the grid is full. False else. bool Grid::isFull(){ for(int i=0;iisFull()) @@ -325,6 +250,7 @@ bool Grid::isOver(){ } +//Return true if line1 is equals to line2. False else. bool Grid::compareLines(std::vector line1, std::vector line2){ for(int i=0;i line1, std::vector line2){ return true; } +//Return the description of the grid in a string. +//In other terms, the grid in ASCII +std::string Grid::description(){ + + //Init stringstream description + std::stringstream description; + + //Get max str len of the grid + int maxStrLen=this->maxStrLenInGrid(); + + //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