From 4716e4d231a26c45fa7d4e38a79f4f44c3b8348a Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sat, 2 May 2015 23:11:36 +0200 Subject: [PATCH 01/11] Fixwhitespace --- src/Model/Game.hpp | 2 +- src/Model/Grid.cpp | 6 +++--- src/Model/Grid.hpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp index ab19340..040885d 100644 --- a/src/Model/Game.hpp +++ b/src/Model/Game.hpp @@ -21,7 +21,7 @@ class Game public: Game(); ~Game(); - + bool swipe(kbdh::Direction direction); void coutGrid(); void popRandomNumber(); diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index 3a8a075..30193dd 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -28,7 +28,7 @@ std::string Grid::description(){ //Start to write description std::stringstream gridBorder; for(int i=0;i<(maxStrLen+2)*4+1;i++){ - gridBorder<<"-"; + gridBorder<<"-"; } description << std::endl << gridBorder.str() << std::endl; for(int i=0;i colVect(this->getCol(i)); diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp index 21c653a..2f5160e 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -29,7 +29,7 @@ class Grid bool setCell(std::tuple coord, int value); bool setCell(int i, int j, int value); - + std::vector swipeLine(std::vector line); std::vector defragmentLine(std::vector line); std::vector mergeLine(std::vector line); From cfc5d425f68c1a98b86ef91d92ac82785d0f33a2 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sun, 3 May 2015 01:09:19 +0200 Subject: [PATCH 02/11] Correct bug on merging and pop number --- .../ConsoleController/ConsoleController.cpp | 12 +- .../ConsoleController/ConsoleController.hpp | 1 + src/Model/Game.cpp | 12 +- src/Model/Grid.cpp | 107 +++++++++++++++--- src/Model/Grid.hpp | 15 ++- 5 files changed, 116 insertions(+), 31 deletions(-) diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp index d91e807..479b25d 100644 --- a/src/Controllers/ConsoleController/ConsoleController.cpp +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -33,10 +33,11 @@ void ConsoleController::run() keyPress=this->waitArrowKeyPress(); //Apply move - m_game.swipe(keyPress); + bool moveDone=m_game.swipe(keyPress); //Pop a random number on the grid - m_game.popRandomNumber(); + if(moveDone) + m_game.popRandomNumber(); //Cout grid m_game.coutGrid(); @@ -95,3 +96,10 @@ kbdh::Direction ConsoleController::waitArrowKeyPress() return keyPress; } + + +void ConsoleController::clearScreen(){ + for(int i;i<100;i++){ + std::cout << std::endl; + } +} diff --git a/src/Controllers/ConsoleController/ConsoleController.hpp b/src/Controllers/ConsoleController/ConsoleController.hpp index 8d73f79..f2c19b8 100644 --- a/src/Controllers/ConsoleController/ConsoleController.hpp +++ b/src/Controllers/ConsoleController/ConsoleController.hpp @@ -21,6 +21,7 @@ class ConsoleController ConsoleController(); ~ConsoleController(); void run(); + void clearScreen(); }; #endif diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index 6039bc9..5be88d9 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -12,23 +12,25 @@ Game::~Game(){ bool Game::swipe(kbdh::Direction direction){ + bool moveDone; + switch(direction){ case kbdh::Left: - m_grid.swipeLeft(); + moveDone=m_grid.swipeLeft(); break; case kbdh::Right: - m_grid.swipeRight(); + moveDone=m_grid.swipeRight(); break; case kbdh::Up: - m_grid.swipeUp(); + moveDone=m_grid.swipeUp(); break; case kbdh::Down: - m_grid.swipeDown(); + moveDone=m_grid.swipeDown(); break; } - return true; + return moveDone; } diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index 30193dd..666f228 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -9,7 +9,9 @@ Grid::Grid(): m_size(4), m_grid(4){ m_grid.at(i).push_back(0); } } - + m_grid.at(0).at(0)=2; + m_grid.at(1).at(0)=2; + m_grid.at(2).at(0)=2; } //Destructor @@ -27,20 +29,23 @@ std::string Grid::description(){ //Start to write description std::stringstream gridBorder; - for(int i=0;i<(maxStrLen+2)*4+1;i++){ + for(int i=0;i<(maxStrLen+1)*4+1;i++){ gridBorder<<"-"; } description << std::endl << gridBorder.str() << std::endl; for(int i=0;i Grid::defragmentLine(std::vector line){ +std::vector Grid::rightDefragment(std::vector line){ for(int j=0; j Grid::defragmentLine(std::vector line){ return line; } -std::vector Grid::mergeLine(std::vector line){ +std::vector Grid::leftDefragment(std::vector line){ + for(int j=0; j0;i--){ + int val1=line.at(i); + int val2=line.at(i-1); + + if(val1 != 0 && val2 == 0){ + line.at(i)=0; + line.at(i-1)=val1; + } + } + } + return line; +} +std::vector Grid::rightMerge(std::vector line){ for(int i=0; i< m_size-1;i++){ int val1=line.at(i); int val2=line.at(i+1); @@ -148,15 +167,28 @@ std::vector Grid::mergeLine(std::vector line){ return line; } +std::vector Grid::leftMerge(std::vector line){ + for(int i=m_size-1; i>0;i--){ + int val1=line.at(i); + int val2=line.at(i-1); + + if(val1==val2){ + line.at(i)=0; + line.at(i-1)=val1*2; + i--; + } + } + return line; +} std::vector Grid::swipeLine(std::vector line){ //Swipe line is : //- A defragmentation //- A merging //- Another defragmentation - line=this->defragmentLine(line); - line=this->mergeLine(line); - line=this->defragmentLine(line); + line=this->rightDefragment(line); + line=this->rightMerge(line); + line=this->rightDefragment(line); //Return swiped line return line; @@ -164,31 +196,60 @@ std::vector Grid::swipeLine(std::vector line){ //Swipe to right -void Grid::swipeRight(){ +bool Grid::swipeRight(){ + + bool moveDone=false; for(int i=0; iswipeLine(m_grid.at(i)); + std::vector swipedLine(this->rightDefragment(this->leftMerge(this->rightDefragment(m_grid.at(i))))); + + if(!this->compareLines(m_grid.at(i), swipedLine)){ + moveDone=true; + m_grid.at(i)=swipedLine; + } } + return moveDone; } //Swipe to right -void Grid::swipeLeft(){ +bool Grid::swipeLeft(){ + bool moveDone=false; for(int i=0; ireverseLine(this->swipeLine(this->reverseLine(m_grid.at(i)))); + std::vector swipedLine(this->leftDefragment(this->rightMerge(this->leftDefragment(m_grid.at(i))))); + if(!this->compareLines(m_grid.at(i), swipedLine)){ + moveDone=true; + m_grid.at(i)=swipedLine; + } } + return moveDone; } -void Grid::swipeUp(){ +bool Grid::swipeUp(){ + bool moveDone=false; for(int i=0; i colVect=this->getCol(i); - this->setCol(i,this->reverseLine(this->swipeLine(this->reverseLine(colVect)))); + + std::vector swipedLine(this->leftDefragment(this->rightMerge(this->leftDefragment(colVect)))); + if(!this->compareLines(colVect, swipedLine)){ + moveDone=true; + this->setCol(i,swipedLine); + } } + return moveDone; } -void Grid::swipeDown(){ +bool Grid::swipeDown(){ + bool moveDone=false; for(int i=0; i colVect=this->getCol(i); - this->setCol(i,this->swipeLine(colVect)); + + std::vector swipedLine(this->rightDefragment(this->leftMerge(this->rightDefragment(colVect)))); + + if(!this->compareLines(colVect, swipedLine)){ + moveDone=true; + this->setCol(i,swipedLine); + } } + return moveDone; } void Grid::setCol(int col, std::vector colVect){ @@ -256,3 +317,13 @@ bool Grid::isOver(){ } + +bool Grid::compareLines(std::vector line1, std::vector line2){ + + for(int i=0;i swipeLine(std::vector line); - std::vector defragmentLine(std::vector line); - std::vector mergeLine(std::vector line); + std::vector rightDefragment(std::vector line); + std::vector leftDefragment(std::vector line); + std::vector rightMerge(std::vector line); + std::vector leftMerge(std::vector line); std::vector getCol(int col); @@ -41,12 +43,13 @@ class Grid void setCol(int col, std::vector colVect); std::vector reverseLine(std::vector line); + bool compareLines(std::vector line1, std::vector line2); //Moves - void swipeRight(); - void swipeLeft(); - void swipeUp(); - void swipeDown(); + bool swipeRight(); + bool swipeLeft(); + bool swipeUp(); + bool swipeDown(); }; From 60cb4a1e323a2f8507db2455de56e9d6737557d8 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sun, 3 May 2015 01:14:14 +0200 Subject: [PATCH 03/11] Move pop number to game class --- src/Controllers/ConsoleController/ConsoleController.cpp | 3 --- src/Model/Game.cpp | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp index 479b25d..225ed16 100644 --- a/src/Controllers/ConsoleController/ConsoleController.cpp +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -35,9 +35,6 @@ void ConsoleController::run() //Apply move bool moveDone=m_game.swipe(keyPress); - //Pop a random number on the grid - if(moveDone) - m_game.popRandomNumber(); //Cout grid m_game.coutGrid(); diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index 5be88d9..3f9fc7a 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -30,6 +30,9 @@ bool Game::swipe(kbdh::Direction direction){ break; } + if(moveDone) + this->popRandomNumber(); + return moveDone; } From 870d354afd7bcf1ef675b2bff57c5bf3be1492e6 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sun, 3 May 2015 01:23:28 +0200 Subject: [PATCH 04/11] Remove fixed grid initialisation --- src/Model/Grid.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index 666f228..7d7e6a7 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -9,9 +9,6 @@ Grid::Grid(): m_size(4), m_grid(4){ m_grid.at(i).push_back(0); } } - m_grid.at(0).at(0)=2; - m_grid.at(1).at(0)=2; - m_grid.at(2).at(0)=2; } //Destructor From 30f603ecd31e5cd849e22bc66d85da2e2d526cc5 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sun, 3 May 2015 01:29:19 +0200 Subject: [PATCH 05/11] Add 4 random pop support with a probability of 10% --- src/Model/Game.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index 3f9fc7a..2ac665f 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -48,7 +48,17 @@ bool Game::isOver(){ void Game::popRandomNumber(){ std::tuple coord(m_grid.getRandomEmptyCellCoord()); - int number=2; + int percent=rand() % 100; + + int number; + + if(percent <= 10){ + number=4; + } + else{ + number=2; + } + m_grid.setCell(coord, number); } From 9977050e6ecb92570698bbacac1a82b74e5e87a4 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sun, 3 May 2015 02:02:22 +0200 Subject: [PATCH 06/11] Add score and nb move(s) support --- .../ConsoleController/ConsoleController.cpp | 12 ++++++++++++ .../ConsoleController/ConsoleController.hpp | 1 + src/Model/Game.cpp | 15 +++++++++++++-- src/Model/Game.hpp | 6 +++++- src/Model/Grid.cpp | 10 ++++++++++ src/Model/Grid.hpp | 5 +++++ 6 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp index 225ed16..39b1073 100644 --- a/src/Controllers/ConsoleController/ConsoleController.cpp +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -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; +} diff --git a/src/Controllers/ConsoleController/ConsoleController.hpp b/src/Controllers/ConsoleController/ConsoleController.hpp index f2c19b8..59d4bb3 100644 --- a/src/Controllers/ConsoleController/ConsoleController.hpp +++ b/src/Controllers/ConsoleController/ConsoleController.hpp @@ -22,6 +22,7 @@ class ConsoleController ~ConsoleController(); void run(); void clearScreen(); + void coutStats(); }; #endif diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index 2ac665f..d75fa32 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -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; +} diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp index 040885d..bb8b995 100644 --- a/src/Model/Game.hpp +++ b/src/Model/Game.hpp @@ -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 diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index 7d7e6a7..b1b9dda 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -158,6 +158,7 @@ std::vector Grid::rightMerge(std::vector 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 Grid::leftMerge(std::vector 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 Grid::swipeLine(std::vector line){ //Swipe to right bool Grid::swipeRight(){ + m_lastMoveScore=0; bool moveDone=false; for(int i=0; i 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 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 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 colVect=this->getCol(i); @@ -324,3 +330,7 @@ bool Grid::compareLines(std::vector line1, std::vector line2){ } return true; } + +int Grid::getLastMoveScore(){ + return m_lastMoveScore; +} diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp index 09f3e5e..4ed5c10 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -18,7 +18,10 @@ class Grid int m_size; std::vector > m_grid; + int m_lastMoveScore; + int maxStrLenInGrid(); + public: Grid(); ~Grid(); @@ -50,6 +53,8 @@ class Grid bool swipeLeft(); bool swipeUp(); bool swipeDown(); + + int getLastMoveScore(); }; From d8b1466ad925c6eb80809c9218d627f806f850cd Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sun, 3 May 2015 02:14:03 +0200 Subject: [PATCH 07/11] Add final stats cout --- src/Controllers/ConsoleController/ConsoleController.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp index 39b1073..8c2d96c 100644 --- a/src/Controllers/ConsoleController/ConsoleController.cpp +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -46,6 +46,8 @@ void ConsoleController::run() m_game.coutGrid(); } + + this->coutStats(); m_game.coutGrid(); } From 98a8e2d520baf254407e589b3de8bde7c73dc871 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sun, 3 May 2015 09:14:30 +0200 Subject: [PATCH 08/11] Try to remove arrow characters printed in console --- .../ConsoleController/ConsoleController.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp index 8c2d96c..66dc370 100644 --- a/src/Controllers/ConsoleController/ConsoleController.cpp +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -65,7 +65,8 @@ kbdh::Direction ConsoleController::waitArrowKeyPress() keyPress=kbdh::Left; while(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - //Wait for release + //Wait for release and try to remove arrow printed characters + std::cout << "\r" << " "; } break; } @@ -74,7 +75,8 @@ kbdh::Direction ConsoleController::waitArrowKeyPress() keyPress=kbdh::Right; while(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - //Wait for release + //Wait for release and try to remove arrow printed characters + std::cout << "\r" << " "; } break; } @@ -83,7 +85,8 @@ kbdh::Direction ConsoleController::waitArrowKeyPress() keyPress=kbdh::Up; while(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { - //Wait for release + //Wait for release and try to remove arrow printed characters + std::cout << "\r" << " "; } break; } @@ -93,7 +96,8 @@ kbdh::Direction ConsoleController::waitArrowKeyPress() keyPress=kbdh::Down; while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { - //Wait for release + //Wait for release and try to remove arrow printed characters + std::cout << "\r" << " "; } break; } From afd4ba110faa7374b2cb0bff5056e29e985fc4e7 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sun, 3 May 2015 09:25:28 +0200 Subject: [PATCH 09/11] Optimise leftDefragmentation method --- src/Model/Grid.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index b1b9dda..ed8686f 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -137,18 +137,22 @@ std::vector Grid::rightDefragment(std::vector line){ } std::vector Grid::leftDefragment(std::vector line){ - for(int j=0; j0;i--){ - int val1=line.at(i); - int val2=line.at(i-1); - if(val1 != 0 && val2 == 0){ - line.at(i)=0; - line.at(i-1)=val1; - } - } - } - return line; + //for(int j=0; j0;i--){ + //int val1=line.at(i); + //int val2=line.at(i-1); + + //if(val1 != 0 && val2 == 0){ + //line.at(i)=0; + //line.at(i-1)=val1; + //} + //} + //} + + std::vector reversedLine= this->reverseLine(line); + + return this->reverseLine(this->rightDefragment(reversedLine)); } std::vector Grid::rightMerge(std::vector line){ for(int i=0; i< m_size-1;i++){ From af7f2fc8700df64f245caf7b864e777831867c3f Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sun, 3 May 2015 09:44:41 +0200 Subject: [PATCH 10/11] Organize Grid cpp code --- src/Model/Grid.cpp | 503 +++++++++++++++++++++++---------------------- 1 file changed, 255 insertions(+), 248 deletions(-) diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp index ed8686f..cc0fe60 100644 --- a/src/Model/Grid.cpp +++ b/src/Model/Grid.cpp @@ -1,5 +1,7 @@ #include "Grid.hpp" +//==================== Constructor and Destructor ==================== + //Constructor Grid::Grid(): m_size(4), m_grid(4){ @@ -16,6 +18,251 @@ Grid::~Grid(){ } +//==================== Merge and defragment methods ==================== + +//Defragment the line to the right +std::vector Grid::rightDefragment(std::vector line){ + for(int j=0; j Grid::leftDefragment(std::vector line){ + + std::vector reversedLine= this->reverseLine(line); + + return this->reverseLine(this->rightDefragment(reversedLine)); +} + +//Merge line to the right +std::vector Grid::rightMerge(std::vector line){ + for(int i=0; i< m_size-1;i++){ + int val1=line.at(i); + int val2=line.at(i+1); + + if(val1==val2){ + line.at(i)=0; + line.at(i+1)=val1*2; + m_lastMoveScore+=val1*2; + i++; + } + } + return line; +} + +//Merge line to the left +std::vector Grid::leftMerge(std::vector line){ + for(int i=m_size-1; i>0;i--){ + int val1=line.at(i); + int val2=line.at(i-1); + + if(val1==val2){ + line.at(i)=0; + line.at(i-1)=val1*2; + m_lastMoveScore+=val1*2; + i--; + } + } + return line; +} + + +//==================== Swipe methods ==================== + + +//Swipe to the right +bool Grid::swipeRight(){ + + m_lastMoveScore=0; + bool moveDone=false; + for(int i=0; i swipedLine(this->rightDefragment(this->leftMerge(this->rightDefragment(m_grid.at(i))))); + + if(!this->compareLines(m_grid.at(i), swipedLine)){ + moveDone=true; + m_grid.at(i)=swipedLine; + } + } + return moveDone; +} + +//Swipe to the left +bool Grid::swipeLeft(){ + m_lastMoveScore=0; + bool moveDone=false; + for(int i=0; i swipedLine(this->leftDefragment(this->rightMerge(this->leftDefragment(m_grid.at(i))))); + if(!this->compareLines(m_grid.at(i), swipedLine)){ + moveDone=true; + m_grid.at(i)=swipedLine; + } + } + return moveDone; +} + + +//Swipe to the top +bool Grid::swipeUp(){ + m_lastMoveScore=0; + bool moveDone=false; + for(int i=0; i colVect=this->getCol(i); + + std::vector swipedLine(this->leftDefragment(this->rightMerge(this->leftDefragment(colVect)))); + if(!this->compareLines(colVect, swipedLine)){ + moveDone=true; + this->setCol(i,swipedLine); + } + } + return moveDone; +} +//Swipe to the bottom +bool Grid::swipeDown(){ + m_lastMoveScore=0; + bool moveDone=false; + for(int i=0; i colVect=this->getCol(i); + + std::vector swipedLine(this->rightDefragment(this->leftMerge(this->rightDefragment(colVect)))); + + if(!this->compareLines(colVect, swipedLine)){ + moveDone=true; + this->setCol(i,swipedLine); + } + } + return moveDone; +} + +//==================== Helpers ==================== + +//Get the max len of a string of the numbers in the grid : +// Exemple: +// - 1 return 1 +// - 22 return 2 +// - 120 return 3 +// - 1000 return 4 +//This method help to calculate the columns size +int Grid::maxStrLenInGrid(){ + int max=0; + for(int i=0;i max) + max=number.size(); + } + } + return max; +} + +//Test if the cell at (i,j) is empty +bool Grid::isEmpty(int i, int j){ + if(m_grid.at(i).at(j) == 0) + return true; + return false; +} + +//Return a tuple that contain a random empty cell +std::tuple Grid::getRandomEmptyCellCoord(){ + + //Init list of candidate + std::vector > candidates; + + //Construct list of candidates + for(int i=0;iisEmpty(i,j)){ + std::tuple currentCandidate(i,j); + candidates.push_back(currentCandidate); + } + } + } + + //If no candidate available + if(candidates.size() == 0) + return std::tuple(-1, -1); + + //Select the candidates + int winnerIs(rand() % candidates.size()); + + //Return the candidate + return candidates.at(winnerIs); + +} + +//Reverse a line +std::vector Grid::reverseLine(std::vector line){ + std::vector reversedLine; + + for(int j=m_size-1; j>=0;j--){ + reversedLine.push_back(line.at(j)); + } + + return reversedLine; +} + +//Return true if the grid is full. False else. +bool Grid::isFull(){ + + for(int i=0;iisFull()) + return false; + + for(int i=0;i colVect(this->getCol(i)); + + for(int j=0;j line1, std::vector line2){ + + for(int i=0;i max) - max=number.size(); - } - } - return max; -} - -bool Grid::isEmpty(int i, int j){ - if(m_grid.at(i).at(j) == 0) - return true; - return false; -} - -std::tuple Grid::getRandomEmptyCellCoord(){ - - //Init list of candidate - std::vector > candidates; - - //Construct list of candidates - for(int i=0;iisEmpty(i,j)){ - std::tuple currentCandidate(i,j); - candidates.push_back(currentCandidate); - } - } - } - - //If no candidate available - if(candidates.size() == 0) - return std::tuple(-1, -1); - - //Select the candidates - int winnerIs(rand() % candidates.size()); - - //Return the candidate - return candidates.at(winnerIs); - +//Getter for m_lastMoveScore +int Grid::getLastMoveScore(){ + return m_lastMoveScore; } - -//Change value of cell +//Change value of cell with a tuple bool Grid::setCell(std::tuple coord, int value){ int i=std::get<0>(coord); int j=std::get<1>(coord); @@ -114,157 +321,20 @@ bool Grid::setCell(std::tuple coord, int value){ return false; } -//Another setCell method +//Change value of cell with int bool Grid::setCell(int i, int j, int value){ std::tuple coord(i,j); return this->setCell(coord, value); } - -std::vector Grid::rightDefragment(std::vector line){ - for(int j=0; j Grid::leftDefragment(std::vector line){ - - //for(int j=0; j0;i--){ - //int val1=line.at(i); - //int val2=line.at(i-1); - - //if(val1 != 0 && val2 == 0){ - //line.at(i)=0; - //line.at(i-1)=val1; - //} - //} - //} - - std::vector reversedLine= this->reverseLine(line); - - return this->reverseLine(this->rightDefragment(reversedLine)); -} -std::vector Grid::rightMerge(std::vector line){ - for(int i=0; i< m_size-1;i++){ - int val1=line.at(i); - int val2=line.at(i+1); - - if(val1==val2){ - line.at(i)=0; - line.at(i+1)=val1*2; - m_lastMoveScore+=val1*2; - i++; - } - } - return line; -} - -std::vector Grid::leftMerge(std::vector line){ - for(int i=m_size-1; i>0;i--){ - int val1=line.at(i); - int val2=line.at(i-1); - - if(val1==val2){ - line.at(i)=0; - line.at(i-1)=val1*2; - m_lastMoveScore+=val1*2; - i--; - } - } - return line; -} -std::vector Grid::swipeLine(std::vector line){ - - //Swipe line is : - //- A defragmentation - //- A merging - //- Another defragmentation - line=this->rightDefragment(line); - line=this->rightMerge(line); - line=this->rightDefragment(line); - - //Return swiped line - return line; -} - - -//Swipe to right -bool Grid::swipeRight(){ - - m_lastMoveScore=0; - bool moveDone=false; - for(int i=0; i swipedLine(this->rightDefragment(this->leftMerge(this->rightDefragment(m_grid.at(i))))); - - if(!this->compareLines(m_grid.at(i), swipedLine)){ - moveDone=true; - m_grid.at(i)=swipedLine; - } - } - return moveDone; -} - -//Swipe to right -bool Grid::swipeLeft(){ - m_lastMoveScore=0; - bool moveDone=false; - for(int i=0; i swipedLine(this->leftDefragment(this->rightMerge(this->leftDefragment(m_grid.at(i))))); - if(!this->compareLines(m_grid.at(i), swipedLine)){ - moveDone=true; - m_grid.at(i)=swipedLine; - } - } - return moveDone; -} - - -bool Grid::swipeUp(){ - m_lastMoveScore=0; - bool moveDone=false; - for(int i=0; i colVect=this->getCol(i); - - std::vector swipedLine(this->leftDefragment(this->rightMerge(this->leftDefragment(colVect)))); - if(!this->compareLines(colVect, swipedLine)){ - moveDone=true; - this->setCol(i,swipedLine); - } - } - return moveDone; -} -bool Grid::swipeDown(){ - m_lastMoveScore=0; - bool moveDone=false; - for(int i=0; i colVect=this->getCol(i); - - std::vector swipedLine(this->rightDefragment(this->leftMerge(this->rightDefragment(colVect)))); - - if(!this->compareLines(colVect, swipedLine)){ - moveDone=true; - this->setCol(i,swipedLine); - } - } - return moveDone; -} - +//Assign a vector to a column. void Grid::setCol(int col, std::vector colVect){ for(int i=0;i Grid::getCol(int col){ std::vector colVect; @@ -275,66 +345,3 @@ std::vector Grid::getCol(int col){ return colVect; } - -std::vector Grid::reverseLine(std::vector line){ - std::vector reversedLine; - - for(int j=m_size-1; j>=0;j--){ - reversedLine.push_back(line.at(j)); - } - - return reversedLine; -} - - -bool Grid::isFull(){ - - for(int i=0;iisFull()) - return false; - - for(int i=0;i colVect(this->getCol(i)); - - for(int j=0;j line1, std::vector line2){ - - for(int i=0;i Date: Sun, 3 May 2015 09:57:31 +0200 Subject: [PATCH 11/11] Organize all the code --- .../ConsoleController/ConsoleController.cpp | 28 ++++++++------ .../ConsoleController/ConsoleController.hpp | 4 +- src/Helpers/Keyboard.hpp | 1 + src/Model/Game.cpp | 12 +++++- src/Model/Game.hpp | 5 +++ src/Model/Grid.hpp | 38 +++++++++---------- src/main.cpp | 2 +- 7 files changed, 57 insertions(+), 33 deletions(-) diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp index 66dc370..1bc9b84 100644 --- a/src/Controllers/ConsoleController/ConsoleController.cpp +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -2,14 +2,22 @@ #include #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; diff --git a/src/Controllers/ConsoleController/ConsoleController.hpp b/src/Controllers/ConsoleController/ConsoleController.hpp index 59d4bb3..f3e9952 100644 --- a/src/Controllers/ConsoleController/ConsoleController.hpp +++ b/src/Controllers/ConsoleController/ConsoleController.hpp @@ -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(); }; diff --git a/src/Helpers/Keyboard.hpp b/src/Helpers/Keyboard.hpp index 9d2dfa3..c24c1ba 100644 --- a/src/Helpers/Keyboard.hpp +++ b/src/Helpers/Keyboard.hpp @@ -10,6 +10,7 @@ namespace kbdh { //Key arrow enum Direction { Up, Down, Left, Right }; + //Key arrow typedef typedef enum Direction Direction; } diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index d75fa32..284d82b 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -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 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; } diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp index bb8b995..0b2ee4d 100644 --- a/src/Model/Game.hpp +++ b/src/Model/Game.hpp @@ -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(); }; diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp index 4ed5c10..51168a9 100644 --- a/src/Model/Grid.hpp +++ b/src/Model/Grid.hpp @@ -15,45 +15,45 @@ class Grid { private: + //Members int m_size; std::vector > 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 getRandomEmptyCellCoord(); - - bool setCell(std::tuple coord, int value); - bool setCell(int i, int j, int value); - - std::vector swipeLine(std::vector line); + //Defragment and merge methods std::vector rightDefragment(std::vector line); std::vector leftDefragment(std::vector line); std::vector rightMerge(std::vector line); std::vector leftMerge(std::vector line); - std::vector getCol(int col); - - bool isFull(); - bool isOver(); - - void setCol(int col, std::vector colVect); - std::vector reverseLine(std::vector line); - bool compareLines(std::vector line1, std::vector 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 getRandomEmptyCellCoord(); + bool compareLines(std::vector line1, std::vector line2); + std::vector reverseLine(std::vector line); + std::string description(); + + //Getters and Setters + bool setCell(std::tuple coord, int value); + bool setCell(int i, int j, int value); + std::vector getCol(int col); + void setCol(int col, std::vector colVect); int getLastMoveScore(); }; diff --git a/src/main.cpp b/src/main.cpp index 49c7a20..48c1766 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,6 @@ int main() //Run the game controller.run(); - + //End the application return 0; }