From 9fbd4e0fedea42a206843981dffeb87095797581 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 29 Apr 2015 19:34:01 +0200 Subject: Enable cmake for model --- src/Model/CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/Model/CMakeLists.txt (limited to 'src/Model/CMakeLists.txt') diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt new file mode 100644 index 0000000..c9cfb6e --- /dev/null +++ b/src/Model/CMakeLists.txt @@ -0,0 +1,8 @@ +#Retrieve crypto++ libraries +get_property(SFML_LIBRARIES GLOBAL PROPERTY SFML_LIBRARIES) + +#Make CryptClass lib +add_library(Model ./Cell.cpp ./Game.cpp ./Grid.cpp) + +#Add crypto++ to CryptClass +target_link_libraries(Model ${SFML_LIBRARIES}) -- cgit v1.2.3 From ad216d86f6573e3575e85f3a2941f6f34b5b1c0e Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 29 Apr 2015 21:18:06 +0200 Subject: Add missing dependency in CMakeList.txt --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 2 +- src/Model/CMakeLists.txt | 10 ++-------- src/main.cpp | 2 +- 4 files changed, 5 insertions(+), 11 deletions(-) (limited to 'src/Model/CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index c822dc4..4ab2346 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,5 +14,5 @@ set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REV}") cmake_minimum_required(VERSION 2.6) #Add source directory -add_subdirectory(src) +add_subdirectory(./src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a946644..89e46d5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,6 @@ 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}) +target_link_libraries(2P11 ${SFML_LIBRARIES} Model) add_subdirectory(./Model) diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index c9cfb6e..c685c7a 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -1,8 +1,2 @@ -#Retrieve crypto++ libraries -get_property(SFML_LIBRARIES GLOBAL PROPERTY SFML_LIBRARIES) - -#Make CryptClass lib -add_library(Model ./Cell.cpp ./Game.cpp ./Grid.cpp) - -#Add crypto++ to CryptClass -target_link_libraries(Model ${SFML_LIBRARIES}) +#Make Model lib +add_library(Model Grid.cpp Cell.cpp Game.cpp) diff --git a/src/main.cpp b/src/main.cpp index 12f6c6d..00473f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include #include -#include "Model/Grid.hpp" +#include "./Model/Grid.hpp" int main() { -- cgit v1.2.3 From 683d7946798634c35df165bf40a97d78f947751c Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Fri, 1 May 2015 15:50:10 +0200 Subject: Add template support to Cell class --- src/Model/CMakeLists.txt | 7 ++++- src/Model/Cell.cpp | 44 ------------------------------ src/Model/Cell.hpp | 53 ++++++++++++++++++++++++++---------- src/Model/Elements/CMakeLists.txt | 2 ++ src/Model/Elements/StringElement.cpp | 24 ++++++++++++++++ src/Model/Elements/StringElement.hpp | 26 ++++++++++++++++++ src/Model/Game.cpp | 2 +- src/Model/Game.hpp | 2 +- src/Model/Grid.cpp | 8 +++--- src/Model/Grid.hpp | 5 ++-- 10 files changed, 106 insertions(+), 67 deletions(-) delete mode 100644 src/Model/Cell.cpp create mode 100644 src/Model/Elements/CMakeLists.txt create mode 100644 src/Model/Elements/StringElement.cpp create mode 100644 src/Model/Elements/StringElement.hpp (limited to 'src/Model/CMakeLists.txt') diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index c685c7a..284be94 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -1,2 +1,7 @@ #Make Model lib -add_library(Model Grid.cpp Cell.cpp Game.cpp) +add_library(Model Grid.cpp Game.cpp) + +target_link_libraries(Model Elements) + +add_subdirectory(./Elements) + diff --git a/src/Model/Cell.cpp b/src/Model/Cell.cpp deleted file mode 100644 index ba4bbbd..0000000 --- a/src/Model/Cell.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "Cell.hpp" - -// Constructors - -Cell::Cell() -{ - m_value = "."; -} - -Cell::Cell(std::string value) -{ - m_value = value; -} - -// Destructor - -Cell::~Cell() -{ -} - -// Getters and setters - -bool Cell::isEmpty() -{ - return (m_value == "."); -} - -std::string Cell::getValue() -{ - return m_value; -} - -bool Cell::equals(Cell *otherCell) -{ - return (m_value == otherCell->getValue()); -} - -// Description - -std::string Cell::description() -{ - return m_value; -} - diff --git a/src/Model/Cell.hpp b/src/Model/Cell.hpp index badd070..7638c4e 100644 --- a/src/Model/Cell.hpp +++ b/src/Model/Cell.hpp @@ -8,24 +8,49 @@ * Date : 29/04/2015 */ #include -#include -class Cell + +template class Cell { private: - std::string m_value; - + T* m_Element; + public: - Cell(); - Cell(std::string value); - ~Cell(); - - bool isEmpty(); - bool equals(Cell * otherCell); - std::string getValue(); - - // Describes the cell in a terminal - std::string description(); + + //Constructor + Cell(std::string value) + { + m_Element=new T(); + m_Element->setValue(value); + } + + //Destructor + ~Cell() + { + delete m_Element; + } + + + bool isEmpty() + { + return true; + } + + std::string getElementValue() + { + return m_Element->getValue(); + } + + bool equals(Cell *otherCell) + { + return true; + } + + // Description + std::string description() + { + return m_Element->description(); + } }; diff --git a/src/Model/Elements/CMakeLists.txt b/src/Model/Elements/CMakeLists.txt new file mode 100644 index 0000000..ef31cd1 --- /dev/null +++ b/src/Model/Elements/CMakeLists.txt @@ -0,0 +1,2 @@ +#Make Model lib +add_library(Elements ./StringElement.cpp) diff --git a/src/Model/Elements/StringElement.cpp b/src/Model/Elements/StringElement.cpp new file mode 100644 index 0000000..f93fe3b --- /dev/null +++ b/src/Model/Elements/StringElement.cpp @@ -0,0 +1,24 @@ +#include "./StringElement.hpp" + + +StringElement::StringElement(){ + this->m_value="."; +} + +StringElement::~StringElement(){ + +} + + + +std::string StringElement::getValue(){ + return this->m_value; +} + +void StringElement::setValue(std::string value){ + this->m_value=value; +} + +std::string StringElement::description(){ + return this->m_value; +} diff --git a/src/Model/Elements/StringElement.hpp b/src/Model/Elements/StringElement.hpp new file mode 100644 index 0000000..db40f58 --- /dev/null +++ b/src/Model/Elements/StringElement.hpp @@ -0,0 +1,26 @@ +#ifndef _STRINGELEMENT_ +#define _STRINGELEMENT_ + + + + +#include + + + +class StringElement +{ + private: + std::string m_value; + + public: + StringElement(); + ~StringElement(); + + std::string getValue(); + void setValue(std::string value); + + std::string description(); +}; + +#endif diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index 069a481..c2252ff 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -45,7 +45,7 @@ void Game::pop() cellChosen = true; } - m_grid->setCell(i, j, new Cell("2")); + m_grid->setCell(i, j, new Cell("2")); } bool Game::isOver() diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp index fb09188..bbdcfcc 100644 --- a/src/Model/Game.hpp +++ b/src/Model/Game.hpp @@ -9,7 +9,7 @@ #include #include - +#include "./Elements/StringElement.hpp" #include "Grid.hpp" class Game diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index cc9cdb1..103b95f 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -5,15 +5,15 @@ Grid::Grid(int size) { //Create Vector m_size = size; - m_table = std::vector >(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); + m_table[i] = std::vector*>(size); for (int j = 0 ; j < size ; j++) { - Cell * cell = new Cell(); + Cell * cell = new Cell(""); m_table[i][j] = cell; } } @@ -72,7 +72,7 @@ bool Grid::gridIsFull() return isFull; } -void Grid::setCell(int i, int j, Cell *cell) +void Grid::setCell(int i, int j, Cell *cell) { if (i >= 0 && i < m_size && j >= 0 && j < m_size) { diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp index c431cb3..a291cc8 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -12,12 +12,13 @@ //#include "ModelConstants.hpp" #include "Cell.hpp" +#include "./Elements/StringElement.hpp" class Grid { private: int m_size; - std::vector > m_table; + std::vector*> > m_table; public: Grid(int size); @@ -26,7 +27,7 @@ class Grid bool isEmpty(int i, int j); bool gridIsFull(); - void setCell(int i, int j, Cell * cell); + void setCell(int i, int j, Cell * cell); }; -- cgit v1.2.3 From 01b25accba1a5329e220aa647255d2c2b284c16e Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Fri, 1 May 2015 18:19:32 +0200 Subject: Add operator overload squeleton --- src/Model/CMakeLists.txt | 2 +- src/Model/Cell.hpp | 30 +++++++++++++++++++++++------- src/Model/Elements/StringElement.cpp | 25 ++++++++++++++++++++++++- src/Model/Elements/StringElement.hpp | 3 +++ src/main.cpp | 15 +++++++++++++++ 5 files changed, 66 insertions(+), 9 deletions(-) (limited to 'src/Model/CMakeLists.txt') diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index 284be94..80ea875 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -1,5 +1,5 @@ #Make Model lib -add_library(Model Grid.cpp Game.cpp) +add_library(Model Grid.cpp Game.cpp ) target_link_libraries(Model Elements) diff --git a/src/Model/Cell.hpp b/src/Model/Cell.hpp index 7638c4e..cb4bc21 100644 --- a/src/Model/Cell.hpp +++ b/src/Model/Cell.hpp @@ -24,35 +24,51 @@ template class Cell m_Element->setValue(value); } + //Destructor ~Cell() { delete m_Element; } - + //Test if the cell is empty bool isEmpty() { return true; } - std::string getElementValue() - { - return m_Element->getValue(); + T getElement(){ + return this->m_Element; } - bool equals(Cell *otherCell) - { + bool equals(Cell *cell){ + /*if(cell->getElement() == this->m_Element){ + return true; + }*/ return true; } + //Return the element value + std::string getElementValue() + { + return m_Element->getValue(); + } + + // Description std::string description() { return m_Element->description(); } - + }; + + +template +bool operator==(Cell a, Cell b){ + return true; +} + #endif diff --git a/src/Model/Elements/StringElement.cpp b/src/Model/Elements/StringElement.cpp index f93fe3b..fbf57c1 100644 --- a/src/Model/Elements/StringElement.cpp +++ b/src/Model/Elements/StringElement.cpp @@ -2,7 +2,7 @@ StringElement::StringElement(){ - this->m_value="."; + this->m_value=""; } StringElement::~StringElement(){ @@ -20,5 +20,28 @@ void StringElement::setValue(std::string value){ } std::string StringElement::description(){ + if(this->m_value==""){ + return " "; + } return this->m_value; } + +bool StringElement::isEmpty(){ + if(this->m_value==""){ + return true; + } + + return false; +} +bool StringElement::equals(StringElement const& element) const{ + if(this->m_value.compare(element.m_value) == 0){ + return true; + } + + return true; +} + +bool operator==(StringElement const& a, StringElement const& b){ + return a.equals(b); +} + diff --git a/src/Model/Elements/StringElement.hpp b/src/Model/Elements/StringElement.hpp index db40f58..16946a8 100644 --- a/src/Model/Elements/StringElement.hpp +++ b/src/Model/Elements/StringElement.hpp @@ -20,7 +20,10 @@ class StringElement std::string getValue(); void setValue(std::string value); + bool isEmpty(); + bool equals(StringElement const& element) const; std::string description(); + }; #endif diff --git a/src/main.cpp b/src/main.cpp index 26062a9..217fe81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,11 +10,26 @@ #include "./Controllers/ConsoleController/ConsoleController.hpp" //----------------------------- +#include "./Model/Cell.hpp" +//#include "./Model/Elements/StringElement.hpp" //----- Start ----- + + int main() { + Cell cell1("loic"); + Cell cell2("loic"); + + + if(cell1==cell2){ + std::cout << "Egale" << std::endl; + } + else{ + std::cout << "Différent" << std::endl; + } + //Init random srand(time(NULL)); -- cgit v1.2.3 From 36d033caeebd8ccbddf711825a5a96e3930438be Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sat, 2 May 2015 19:26:01 +0200 Subject: Make clean code --- src/CMakeLists.txt | 4 +- src/Model/CMakeLists.txt | 7 +- src/Model/Game.cpp | 54 +------------- src/Model/Game.hpp | 8 +- src/Model/Grid.cpp | 187 ++++++++++++++++------------------------------- src/Model/Grid.hpp | 22 ++---- src/main.cpp | 32 ++++---- 7 files changed, 93 insertions(+), 221 deletions(-) (limited to 'src/Model/CMakeLists.txt') 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() -{ - 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))); +Game::Game() : m_grid(){ } -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); - - //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; +Grid::Grid(): m_size(4), m_grid(4){ + + //Init all cells + for(int i=0;idescription() << " |"; - } - 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 *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* Grid::getCell(short i, short j){ - return m_table[i][j]; -} +std::tuple Grid::getRandomEmptyCellCoord(){ -int Grid::getNRows(){ - return m_table[0].size(); -} + //Init list of candidate + std::vector > candidates; -int Grid::getNCols(){ - return m_table.size(); -} + //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); -std::vector* > Grid::swipeLine(std::vector* > line){ - std::vector*> newLine = std::vector*>(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 * 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); +//Change value of cell +bool Grid::setCell(std::tuple coord, int value){ + int i=std::get<0>(coord); + int j=std::get<1>(coord); - } - delete cell; - delete cellp1; - + if(i>=0 && i=0 && j 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(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; -} 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; } -- cgit v1.2.3