Merge branch 'MakeClass' of github.com:manzerbredes/2P11 into MakeClass
This commit is contained in:
commit
8e94318a96
6 changed files with 243 additions and 8 deletions
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
Cell::Cell()
|
Cell::Cell()
|
||||||
{
|
{
|
||||||
m_value = " ";
|
m_value = ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
Cell::Cell(std::string value)
|
Cell::Cell(std::string value)
|
||||||
|
@ -18,6 +18,23 @@ Cell::~Cell()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Getters and setters
|
||||||
|
|
||||||
|
bool Cell::isEmpty()
|
||||||
|
{
|
||||||
|
return (m_value == ".");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Cell::getValue()
|
||||||
|
{
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Cell::equals(Cell *otherCell)
|
||||||
|
{
|
||||||
|
return (m_value == otherCell->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
// Description
|
// Description
|
||||||
|
|
||||||
std::string Cell::description()
|
std::string Cell::description()
|
||||||
|
|
|
@ -20,6 +20,10 @@ class Cell
|
||||||
Cell(std::string value);
|
Cell(std::string value);
|
||||||
~Cell();
|
~Cell();
|
||||||
|
|
||||||
|
bool isEmpty();
|
||||||
|
bool equals(Cell * otherCell);
|
||||||
|
std::string getValue();
|
||||||
|
|
||||||
// Describes the cell in a terminal
|
// Describes the cell in a terminal
|
||||||
std::string description();
|
std::string description();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
#include "Game.hpp"
|
||||||
|
|
||||||
|
Game::Game()
|
||||||
|
{
|
||||||
|
m_grid = new Grid(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
Game::~Game()
|
||||||
|
{
|
||||||
|
delete m_grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::play()
|
||||||
|
{
|
||||||
|
while(!m_grid->gridIsFull())
|
||||||
|
{
|
||||||
|
m_grid->show();
|
||||||
|
|
||||||
|
pop();
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_grid->show();
|
||||||
|
}
|
||||||
|
|
||||||
|
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("2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Game::isOver()
|
||||||
|
{
|
||||||
|
return m_grid->gridIsFull();
|
||||||
|
}
|
29
src/Model/Game.hpp
Normal file
29
src/Model/Game.hpp
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
#ifndef DEF_GAME
|
||||||
|
#define DEF_GAME
|
||||||
|
|
||||||
|
/* Game.h
|
||||||
|
* Defines the class Game
|
||||||
|
* A game allows a player to play. It contains a grid and pops numbers
|
||||||
|
* Creators : krilius, manzerbredes
|
||||||
|
* Date : 29/04/2015 */
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "Grid.hpp"
|
||||||
|
|
||||||
|
class Game
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
Grid * m_grid;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Game();
|
||||||
|
~Game();
|
||||||
|
|
||||||
|
void play();
|
||||||
|
void pop();
|
||||||
|
void showGrid();
|
||||||
|
bool isOver();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
Grid::Grid(int size)
|
Grid::Grid(int size)
|
||||||
{
|
{
|
||||||
|
m_size = size;
|
||||||
m_table = std::vector<std::vector<Cell*> >(size);
|
m_table = std::vector<std::vector<Cell*> >(size);
|
||||||
for(int i = 0 ; i < size ; i++)
|
for(int i = 0 ; i < size ; i++)
|
||||||
{
|
{
|
||||||
|
@ -17,18 +18,18 @@ Grid::Grid(int size)
|
||||||
|
|
||||||
Grid::~Grid()
|
Grid::~Grid()
|
||||||
{
|
{
|
||||||
for(int i = 0 ; i < m_table.size() ; i++)
|
for(int i = 0 ; i < m_size ; i++)
|
||||||
{
|
{
|
||||||
for(int j = 0 ; j < m_table[i].size() ; j++)
|
for(int j = 0 ; j < m_size ; j++)
|
||||||
delete m_table[i][j];
|
delete m_table[i][j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Grid::afficher()
|
void Grid::show()
|
||||||
{
|
{
|
||||||
for(int i = 0 ; i < m_table.size() ; i++)
|
for(int i = 0 ; i < m_size ; i++)
|
||||||
{
|
{
|
||||||
for(int j = 0 ; j < m_table[i].size() ; j++)
|
for(int j = 0 ; j < m_size ; j++)
|
||||||
{
|
{
|
||||||
std::cout << m_table[i][j]->description();
|
std::cout << m_table[i][j]->description();
|
||||||
}
|
}
|
||||||
|
@ -36,3 +37,120 @@ void Grid::afficher()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Grid::isEmpty(int i, int j)
|
||||||
|
{
|
||||||
|
if (i >= 0 && i < m_size && j >= 0 && j < m_size)
|
||||||
|
return m_table[i][j]->isEmpty();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Grid::gridIsFull()
|
||||||
|
{
|
||||||
|
bool isFull = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < m_size && isFull; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < m_size && isFull; j++)
|
||||||
|
{
|
||||||
|
if (m_table[i][j]->isEmpty())
|
||||||
|
isFull = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return isFull;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Grid::setCell(int i, int j, Cell *cell)
|
||||||
|
{
|
||||||
|
if (i >= 0 && i < m_size && j >= 0 && j < m_size)
|
||||||
|
{
|
||||||
|
delete m_table[i][j];
|
||||||
|
m_table[i][j] = cell;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Grid::move(Direction direction)
|
||||||
|
{
|
||||||
|
switch (direction)
|
||||||
|
{
|
||||||
|
case Direction::UP:
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Direction::DOWN:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Direction::LEFT:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Direction::RIGHT:
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Grid::moveDown()
|
||||||
|
{/*
|
||||||
|
for (int i = 0 ; i < m_size; i++)
|
||||||
|
{
|
||||||
|
// If the column is full, check the next column
|
||||||
|
bool columnIsFull = true;
|
||||||
|
|
||||||
|
for (int j = 0; j < m_size; j++)
|
||||||
|
{
|
||||||
|
if (m_table[j][i]->isEmpty())
|
||||||
|
{
|
||||||
|
columnIsFull = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!columnIsFull)
|
||||||
|
{
|
||||||
|
// Calculate the first line to merge
|
||||||
|
int firstLine = m_size - 1;
|
||||||
|
while (m_table[firstLine][i]->isEmpty() && firstLine > 0)
|
||||||
|
firstLine--;
|
||||||
|
if (firstLine == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Calculate the second line to merge
|
||||||
|
int secondLine = firstLine - 1;
|
||||||
|
while (m_table[secondLine][i]->isEmpty() && secondLine > 0)
|
||||||
|
secondLine--;
|
||||||
|
|
||||||
|
// If there is only one element, pull it down
|
||||||
|
if (m_table[secondLine][i]->isEmpty() && firstLine != m_size - 1)
|
||||||
|
{
|
||||||
|
Cell * originalCell = m_table[firstLine][i];
|
||||||
|
Cell * finalCell = m_table[m_size-1][i];
|
||||||
|
|
||||||
|
m_table[firstLine][i] = finalCell;
|
||||||
|
m_table[m_size-1][i] = originalCell;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is only one element which is at the full bottom, break the loop
|
||||||
|
if (m_table[secondLine][i]->isEmpty())
|
||||||
|
break;
|
||||||
|
|
||||||
|
// If there are two "good" elements, begin the merge process
|
||||||
|
Cell * cell1 = m_table[firstLine][i];
|
||||||
|
Cell * cell2 = m_table[secondLine][i];
|
||||||
|
|
||||||
|
Cell * mergedCell = NULL;
|
||||||
|
|
||||||
|
// If the two cells are the same, merge them
|
||||||
|
if (cell1->equals(cell2))
|
||||||
|
{
|
||||||
|
int value = std::stoi(cell1->getValue());
|
||||||
|
mergedCell = new Cell(std::to_string(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
|
|
@ -10,17 +10,30 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "ModelConstants.hpp"
|
||||||
#include "Cell.hpp"
|
#include "Cell.hpp"
|
||||||
|
|
||||||
class Grid
|
class Grid
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::vector<std::vector<Cell*> > m_table;
|
int m_size;
|
||||||
|
std::vector<std::vector<Cell*> > m_table;
|
||||||
|
|
||||||
|
void moveUp();
|
||||||
|
void moveDown();
|
||||||
|
void moveLeft();
|
||||||
|
void moveRight();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Grid(int size);
|
Grid(int size);
|
||||||
~Grid();
|
~Grid();
|
||||||
void afficher();
|
void show();
|
||||||
|
|
||||||
|
bool isEmpty(int i, int j);
|
||||||
|
bool gridIsFull();
|
||||||
|
void setCell(int i, int j, Cell * cell);
|
||||||
|
|
||||||
|
void move(Direction direction);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue