diff --git a/.gitignore b/.gitignore index ab510cc..c19474c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,15 @@ CMakeFiles +<<<<<<< HEAD *.sh 2P11 +======= +*.a +*.o +>>>>>>> develop cmake_install.cmake Makefile CMakeCache.txt +*.app +2P11 +clear.sh +*.vim diff --git a/CMakeLists.txt b/CMakeLists.txt index c822dc4..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) @@ -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 b30833e..da444d2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,9 +7,12 @@ 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}) +target_link_libraries(2P11 ${SFML_LIBRARIES} Model ConsoleController) -message("${SFML_LIBRARIES}") +add_subdirectory(./Model) +add_subdirectory(./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..d91e807 --- /dev/null +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -0,0 +1,97 @@ +#include "./ConsoleController.hpp" +#include +#include "../../Helpers/Keyboard.hpp" + +ConsoleController::ConsoleController() +{ +} + +ConsoleController::~ConsoleController() +{ +} + +void ConsoleController::run() +{ + + //Init keyPress + kbdh::Direction keyPress; + + //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 (!m_game.isOver()) + { + //Get key press + keyPress=this->waitArrowKeyPress(); + + //Apply move + m_game.swipe(keyPress); + + //Pop a random number on the grid + m_game.popRandomNumber(); + + //Cout grid + m_game.coutGrid(); + + } + m_game.coutGrid(); +} + + + +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..8d73f79 --- /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 run(); +}; + +#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/CMakeLists.txt b/src/Model/CMakeLists.txt new file mode 100644 index 0000000..888589e --- /dev/null +++ b/src/Model/CMakeLists.txt @@ -0,0 +1,2 @@ +#Make Model lib +add_library(Model Grid.cpp Game.cpp) diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp new file mode 100644 index 0000000..6039bc9 --- /dev/null +++ b/src/Model/Game.cpp @@ -0,0 +1,49 @@ +#include "Game.hpp" + + + +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 new file mode 100644 index 0000000..ab19340 --- /dev/null +++ b/src/Model/Game.hpp @@ -0,0 +1,31 @@ +#ifndef DEF_GAME +#define DEF_GAME + +/* Game.h + * Defines the class Game + * A game allows a player to play. It contains a grid and pops numbers + * Creators : krilius, manzerbredes + * Date : 29/04/2015 */ + +#include +#include +#include "../Helpers/Keyboard.hpp" +#include "Grid.hpp" +#include + +class Game +{ + private: + Grid m_grid; + + 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 new file mode 100644 index 0000000..3a8a075 --- /dev/null +++ b/src/Model/Grid.cpp @@ -0,0 +1,258 @@ +#include "Grid.hpp" + +//Constructor +Grid::Grid(): m_size(4), m_grid(4){ + + //Init all cells + for(int i=0;imaxStrLenInGrid(); + + //Start to write description + 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; + return false; +} + +std::tuple Grid::getRandomEmptyCellCoord(){ + + //Init list of candidate + std::vector > candidates; + + //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); + + //Select the candidates + int winnerIs(rand() % candidates.size()); + + //Return the candidate + return candidates.at(winnerIs); + +} + + + +//Change value of cell +bool Grid::setCell(std::tuple coord, int value){ + int i=std::get<0>(coord); + int j=std::get<1>(coord); + + if(i>=0 && i=0 && j coord(i,j); + return this->setCell(coord, value); +} + + +std::vector 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 +#include +#include +#include + +class Grid +{ + private: + int m_size; + std::vector > m_grid; + + int maxStrLenInGrid(); + public: + Grid(); + ~Grid(); + + std::string description(); + bool isEmpty(int i, int j); + std::tuple getRandomEmptyCellCoord(); + + 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(); +}; + + +#endif diff --git a/src/main.cpp b/src/main.cpp index 0d3b950..49c7a20 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,25 +1,30 @@ -#include +//----- STD include ----- +#include #include +#include +#include +//---------------------- +//----- Personnal include ----- +#include "./Controllers/ConsoleController/ConsoleController.hpp" +//----------------------------- + + + + + +//----- Start ----- int main() { - sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!"); - sf::CircleShape shape(100.f); - shape.setFillColor(sf::Color::Green); + //Init random + srand(time(NULL)); - while (window.isOpen()) - { - sf::Event event; - while (window.pollEvent(event)) - { - if (event.type == sf::Event::Closed) - window.close(); - } + //Init controller + ConsoleController controller; - window.clear(); - window.draw(shape); - window.display(); - } + //Run the game + controller.run(); - return 0; + + return 0; }