End console clean game

This commit is contained in:
manzerbredes 2015-05-02 22:57:08 +02:00
parent 36d033caee
commit 1d09a0fd3a
8 changed files with 258 additions and 68 deletions

View file

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

View file

@ -4,62 +4,45 @@
ConsoleController::ConsoleController()
{
m_game = new Game();
}
ConsoleController::~ConsoleController()
{
delete m_game;
}
void ConsoleController::play()
void ConsoleController::run()
{
//Intruction msg
std::cout << "Use arrows to play !" << std::endl;
//Init keyPress
kbdh::Direction keyPress;
//Display the first grid
m_game->showGrid();
//Intruction msg
std::cout << "Use arrows to play !" << std::endl;
//Pop a random number on the grid
m_game.popRandomNumber();
//First cout grid
m_game.coutGrid();
//Start game
while (1)
while (!m_game.isOver())
{
std::cout << "Game over : " << m_game->isOver() << "fin";
//Get key press
keyPress=this->waitArrowKeyPress();
//New line for the console print arrow press
std::cout << std::endl;
//Apply move
m_game.swipe(keyPress);
//Pop a random number on the grid
m_game.popRandomNumber();
//Print keyPress
switch(keyPress){
case kbdh::Up:
std::cout << "Keypress : Up" << std::endl;
break;
case kbdh::Down:
std::cout << "Keypress : Down" << std::endl;
break;
case kbdh::Left:
std::cout << "Keypress : Left" << std::endl;
break;
case kbdh::Right:
std::cout << "Keypress : Right" << std::endl;
m_game->swipeRight();
break;
}
//Show the Grid
m_game->showGrid();
std::cout << std::endl;
//Pop new number
m_game->pop();
//Cout grid
m_game.coutGrid();
}
m_game.coutGrid();
}

View file

@ -15,12 +15,12 @@ class ConsoleController
{
private:
Game * m_game;
Game m_game;
kbdh::Direction waitArrowKeyPress();
public:
ConsoleController();
~ConsoleController();
void play();
void run();
};
#endif

View file

