Prefer extends Game to grid than encapsulation

This commit is contained in:
manzerbredes 2015-05-04 13:08:45 +02:00
parent 3384204edc
commit 1bebaf0f92
3 changed files with 25 additions and 24 deletions

View file

@ -4,7 +4,7 @@
//==================== Constructor and Destructor ==================== //==================== Constructor and Destructor ====================
//Constructor //Constructor
Game::Game() : m_grid(), m_score(0), m_nbMove(0){ Game::Game() : Grid(), m_nbMove(0), m_score(0){
} }
//Destructor //Destructor
@ -21,21 +21,21 @@ bool Game::swipe(kbdh::Direction direction){
switch(direction){ switch(direction){
case kbdh::Left: case kbdh::Left:
moveDone=m_grid.swipeLeft(); moveDone=swipeLeft();
break; break;
case kbdh::Right: case kbdh::Right:
moveDone=m_grid.swipeRight(); moveDone=swipeRight();
break; break;
case kbdh::Up: case kbdh::Up:
moveDone=m_grid.swipeUp(); moveDone=swipeUp();
break; break;
case kbdh::Down: case kbdh::Down:
moveDone=m_grid.swipeDown(); moveDone=swipeDown();
break; break;
} }
if(moveDone){ if(moveDone){
m_score+=m_grid.getLastMoveScore(); m_score+=m_lastMoveScore;
m_nbMove++; m_nbMove++;
this->popRandomNumber(); this->popRandomNumber();
} }
@ -46,17 +46,17 @@ bool Game::swipe(kbdh::Direction direction){
//Cout the grid //Cout the grid
void Game::coutGrid(){ void Game::coutGrid(){
std::cout << m_grid.description(); std::cout << this->description();
} }
//Return true if the game is lost. False else. //Return true if the game is lost. False else.
bool Game::isOver(){ //bool Game::isOver(){
return m_grid.isOver(); //return m_grid.isOver();
} //}
//Pop a random number on the grid //Pop a random number on the grid
void Game::popRandomNumber(){ void Game::popRandomNumber(){
std::tuple<int, int> coord(m_grid.getRandomEmptyCellCoord()); std::tuple<int, int> coord(Grid::getRandomEmptyCellCoord());
int percent=rand() % 100; int percent=rand() % 100;
@ -70,7 +70,7 @@ void Game::popRandomNumber(){
} }
m_grid.setCell(coord, number); Grid::setCell(coord, number);
} }
//==================== Getters and Setter ==================== //==================== Getters and Setter ====================
@ -84,11 +84,11 @@ int Game::getNbMove(){
return m_nbMove; return m_nbMove;
} }
std::vector<std::vector<int> > Game::getGrid(){ //std::vector<std::vector<int> > Game::getGrid(){
return m_grid.getGrid(); //return m_grid.getGrid();
} //}
int Game::maxStrLenInGrid(){ //int Game::maxStrLenInGrid(){
return m_grid.maxStrLenInGrid(); //return m_grid.maxStrLenInGrid();
} //}

View file

@ -13,11 +13,11 @@
#include "Grid.hpp" #include "Grid.hpp"
#include <tuple> #include <tuple>
class Game class Game : public Grid
{ {
private: private:
//Members //Members
Grid m_grid; //Grid m_grid;
int m_score; int m_score;
int m_nbMove; int m_nbMove;
@ -30,13 +30,13 @@ class Game
bool swipe(kbdh::Direction direction); bool swipe(kbdh::Direction direction);
void coutGrid(); void coutGrid();
void popRandomNumber(); void popRandomNumber();
bool isOver(); //bool isOver();
//Getters and Setters //Getters and Setters
int getScore(); int getScore();
int getNbMove(); int getNbMove();
int maxStrLenInGrid(); //int maxStrLenInGrid();
std::vector<std::vector<int> > getGrid(); //std::vector<std::vector<int> > getGrid();
}; };
#endif #endif

View file

@ -16,8 +16,9 @@ class Grid
{ {
private: private:
//Members //Members
int m_size; protected:
std::vector<std::vector<int> > m_grid; std::vector<std::vector<int> > m_grid;
int m_size;
int m_lastMoveScore; int m_lastMoveScore;
//Private methods //Private methods