Make clean code

This commit is contained in:
manzerbredes 2015-05-02 19:26:01 +02:00
parent 27d646af15
commit 36d033caee
7 changed files with 103 additions and 231 deletions

View file

@ -12,7 +12,7 @@ set_property(GLOBAL PROPERTY SFML_INCLUDE_DIR "${SFML_INCLUDE_DIR}")
#Include "Includes" and "Libraries" #Include "Includes" and "Libraries"
include_directories(${SFML_INCLUDE_DIR}) include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(2P11 ${SFML_LIBRARIES} Model ConsoleController) target_link_libraries(2P11 ${SFML_LIBRARIES} Model )
add_subdirectory(./Model) add_subdirectory(./Model)
add_subdirectory(./Controllers/) #add_subdirectory(./Controllers/)

View file

@ -1,7 +1,2 @@
#Make Model lib #Make Model lib
add_library(Model Grid.cpp Game.cpp ) add_library(Model Grid.cpp Game.cpp)
target_link_libraries(Model Elements)
add_subdirectory(./Elements)

View file

@ -1,59 +1,9 @@
#include "Game.hpp" #include "Game.hpp"
Game::Game()
{
m_grid = new Grid(4); Game::Game() : m_grid(){
} }
Game::~Game() Game::~Game(){
{
delete m_grid;
}
void Game::showGrid()
{
m_grid->show();
std::cout << std::endl;
}
void Game::pop()
{
bool cellChosen = false;
int i;
int j;
while(!cellChosen)
{
i = rand() % 4;
j = rand() % 4;
if (m_grid->isEmpty(i,j))
cellChosen = true;
}
m_grid->setCell(i, j, new Cell<StringElement>(std::to_string(2)));
}
void Game::swipeRight(){
m_grid->swipeRight();
}
bool Game::isOver()
{
if(m_grid->gridIsFull()){
for(int i=0;i<m_grid->getNRows();i++){
for(int j=0;j<m_grid->getNCols()-1;j++){
if(m_grid->getCell(i,j)->equals(m_grid->getCell(i,j+1))){
return false;
}
}
}
}
else {
return false;
}
return true;
} }

View file

@ -8,24 +8,18 @@
* Date : 29/04/2015 */ * Date : 29/04/2015 */
#include <iostream> #include <iostream>
#include <cstdlib>
#include <string> #include <string>
#include "./Elements/StringElement.hpp"
#include "Grid.hpp" #include "Grid.hpp"
class Game class Game
{ {
private: private:
Grid *m_grid; Grid m_grid;
public: public:
Game(); Game();
~Game(); ~Game();
void pop();
void showGrid();
void swipeRight();
bool isOver();
}; };
#endif #endif

View file

@ -1,161 +1,100 @@
#include "Grid.hpp" #include "Grid.hpp"
//Constructor //Constructor
Grid::Grid(int size) Grid::Grid(): m_size(4), m_grid(4){
{
//Create Vector
m_size = size;
m_table = std::vector<std::vector<Cell<StringElement>*> >(size);
//Init all of line and cell //Init all cells
for(int i = 0 ; i < size ; i++) for(int i=0;i<m_size;i++){
{ for(int j=0;j<m_size;j++){
m_table[i] = std::vector<Cell<StringElement>*>(size); m_grid.at(i).push_back(0);
for (int j = 0 ; j < size ; j++)
{
Cell<StringElement> * cell = new Cell<StringElement>("");
m_table[i][j] = cell;
} }
} }
} }
//Destructor //Destructor
Grid::~Grid() Grid::~Grid(){
{
for(int i = 0 ; i < m_size ; i++)
{
for(int j = 0 ; j < m_size ; j++)
delete m_table[i][j];
}
} }
void Grid::show()
{ std::string Grid::description(){
std::cout << "_________________" << std::endl;
std::cout << std::endl; //Init stringstream description
for(int i = 0 ; i < m_size ; i++) std::stringstream description;
{
std::cout << "|"; //Start to write description
for(int j = 0 ; j < m_size ; j++) description << "-----------------" << std::endl;
{ for(int i=0;i<m_size;i++){
std::cout << " " << m_table[i][j]->description() << " |"; for(int j=0;j<m_size;j++){
if(m_grid.at(i).at(j) == 0)
description << "| " << " " << " ";
else
description << "| " << m_grid.at(i).at(j) << " ";
} }
std::cout << std::endl; description << "|";
description << std::endl;
if (i != m_size -1)
std::cout << std::endl;
} }
std::cout << "_________________" << std::endl; description << "-----------------" << std::endl << std::endl;
//Return description
return description.str();
} }
bool Grid::isEmpty(int i, int j) bool Grid::isEmpty(int i, int j){
{ if(m_grid.at(i).at(j) == 0)
if (i >= 0 && i < m_size && j >= 0 && j < m_size) return true;
return m_table[i][j]->isEmpty();
return false; return false;
} }
bool Grid::gridIsFull() std::tuple<int, int> Grid::getRandomEmptyCellCoord(){
{
for (int i = 0; i < m_size ; i++) //Init list of candidate
{ std::vector<std::tuple<int, int> > candidates;
for (int j = 0; j < m_size ; j++)
{
if (m_table[i][j]->isEmpty())
return false;
}
}
return true; //Construct list of candidates
} for(int i=0;i<m_size;i++){
for(int j=0;j<m_size;j++){
void Grid::setCell(int i, int j, Cell<StringElement> *cell) if(this->isEmpty(i,j)){
{ std::tuple<int, int> currentCandidate(i,j);
if (i >= 0 && i < m_size && j >= 0 && j < m_size) candidates.push_back(currentCandidate);
{
delete m_table[i][j];
m_table[i][j] = cell;
}
}
Cell<StringElement>* Grid::getCell(short i, short j){
return m_table[i][j];
}
int Grid::getNRows(){
return m_table[0].size();
}
int Grid::getNCols(){
return m_table.size();
}
std::vector<Cell<StringElement>* > Grid::swipeLine(std::vector<Cell<StringElement>* > line){
std::vector<Cell<StringElement>*> newLine = std::vector<Cell<StringElement>*>(4);
for (int j = 0 ; j < 3 ; j++)
{
if(j>3)
break;
Cell<StringElement> * cell = new Cell<StringElement>(line.at(j)->getElementValue());
Cell<StringElement> * cellp1 = new Cell<StringElement>(line.at(j+1)->getElementValue());
int a=atoi(cell->getElementValue().c_str());
int ap1=atoi(cellp1->getElementValue().c_str());
std::string s=std::to_string(a);
std::string sp1=std::to_string(ap1);
if(a==ap1 && a!=0){
s="";
sp1=std::to_string(a+ap1);
if(ap1 == 0)
newLine[j+1] = new Cell<StringElement>("");
else
newLine[j+1] = new Cell<StringElement>(sp1);
newLine[j] = new Cell<StringElement>(s);
j++;
}
else{
if(ap1==0)
newLine[j+1] = new Cell<StringElement>("");
else
newLine[j+1] = new Cell<StringElement>(sp1);
if(a==0)
newLine[j] = new Cell<StringElement>("");
else
newLine[j] = new Cell<StringElement>(s);
}
delete cell;
delete cellp1;
}
for (int j = 0 ; j < 3 ; j++){
if(!newLine[j]->isEmpty()){
if(newLine[j+1]->isEmpty()){
newLine[j+1]=new Cell<StringElement>(newLine[j]->getElementValue());
newLine[j]=new Cell<StringElement>("");
} }
} }
} }
for(int i=0; i<4;i++){ //If no candidate available
if(candidates.size() == 0)
return std::tuple<int, int>(-1, -1);
std::cout << "|" << newLine[i]->description() << "|"; //Select the candidates
int winnerIs(rand() % candidates.size());
//Return the candidate
return candidates.at(winnerIs);
}
//Change value of cell
bool Grid::setCell(std::tuple<int, int> coord, int value){
int i=std::get<0>(coord);
int j=std::get<1>(coord);
if(i>=0 && i<m_size && j>=0 && j<m_size){
m_grid.at(i).at(j)=value;
return true;
} }
std::cout << "done";
return newLine; return false;
} }
void Grid::swipeRight(){ //Another setCell method
std::vector<Cell<StringElement>*> a=this->swipeLine(m_table.at(0)); bool Grid::setCell(int i, int j, int value){
m_table[0]=a; std::tuple<int, int> coord(i,j);
return this->setCell(coord, value);
} }

View file

@ -8,32 +8,26 @@
* Date : 29/04/2015 */ * Date : 29/04/2015 */
#include <iostream> #include <iostream>
#include <sstream>
#include <vector> #include <vector>
#include <tuple>
//#include "ModelConstants.hpp"
#include "Cell.hpp"
#include "./Elements/StringElement.hpp"
class Grid class Grid
{ {
private: private:
int m_size; int m_size;
std::vector<std::vector<Cell<StringElement>*> > m_table; std::vector<std::vector<int> > m_grid;
public: public:
Grid(int size); Grid();
~Grid(); ~Grid();
void show();
std::string description();
bool isEmpty(int i, int j); bool isEmpty(int i, int j);
bool gridIsFull(); std::tuple<int, int> getRandomEmptyCellCoord();
int getNRows();
int getNCols();
std::vector<Cell<StringElement>* > swipeLine(std::vector<Cell<StringElement>* > line);
void swipeRight();
void setCell(int i, int j, Cell<StringElement> *cell);
Cell<StringElement>* getCell(short i, short j);
bool setCell(std::tuple<int, int> coord, int value);
bool setCell(int i, int j, int value);
}; };

View file

@ -3,14 +3,13 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <time.h> #include <time.h>
#include <tuple>
//---------------------- //----------------------
//----- Personnal include ----- //----- Personnal include -----
#include "./Model/Grid.hpp" #include "./Model/Grid.hpp"
#include "./Controllers/ConsoleController/ConsoleController.hpp"
//----------------------------- //-----------------------------
#include "./Model/Cell.hpp"
//#include "./Model/Elements/StringElement.hpp" //#include "./Model/Elements/StringElement.hpp"
//----- Start ----- //----- Start -----
@ -19,28 +18,29 @@
int main() int main()
{ {
Cell<StringElement> *cell1 = new Cell<StringElement>("");
Cell<StringElement> *cell2 = new Cell<StringElement>("i");
if(cell2->isEmpty()){
std::cout << "Empty" << std::endl;
}
else{
std::cout << "Not empty" << std::endl;
}
//Init random //Init random
srand(time(NULL)); srand(time(NULL));
Grid a;
std::cout << a.description();
std::cout << std::get<0>(a.getRandomEmptyCellCoord()) << ","<< std::get<1>(a.getRandomEmptyCellCoord());
while(1){
std::tuple<int, int> c(a.getRandomEmptyCellCoord());
a.setCell(1,2, 15);
std::cout << a.description();
std::string chaine;
std::cin >> chaine;
}
//Init console controller //Init console controller
ConsoleController * controller = new ConsoleController(); //ConsoleController * controller = new ConsoleController();
//Launch game //Launch game
controller->play(); //controller->play();
//Remove controlelr //Remove controlelr
delete controller; //delete controller;
return 0; return 0;
} }