@ -7,3 +7,43 @@ Game::Game() : m_grid(){
Game::~Game(){
}
bool Game::swipe(kbdh::Direction direction){
switch(direction){
case kbdh::Left:
m_grid.swipeLeft();
break;
case kbdh::Right:
m_grid.swipeRight();
break;
case kbdh::Up:
m_grid.swipeUp();
break;
case kbdh::Down:
m_grid.swipeDown();
break;
}
return true;
}
void Game::coutGrid(){
std::cout << m_grid.description();
}
bool Game::isOver(){
return m_grid.isOver();
}
void Game::popRandomNumber(){
std::tuple<int, int> coord(m_grid.getRandomEmptyCellCoord());
int number=2;
m_grid.setCell(coord, number);
}

View file

@ -9,7 +9,9 @@
#include <iostream>
#include <string>
#include "../Helpers/Keyboard.hpp"
#include "Grid.hpp"
#include <tuple>
class Game
{
@ -19,7 +21,11 @@ class Game
public:
Game();
~Game();
bool swipe(kbdh::Direction direction);
void coutGrid();
void popRandomNumber();
bool isOver();
};
#endif

View file

@ -22,24 +22,48 @@ std::string Grid::description(){
//Init stringstream description
std::stringstream description;
//Get max str len of the grid
int maxStrLen=this->maxStrLenInGrid();
//Start to write description
description << "-----------------" << std::endl;
std::stringstream gridBorder;
for(int i=0;i<(maxStrLen+2)*4+1;i++){
gridBorder<<"-";
}
description << std::endl << gridBorder.str() << std::endl;
for(int i=0;i<m_size;i++){
for(int j=0;j<m_size;j++){
std::stringstream spaceCol;
for(int k=0;k<maxStrLen-std::to_string(m_grid.at(i).at(j)).size();k++){
spaceCol << " ";
}
if(m_grid.at(i).at(j) == 0)
description << "| " << " " << " ";
description << "| " << " " << spaceCol.str();
else
description << "| " << m_grid.at(i).at(j) << " ";
description << "| " << m_grid.at(i).at(j) << spaceCol.str();
}
description << "|";
description << std::endl;
}
description << "-----------------" << std::endl << std::endl;
description << gridBorder.str() << std::endl << std::endl;
//Return description
return description.str();
}
int Grid::maxStrLenInGrid(){
int max=0;
for(int i=0;i<m_size;i++){
for(int j=0;j<m_size;j++){
std::string number=std::to_string(m_grid.at(i).at(j));
if(number.size() > max)
max=number.size();
}
}
return max;
}
bool Grid::isEmpty(int i, int j){
if(m_grid.at(i).at(j) == 0)
return true;
@ -77,8 +101,8 @@ std::tuple<int, int> Grid::getRandomEmptyCellCoord(){
//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);
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;
@ -95,6 +119,140 @@ bool Grid::setCell(int i, int j, int value){
}
std::vector<int> Grid::defragmentLine(std::vector<int> line){
for(int j=0; j<m_size-1;j++){
for(int i=0; i<m_size-1;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<int> Grid::mergeLine(std::vector<int> 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;
i++;
}
}
return line;
}
std::vector<int> Grid::swipeLine(std::vector<int> line){
//Swipe line is :
//- A defragmentation
//- A merging
//- Another defragmentation
line=this->defragmentLine(line);
line=this->mergeLine(line);
line=this->defragmentLine(line);
//Return swiped line
return line;
}
//Swipe to right
void Grid::swipeRight(){
for(int i=0; i<m_size;i++){
m_grid.at(i)=this->swipeLine(m_grid.at(i));
}
}
//Swipe to right
void Grid::swipeLeft(){
for(int i=0; i<m_size;i++){
m_grid.at(i)=this->reverseLine(this->swipeLine(this->reverseLine(m_grid.at(i))));
}
}
void Grid::swipeUp(){
for(int i=0; i<m_size;i++){
std::vector<int> colVect=this->getCol(i);
this->setCol(i,this->reverseLine(this->swipeLine(this->reverseLine(colVect))));
}
}
void Grid::swipeDown(){
for(int i=0; i<m_size;i++){
std::vector<int> colVect=this->getCol(i);
this->setCol(i,this->swipeLine(colVect));
}
}
void Grid::setCol(int col, std::vector<int> colVect){
for(int i=0;i<m_size;i++){
m_grid.at(i).at(col)=colVect.at(i);
}
}
std::vector<int> Grid::getCol(int col){
std::vector<int> colVect;
for(int i=0;i<m_size;i++){
colVect.push_back(m_grid.at(i).at(col));
}
return colVect;
}
std::vector<int> Grid::reverseLine(std::vector<int> line){
std::vector<int> 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;i<m_size;i++){
for(int j=0;j<m_size;j++){
if(m_grid.at(i).at(j) == 0)
return false;
}
}
return true;
}
bool Grid::isOver(){
if(!this->isFull())
return false;
for(int i=0;i<m_size;i++){
for(int j=0;j<m_size-1;j++){
if(m_grid.at(i).at(j) == m_grid.at(i).at(j+1))
return false;
}
}
for(int i=0;i<m_size;i++){
std::vector<int> colVect(this->getCol(i));
for(int j=0;j<m_size-1;j++){
if(colVect.at(j) == colVect.at(j+1))
return false;
}
}
return true;
}

View file

@ -18,6 +18,7 @@ class Grid
int m_size;
std::vector<std::vector<int> > m_grid;
int maxStrLenInGrid();
public:
Grid();
~Grid();
@ -28,6 +29,24 @@ class Grid
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);
std::vector<int> defragmentLine(std::vector<int> line);
std::vector<int> mergeLine(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);
//Moves
void swipeRight();
void swipeLeft();
void swipeUp();
void swipeDown();
};

View file

@ -1,4 +1,3 @@
//----- STD include -----
#include <iostream>
#include <string>
@ -7,40 +6,25 @@
//----------------------
//----- Personnal include -----
#include "./Model/Grid.hpp"
#include "./Controllers/ConsoleController/ConsoleController.hpp"
//-----------------------------
//#include "./Model/Elements/StringElement.hpp"
//----- Start -----
//----- Start -----
int main()
{
//Init random
srand(time(NULL));
//Init controller
ConsoleController controller;
Grid a;
std::cout << a.description();
std::cout << std::get<0>(a.getRandomEmptyCellCoord()) << ","<< std::get<1>(a.getRandomEmptyCellCoord());
while(1){
//Run the game
controller.run();
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
//ConsoleController * controller = new ConsoleController();
//Launch game
//controller->play();
//Remove controlelr
//delete controller;
return 0;
}