Add score and nb move(s) support
This commit is contained in:
parent
30f603ecd3
commit
9977050e6e
6 changed files with 46 additions and 3 deletions
|
@ -22,6 +22,9 @@ void ConsoleController::run()
|
||||||
//Pop a random number on the grid
|
//Pop a random number on the grid
|
||||||
m_game.popRandomNumber();
|
m_game.popRandomNumber();
|
||||||
|
|
||||||
|
|
||||||
|
this->coutStats();
|
||||||
|
|
||||||
//First cout grid
|
//First cout grid
|
||||||
m_game.coutGrid();
|
m_game.coutGrid();
|
||||||
|
|
||||||
|
@ -36,6 +39,9 @@ void ConsoleController::run()
|
||||||
bool moveDone=m_game.swipe(keyPress);
|
bool moveDone=m_game.swipe(keyPress);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
this->coutStats();
|
||||||
|
|
||||||
//Cout grid
|
//Cout grid
|
||||||
m_game.coutGrid();
|
m_game.coutGrid();
|
||||||
|
|
||||||
|
@ -100,3 +106,9 @@ void ConsoleController::clearScreen(){
|
||||||
std::cout << std::endl;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ class ConsoleController
|
||||||
~ConsoleController();
|
~ConsoleController();
|
||||||
void run();
|
void run();
|
||||||
void clearScreen();
|
void clearScreen();
|
||||||
|
void coutStats();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Game::Game() : m_grid(){
|
Game::Game() : m_grid(), m_score(0), m_nbMove(0){
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::~Game(){
|
Game::~Game(){
|
||||||
|
@ -30,8 +30,11 @@ bool Game::swipe(kbdh::Direction direction){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(moveDone)
|
if(moveDone){
|
||||||
|
m_score+=m_grid.getLastMoveScore();
|
||||||
|
m_nbMove++;
|
||||||
this->popRandomNumber();
|
this->popRandomNumber();
|
||||||
|
}
|
||||||
|
|
||||||
return moveDone;
|
return moveDone;
|
||||||
}
|
}
|
||||||
|
@ -62,3 +65,11 @@ void Game::popRandomNumber(){
|
||||||
|
|
||||||
m_grid.setCell(coord, number);
|
m_grid.setCell(coord, number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Game::getScore(){
|
||||||
|
return m_score;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Game::getNbMove(){
|
||||||
|
return m_nbMove;
|
||||||
|
}
|
||||||
|
|
|
@ -17,7 +17,8 @@ class Game
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Grid m_grid;
|
Grid m_grid;
|
||||||
|
int m_score;
|
||||||
|
int m_nbMove;
|
||||||
public:
|
public:
|
||||||
Game();
|
Game();
|
||||||
~Game();
|
~Game();
|
||||||
|
@ -26,6 +27,9 @@ class Game
|
||||||
void coutGrid();
|
void coutGrid();
|
||||||
void popRandomNumber();
|
void popRandomNumber();
|
||||||
bool isOver();
|
bool isOver();
|
||||||
|
|
||||||
|
int getScore();
|
||||||
|
int getNbMove();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -158,6 +158,7 @@ std::vector<int> Grid::rightMerge(std::vector<int> line){
|
||||||
if(val1==val2){
|
if(val1==val2){
|
||||||
line.at(i)=0;
|
line.at(i)=0;
|
||||||
line.at(i+1)=val1*2;
|
line.at(i+1)=val1*2;
|
||||||
|
m_lastMoveScore+=val1*2;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,6 +173,7 @@ std::vector<int> Grid::leftMerge(std::vector<int> line){
|
||||||
if(val1==val2){
|
if(val1==val2){
|
||||||
line.at(i)=0;
|
line.at(i)=0;
|
||||||
line.at(i-1)=val1*2;
|
line.at(i-1)=val1*2;
|
||||||
|
m_lastMoveScore+=val1*2;
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -195,6 +197,7 @@ std::vector<int> Grid::swipeLine(std::vector<int> line){
|
||||||
//Swipe to right
|
//Swipe to right
|
||||||
bool Grid::swipeRight(){
|
bool Grid::swipeRight(){
|
||||||
|
|
||||||
|
m_lastMoveScore=0;
|
||||||
bool moveDone=false;
|
bool moveDone=false;
|
||||||
for(int i=0; i<m_size;i++){
|
for(int i=0; i<m_size;i++){
|
||||||
std::vector<int> swipedLine(this->rightDefragment(this->leftMerge(this->rightDefragment(m_grid.at(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
|
//Swipe to right
|
||||||
bool Grid::swipeLeft(){
|
bool Grid::swipeLeft(){
|
||||||
|
m_lastMoveScore=0;
|
||||||
bool moveDone=false;
|
bool moveDone=false;
|
||||||
for(int i=0; i<m_size;i++){
|
for(int i=0; i<m_size;i++){
|
||||||
std::vector<int> swipedLine(this->leftDefragment(this->rightMerge(this->leftDefragment(m_grid.at(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(){
|
bool Grid::swipeUp(){
|
||||||
|
m_lastMoveScore=0;
|
||||||
bool moveDone=false;
|
bool moveDone=false;
|
||||||
for(int i=0; i<m_size;i++){
|
for(int i=0; i<m_size;i++){
|
||||||
std::vector<int> colVect=this->getCol(i);
|
std::vector<int> colVect=this->getCol(i);
|
||||||
|
@ -235,6 +240,7 @@ bool Grid::swipeUp(){
|
||||||
return moveDone;
|
return moveDone;
|
||||||
}
|
}
|
||||||
bool Grid::swipeDown(){
|
bool Grid::swipeDown(){
|
||||||
|
m_lastMoveScore=0;
|
||||||
bool moveDone=false;
|
bool moveDone=false;
|
||||||
for(int i=0; i<m_size;i++){
|
for(int i=0; i<m_size;i++){
|
||||||
std::vector<int> colVect=this->getCol(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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Grid::getLastMoveScore(){
|
||||||
|
return m_lastMoveScore;
|
||||||
|
}
|
||||||
|
|
|
@ -18,7 +18,10 @@ class Grid
|
||||||
int m_size;
|
int m_size;
|
||||||
std::vector<std::vector<int> > m_grid;
|
std::vector<std::vector<int> > m_grid;
|
||||||
|
|
||||||
|
int m_lastMoveScore;
|
||||||
|
|
||||||
int maxStrLenInGrid();
|
int maxStrLenInGrid();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Grid();
|
Grid();
|
||||||
~Grid();
|
~Grid();
|
||||||
|
@ -50,6 +53,8 @@ class Grid
|
||||||
bool swipeLeft();
|
bool swipeLeft();
|
||||||
bool swipeUp();
|
bool swipeUp();
|
||||||
bool swipeDown();
|
bool swipeDown();
|
||||||
|
|
||||||
|
int getLastMoveScore();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue