Organize all the code
This commit is contained in:
parent
af7f2fc870
commit
4cc6c6596b
7 changed files with 57 additions and 33 deletions
|
@ -2,14 +2,22 @@
|
|||
#include <SFML/Window/Keyboard.hpp>
|
||||
#include "../../Helpers/Keyboard.hpp"
|
||||
|
||||
|
||||
//==================== Constructor and Destructor ====================
|
||||
|
||||
//Constructor
|
||||
ConsoleController::ConsoleController()
|
||||
{
|
||||
}
|
||||
|
||||
//Destructor
|
||||
ConsoleController::~ConsoleController()
|
||||
{
|
||||
}
|
||||
|
||||
//==================== Helpers ====================
|
||||
|
||||
//Run the game.
|
||||
void ConsoleController::run()
|
||||
{
|
||||
|
||||
|
@ -22,7 +30,7 @@ void ConsoleController::run()
|
|||
//Pop a random number on the grid
|
||||
m_game.popRandomNumber();
|
||||
|
||||
|
||||
//First cout stats
|
||||
this->coutStats();
|
||||
|
||||
//First cout grid
|
||||
|
@ -38,21 +46,25 @@ void ConsoleController::run()
|
|||
//Apply move
|
||||
bool moveDone=m_game.swipe(keyPress);
|
||||
|
||||
|
||||
|
||||
//Cout stats
|
||||
this->coutStats();
|
||||
|
||||
//Cout grid
|
||||
m_game.coutGrid();
|
||||
|
||||
}
|
||||
|
||||
|
||||
//Last cout stats
|
||||
this->coutStats();
|
||||
|
||||
//Last cout grid
|
||||
m_game.coutGrid();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//Wait for keypress and return the keyPress.
|
||||
kbdh::Direction ConsoleController::waitArrowKeyPress()
|
||||
{
|
||||
//Initialise keyPress
|
||||
|
@ -92,7 +104,6 @@ kbdh::Direction ConsoleController::waitArrowKeyPress()
|
|||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
||||
{
|
||||
// la touche "flèche gauche" est enfoncée : on bouge le personnage
|
||||
keyPress=kbdh::Down;
|
||||
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
||||
{
|
||||
|
@ -107,12 +118,7 @@ kbdh::Direction ConsoleController::waitArrowKeyPress()
|
|||
}
|
||||
|
||||
|
||||
void ConsoleController::clearScreen(){
|
||||
for(int i;i<100;i++){
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
//Cout the stats of the game
|
||||
void ConsoleController::coutStats(){
|
||||
|
||||
std::cout << std::endl << "Score : " << m_game.getScore() << std::endl;
|
||||
|
|
|
@ -18,10 +18,12 @@ class ConsoleController
|
|||
Game m_game;
|
||||
kbdh::Direction waitArrowKeyPress();
|
||||
public:
|
||||
//Constructor and Destructor
|
||||
ConsoleController();
|
||||
~ConsoleController();
|
||||
|
||||
//Helpers
|
||||
void run();
|
||||
void clearScreen();
|
||||
void coutStats();
|
||||
};
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace kbdh {
|
|||
|
||||
//Key arrow
|
||||
enum Direction { Up, Down, Left, Right };
|
||||
//Key arrow typedef
|
||||
typedef enum Direction Direction;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
#include "Game.hpp"
|
||||
|
||||
|
||||
//==================== Constructor and Destructor ====================
|
||||
|
||||
//Constructor
|
||||
Game::Game() : m_grid(), m_score(0), m_nbMove(0){
|
||||
}
|
||||
|
||||
//Destructor
|
||||
Game::~Game(){
|
||||
}
|
||||
|
||||
//==================== Helpers ====================
|
||||
|
||||
|
||||
//Swipe action
|
||||
bool Game::swipe(kbdh::Direction direction){
|
||||
|
||||
bool moveDone;
|
||||
|
@ -40,14 +44,17 @@ bool Game::swipe(kbdh::Direction direction){
|
|||
}
|
||||
|
||||
|
||||
//Cout the grid
|
||||
void Game::coutGrid(){
|
||||
std::cout << m_grid.description();
|
||||
}
|
||||
|
||||
//Return true if the game is lost. False else.
|
||||
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());
|
||||
|
||||
|
@ -65,11 +72,14 @@ void Game::popRandomNumber(){
|
|||
|
||||
m_grid.setCell(coord, number);
|
||||
}
|
||||
//==================== Getters and Setter ====================
|
||||
|
||||
//Retrieve the Score
|
||||
int Game::getScore(){
|
||||
return m_score;
|
||||
}
|
||||
|
||||
//Retrieve the number of moves
|
||||
int Game::getNbMove(){
|
||||
return m_nbMove;
|
||||
}
|
||||
|
|
|
@ -16,18 +16,23 @@
|
|||
class Game
|
||||
{
|
||||
private:
|
||||
//Members
|
||||
Grid m_grid;
|
||||
int m_score;
|
||||
int m_nbMove;
|
||||
|
||||
public:
|
||||
//Constructor and Destructor
|
||||
Game();
|
||||
~Game();
|
||||
|
||||
//Helpers
|
||||
bool swipe(kbdh::Direction direction);
|
||||
void coutGrid();
|
||||
void popRandomNumber();
|
||||
bool isOver();
|
||||
|
||||
//Getters and Setters
|
||||
int getScore();
|
||||
int getNbMove();
|
||||
};
|
||||
|
|
|
@ -15,45 +15,45 @@
|
|||
class Grid
|
||||
{
|
||||
private:
|
||||
//Members
|
||||
int m_size;
|
||||
std::vector<std::vector<int> > m_grid;
|
||||
|
||||
int m_lastMoveScore;
|
||||
|
||||
//Private methods
|
||||
int maxStrLenInGrid();
|
||||
|
||||
public:
|
||||
//Constructor and Destructor
|
||||
Grid();
|
||||
~Grid();
|
||||
|
||||
std::string description();
|
||||
bool isEmpty(int i, int j);
|
||||
std::tuple<int, int> getRandomEmptyCellCoord();
|
||||
|
||||
bool setCell(std::tuple<int, int> coord, int value);
|
||||
bool setCell(int i, int j, int value);
|
||||
|
||||
std::vector<int> swipeLine(std::vector<int> line);
|
||||
//Defragment and merge methods
|
||||
std::vector<int> rightDefragment(std::vector<int> line);
|
||||
std::vector<int> leftDefragment(std::vector<int> line);
|
||||
std::vector<int> rightMerge(std::vector<int> line);
|
||||
std::vector<int> leftMerge(std::vector<int> line);
|
||||
|
||||
std::vector<int> getCol(int col);
|
||||
|
||||
bool isFull();
|
||||
bool isOver();
|
||||
|
||||
void setCol(int col, std::vector<int> colVect);
|
||||
std::vector<int> reverseLine(std::vector<int> line);
|
||||
bool compareLines(std::vector<int> line1, std::vector<int> line2);
|
||||
|
||||
//Moves
|
||||
//Swipe methods
|
||||
bool swipeRight();
|
||||
bool swipeLeft();
|
||||
bool swipeUp();
|
||||
bool swipeDown();
|
||||
|
||||
//Helpers
|
||||
bool isFull();
|
||||
bool isOver();
|
||||
bool isEmpty(int i, int j);
|
||||
std::tuple<int, int> getRandomEmptyCellCoord();
|
||||
bool compareLines(std::vector<int> line1, std::vector<int> line2);
|
||||
std::vector<int> reverseLine(std::vector<int> line);
|
||||
std::string description();
|
||||
|
||||
//Getters and Setters
|
||||
bool setCell(std::tuple<int, int> coord, int value);
|
||||
bool setCell(int i, int j, int value);
|
||||
std::vector<int> getCol(int col);
|
||||
void setCol(int col, std::vector<int> colVect);
|
||||
int getLastMoveScore();
|
||||
};
|
||||
|
||||
|
|
|
@ -25,6 +25,6 @@ int main()
|
|||
//Run the game
|
||||
controller.run();
|
||||
|
||||
|
||||
//End the application
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue