From 1bebaf0f924c83bfb5a91bafefcb69d98e3b8b05 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Mon, 4 May 2015 13:08:45 +0200 Subject: [PATCH] Prefer extends Game to grid than encapsulation --- src/Model/Game.cpp | 36 ++++++++++++++++++------------------ src/Model/Game.hpp | 10 +++++----- src/Model/Grid.hpp | 3 ++- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index e2ac799..9c67184 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -4,7 +4,7 @@ //==================== Constructor and Destructor ==================== //Constructor -Game::Game() : m_grid(), m_score(0), m_nbMove(0){ +Game::Game() : Grid(), m_nbMove(0), m_score(0){ } //Destructor @@ -21,21 +21,21 @@ bool Game::swipe(kbdh::Direction direction){ switch(direction){ case kbdh::Left: - moveDone=m_grid.swipeLeft(); + moveDone=swipeLeft(); break; case kbdh::Right: - moveDone=m_grid.swipeRight(); + moveDone=swipeRight(); break; case kbdh::Up: - moveDone=m_grid.swipeUp(); + moveDone=swipeUp(); break; case kbdh::Down: - moveDone=m_grid.swipeDown(); + moveDone=swipeDown(); break; } if(moveDone){ - m_score+=m_grid.getLastMoveScore(); + m_score+=m_lastMoveScore; m_nbMove++; this->popRandomNumber(); } @@ -46,17 +46,17 @@ bool Game::swipe(kbdh::Direction direction){ //Cout the grid void Game::coutGrid(){ - std::cout << m_grid.description(); + std::cout << this->description(); } //Return true if the game is lost. False else. -bool Game::isOver(){ - return m_grid.isOver(); -} +//bool Game::isOver(){ + //return m_grid.isOver(); +//} //Pop a random number on the grid void Game::popRandomNumber(){ - std::tuple coord(m_grid.getRandomEmptyCellCoord()); + std::tuple coord(Grid::getRandomEmptyCellCoord()); int percent=rand() % 100; @@ -70,7 +70,7 @@ void Game::popRandomNumber(){ } - m_grid.setCell(coord, number); + Grid::setCell(coord, number); } //==================== Getters and Setter ==================== @@ -84,11 +84,11 @@ int Game::getNbMove(){ return m_nbMove; } -std::vector > Game::getGrid(){ - return m_grid.getGrid(); -} +//std::vector > Game::getGrid(){ + //return m_grid.getGrid(); +//} -int Game::maxStrLenInGrid(){ - return m_grid.maxStrLenInGrid(); -} +//int Game::maxStrLenInGrid(){ + //return m_grid.maxStrLenInGrid(); +//} diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp index e5d19e5..77be7d7 100644 --- a/src/Model/Game.hpp +++ b/src/Model/Game.hpp @@ -13,11 +13,11 @@ #include "Grid.hpp" #include -class Game +class Game : public Grid { private: //Members - Grid m_grid; + //Grid m_grid; int m_score; int m_nbMove; @@ -30,13 +30,13 @@ class Game bool swipe(kbdh::Direction direction); void coutGrid(); void popRandomNumber(); - bool isOver(); + //bool isOver(); //Getters and Setters int getScore(); int getNbMove(); - int maxStrLenInGrid(); - std::vector > getGrid(); + //int maxStrLenInGrid(); + //std::vector > getGrid(); }; #endif diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp index 604a6ff..22416b0 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -16,8 +16,9 @@ class Grid { private: //Members - int m_size; + protected: std::vector > m_grid; + int m_size; int m_lastMoveScore; //Private methods