summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormanzerbredes <loic.guegan_secondary@yahoo.fr>2015-05-01 15:50:10 +0200
committermanzerbredes <loic.guegan_secondary@yahoo.fr>2015-05-01 15:50:10 +0200
commit683d7946798634c35df165bf40a97d78f947751c (patch)
tree6dd80b60ed63ec92d7b6e7f291079493f64f219b
parent048f1e17b752d2af53db82c1861002283fc300fa (diff)
Add template support to Cell class
-rw-r--r--src/Model/CMakeLists.txt7
-rw-r--r--src/Model/Cell.cpp44
-rw-r--r--src/Model/Cell.hpp53
-rw-r--r--src/Model/Elements/CMakeLists.txt2
-rw-r--r--src/Model/Elements/StringElement.cpp24
-rw-r--r--src/Model/Elements/StringElement.hpp26
-rw-r--r--src/Model/Game.cpp2
-rw-r--r--src/Model/Game.hpp2
-rw-r--r--src/Model/Grid.cpp8
-rw-r--r--src/Model/Grid.hpp5
10 files changed, 106 insertions, 67 deletions
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 <iostream>
-#include <string>
-class Cell
+
+template<class T> 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 <string>
+
+
+
+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<StringElement>("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 <iostream>
#include <cstdlib>
-
+#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<std::vector<Cell*> >(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*>(size);
+ m_table[i] = std::vector<Cell<StringElement>*>(size);
for (int j = 0 ; j < size ; j++)
{
- Cell * cell = new Cell();
+ Cell<StringElement> * cell = new Cell<StringElement>("");
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<StringElement> *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<std::vector<Cell*> > m_table;
+ std::vector<std::vector<Cell<StringElement>*> > 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<StringElement> * cell);
};