From 590f736a7a9d7b4b185d15cd01eb0dde3d7edaa0 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 29 Apr 2015 14:34:26 +0200 Subject: Add class --- src/Model/Game.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/Model/Game.cpp (limited to 'src/Model/Game.cpp') diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From a3b805ee7b96d1d693c4108f0104650cfd60b565 Mon Sep 17 00:00:00 2001 From: krilius Date: Wed, 29 Apr 2015 22:28:29 +0400 Subject: first step of the model --- src/Model/Cell.cpp | 19 +++++++- src/Model/Cell.hpp | 4 ++ src/Model/Game.cpp | 54 ++++++++++++++++++++++ src/Model/Game.hpp | 29 ++++++++++++ src/Model/Grid.cpp | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/Model/Grid.hpp | 17 ++++++- 6 files changed, 243 insertions(+), 8 deletions(-) create mode 100644 src/Model/Game.hpp (limited to 'src/Model/Game.cpp') diff --git a/src/Model/Cell.cpp b/src/Model/Cell.cpp index 22eaafe..ba4bbbd 100644 --- a/src/Model/Cell.cpp +++ b/src/Model/Cell.cpp @@ -4,7 +4,7 @@ Cell::Cell() { - m_value = " "; + m_value = "."; } Cell::Cell(std::string value) @@ -18,6 +18,23 @@ 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() diff --git a/src/Model/Cell.hpp b/src/Model/Cell.hpp index 7775bf5..badd070 100644 --- a/src/Model/Cell.hpp +++ b/src/Model/Cell.hpp @@ -20,6 +20,10 @@ class Cell Cell(std::string value); ~Cell(); + bool isEmpty(); + bool equals(Cell * otherCell); + std::string getValue(); + // Describes the cell in a terminal std::string description(); diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index e69de29..1e24b11 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -0,0 +1,54 @@ +#include "Game.hpp" + +Game::Game() +{ + m_grid = new Grid(4); +} + +Game::~Game() +{ + delete m_grid; +} + +void Game::play() +{ + while(!m_grid->gridIsFull()) + { + m_grid->show(); + + pop(); + std::cout << std::endl; + } + + m_grid->show(); +} + +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("2")); +} + +bool Game::isOver() +{ + return m_grid->gridIsFull(); +} \ No newline at end of file diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp new file mode 100644 index 0000000..8dcaf64 --- /dev/null +++ b/src/Model/Game.hpp @@ -0,0 +1,29 @@ +#ifndef DEF_GAME +#define DEF_GAME + +/* Game.h + * Defines the class Game + * A game allows a player to play. It contains a grid and pops numbers + * Creators : krilius, manzerbredes + * Date : 29/04/2015 */ + +#include + +#include "Grid.hpp" + +class Game +{ + private: + Grid * m_grid; + + public: + Game(); + ~Game(); + + void play(); + void pop(); + void showGrid(); + bool isOver(); +}; + +#endif \ No newline at end of file diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index ade3f74..295b9b4 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -3,6 +3,7 @@ Grid::Grid(int size) { + m_size = size; m_table = std::vector >(size); for(int i = 0 ; i < size ; i++) { @@ -17,18 +18,18 @@ Grid::Grid(int size) Grid::~Grid() { - for(int i = 0 ; i < m_table.size() ; i++) + for(int i = 0 ; i < m_size ; i++) { - for(int j = 0 ; j < m_table[i].size() ; j++) + for(int j = 0 ; j < m_size ; j++) delete m_table[i][j]; } } -void Grid::afficher() +void Grid::show() { - for(int i = 0 ; i < m_table.size() ; i++) + for(int i = 0 ; i < m_size ; i++) { - for(int j = 0 ; j < m_table[i].size() ; j++) + for(int j = 0 ; j < m_size ; j++) { std::cout << m_table[i][j]->description(); } @@ -36,3 +37,120 @@ void Grid::afficher() } } +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; +} + +bool Grid::gridIsFull() +{ + bool isFull = true; + + for (int i = 0; i < m_size && isFull; i++) + { + for (int j = 0; j < m_size && isFull; j++) + { + if (m_table[i][j]->isEmpty()) + isFull = false; + } + } + + return isFull; +} + +void Grid::setCell(int i, int j, Cell *cell) +{ + if (i >= 0 && i < m_size && j >= 0 && j < m_size) + { + delete m_table[i][j]; + m_table[i][j] = cell; + } +} + + +void Grid::move(Direction direction) +{ + switch (direction) + { + case Direction::UP: + + break; + + case Direction::DOWN: + break; + + case Direction::LEFT: + break; + + case Direction::RIGHT: + break; + + default: + break; + } +} + +void Grid::moveDown() +{/* + for (int i = 0 ; i < m_size; i++) + { + // If the column is full, check the next column + bool columnIsFull = true; + + for (int j = 0; j < m_size; j++) + { + if (m_table[j][i]->isEmpty()) + { + columnIsFull = false; + break; + } + } + + while (!columnIsFull) + { + // Calculate the first line to merge + int firstLine = m_size - 1; + while (m_table[firstLine][i]->isEmpty() && firstLine > 0) + firstLine--; + if (firstLine == 0) + break; + + // Calculate the second line to merge + int secondLine = firstLine - 1; + while (m_table[secondLine][i]->isEmpty() && secondLine > 0) + secondLine--; + + // If there is only one element, pull it down + if (m_table[secondLine][i]->isEmpty() && firstLine != m_size - 1) + { + Cell * originalCell = m_table[firstLine][i]; + Cell * finalCell = m_table[m_size-1][i]; + + m_table[firstLine][i] = finalCell; + m_table[m_size-1][i] = originalCell; + + break; + } + + // If there is only one element which is at the full bottom, break the loop + if (m_table[secondLine][i]->isEmpty()) + break; + + // If there are two "good" elements, begin the merge process + Cell * cell1 = m_table[firstLine][i]; + Cell * cell2 = m_table[secondLine][i]; + + Cell * mergedCell = NULL; + + // If the two cells are the same, merge them + if (cell1->equals(cell2)) + { + int value = std::stoi(cell1->getValue()); + mergedCell = new Cell(std::to_string(value)); + } + } + }*/ +} diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp index e3bc888..2a28559 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -10,17 +10,30 @@ #include #include +#include "ModelConstants.hpp" #include "Cell.hpp" class Grid { private: - std::vector > m_table; + int m_size; + std::vector > m_table; + + void moveUp(); + void moveDown(); + void moveLeft(); + void moveRight(); public: Grid(int size); ~Grid(); - void afficher(); + void show(); + + bool isEmpty(int i, int j); + bool gridIsFull(); + void setCell(int i, int j, Cell * cell); + + void move(Direction direction); }; -- cgit v1.2.3 From a2ed149069a29212a11eb4a1fa676e6f27d941fd Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Thu, 30 Apr 2015 09:13:23 +0200 Subject: Fix whitespace --- src/Model/Game.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/Model/Game.cpp') diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index 1e24b11..aa3626c 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -15,11 +15,11 @@ void Game::play() while(!m_grid->gridIsFull()) { m_grid->show(); - + pop(); std::cout << std::endl; } - + m_grid->show(); } @@ -32,23 +32,23 @@ void Game::showGrid() 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("2")); } bool Game::isOver() { return m_grid->gridIsFull(); -} \ No newline at end of file +} -- cgit v1.2.3 From 40e0b381c1bb5b8c8274f9149808c2929b16dfa2 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Fri, 1 May 2015 10:20:19 +0200 Subject: Correct tab --- src/Model/Game.cpp | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'src/Model/Game.cpp') diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index aa3626c..069a481 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -2,53 +2,53 @@ Game::Game() { - m_grid = new Grid(4); + m_grid = new Grid(4); } Game::~Game() { - delete m_grid; + delete m_grid; } void Game::play() { - while(!m_grid->gridIsFull()) - { - m_grid->show(); + while(!m_grid->gridIsFull()) + { + m_grid->show(); - pop(); - std::cout << std::endl; - } + pop(); + std::cout << std::endl; + } - m_grid->show(); + m_grid->show(); } void Game::showGrid() { - m_grid->show(); - std::cout << std::endl; + m_grid->show(); + std::cout << std::endl; } void Game::pop() { - bool cellChosen = false; + bool cellChosen = false; - int i; - int j; + int i; + int j; - while(!cellChosen) - { - i = rand() % 4; - j = rand() % 4; + while(!cellChosen) + { + i = rand() % 4; + j = rand() % 4; - if (m_grid->isEmpty(i,j)) - cellChosen = true; - } + if (m_grid->isEmpty(i,j)) + cellChosen = true; + } - m_grid->setCell(i, j, new Cell("2")); + m_grid->setCell(i, j, new Cell("2")); } bool Game::isOver() { - return m_grid->gridIsFull(); + return m_grid->gridIsFull(); } -- 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/Game.cpp') 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 6b8a144bd192de206e11a171841ec6161d11b6aa Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sat, 2 May 2015 11:06:54 +0200 Subject: Correct bugs --- .gitignore | 1 + .../ConsoleController/ConsoleController.cpp | 3 ++- src/Model/Cell.hpp | 12 ++++++------ src/Model/Elements/StringElement.cpp | 11 +++-------- src/Model/Elements/StringElement.hpp | 2 +- src/Model/Game.cpp | 20 ++++++++------------ src/Model/Game.hpp | 3 +-- src/Model/Grid.cpp | 18 +++++++++++++----- src/Model/Grid.hpp | 5 ++++- src/main.cpp | 10 +++++----- 10 files changed, 44 insertions(+), 41 deletions(-) (limited to 'src/Model/Game.cpp') diff --git a/.gitignore b/.gitignore index 766f778..5609314 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ CMakeCache.txt *.app 2P11 clear.sh +*.vim diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp index 4be0375..4ee4965 100644 --- a/src/Controllers/ConsoleController/ConsoleController.cpp +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -24,8 +24,9 @@ void ConsoleController::play() m_game->showGrid(); //Start game - while (!m_game->isOver()) + while (1) { + std::cout << m_game->isOver(); //Get key press keyPress=this->waitArrowKeyPress(); diff --git a/src/Model/Cell.hpp b/src/Model/Cell.hpp index cb4bc21..3371fda 100644 --- a/src/Model/Cell.hpp +++ b/src/Model/Cell.hpp @@ -34,18 +34,18 @@ template class Cell //Test if the cell is empty bool isEmpty() { - return true; + return this->m_Element->isEmpty(); } - T getElement(){ + T* getElement(){ return this->m_Element; } bool equals(Cell *cell){ - /*if(cell->getElement() == this->m_Element){ + if(m_Element->equals(cell->getElement())){ return true; - }*/ - return true; + } + return false; } //Return the element value @@ -67,7 +67,7 @@ template class Cell template bool operator==(Cell a, Cell b){ - return true; + return a.equals(&b); } #endif diff --git a/src/Model/Elements/StringElement.cpp b/src/Model/Elements/StringElement.cpp index fbf57c1..a197c22 100644 --- a/src/Model/Elements/StringElement.cpp +++ b/src/Model/Elements/StringElement.cpp @@ -33,15 +33,10 @@ bool StringElement::isEmpty(){ return false; } -bool StringElement::equals(StringElement const& element) const{ - if(this->m_value.compare(element.m_value) == 0){ +bool StringElement::equals(StringElement *element){ + 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); + return false; } - diff --git a/src/Model/Elements/StringElement.hpp b/src/Model/Elements/StringElement.hpp index 16946a8..55c5473 100644 --- a/src/Model/Elements/StringElement.hpp +++ b/src/Model/Elements/StringElement.hpp @@ -21,7 +21,7 @@ class StringElement void setValue(std::string value); bool isEmpty(); - bool equals(StringElement const& element) const; + bool equals(StringElement *element); std::string description(); }; diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index c2252ff..3054990 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -10,18 +10,7 @@ Game::~Game() delete m_grid; } -void Game::play() -{ - while(!m_grid->gridIsFull()) - { - m_grid->show(); - pop(); - std::cout << std::endl; - } - - m_grid->show(); -} void Game::showGrid() { @@ -50,5 +39,12 @@ void Game::pop() bool Game::isOver() { - return m_grid->gridIsFull(); + if(m_grid->gridIsFull()){ + for(int i=0;i<4;i++){ + std::cout << m_grid->getCell(0,i)->description(); + } + + } + + return true; } diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp index bbdcfcc..0666191 100644 --- a/src/Model/Game.hpp +++ b/src/Model/Game.hpp @@ -15,13 +15,12 @@ class Game { private: - Grid * m_grid; + Grid *m_grid; public: Game(); ~Game(); - void play(); void pop(); void showGrid(); bool isOver(); diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index 103b95f..b6ef8c4 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -58,18 +58,17 @@ bool Grid::isEmpty(int i, int j) bool Grid::gridIsFull() { - bool isFull = true; - for (int i = 0; i < m_size && isFull; i++) + for (int i = 0; i < m_size ; i++) { - for (int j = 0; j < m_size && isFull; j++) + for (int j = 0; j < m_size ; j++) { if (m_table[i][j]->isEmpty()) - isFull = false; + return false; } } - return isFull; + return true; } void Grid::setCell(int i, int j, Cell *cell) @@ -81,5 +80,14 @@ void Grid::setCell(int i, int j, Cell *cell) } } +Cell* Grid::getCell(short i, short j){ + return m_table[i][j]; +} +int Grid::getNRows(){ + return m_table[0].size(); +} +int Grid::getNCols(){ + return m_table.size(); +} diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp index a291cc8..b7c1f9f 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -27,7 +27,10 @@ class Grid bool isEmpty(int i, int j); bool gridIsFull(); - void setCell(int i, int j, Cell * cell); + int getNRows(); + int getNCols(); + void setCell(int i, int j, Cell *cell); + Cell* getCell(short i, short j); }; diff --git a/src/main.cpp b/src/main.cpp index 217fe81..cfb0c7d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,15 +19,15 @@ int main() { - Cell cell1("loic"); - Cell cell2("loic"); + Cell *cell1 = new Cell(""); + Cell *cell2 = new Cell("i"); - if(cell1==cell2){ - std::cout << "Egale" << std::endl; + if(cell2->isEmpty()){ + std::cout << "Empty" << std::endl; } else{ - std::cout << "Différent" << std::endl; + std::cout << "Not empty" << std::endl; } //Init random -- cgit v1.2.3 From 27d646af15bc9147a141aced8cebd30668de9a8e Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sat, 2 May 2015 18:15:14 +0200 Subject: Restart project --- CMakeLists.txt | 2 +- .../ConsoleController/ConsoleController.cpp | 3 +- src/Model/Cell.hpp | 74 ---------------------- src/Model/Elements/CMakeLists.txt | 2 - src/Model/Elements/StringElement.cpp | 42 ------------ src/Model/Elements/StringElement.hpp | 29 --------- src/Model/Game.cpp | 19 ++++-- src/Model/Game.hpp | 2 + src/Model/Grid.cpp | 68 ++++++++++++++++++++ src/Model/Grid.hpp | 22 ++++--- src/main.cpp | 2 +- 11 files changed, 100 insertions(+), 165 deletions(-) delete mode 100644 src/Model/Cell.hpp delete mode 100644 src/Model/Elements/CMakeLists.txt delete mode 100644 src/Model/Elements/StringElement.cpp delete mode 100644 src/Model/Elements/StringElement.hpp (limited to 'src/Model/Game.cpp') 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 - - -template 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 *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 -bool operator==(Cell a, Cell 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 - - - -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("2")); + m_grid->setCell(i, j, new Cell(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;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; } 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 #include +#include #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* > Grid::swipeLine(std::vector* > line){ + std::vector*> newLine = std::vector*>(4); + + + 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); + + } + 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(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 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*> > 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 *cell); - Cell* getCell(short i, short j); - + + 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); + }; 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 *cell1 = new Cell(""); + Cell *cell1 = new Cell(""); Cell *cell2 = new Cell("i"); -- 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/Game.cpp') 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 From 1d09a0fd3ae35ccf51a3b5f929f77a8c8850712c Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sat, 2 May 2015 22:57:08 +0200 Subject: End console clean game --- src/CMakeLists.txt | 4 +- .../ConsoleController/ConsoleController.cpp | 53 +++---- .../ConsoleController/ConsoleController.hpp | 4 +- src/Model/Game.cpp | 40 +++++ src/Model/Game.hpp | 8 +- src/Model/Grid.cpp | 170 ++++++++++++++++++++- src/Model/Grid.hpp | 19 +++ src/main.cpp | 28 +--- 8 files changed, 258 insertions(+), 68 deletions(-) (limited to 'src/Model/Game.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ed13eb5..da444d2 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 ) +target_link_libraries(2P11 ${SFML_LIBRARIES} Model ConsoleController) add_subdirectory(./Model) -#add_subdirectory(./Controllers/) +add_subdirectory(./Controllers/) diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp index bca3ad8..d91e807 100644 --- a/src/Controllers/ConsoleController/ConsoleController.cpp +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -4,62 +4,45 @@ ConsoleController::ConsoleController() { - m_game = new Game(); } ConsoleController::~ConsoleController() { - delete m_game; } -void ConsoleController::play() +void ConsoleController::run() { - //Intruction msg - std::cout << "Use arrows to play !" << std::endl; //Init keyPress kbdh::Direction keyPress; - //Display the first grid - m_game->showGrid(); + //Intruction msg + std::cout << "Use arrows to play !" << std::endl; + + //Pop a random number on the grid + m_game.popRandomNumber(); + + //First cout grid + m_game.coutGrid(); + //Start game - while (1) + while (!m_game.isOver()) { - std::cout << "Game over : " << m_game->isOver() << "fin"; //Get key press keyPress=this->waitArrowKeyPress(); - //New line for the console print arrow press - std::cout << std::endl; - - - //Print keyPress - switch(keyPress){ - case kbdh::Up: - std::cout << "Keypress : Up" << std::endl; - break; - case kbdh::Down: - std::cout << "Keypress : Down" << std::endl; - break; - case kbdh::Left: - std::cout << "Keypress : Left" << std::endl; - break; - case kbdh::Right: - std::cout << "Keypress : Right" << std::endl; - m_game->swipeRight(); - break; - } - - //Show the Grid - m_game->showGrid(); - std::cout << std::endl; + //Apply move + m_game.swipe(keyPress); + //Pop a random number on the grid + m_game.popRandomNumber(); - //Pop new number - m_game->pop(); + //Cout grid + m_game.coutGrid(); } + m_game.coutGrid(); } diff --git a/src/Controllers/ConsoleController/ConsoleController.hpp b/src/Controllers/ConsoleController/ConsoleController.hpp index 6ce8df9..8d73f79 100644 --- a/src/Controllers/ConsoleController/ConsoleController.hpp +++ b/src/Controllers/ConsoleController/ConsoleController.hpp @@ -15,12 +15,12 @@ class ConsoleController { private: - Game * m_game; + Game m_game; kbdh::Direction waitArrowKeyPress(); public: ConsoleController(); ~ConsoleController(); - void play(); + void run(); }; #endif diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index 32482b7..6039bc9 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -7,3 +7,43 @@ Game::Game() : m_grid(){ Game::~Game(){ } + + + +bool Game::swipe(kbdh::Direction direction){ + + switch(direction){ + + case kbdh::Left: + m_grid.swipeLeft(); + break; + case kbdh::Right: + m_grid.swipeRight(); + break; + case kbdh::Up: + m_grid.swipeUp(); + break; + case kbdh::Down: + m_grid.swipeDown(); + break; + } + + return true; +} + + +void Game::coutGrid(){ + std::cout << m_grid.description(); +} + +bool Game::isOver(){ + return m_grid.isOver(); +} + +void Game::popRandomNumber(){ + std::tuple coord(m_grid.getRandomEmptyCellCoord()); + + int number=2; + + m_grid.setCell(coord, number); +} diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp index ee3a6a9..ab19340 100644 --- a/src/Model/Game.hpp +++ b/src/Model/Game.hpp @@ -9,7 +9,9 @@ #include #include +#include "../Helpers/Keyboard.hpp" #include "Grid.hpp" +#include class Game { @@ -19,7 +21,11 @@ class Game public: Game(); ~Game(); - + + bool swipe(kbdh::Direction direction); + void coutGrid(); + void popRandomNumber(); + bool isOver(); }; #endif diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index 92837f6..3a8a075 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -22,24 +22,48 @@ std::string Grid::description(){ //Init stringstream description std::stringstream description; + //Get max str len of the grid + int maxStrLen=this->maxStrLenInGrid(); + //Start to write description - description << "-----------------" << std::endl; + std::stringstream gridBorder; + for(int i=0;i<(maxStrLen+2)*4+1;i++){ + gridBorder<<"-"; + } + description << std::endl << gridBorder.str() << std::endl; for(int i=0;i max) + max=number.size(); + } + } + return max; +} + bool Grid::isEmpty(int i, int j){ if(m_grid.at(i).at(j) == 0) return true; @@ -77,8 +101,8 @@ std::tuple Grid::getRandomEmptyCellCoord(){ //Change value of cell bool Grid::setCell(std::tuple coord, int value){ - int i=std::get<0>(coord); - int j=std::get<1>(coord); + int i=std::get<0>(coord); + int j=std::get<1>(coord); if(i>=0 && i=0 && j Grid::defragmentLine(std::vector line){ + for(int j=0; j Grid::mergeLine(std::vector line){ + for(int i=0; i< m_size-1;i++){ + int val1=line.at(i); + int val2=line.at(i+1); + + if(val1==val2){ + line.at(i)=0; + line.at(i+1)=val1*2; + i++; + } + } + return line; +} +std::vector Grid::swipeLine(std::vector line){ + //Swipe line is : + //- A defragmentation + //- A merging + //- Another defragmentation + line=this->defragmentLine(line); + line=this->mergeLine(line); + line=this->defragmentLine(line); + + //Return swiped line + return line; +} + + +//Swipe to right +void Grid::swipeRight(){ + for(int i=0; iswipeLine(m_grid.at(i)); + } +} + +//Swipe to right +void Grid::swipeLeft(){ + for(int i=0; ireverseLine(this->swipeLine(this->reverseLine(m_grid.at(i)))); + } +} + + +void Grid::swipeUp(){ + for(int i=0; i colVect=this->getCol(i); + this->setCol(i,this->reverseLine(this->swipeLine(this->reverseLine(colVect)))); + } +} +void Grid::swipeDown(){ + for(int i=0; i colVect=this->getCol(i); + this->setCol(i,this->swipeLine(colVect)); + } +} + +void Grid::setCol(int col, std::vector colVect){ + for(int i=0;i Grid::getCol(int col){ + + std::vector colVect; + + for(int i=0;i Grid::reverseLine(std::vector line){ + std::vector reversedLine; + + for(int j=m_size-1; j>=0;j--){ + reversedLine.push_back(line.at(j)); + } + + return reversedLine; +} + + +bool Grid::isFull(){ + + for(int i=0;iisFull()) + return false; + + for(int i=0;i colVect(this->getCol(i)); + + for(int j=0;j > m_grid; + int maxStrLenInGrid(); public: Grid(); ~Grid(); @@ -28,6 +29,24 @@ class Grid bool setCell(std::tuple coord, int value); bool setCell(int i, int j, int value); + + std::vector swipeLine(std::vector line); + std::vector defragmentLine(std::vector line); + std::vector mergeLine(std::vector line); + + std::vector getCol(int col); + + bool isFull(); + bool isOver(); + + void setCol(int col, std::vector colVect); + std::vector reverseLine(std::vector line); + + //Moves + void swipeRight(); + void swipeLeft(); + void swipeUp(); + void swipeDown(); }; diff --git a/src/main.cpp b/src/main.cpp index a7abcae..49c7a20 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ - //----- STD include ----- #include #include @@ -7,40 +6,25 @@ //---------------------- //----- Personnal include ----- -#include "./Model/Grid.hpp" +#include "./Controllers/ConsoleController/ConsoleController.hpp" //----------------------------- -//#include "./Model/Elements/StringElement.hpp" -//----- Start ----- +//----- Start ----- int main() { //Init random srand(time(NULL)); + //Init controller + ConsoleController controller; - 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(); - - //Launch game - //controller->play(); + //Run the game + controller.run(); - //Remove controlelr - //delete controller; return 0; } -- cgit v1.2.3