summaryrefslogtreecommitdiff
path: root/src/Model
diff options
context:
space:
mode:
Diffstat (limited to 'src/Model')
-rw-r--r--src/Model/CMakeLists.txt2
-rw-r--r--src/Model/Game.cpp43
-rw-r--r--src/Model/Game.hpp16
-rw-r--r--src/Model/Grid.cpp6
-rw-r--r--src/Model/Grid.hpp6
-rw-r--r--src/Model/Stats.cpp29
-rw-r--r--src/Model/Stats.hpp30
7 files changed, 102 insertions, 30 deletions
diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt
index 888589e..4ebe544 100644
--- a/src/Model/CMakeLists.txt
+++ b/src/Model/CMakeLists.txt
@@ -1,2 +1,2 @@
#Make Model lib
-add_library(Model Grid.cpp Game.cpp)
+add_library(Model Grid.cpp Game.cpp Stats.cpp)
diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp
index 284d82b..5781f54 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_stats(){
}
//Destructor
@@ -21,22 +21,22 @@ 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_nbMove++;
+ m_stats.incScore(m_lastMoveScore);
+ m_stats.incnbMove();
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<int, int> coord(m_grid.getRandomEmptyCellCoord());
+ std::tuple<int, int> coord(Grid::getRandomEmptyCellCoord());
int percent=rand() % 100;
@@ -70,16 +70,21 @@ void Game::popRandomNumber(){
}
- m_grid.setCell(coord, number);
+ Grid::setCell(coord, number);
}
//==================== Getters and Setter ====================
//Retrieve the Score
-int Game::getScore(){
- return m_score;
+Stats Game::getStats(){
+ return m_stats;
}
-//Retrieve the number of moves
-int Game::getNbMove(){
- return m_nbMove;
-}
+
+//std::vector<std::vector<int> > Game::getGrid(){
+ //return m_grid.getGrid();
+//}
+
+
+//int Game::maxStrLenInGrid(){
+ //return m_grid.maxStrLenInGrid();
+//}
diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp
index 0b2ee4d..e468176 100644
--- a/src/Model/Game.hpp
+++ b/src/Model/Game.hpp
@@ -11,16 +11,15 @@
#include <string>
#include "../Helpers/Keyboard.hpp"
#include "Grid.hpp"
+#include "Stats.hpp"
#include <tuple>
-class Game
+class Game : public Grid
{
private:
//Members
- Grid m_grid;
- int m_score;
- int m_nbMove;
-
+ //Grid m_grid;
+ Stats m_stats;
public:
//Constructor and Destructor
Game();
@@ -30,11 +29,12 @@ class Game
bool swipe(kbdh::Direction direction);
void coutGrid();
void popRandomNumber();
- bool isOver();
+ //bool isOver();
//Getters and Setters
- int getScore();
- int getNbMove();
+ Stats getStats();
+ //int maxStrLenInGrid();
+ //std::vector<std::vector<int> > getGrid();
};
#endif
diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp
index cc0fe60..64d8048 100644
--- a/src/Model/Grid.cpp
+++ b/src/Model/Grid.cpp
@@ -11,6 +11,7 @@ Grid::Grid(): m_size(4), m_grid(4){
m_grid.at(i).push_back(0);
}
}
+ //m_grid.at(3).at(0)=2048;
}
//Destructor
@@ -345,3 +346,8 @@ std::vector<int> Grid::getCol(int col){
return colVect;
}
+
+
+std::vector<std::vector<int> > Grid::getGrid(){
+ return m_grid;
+}
diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp
index 51168a9..22416b0 100644
--- a/src/Model/Grid.hpp
+++ b/src/Model/Grid.hpp
@@ -16,12 +16,12 @@ class Grid
{
private:
//Members
- int m_size;
+ protected:
std::vector<std::vector<int> > m_grid;
+ int m_size;
int m_lastMoveScore;
//Private methods
- int maxStrLenInGrid();
public:
//Constructor and Destructor
@@ -34,6 +34,7 @@ class Grid
std::vector<int> rightMerge(std::vector<int> line);
std::vector<int> leftMerge(std::vector<int> line);
+ int maxStrLenInGrid();
//Swipe methods
bool swipeRight();
bool swipeLeft();
@@ -55,6 +56,7 @@ class Grid
std::vector<int> getCol(int col);
void setCol(int col, std::vector<int> colVect);
int getLastMoveScore();
+ std::vector<std::vector<int> > getGrid();
};
diff --git a/src/Model/Stats.cpp b/src/Model/Stats.cpp
new file mode 100644
index 0000000..8bada78
--- /dev/null
+++ b/src/Model/Stats.cpp
@@ -0,0 +1,29 @@
+#include "Stats.hpp"
+
+
+Stats::Stats() :
+ m_score(0),
+ m_nbMove(0)
+{
+
+}
+
+Stats::~Stats(){
+}
+
+
+
+void Stats::incScore(int value){
+ m_score+=value;
+}
+void Stats::incnbMove(){
+ m_nbMove++;
+}
+
+
+int Stats::getScore(){
+ return m_score;
+}
+int Stats::getNbMove(){
+ return m_nbMove;
+}
diff --git a/src/Model/Stats.hpp b/src/Model/Stats.hpp
new file mode 100644
index 0000000..49c7356
--- /dev/null
+++ b/src/Model/Stats.hpp
@@ -0,0 +1,30 @@
+#ifndef __STATS__
+#define __STATS__
+
+#include <iostream>
+
+
+
+
+
+class Stats{
+
+ private:
+ int m_score;
+ int m_nbMove;
+ public:
+
+ Stats();
+ ~Stats();
+ void incScore(int value);
+ void incnbMove();
+
+
+ int getScore();
+ int getNbMove();
+
+
+
+};
+
+#endif