#include "Grid.hpp" //Constructor Grid::Grid(int size) { //Create Vector m_size = size; m_table = std::vector*> >(size); //Init all of line and cell for(int i = 0 ; i < size ; i++) { m_table[i] = std::vector*>(size); for (int j = 0 ; j < size ; j++) { Cell * cell = new Cell(""); m_table[i][j] = cell; } } } //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; } 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; } bool Grid::gridIsFull() { for (int i = 0; i < m_size ; i++) { for (int j = 0; j < m_size ; j++) { if (m_table[i][j]->isEmpty()) return false; } } return true; } void Grid::setCell(int i, int j, Cell *cell) { if (i >= 0 && i < m_size && j >= 0 && j < m_size) { delete m_table[i][j]; m_table[i][j] = cell; } } Cell* Grid::getCell(short i, short j){ return m_table[i][j]; } int Grid::getNRows(){ return m_table[0].size(); } int Grid::getNCols(){ return m_table.size(); } std::vector* > Grid::swipeLine(std::vector* > line){ std::vector*> newLine = std::vector*>(4); for (int j = 0 ; j < 3 ; j++) { if(j>3) break; Cell * cell = new Cell(line.at(j)->getElementValue()); Cell * cellp1 = new Cell(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(""); else newLine[j+1] = new Cell(sp1); newLine[j] = new Cell(s); j++; } else{ if(ap1==0) newLine[j+1] = new Cell(""); else newLine[j+1] = new Cell(sp1); if(a==0) newLine[j] = new Cell(""); else newLine[j] = new Cell(s); } delete cell; delete cellp1; } for (int j = 0 ; j < 3 ; j++){ if(!newLine[j]->isEmpty()){ if(newLine[j+1]->isEmpty()){ newLine[j+1]=new Cell(newLine[j]->getElementValue()); newLine[j]=new Cell(""); } } } for(int i=0; i<4;i++){ std::cout << "|" << newLine[i]->description() << "|"; } std::cout << "done"; return newLine; } void Grid::swipeRight(){ std::vector*> a=this->swipeLine(m_table.at(0)); m_table[0]=a; }