From 048f1e17b752d2af53db82c1861002283fc300fa Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Fri, 1 May 2015 13:18:26 +0200 Subject: Add some newers things : -Change helper ModelConstants to Keyboard -Now helpers is in folder Helpers -Change direction name to use CamelCase --- src/Controllers/CMakeLists.txt | 1 + src/Controllers/ConsoleController/CMakeLists.txt | 3 + .../ConsoleController/ConsoleController.cpp | 112 +++++++++++++++++++++ .../ConsoleController/ConsoleController.hpp | 26 +++++ 4 files changed, 142 insertions(+) create mode 100644 src/Controllers/CMakeLists.txt create mode 100644 src/Controllers/ConsoleController/CMakeLists.txt create mode 100644 src/Controllers/ConsoleController/ConsoleController.cpp create mode 100644 src/Controllers/ConsoleController/ConsoleController.hpp (limited to 'src/Controllers') diff --git a/src/Controllers/CMakeLists.txt b/src/Controllers/CMakeLists.txt new file mode 100644 index 0000000..7ebeb19 --- /dev/null +++ b/src/Controllers/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(./ConsoleController/) diff --git a/src/Controllers/ConsoleController/CMakeLists.txt b/src/Controllers/ConsoleController/CMakeLists.txt new file mode 100644 index 0000000..d42803f --- /dev/null +++ b/src/Controllers/ConsoleController/CMakeLists.txt @@ -0,0 +1,3 @@ +#Make Model lib +add_library(ConsoleController ./ConsoleController.cpp) +target_link_libraries(ConsoleController Model) diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp new file mode 100644 index 0000000..4be0375 --- /dev/null +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -0,0 +1,112 @@ +#include "./ConsoleController.hpp" +#include +#include "../../Helpers/Keyboard.hpp" + +ConsoleController::ConsoleController() +{ + m_game = new Game(); +} + +ConsoleController::~ConsoleController() +{ + delete m_game; +} + +void ConsoleController::play() +{ + //Intruction msg + std::cout << "Use arrows to play !" << std::endl; + + //Init keyPress + kbdh::Direction keyPress; + + //Display the first grid + m_game->showGrid(); + + //Start game + while (!m_game->isOver()) + { + //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; + break; + } + + //Show the Grid + m_game->showGrid(); + std::cout << std::endl; + + + //Pop new number + m_game->pop(); + + } +} + + + +kbdh::Direction ConsoleController::waitArrowKeyPress() +{ + //Initialise keyPress + kbdh::Direction keyPress; + + //Wait for keypress + while(1){ + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) + { + keyPress=kbdh::Left; + while(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) + { + //Wait for release + } + break; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) + { + keyPress=kbdh::Right; + while(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) + { + //Wait for release + } + break; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) + { + keyPress=kbdh::Up; + while(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) + { + //Wait for release + } + break; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) + { + // la touche "flèche gauche" est enfoncée : on bouge le personnage + keyPress=kbdh::Down; + while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) + { + //Wait for release + } + break; + } + } + + return keyPress; +} diff --git a/src/Controllers/ConsoleController/ConsoleController.hpp b/src/Controllers/ConsoleController/ConsoleController.hpp new file mode 100644 index 0000000..6ce8df9 --- /dev/null +++ b/src/Controllers/ConsoleController/ConsoleController.hpp @@ -0,0 +1,26 @@ +#ifndef DEF_CTCONSOLE +#define DEF_CTCONSOLE + +/* CTConsole.hpp + * Defines the class CTConsole + * CTConsole is a controller which displays a game in a terminal + * Creators : krilius, manzerbredes + * Date : 29/04/2915 */ + +#include +#include "../../Helpers/Keyboard.hpp" +#include "../../Model/Game.hpp" + +class ConsoleController +{ + private: + + Game * m_game; + kbdh::Direction waitArrowKeyPress(); + public: + ConsoleController(); + ~ConsoleController(); + void play(); +}; + +#endif -- 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/Controllers') 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/Controllers') 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 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/Controllers') 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