Restart project

This commit is contained in:
manzerbredes 2015-05-02 18:15:14 +02:00
parent 6b8a144bd1
commit 27d646af15
11 changed files with 100 additions and 165 deletions

View file

@ -3,7 +3,7 @@ project(2P11)
#Assign Modules path
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FGS} -std=c++11")
#Defined project VERSION
set(VERSION_MAJOR 0)
set(VERSION_MINOR 1)

View file

@ -26,7 +26,7 @@ void ConsoleController::play()
//Start game
while (1)
{
std::cout << m_game->isOver();
std::cout << "Game over : " << m_game->isOver() << "fin";
//Get key press
keyPress=this->waitArrowKeyPress();
@ -47,6 +47,7 @@ void ConsoleController::play()
break;
case kbdh::Right:
std::cout << "Keypress : Right" << std::endl;
m_game->swipeRight();
break;
}

View file

@ -1,74 +0,0 @@
#ifndef DEF_CELL
#define DEF_CELL
/* Cell.h
* Defines the class Cell
* A cell represents a cell in the grid
* Creators : krilius, manzerbredes
* Date : 29/04/2015 */
#include <iostream>
template<class T> class Cell
{
private:
T* m_Element;
public:
//Constructor
Cell(std::string value)
{
m_Element=new T();
m_Element->setValue(value);
}
//Destructor
~Cell()
{
delete m_Element;
}
//Test if the cell is empty
bool isEmpty()
{
return this->m_Element->isEmpty();
}
T* getElement(){
return this->m_Element;
}
bool equals(Cell<T> *cell){
if(m_Element->equals(cell->getElement())){
return true;
}
return false;
}
//Return the element value
std::string getElementValue()
{
return m_Element->getValue();
}
// Description
std::string description()
{
return m_Element->description();
}
};
template<class T>
bool operator==(Cell<T> a, Cell<T> b){
return a.equals(&b);
}
#endif

View file

@ -1,2 +0,0 @@
#Make Model lib
add_library(Elements ./StringElement.cpp)

View file

@ -1,42 +0,0 @@
#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(){
if(this->m_value==""){
return " ";
}
return this->m_value;
}
bool StringElement::isEmpty(){
if(this->m_value==""){
return true;
}
return false;
}
bool StringElement::equals(StringElement *element){
if(this->m_value.compare(element->m_value) == 0){
return true;
}
return false;
}

View file

@ -1,29 +0,0 @@
#ifndef _STRINGELEMENT_
#define _STRINGELEMENT_
#include <string>
class StringElement
{
private:
std::string m_value;
public:
StringElement();
~StringElement();
std::string getValue();
void setValue(std::string value);
bool isEmpty();
bool equals(StringElement *element);
std::string description();
};
#endif

View file

@ -33,18 +33,27 @@ void Game::pop()
if (m_grid->isEmpty(i,j))
cellChosen = true;
}
m_grid->setCell(i, j, new Cell<StringElement>("2"));
m_grid->setCell(i, j, new Cell<StringElement>(std::to_string(2)));
}
void Game::swipeRight(){
m_grid->swipeRight();
}
bool Game::isOver()
{
if(m_grid->gridIsFull()){
for(int i=0;i<4;i++){
std::cout << m_grid->getCell(0,i)->description();
for(int i=0;i<m_grid->getNRows();i++){
for(int j=0;j<m_grid->getNCols()-1;j++){
if(m_grid->getCell(i,j)->equals(m_grid->getCell(i,j+1))){
return false;
}
}
}
}
else {
return false;
}
return true;
}

View file

@ -9,6 +9,7 @@
#include <iostream>
#include <cstdlib>
#include <string>
#include "./Elements/StringElement.hpp"
#include "Grid.hpp"
@ -23,6 +24,7 @@ class Game
void pop();
void showGrid();
void swipeRight();
bool isOver();
};

View file

@ -91,3 +91,71 @@ int Grid::getNRows(){
int Grid::getNCols(){
return m_table.size();
}
std::vector<Cell<StringElement>* > Grid::swipeLine(std::vector<Cell<StringElement>* > line){
std::vector<Cell<StringElement>*> newLine = std::vector<Cell<StringElement>*>(4);
for (int j = 0 ; j < 3 ; j++)
{
if(j>3)
break;
Cell<StringElement> * cell = new Cell<StringElement>(line.at(j)->getElementValue());
Cell<StringElement> * cellp1 = new Cell<StringElement>(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<StringElement>("");
else
newLine[j+1] = new Cell<StringElement>(sp1);
newLine[j] = new Cell<StringElement>(s);
j++;
}
else{
if(ap1==0)
newLine[j+1] = new Cell<StringElement>("");
else
newLine[j+1] = new Cell<StringElement>(sp1);
if(a==0)
newLine[j] = new Cell<StringElement>("");
else
newLine[j] = new Cell<StringElement>(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<StringElement>(newLine[j]->getElementValue());
newLine[j]=new Cell<StringElement>("");
}
}
}
for(int i=0; i<4;i++){
std::cout << "|" << newLine[i]->description() << "|";
}
std::cout << "done";
return newLine;
}
void Grid::swipeRight(){
std::vector<Cell<StringElement>*> a=this->swipeLine(m_table.at(0));
m_table[0]=a;
}

View file

@ -17,7 +17,7 @@
class Grid
{
private:
int m_size;
int m_size;
std::vector<std::vector<Cell<StringElement>*> > m_table;
public:
@ -25,12 +25,14 @@ class Grid
~Grid();
void show();
bool isEmpty(int i, int j);
bool gridIsFull();
int getNRows();
int getNCols();
void setCell(int i, int j, Cell<StringElement> *cell);
Cell<StringElement>* getCell(short i, short j);
bool isEmpty(int i, int j);
bool gridIsFull();
int getNRows();
int getNCols();
std::vector<Cell<StringElement>* > swipeLine(std::vector<Cell<StringElement>* > line);
void swipeRight();
void setCell(int i, int j, Cell<StringElement> *cell);
Cell<StringElement>* getCell(short i, short j);
};