Add score and nb move(s) support

This commit is contained in:
manzerbredes 2015-05-03 02:02:22 +02:00
parent 30f603ecd3
commit 9977050e6e
6 changed files with 46 additions and 3 deletions

View file

@ -22,6 +22,9 @@ void ConsoleController::run()
//Pop a random number on the grid
m_game.popRandomNumber();
this->coutStats();
//First cout grid
m_game.coutGrid();
@ -36,6 +39,9 @@ void ConsoleController::run()
bool moveDone=m_game.swipe(keyPress);
this->coutStats();
//Cout grid
m_game.coutGrid();
@ -100,3 +106,9 @@ void ConsoleController::clearScreen(){
std::cout << std::endl;
}
}
void ConsoleController::coutStats(){
std::cout << std::endl << "Score : " << m_game.getScore() << std::endl;
std::cout << "Nombre de coups : " << m_game.getNbMove() << std::endl;
}

View file

@ -22,6 +22,7 @@ class ConsoleController
~ConsoleController();
void run();
void clearScreen();
void coutStats();
};
#endif

View file

@ -2,7 +2,7 @@
Game::Game() : m_grid(){
Game::Game() : m_grid(), m_score(0), m_nbMove(0){
}
Game::~Game(){
@ -30,8 +30,11 @@ bool Game::swipe(kbdh::Direction direction){
break;
}
if(moveDone)
if(moveDone){
m_score+=m_grid.getLastMoveScore();
m_nbMove++;
this->popRandomNumber();
}
return moveDone;
}
@ -62,3 +65,11 @@ void Game::popRandomNumber(){
m_grid.setCell(coord, number);
}
int Game::getScore(){
return m_score;
}
int Game::getNbMove(){
return m_nbMove;
}

View file

@ -17,7 +17,8 @@ class Game
{
private:
Grid m_grid;
int m_score;
int m_nbMove;
public:
Game();
~Game();
@ -26,6 +27,9 @@ class Game
void coutGrid();
void popRandomNumber();
bool isOver();
int getScore();
int getNbMove();
};
#endif

View file

@ -158,6 +158,7 @@ std::vector<int> Grid::rightMerge(std::vector<int> line){
if(val1==val2){
line.at(i)=0;
line.at(i+1)=val1*2;
m_lastMoveScore+=val1*2;
i++;
}
}
@ -172,6 +173,7 @@ std::vector<int> Grid::leftMerge(std::vector<int> line){
if(val1==val2){
line.at(i)=0;
line.at(i-1)=val1*2;
m_lastMoveScore+=val1*2;
i--;
}
}
@ -195,6 +197,7 @@ std::vector<int> Grid::swipeLine(std::vector<int> line){
//Swipe to right
bool Grid::swipeRight(){
m_lastMoveScore=0;
bool moveDone=false;
for(int i=0; i<m_size;i++){
std::vector<int> swipedLine(this->rightDefragment(this->leftMerge(this->rightDefragment(m_grid.at(i)))));
@ -209,6 +212,7 @@ bool Grid::swipeRight(){
//Swipe to right
bool Grid::swipeLeft(){
m_lastMoveScore=0;
bool moveDone=false;
for(int i=0; i<m_size;i++){
std::vector<int> swipedLine(this->leftDefragment(this->rightMerge(this->leftDefragment(m_grid.at(i)))));
@ -222,6 +226,7 @@ bool Grid::swipeLeft(){
bool Grid::swipeUp(){
m_lastMoveScore=0;
bool moveDone=false;
for(int i=0; i<m_size;i++){
std::vector<int> colVect=this->getCol(i);
@ -235,6 +240,7 @@ bool Grid::swipeUp(){
return moveDone;
}
bool Grid::swipeDown(){
m_lastMoveScore=0;
bool moveDone=false;
for(int i=0; i<m_size;i++){
std::vector<int> colVect=this->getCol(i);
@ -324,3 +330,7 @@ bool Grid::compareLines(std::vector<int> line1, std::vector<int> line2){
}
return true;
}
int Grid::getLastMoveScore(){
return m_lastMoveScore;
}

View file

@ -18,7 +18,10 @@ class Grid
int m_size;
std::vector<std::vector<int> > m_grid;
int m_lastMoveScore;
int maxStrLenInGrid();
public:
Grid();
~Grid();
@ -50,6 +53,8 @@ class Grid
bool swipeLeft();
bool swipeUp();
bool swipeDown();
int getLastMoveScore();
};