From 36d033caeebd8ccbddf711825a5a96e3930438be Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sat, 2 May 2015 19:26:01 +0200 Subject: [PATCH] Make clean code --- src/CMakeLists.txt | 4 +- src/Model/CMakeLists.txt | 7 +- src/Model/Game.cpp | 58 +---------- src/Model/Game.hpp | 8 +- src/Model/Grid.cpp | 203 ++++++++++++++------------------------- src/Model/Grid.hpp | 22 ++--- src/main.cpp | 32 +++--- 7 files changed, 103 insertions(+), 231 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index da444d2..ed13eb5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,7 +12,7 @@ set_property(GLOBAL PROPERTY SFML_INCLUDE_DIR "${SFML_INCLUDE_DIR}") #Include "Includes" and "Libraries" include_directories(${SFML_INCLUDE_DIR}) -target_link_libraries(2P11 ${SFML_LIBRARIES} Model ConsoleController) +target_link_libraries(2P11 ${SFML_LIBRARIES} Model ) add_subdirectory(./Model) -add_subdirectory(./Controllers/) +#add_subdirectory(./Controllers/) diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index 80ea875..888589e 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -1,7 +1,2 @@ #Make Model lib -add_library(Model Grid.cpp Game.cpp ) - -target_link_libraries(Model Elements) - -add_subdirectory(./Elements) - +add_library(Model Grid.cpp Game.cpp) diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index 7dc32c6..32482b7 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -1,59 +1,9 @@ #include "Game.hpp" -Game::Game() -{ - m_grid = new Grid(4); + + +Game::Game() : m_grid(){ } -Game::~Game() -{ - delete m_grid; -} - - - -void Game::showGrid() -{ - m_grid->show(); - std::cout << std::endl; -} - -void Game::pop() -{ - bool cellChosen = false; - - int i; - int j; - - while(!cellChosen) - { - i = rand() % 4; - j = rand() % 4; - - if (m_grid->isEmpty(i,j)) - cellChosen = true; - } - m_grid->setCell(i, j, new Cell(std::to_string(2))); -} -void Game::swipeRight(){ - m_grid->swipeRight(); -} -bool Game::isOver() -{ - if(m_grid->gridIsFull()){ - for(int i=0;igetNRows();i++){ - - for(int j=0;jgetNCols()-1;j++){ - if(m_grid->getCell(i,j)->equals(m_grid->getCell(i,j+1))){ - return false; - } - } - } - - } - else { - return false; - } - - return true; +Game::~Game(){ } diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp index c1c636a..ee3a6a9 100644 --- a/src/Model/Game.hpp +++ b/src/Model/Game.hpp @@ -8,24 +8,18 @@ * Date : 29/04/2015 */ #include -#include #include -#include "./Elements/StringElement.hpp" #include "Grid.hpp" class Game { private: - Grid *m_grid; + Grid m_grid; public: Game(); ~Game(); - void pop(); - void showGrid(); - void swipeRight(); - bool isOver(); }; #endif 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*> >(size); +Grid::Grid(): m_size(4), m_grid(4){ - //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; + //Init all cells + for(int i=0;idescription() << " |"; + +std::string Grid::description(){ + + //Init stringstream description + std::stringstream description; + + //Start to write description + description << "-----------------" << std::endl; + for(int i=0;i= 0 && i < m_size && j >= 0 && j < m_size) - return m_table[i][j]->isEmpty(); - +bool Grid::isEmpty(int i, int j){ + if(m_grid.at(i).at(j) == 0) + return true; return false; } -bool Grid::gridIsFull() -{ +std::tuple Grid::getRandomEmptyCellCoord(){ - for (int i = 0; i < m_size ; i++) - { - for (int j = 0; j < m_size ; j++) - { - if (m_table[i][j]->isEmpty()) - return false; - } - } + //Init list of candidate + std::vector > candidates; - 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(""); + //Construct list of candidates + for(int i=0;iisEmpty(i,j)){ + std::tuple currentCandidate(i,j); + candidates.push_back(currentCandidate); } } } - for(int i=0; i<4;i++){ + //If no candidate available + if(candidates.size() == 0) + return std::tuple(-1, -1); - std::cout << "|" << newLine[i]->description() << "|"; + //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*> a=this->swipeLine(m_table.at(0)); - m_table[0]=a; +//Another setCell method +bool Grid::setCell(int i, int j, int value){ + std::tuple coord(i,j); + return this->setCell(coord, value); } + + + + + diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp index 810cb9b..6942188 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -8,32 +8,26 @@ * Date : 29/04/2015 */ #include +#include #include - -//#include "ModelConstants.hpp" -#include "Cell.hpp" -#include "./Elements/StringElement.hpp" +#include class Grid { private: int m_size; - std::vector*> > m_table; + std::vector > m_grid; public: - Grid(int size); + Grid(); ~Grid(); - void show(); + std::string description(); bool isEmpty(int i, int j); - bool gridIsFull(); - int getNRows(); - int getNCols(); - std::vector* > swipeLine(std::vector* > line); - void swipeRight(); - void setCell(int i, int j, Cell *cell); - Cell* getCell(short i, short j); + std::tuple getRandomEmptyCellCoord(); + bool setCell(std::tuple coord, int value); + bool setCell(int i, int j, int value); }; diff --git a/src/main.cpp b/src/main.cpp index 24a6af6..a7abcae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,14 +3,13 @@ #include #include #include +#include //---------------------- //----- Personnal include ----- #include "./Model/Grid.hpp" -#include "./Controllers/ConsoleController/ConsoleController.hpp" //----------------------------- -#include "./Model/Cell.hpp" //#include "./Model/Elements/StringElement.hpp" //----- Start ----- @@ -19,28 +18,29 @@ int main() { - Cell *cell1 = new Cell(""); - Cell *cell2 = new Cell("i"); - - - if(cell2->isEmpty()){ - std::cout << "Empty" << std::endl; - } - else{ - std::cout << "Not empty" << std::endl; - } - //Init random srand(time(NULL)); + + Grid a; + std::cout << a.description(); + std::cout << std::get<0>(a.getRandomEmptyCellCoord()) << ","<< std::get<1>(a.getRandomEmptyCellCoord()); + while(1){ + + std::tuple c(a.getRandomEmptyCellCoord()); + a.setCell(1,2, 15); + std::cout << a.description(); + std::string chaine; + std::cin >> chaine; + } //Init console controller - ConsoleController * controller = new ConsoleController(); + //ConsoleController * controller = new ConsoleController(); //Launch game - controller->play(); + //controller->play(); //Remove controlelr - delete controller; + //delete controller; return 0; }