diff options
Diffstat (limited to 'src/Model/Grid.cpp')
| -rw-r--r-- | src/Model/Grid.cpp | 187 |
1 files changed, 63 insertions, 124 deletions
diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index 6fc41c6..92837f6 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -1,161 +1,100 @@ #include "Grid.hpp" //Constructor -Grid::Grid(int size) -{ - //Create Vector - m_size = size; - m_table = std::vector<std::vector<Cell<StringElement>*> >(size); - - //Init all of line and cell - for(int i = 0 ; i < size ; i++) - { - m_table[i] = std::vector<Cell<StringElement>*>(size); - for (int j = 0 ; j < size ; j++) - { - Cell<StringElement> * cell = new Cell<StringElement>(""); - m_table[i][j] = cell; +Grid::Grid(): m_size(4), m_grid(4){ + + //Init all cells + for(int i=0;i<m_size;i++){ + for(int j=0;j<m_size;j++){ + m_grid.at(i).push_back(0); } } -} -//Destructor -Grid::~Grid() -{ - for(int i = 0 ; i < m_size ; i++) - { - for(int j = 0 ; j < m_size ; j++) - delete m_table[i][j]; - } } -void Grid::show() -{ - std::cout << "_________________" << std::endl; - std::cout << std::endl; - for(int i = 0 ; i < m_size ; i++) - { - std::cout << "|"; - for(int j = 0 ; j < m_size ; j++) - { - std::cout << " " << m_table[i][j]->description() << " |"; - } - std::cout << std::endl; - - if (i != m_size -1) - std::cout << std::endl; - } - std::cout << "_________________" << std::endl; +//Destructor +Grid::~Grid(){ } -bool Grid::isEmpty(int i, int j) -{ - if (i >= 0 && i < m_size && j >= 0 && j < m_size) - return m_table[i][j]->isEmpty(); - return false; -} +std::string Grid::description(){ -bool Grid::gridIsFull() -{ + //Init stringstream description + std::stringstream description; - for (int i = 0; i < m_size ; i++) - { - for (int j = 0; j < m_size ; j++) - { - if (m_table[i][j]->isEmpty()) - return false; + //Start to write description + description << "-----------------" << std::endl; + for(int i=0;i<m_size;i++){ + for(int j=0;j<m_size;j++){ + if(m_grid.at(i).at(j) == 0) + description << "| " << " " << " "; + else + description << "| " << m_grid.at(i).at(j) << " "; } + description << "|"; + description << std::endl; } + description << "-----------------" << std::endl << std::endl; - return true; + //Return description + return description.str(); } -void Grid::setCell(int i, int j, Cell<StringElement> *cell) -{ - if (i >= 0 && i < m_size && j >= 0 && j < m_size) - { - delete m_table[i][j]; - m_table[i][j] = cell; - } +bool Grid::isEmpty(int i, int j){ + if(m_grid.at(i).at(j) == 0) + return true; + return false; } -Cell<StringElement>* Grid::getCell(short i, short j){ - return m_table[i][j]; -} +std::tuple<int, int> Grid::getRandomEmptyCellCoord(){ -int Grid::getNRows(){ - return m_table[0].size(); -} + //Init list of candidate + std::vector<std::tuple<int, int> > candidates; -int Grid::getNCols(){ - return m_table.size(); -} + //Construct list of candidates + for(int i=0;i<m_size;i++){ + for(int j=0;j<m_size;j++){ + if(this->isEmpty(i,j)){ + std::tuple<int, int> currentCandidate(i,j); + candidates.push_back(currentCandidate); + } + } + } + //If no candidate available + if(candidates.size() == 0) + return std::tuple<int, int>(-1, -1); -std::vector<Cell<StringElement>* > Grid::swipeLine(std::vector<Cell<StringElement>* > line){ - std::vector<Cell<StringElement>*> newLine = std::vector<Cell<StringElement>*>(4); + //Select the candidates + int winnerIs(rand() % candidates.size()); + //Return the candidate + return candidates.at(winnerIs); - for (int j = 0 ; j < 3 ; j++) - { - if(j>3) - break; - Cell<StringElement> * cell = new Cell<StringElement>(line.at(j)->getElementValue()); - Cell<StringElement> * cellp1 = new Cell<StringElement>(line.at(j+1)->getElementValue()); +} - int a=atoi(cell->getElementValue().c_str()); - int ap1=atoi(cellp1->getElementValue().c_str()); - std::string s=std::to_string(a); - std::string sp1=std::to_string(ap1); - if(a==ap1 && a!=0){ - s=""; - sp1=std::to_string(a+ap1); - if(ap1 == 0) - newLine[j+1] = new Cell<StringElement>(""); - else - newLine[j+1] = new Cell<StringElement>(sp1); - newLine[j] = new Cell<StringElement>(s); - j++; - } - else{ - if(ap1==0) - newLine[j+1] = new Cell<StringElement>(""); - else - newLine[j+1] = new Cell<StringElement>(sp1); - if(a==0) - newLine[j] = new Cell<StringElement>(""); - else - newLine[j] = new Cell<StringElement>(s); +//Change value of cell +bool Grid::setCell(std::tuple<int, int> coord, int value){ + int i=std::get<0>(coord); + int j=std::get<1>(coord); - } - delete cell; - delete cellp1; - + if(i>=0 && i<m_size && j>=0 && j<m_size){ + m_grid.at(i).at(j)=value; + return true; } + return false; +} + +//Another setCell method +bool Grid::setCell(int i, int j, int value){ + std::tuple<int, int> coord(i,j); + return this->setCell(coord, value); +} - for (int j = 0 ; j < 3 ; j++){ - if(!newLine[j]->isEmpty()){ - if(newLine[j+1]->isEmpty()){ - newLine[j+1]=new Cell<StringElement>(newLine[j]->getElementValue()); - newLine[j]=new Cell<StringElement>(""); - } - } - } - for(int i=0; i<4;i++){ - std::cout << "|" << newLine[i]->description() << "|"; - } - std::cout << "done"; - return newLine; -} -void Grid::swipeRight(){ - std::vector<Cell<StringElement>*> a=this->swipeLine(m_table.at(0)); - m_table[0]=a; -} |
