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/Model/Game.cpp | 40 +++++++++++++ src/Model/Game.hpp | 8 ++- src/Model/Grid.cpp | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++-- src/Model/Grid.hpp | 19 ++++++ 4 files changed, 230 insertions(+), 7 deletions(-) (limited to 'src/Model') 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(); }; -- cgit v1.2.3