Add template support to Cell class
This commit is contained in:
parent
048f1e17b7
commit
683d794679
10 changed files with 106 additions and 67 deletions
|
@ -1,2 +1,7 @@
|
||||||
#Make Model lib
|
#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)
|
||||||
|
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -8,24 +8,49 @@
|
||||||
* Date : 29/04/2015 */
|
* Date : 29/04/2015 */
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class Cell
|
|
||||||
|
template<class T> class Cell
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::string m_value;
|
T* m_Element;
|
||||||
|
|
||||||
public:
|
|
||||||
Cell();
|
|
||||||
Cell(std::string value);
|
|
||||||
~Cell();
|
|
||||||
|
|
||||||
bool isEmpty();
|
public:
|
||||||
bool equals(Cell * otherCell);
|
|
||||||
std::string getValue();
|
//Constructor
|
||||||
|
Cell(std::string value)
|
||||||
// Describes the cell in a terminal
|
{
|
||||||
std::string description();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
2
src/Model/Elements/CMakeLists.txt
Normal file
2
src/Model/Elements/CMakeLists.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
#Make Model lib
|
||||||
|
add_library(Elements ./StringElement.cpp)
|
24
src/Model/Elements/StringElement.cpp
Normal file
24
src/Model/Elements/StringElement.cpp
Normal file
|
@ -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;
|
||||||
|
}
|
26
src/Model/Elements/StringElement.hpp
Normal file
26
src/Model/Elements/StringElement.hpp
Normal file
|
@ -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
|
|
@ -45,7 +45,7 @@ void Game::pop()
|
||||||
cellChosen = true;
|
cellChosen = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_grid->setCell(i, j, new Cell("2"));
|
m_grid->setCell(i, j, new Cell<StringElement>("2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Game::isOver()
|
bool Game::isOver()
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include "./Elements/StringElement.hpp"
|
||||||
#include "Grid.hpp"
|
#include "Grid.hpp"
|
||||||
|
|
||||||
class Game
|
class Game
|
||||||
|
|
|
@ -5,15 +5,15 @@ Grid::Grid(int size)
|
||||||
{
|
{
|
||||||
//Create Vector
|
//Create Vector
|
||||||
m_size = size;
|
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
|
//Init all of line and cell
|
||||||
for(int i = 0 ; i < size ; i++)
|
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++)
|
for (int j = 0 ; j < size ; j++)
|
||||||
{
|
{
|
||||||
Cell * cell = new Cell();
|
Cell<StringElement> * cell = new Cell<StringElement>("");
|
||||||
m_table[i][j] = cell;
|
m_table[i][j] = cell;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ bool Grid::gridIsFull()
|
||||||
return isFull;
|
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)
|
if (i >= 0 && i < m_size && j >= 0 && j < m_size)
|
||||||
{
|
{
|
||||||
|
|
|
@ -12,12 +12,13 @@
|
||||||
|
|
||||||
//#include "ModelConstants.hpp"
|
//#include "ModelConstants.hpp"
|
||||||
#include "Cell.hpp"
|
#include "Cell.hpp"
|
||||||
|
#include "./Elements/StringElement.hpp"
|
||||||
|
|
||||||
class Grid
|
class Grid
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int m_size;
|
int m_size;
|
||||||
std::vector<std::vector<Cell*> > m_table;
|
std::vector<std::vector<Cell<StringElement>*> > m_table;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Grid(int size);
|
Grid(int size);
|
||||||
|
@ -26,7 +27,7 @@ class Grid
|
||||||
|
|
||||||
bool isEmpty(int i, int j);
|
bool isEmpty(int i, int j);
|
||||||
bool gridIsFull();
|
bool gridIsFull();
|
||||||
void setCell(int i, int j, Cell * cell);
|
void setCell(int i, int j, Cell<StringElement> * cell);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue