summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt2
-rw-r--r--src/Controllers/ConsoleController/ConsoleController.cpp3
-rw-r--r--src/Model/Cell.hpp74
-rw-r--r--src/Model/Elements/CMakeLists.txt2
-rw-r--r--src/Model/Elements/StringElement.cpp42
-rw-r--r--src/Model/Elements/StringElement.hpp29
-rw-r--r--src/Model/Game.cpp19
-rw-r--r--src/Model/Game.hpp2
-rw-r--r--src/Model/Grid.cpp68
-rw-r--r--src/Model/Grid.hpp22
-rw-r--r--src/main.cpp2
11 files changed, 100 insertions, 165 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4ab2346..b47e302 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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)
diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp
index 4ee4965..bca3ad8 100644
--- a/src/Controllers/ConsoleController/ConsoleController.cpp
+++ b/src/Controllers/ConsoleController/ConsoleController.cpp
@@ -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;
}
diff --git a/src/Model/Cell.hpp b/src/Model/Cell.hpp
deleted file mode 100644
index 3371fda..0000000
--- a/src/Model/Cell.hpp
+++ /dev/null
@@ -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
-
diff --git a/src/Model/Elements/CMakeLists.txt b/src/Model/Elements/CMakeLists.txt
deleted file mode 100644
index ef31cd1..0000000
--- a/src/Model/Elements/CMakeLists.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-#Make Model lib
-add_library(Elements ./StringElement.cpp)
diff --git a/src/Model/Elements/StringElement.cpp b/src/Model/Elements/StringElement.cpp
deleted file mode 100644
index a197c22..0000000
--- a/src/Model/Elements/StringElement.cpp
+++ /dev/null
@@ -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;
-}
diff --git a/src/Model/Elements/StringElement.hpp b/src/Model/Elements/StringElement.hpp
deleted file mode 100644
index 55c5473..0000000
--- a/src/Model/Elements/StringElement.hpp
+++ /dev/null
@@ -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
diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp
index 3054990..7dc32c6 100644
--- a/src/Model/Game.cpp
+++ b/src/Model/Game.cpp
@@ -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;
}
diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp
index 0666191..c1c636a 100644
--- a/src/Model/Game.hpp
+++ b/src/Model/Game.hpp
@@ -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();
};
diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp
index b6ef8c4..6fc41c6 100644
--- a/src/Model/Grid.cpp
+++ b/src/Model/Grid.cpp
@@ -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;
+}
diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp
index b7c1f9f..810cb9b 100644
--- a/src/Model/Grid.hpp
+++ b/src/Model/Grid.hpp
@@ -17,21 +17,23 @@
class Grid
{
private:
- int m_size;
+ int m_size;
std::vector<std::vector<Cell<StringElement>*> > m_table;
-
+
public:
Grid(int size);
~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);
+
};
diff --git a/src/main.cpp b/src/main.cpp
index cfb0c7d..24a6af6 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -19,7 +19,7 @@
int main()
{
- Cell<StringElement> *cell1 = new Cell<StringElement>("");
+ Cell<StringElement> *cell1 = new Cell<StringElement>("");
Cell<StringElement> *cell2 = new Cell<StringElement>("i");