From 3840053da7d3343eeb0c60854f20bcf72c58c45d Mon Sep 17 00:00:00 2001 From: krilius Date: Wed, 29 Apr 2015 15:42:41 +0400 Subject: First main created --- src/main.cpp | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 0d3b950..597ce39 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,25 +1,13 @@ -#include +#include #include +#include "Model/Grid.hpp" + int main() { - sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!"); - sf::CircleShape shape(100.f); - shape.setFillColor(sf::Color::Green); - - while (window.isOpen()) - { - sf::Event event; - while (window.pollEvent(event)) - { - if (event.type == sf::Event::Closed) - window.close(); - } + Grid * grid = new Grid(); - window.clear(); - window.draw(shape); - window.display(); - } + grid->description(); - return 0; + return 0; } -- cgit v1.2.3 From 9fbd4e0fedea42a206843981dffeb87095797581 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 29 Apr 2015 19:34:01 +0200 Subject: Enable cmake for model --- .gitignore | 2 ++ src/CMakeLists.txt | 4 +++- src/Model/CMakeLists.txt | 8 ++++++++ src/Model/Cell.cpp | 4 ++-- src/Model/Cell.hpp | 2 +- src/Model/Grid.cpp | 5 +++-- src/Model/Grid.hpp | 4 ++-- src/main.cpp | 4 ++-- 8 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 src/Model/CMakeLists.txt (limited to 'src/main.cpp') diff --git a/.gitignore b/.gitignore index 8f10546..5c15316 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ CMakeFiles +*.a +*.o cmake_install.cmake Makefile CMakeCache.txt diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b30833e..a946644 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,9 +7,11 @@ add_executable( #Find all libraries find_package(SFML 2.2 COMPONENTS system window graphics audio REQUIRED) +set_property(GLOBAL PROPERTY SFML_LIBRARIES "${SFML_LIBRARIES}") +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}) -message("${SFML_LIBRARIES}") +add_subdirectory(./Model) diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt new file mode 100644 index 0000000..c9cfb6e --- /dev/null +++ b/src/Model/CMakeLists.txt @@ -0,0 +1,8 @@ +#Retrieve crypto++ libraries +get_property(SFML_LIBRARIES GLOBAL PROPERTY SFML_LIBRARIES) + +#Make CryptClass lib +add_library(Model ./Cell.cpp ./Game.cpp ./Grid.cpp) + +#Add crypto++ to CryptClass +target_link_libraries(Model ${SFML_LIBRARIES}) diff --git a/src/Model/Cell.cpp b/src/Model/Cell.cpp index 24fc53c..a0729f7 100644 --- a/src/Model/Cell.cpp +++ b/src/Model/Cell.cpp @@ -19,9 +19,9 @@ Cell::~Cell() } // Description - +/* void Cell::description() { return m_value; } - +*/ diff --git a/src/Model/Cell.hpp b/src/Model/Cell.hpp index 7775bf5..dd946ab 100644 --- a/src/Model/Cell.hpp +++ b/src/Model/Cell.hpp @@ -21,7 +21,7 @@ class Cell ~Cell(); // Describes the cell in a terminal - std::string description(); + //std::string description(); }; diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index 94ef5d7..d7d3f15 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -1,5 +1,6 @@ -#include "Grid.h" +#include "Grid.hpp" +/* Grid::Grid(int size) { m_table = std::vector>(size); @@ -34,4 +35,4 @@ void Grid::description() std::cout << std::endl; } } - +*/ diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp index a9c6c86..304e436 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -11,7 +11,7 @@ #include #include "Cell.hpp" - +/* class Grid { private: @@ -22,6 +22,6 @@ class Grid ~Grid(); void description(); }; +*/ #endif - diff --git a/src/main.cpp b/src/main.cpp index 597ce39..689767a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,9 +5,9 @@ int main() { - Grid * grid = new Grid(); +// Grid * grid = new Grid(); - grid->description(); +// grid->description(); return 0; } -- cgit v1.2.3 From e241abc9df586cce683d6ae89e1d02c13084a10f Mon Sep 17 00:00:00 2001 From: krilius Date: Wed, 29 Apr 2015 20:52:18 +0400 Subject: bug sous osx --- src/Model/Cell.cpp | 6 +++--- src/Model/Cell.hpp | 2 +- src/Model/Grid.cpp | 12 ++++++------ src/Model/Grid.hpp | 8 ++++---- src/main.cpp | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src/main.cpp') diff --git a/src/Model/Cell.cpp b/src/Model/Cell.cpp index a0729f7..22eaafe 100644 --- a/src/Model/Cell.cpp +++ b/src/Model/Cell.cpp @@ -19,9 +19,9 @@ Cell::~Cell() } // Description -/* -void Cell::description() + +std::string Cell::description() { return m_value; } -*/ + diff --git a/src/Model/Cell.hpp b/src/Model/Cell.hpp index dd946ab..7775bf5 100644 --- a/src/Model/Cell.hpp +++ b/src/Model/Cell.hpp @@ -21,7 +21,7 @@ class Cell ~Cell(); // Describes the cell in a terminal - //std::string description(); + std::string description(); }; diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index d7d3f15..ade3f74 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -1,9 +1,9 @@ #include "Grid.hpp" -/* + Grid::Grid(int size) { - m_table = std::vector>(size); + m_table = std::vector >(size); for(int i = 0 ; i < size ; i++) { m_table[i] = std::vector(size); @@ -19,20 +19,20 @@ Grid::~Grid() { for(int i = 0 ; i < m_table.size() ; i++) { - for(int i = 0 ; j < m_table[i].size() ; j++) + for(int j = 0 ; j < m_table[i].size() ; j++) delete m_table[i][j]; } } -void Grid::description() +void Grid::afficher() { for(int i = 0 ; i < m_table.size() ; i++) { - for(int j = 0 ; j < m_table[i].size() ; i++) + for(int j = 0 ; j < m_table[i].size() ; j++) { std::cout << m_table[i][j]->description(); } std::cout << std::endl; } } -*/ + diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp index 304e436..e3bc888 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -11,17 +11,17 @@ #include #include "Cell.hpp" -/* + class Grid { private: - std::vector> m_table; + std::vector > m_table; public: Grid(int size); ~Grid(); - void description(); + void afficher(); }; -*/ + #endif diff --git a/src/main.cpp b/src/main.cpp index 689767a..12f6c6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,9 +5,9 @@ int main() { -// Grid * grid = new Grid(); + Grid * grid = new Grid(4); -// grid->description(); + grid->afficher(); return 0; } -- cgit v1.2.3 From ad216d86f6573e3575e85f3a2941f6f34b5b1c0e Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 29 Apr 2015 21:18:06 +0200 Subject: Add missing dependency in CMakeList.txt --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 2 +- src/Model/CMakeLists.txt | 10 ++-------- src/main.cpp | 2 +- 4 files changed, 5 insertions(+), 11 deletions(-) (limited to 'src/main.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index c822dc4..4ab2346 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,5 +14,5 @@ set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REV}") cmake_minimum_required(VERSION 2.6) #Add source directory -add_subdirectory(src) +add_subdirectory(./src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a946644..89e46d5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,6 @@ 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}) +target_link_libraries(2P11 ${SFML_LIBRARIES} Model) add_subdirectory(./Model) diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index c9cfb6e..c685c7a 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -1,8 +1,2 @@ -#Retrieve crypto++ libraries -get_property(SFML_LIBRARIES GLOBAL PROPERTY SFML_LIBRARIES) - -#Make CryptClass lib -add_library(Model ./Cell.cpp ./Game.cpp ./Grid.cpp) - -#Add crypto++ to CryptClass -target_link_libraries(Model ${SFML_LIBRARIES}) +#Make Model lib +add_library(Model Grid.cpp Cell.cpp Game.cpp) diff --git a/src/main.cpp b/src/main.cpp index 12f6c6d..00473f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include #include -#include "Model/Grid.hpp" +#include "./Model/Grid.hpp" int main() { -- cgit v1.2.3 From 71ebf1235ed023425c2539eabf1d15ba922c1dda Mon Sep 17 00:00:00 2001 From: krilius Date: Thu, 30 Apr 2015 08:48:29 +0400 Subject: first version of the controller --- src/CTController/CTConsole.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++ src/CTController/CTConsole.hpp | 26 +++++++++++++++++++++++ src/Model/ModelConstants.hpp | 12 +++++++++++ src/main.cpp | 17 +++++++++------ 4 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 src/CTController/CTConsole.cpp create mode 100644 src/CTController/CTConsole.hpp create mode 100644 src/Model/ModelConstants.hpp (limited to 'src/main.cpp') diff --git a/src/CTController/CTConsole.cpp b/src/CTController/CTConsole.cpp new file mode 100644 index 0000000..44b598e --- /dev/null +++ b/src/CTController/CTConsole.cpp @@ -0,0 +1,48 @@ +#include "CTConsole.hpp" + +CTConsole::CTConsole() +{ + m_game = new Game(); +} + +CTConsole::~CTConsole() +{ + delete m_game; +} + +void CTConsole::play() +{ + while (!m_game->isOver()) + { + m_game->showGrid(); + char moveChoice = ' '; + + std::cin >> moveChoice; + + switch (moveChoice) + { + case 'z': + std::cout << "up" << std::endl; + break; + + case 's': + std::cout << "down" << std::endl; + break; + + case 'q': + std::cout << "left" << std::endl; + break; + + case 'd': + std::cout << "right" << std::endl; + break; + + default: + break; + } + + std::cout << std::endl; + + m_game->pop(); + } +} \ No newline at end of file diff --git a/src/CTController/CTConsole.hpp b/src/CTController/CTConsole.hpp new file mode 100644 index 0000000..9534877 --- /dev/null +++ b/src/CTController/CTConsole.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 "../Model/Game.hpp" + +class CTConsole +{ +private: + Game * m_game; + +public: + CTConsole(); + ~CTConsole(); + + void play(); +}; + +#endif \ No newline at end of file diff --git a/src/Model/ModelConstants.hpp b/src/Model/ModelConstants.hpp new file mode 100644 index 0000000..5cbc21b --- /dev/null +++ b/src/Model/ModelConstants.hpp @@ -0,0 +1,12 @@ +#ifndef DEF_MODELCONSTANTS +#define DEF_MODELCONSTANTS + +/* ModelConstants.hpp + * Constains constants and enums used in the whole model + * Creators : krilius, manzerbredes + * Date : 29/04/2015 */ + +enum Direction { UP, DOWN, LEFT, RIGHT }; +typedef enum Direction Direction; + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 12f6c6d..9006fcd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,18 @@ #include #include +#include -#include "Model/Grid.hpp" +#include "CTController/CTConsole.hpp" int main() { - Grid * grid = new Grid(4); - - grid->afficher(); - - return 0; + srand(time(NULL)); + + CTConsole * controller = new CTConsole(); + + controller->play(); + + delete controller; + + return 0; } -- cgit v1.2.3 From fa88c91351388c4b2e1791ccab2cd8a959dc976b Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Thu, 30 Apr 2015 09:07:40 +0200 Subject: Fix some things --- src/CTController/CTConsole.cpp | 48 ---------------------- src/CTController/CTConsole.hpp | 26 ------------ .../ConsoleController/ConsoleController.cpp | 48 ++++++++++++++++++++++ .../ConsoleController/ConsoleController.hpp | 26 ++++++++++++ src/View/MainController.cpp | 0 src/ViewController/MainController.cpp | 0 src/main.cpp | 33 +++++++++------ 7 files changed, 94 insertions(+), 87 deletions(-) delete mode 100644 src/CTController/CTConsole.cpp delete mode 100644 src/CTController/CTConsole.hpp create mode 100644 src/Controller/ConsoleController/ConsoleController.cpp create mode 100644 src/Controller/ConsoleController/ConsoleController.hpp create mode 100644 src/View/MainController.cpp delete mode 100644 src/ViewController/MainController.cpp (limited to 'src/main.cpp') diff --git a/src/CTController/CTConsole.cpp b/src/CTController/CTConsole.cpp deleted file mode 100644 index 44b598e..0000000 --- a/src/CTController/CTConsole.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "CTConsole.hpp" - -CTConsole::CTConsole() -{ - m_game = new Game(); -} - -CTConsole::~CTConsole() -{ - delete m_game; -} - -void CTConsole::play() -{ - while (!m_game->isOver()) - { - m_game->showGrid(); - char moveChoice = ' '; - - std::cin >> moveChoice; - - switch (moveChoice) - { - case 'z': - std::cout << "up" << std::endl; - break; - - case 's': - std::cout << "down" << std::endl; - break; - - case 'q': - std::cout << "left" << std::endl; - break; - - case 'd': - std::cout << "right" << std::endl; - break; - - default: - break; - } - - std::cout << std::endl; - - m_game->pop(); - } -} \ No newline at end of file diff --git a/src/CTController/CTConsole.hpp b/src/CTController/CTConsole.hpp deleted file mode 100644 index 9534877..0000000 --- a/src/CTController/CTConsole.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#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 "../Model/Game.hpp" - -class CTConsole -{ -private: - Game * m_game; - -public: - CTConsole(); - ~CTConsole(); - - void play(); -}; - -#endif \ No newline at end of file diff --git a/src/Controller/ConsoleController/ConsoleController.cpp b/src/Controller/ConsoleController/ConsoleController.cpp new file mode 100644 index 0000000..44b598e --- /dev/null +++ b/src/Controller/ConsoleController/ConsoleController.cpp @@ -0,0 +1,48 @@ +#include "CTConsole.hpp" + +CTConsole::CTConsole() +{ + m_game = new Game(); +} + +CTConsole::~CTConsole() +{ + delete m_game; +} + +void CTConsole::play() +{ + while (!m_game->isOver()) + { + m_game->showGrid(); + char moveChoice = ' '; + + std::cin >> moveChoice; + + switch (moveChoice) + { + case 'z': + std::cout << "up" << std::endl; + break; + + case 's': + std::cout << "down" << std::endl; + break; + + case 'q': + std::cout << "left" << std::endl; + break; + + case 'd': + std::cout << "right" << std::endl; + break; + + default: + break; + } + + std::cout << std::endl; + + m_game->pop(); + } +} \ No newline at end of file diff --git a/src/Controller/ConsoleController/ConsoleController.hpp b/src/Controller/ConsoleController/ConsoleController.hpp new file mode 100644 index 0000000..9534877 --- /dev/null +++ b/src/Controller/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 "../Model/Game.hpp" + +class CTConsole +{ +private: + Game * m_game; + +public: + CTConsole(); + ~CTConsole(); + + void play(); +}; + +#endif \ No newline at end of file diff --git a/src/View/MainController.cpp b/src/View/MainController.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/ViewController/MainController.cpp b/src/ViewController/MainController.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/main.cpp b/src/main.cpp index 206b47a..91a1fe6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,22 +1,29 @@ + +//----- STD include ----- #include #include #include +//---------------------- -<<<<<<< HEAD +//----- Personnal include ----- #include "./Model/Grid.hpp" -======= -#include "CTController/CTConsole.hpp" ->>>>>>> 71ebf1235ed023425c2539eabf1d15ba922c1dda +#include "./Controller/ConsoleController/ConsoleController.hpp" +//----------------------------- + + +//----- Start ----- int main() { - srand(time(NULL)); - - CTConsole * controller = new CTConsole(); - - controller->play(); - - delete controller; - - return 0; + //Init random + srand(time(NULL)); + + //Init console controller + ConsoleController * controller = new ConsoleController(); + + controller->play(); + + delete controller; + + return 0; } -- cgit v1.2.3 From 80c37e438ee57f0eceb322dbee778a49d9db0896 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Thu, 30 Apr 2015 10:09:58 +0200 Subject: Fix space --- .../ConsoleController/ConsoleController.cpp | 68 +++++++++++----------- .../ConsoleController/ConsoleController.hpp | 13 ++--- src/main.cpp | 2 + 3 files changed, 41 insertions(+), 42 deletions(-) (limited to 'src/main.cpp') diff --git a/src/Controller/ConsoleController/ConsoleController.cpp b/src/Controller/ConsoleController/ConsoleController.cpp index dc61ae8..f4d6b4d 100644 --- a/src/Controller/ConsoleController/ConsoleController.cpp +++ b/src/Controller/ConsoleController/ConsoleController.cpp @@ -2,47 +2,45 @@ ConsoleController::ConsoleController() { - m_game = new Game(); + m_game = new Game(); } ConsoleController::~ConsoleController() { - delete m_game; + delete m_game; } void ConsoleController::play() { - while (!m_game->isOver()) - { - m_game->showGrid(); - char moveChoice = ' '; - - std::cin >> moveChoice; - - switch (moveChoice) - { - case 'z': - std::cout << "up" << std::endl; - break; - - case 's': - std::cout << "down" << std::endl; - break; - - case 'q': - std::cout << "left" << std::endl; - break; - - case 'd': - std::cout << "right" << std::endl; - break; - - default: - break; - } - - std::cout << std::endl; - - m_game->pop(); - } + while (!m_game->isOver()) + { + m_game->showGrid(); + char moveChoice = ' '; + + std::cin >> moveChoice; + + switch (moveChoice) + { + case 'z': + std::cout << "up" << std::endl; + break; + + case 's': + std::cout << "down" << std::endl; + break; + + case 'q': + std::cout << "left" << std::endl; + break; + case 'd': + std::cout << "right" << std::endl; + break; + + default: + break; + } + + std::cout << std::endl; + m_game->pop(); + } } diff --git a/src/Controller/ConsoleController/ConsoleController.hpp b/src/Controller/ConsoleController/ConsoleController.hpp index 83c44fc..2cda804 100644 --- a/src/Controller/ConsoleController/ConsoleController.hpp +++ b/src/Controller/ConsoleController/ConsoleController.hpp @@ -8,19 +8,18 @@ * Date : 29/04/2915 */ #include - #include "../../Model/Game.hpp" class ConsoleController { -private: - Game * m_game; + private: -public: - ConsoleController(); - ~ConsoleController(); + Game * m_game; - void play(); + public: + ConsoleController(); + ~ConsoleController(); + void play(); }; #endif diff --git a/src/main.cpp b/src/main.cpp index 91a1fe6..52444a9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,8 +21,10 @@ int main() //Init console controller ConsoleController * controller = new ConsoleController(); + //Launch game controller->play(); + //Remove controlelr delete controller; return 0; -- cgit v1.2.3 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/CMakeLists.txt | 2 +- src/Controller/CMakeLists.txt | 1 - src/Controller/ConsoleController/CMakeLists.txt | 3 - .../ConsoleController/ConsoleController.cpp | 112 --------------------- .../ConsoleController/ConsoleController.hpp | 25 ----- src/Controllers/CMakeLists.txt | 1 + src/Controllers/ConsoleController/CMakeLists.txt | 3 + .../ConsoleController/ConsoleController.cpp | 112 +++++++++++++++++++++ .../ConsoleController/ConsoleController.hpp | 26 +++++ src/Helpers/Keyboard.hpp | 17 ++++ src/Model/Grid.cpp | 82 --------------- src/Model/Grid.hpp | 8 +- src/Model/ModelConstants.hpp | 12 --- src/main.cpp | 2 +- 14 files changed, 162 insertions(+), 244 deletions(-) delete mode 100644 src/Controller/CMakeLists.txt delete mode 100644 src/Controller/ConsoleController/CMakeLists.txt delete mode 100644 src/Controller/ConsoleController/ConsoleController.cpp delete mode 100644 src/Controller/ConsoleController/ConsoleController.hpp 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 create mode 100644 src/Helpers/Keyboard.hpp delete mode 100644 src/Model/ModelConstants.hpp (limited to 'src/main.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a1c4ccb..da444d2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,4 +15,4 @@ include_directories(${SFML_INCLUDE_DIR}) target_link_libraries(2P11 ${SFML_LIBRARIES} Model ConsoleController) add_subdirectory(./Model) -add_subdirectory(./Controller/) +add_subdirectory(./Controllers/) diff --git a/src/Controller/CMakeLists.txt b/src/Controller/CMakeLists.txt deleted file mode 100644 index 7ebeb19..0000000 --- a/src/Controller/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(./ConsoleController/) diff --git a/src/Controller/ConsoleController/CMakeLists.txt b/src/Controller/ConsoleController/CMakeLists.txt deleted file mode 100644 index d42803f..0000000 --- a/src/Controller/ConsoleController/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -#Make Model lib -add_library(ConsoleController ./ConsoleController.cpp) -target_link_libraries(ConsoleController Model) diff --git a/src/Controller/ConsoleController/ConsoleController.cpp b/src/Controller/ConsoleController/ConsoleController.cpp deleted file mode 100644 index a2a4a77..0000000 --- a/src/Controller/ConsoleController/ConsoleController.cpp +++ /dev/null @@ -1,112 +0,0 @@ -#include "./ConsoleController.hpp" -#include -#include "../../Model/ModelConstants.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 - 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 UP: - std::cout << "Keypress : Up" << std::endl; - break; - case DOWN: - std::cout << "Keypress : Down" << std::endl; - break; - case LEFT: - std::cout << "Keypress : Left" << std::endl; - break; - case RIGHT: - std::cout << "Keypress : Right" << std::endl; - break; - } - - //Show the Grid - m_game->showGrid(); - std::cout << std::endl; - - - //Pop new number - m_game->pop(); - - } -} - - - -Direction ConsoleController::waitArrowKeyPress() -{ - //Initialise keyPress - Direction keyPress; - - //Wait for keypress - while(1){ - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) - { - keyPress=LEFT; - while(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) - { - //Wait for release - } - break; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) - { - keyPress=RIGHT; - while(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) - { - //Wait for release - } - break; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) - { - keyPress=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=DOWN; - while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) - { - //Wait for release - } - break; - } - } - - return keyPress; -} diff --git a/src/Controller/ConsoleController/ConsoleController.hpp b/src/Controller/ConsoleController/ConsoleController.hpp deleted file mode 100644 index 317759c..0000000 --- a/src/Controller/ConsoleController/ConsoleController.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#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 "../../Model/Game.hpp" - -class ConsoleController -{ - private: - - Game * m_game; - Direction waitArrowKeyPress(); - public: - ConsoleController(); - ~ConsoleController(); - void play(); -}; - -#endif 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 diff --git a/src/Helpers/Keyboard.hpp b/src/Helpers/Keyboard.hpp new file mode 100644 index 0000000..9d2dfa3 --- /dev/null +++ b/src/Helpers/Keyboard.hpp @@ -0,0 +1,17 @@ +#ifndef DEF_MODELCONSTANTS +#define DEF_MODELCONSTANTS + +/* ModelConstants.hpp + * Constains constants and enums used in the whole model + * Creators : krilius, manzerbredes + * Date : 29/04/2015 */ + +namespace kbdh { + + //Key arrow + enum Direction { Up, Down, Left, Right }; + typedef enum Direction Direction; + +} + +#endif diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index 75c0209..cc9cdb1 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -82,86 +82,4 @@ void Grid::setCell(int i, int j, Cell *cell) } -void Grid::move(Direction direction) -{ - switch (direction) - { - case UP: - - break; - - case DOWN: - break; - - case LEFT: - break; - - case 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 2a28559..c431cb3 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -10,7 +10,7 @@ #include #include -#include "ModelConstants.hpp" +//#include "ModelConstants.hpp" #include "Cell.hpp" class Grid @@ -19,11 +19,6 @@ class Grid int m_size; std::vector > m_table; - void moveUp(); - void moveDown(); - void moveLeft(); - void moveRight(); - public: Grid(int size); ~Grid(); @@ -33,7 +28,6 @@ class Grid bool gridIsFull(); void setCell(int i, int j, Cell * cell); - void move(Direction direction); }; diff --git a/src/Model/ModelConstants.hpp b/src/Model/ModelConstants.hpp deleted file mode 100644 index 5cbc21b..0000000 --- a/src/Model/ModelConstants.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef DEF_MODELCONSTANTS -#define DEF_MODELCONSTANTS - -/* ModelConstants.hpp - * Constains constants and enums used in the whole model - * Creators : krilius, manzerbredes - * Date : 29/04/2015 */ - -enum Direction { UP, DOWN, LEFT, RIGHT }; -typedef enum Direction Direction; - -#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 52444a9..26062a9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,7 @@ //----- Personnal include ----- #include "./Model/Grid.hpp" -#include "./Controller/ConsoleController/ConsoleController.hpp" +#include "./Controllers/ConsoleController/ConsoleController.hpp" //----------------------------- -- cgit v1.2.3 From 01b25accba1a5329e220aa647255d2c2b284c16e Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Fri, 1 May 2015 18:19:32 +0200 Subject: Add operator overload squeleton --- src/Model/CMakeLists.txt | 2 +- src/Model/Cell.hpp | 30 +++++++++++++++++++++++------- src/Model/Elements/StringElement.cpp | 25 ++++++++++++++++++++++++- src/Model/Elements/StringElement.hpp | 3 +++ src/main.cpp | 15 +++++++++++++++ 5 files changed, 66 insertions(+), 9 deletions(-) (limited to 'src/main.cpp') diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index 284be94..80ea875 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -1,5 +1,5 @@ #Make Model lib -add_library(Model Grid.cpp Game.cpp) +add_library(Model Grid.cpp Game.cpp ) target_link_libraries(Model Elements) diff --git a/src/Model/Cell.hpp b/src/Model/Cell.hpp index 7638c4e..cb4bc21 100644 --- a/src/Model/Cell.hpp +++ b/src/Model/Cell.hpp @@ -24,35 +24,51 @@ template class Cell m_Element->setValue(value); } + //Destructor ~Cell() { delete m_Element; } - + //Test if the cell is empty bool isEmpty() { return true; } - std::string getElementValue() - { - return m_Element->getValue(); + T getElement(){ + return this->m_Element; } - bool equals(Cell *otherCell) - { + bool equals(Cell *cell){ + /*if(cell->getElement() == this->m_Element){ + return true; + }*/ return true; } + //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 true; +} + #endif diff --git a/src/Model/Elements/StringElement.cpp b/src/Model/Elements/StringElement.cpp index f93fe3b..fbf57c1 100644 --- a/src/Model/Elements/StringElement.cpp +++ b/src/Model/Elements/StringElement.cpp @@ -2,7 +2,7 @@ StringElement::StringElement(){ - this->m_value="."; + this->m_value=""; } StringElement::~StringElement(){ @@ -20,5 +20,28 @@ void StringElement::setValue(std::string 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 const& element) const{ + 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); +} + diff --git a/src/Model/Elements/StringElement.hpp b/src/Model/Elements/StringElement.hpp index db40f58..16946a8 100644 --- a/src/Model/Elements/StringElement.hpp +++ b/src/Model/Elements/StringElement.hpp @@ -20,7 +20,10 @@ class StringElement std::string getValue(); void setValue(std::string value); + bool isEmpty(); + bool equals(StringElement const& element) const; std::string description(); + }; #endif diff --git a/src/main.cpp b/src/main.cpp index 26062a9..217fe81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,11 +10,26 @@ #include "./Controllers/ConsoleController/ConsoleController.hpp" //----------------------------- +#include "./Model/Cell.hpp" +//#include "./Model/Elements/StringElement.hpp" //----- Start ----- + + int main() { + Cell cell1("loic"); + Cell cell2("loic"); + + + if(cell1==cell2){ + std::cout << "Egale" << std::endl; + } + else{ + std::cout << "Différent" << std::endl; + } + //Init random srand(time(NULL)); -- 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/main.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/main.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/main.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/main.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