Correct space

This commit is contained in:
manzerbredes 2015-05-01 10:24:45 +02:00
parent 40e0b381c1
commit 9f07e3c6bd
2 changed files with 107 additions and 107 deletions

View file

@ -14,17 +14,17 @@
class Game class Game
{ {
private: private:
Grid * m_grid; Grid * m_grid;
public: public:
Game(); Game();
~Game(); ~Game();
void play(); void play();
void pop(); void pop();
void showGrid(); void showGrid();
bool isOver(); bool isOver();
}; };
#endif #endif

View file

@ -3,7 +3,7 @@
Grid::Grid(int size) Grid::Grid(int size)
{ {
m_size = 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++)
{ {
@ -39,118 +39,118 @@ void Grid::show()
bool Grid::isEmpty(int i, int j) bool Grid::isEmpty(int i, int j)
{ {
if (i >= 0 && i < m_size && j >= 0 && j < m_size) if (i >= 0 && i < m_size && j >= 0 && j < m_size)
return m_table[i][j]->isEmpty(); return m_table[i][j]->isEmpty();
return false; return false;
} }
bool Grid::gridIsFull() bool Grid::gridIsFull()
{ {
bool isFull = true; bool isFull = true;
for (int i = 0; i < m_size && isFull; i++) for (int i = 0; i < m_size && isFull; i++)
{ {
for (int j = 0; j < m_size && isFull; j++) for (int j = 0; j < m_size && isFull; j++)
{ {
if (m_table[i][j]->isEmpty()) if (m_table[i][j]->isEmpty())
isFull = false; isFull = false;
} }
} }
return isFull; return isFull;
} }
void Grid::setCell(int i, int j, Cell *cell) void Grid::setCell(int i, int j, Cell *cell)
{ {
if (i >= 0 && i < m_size && j >= 0 && j < m_size) if (i >= 0 && i < m_size && j >= 0 && j < m_size)
{ {
delete m_table[i][j]; delete m_table[i][j];
m_table[i][j] = cell; m_table[i][j] = cell;
} }
} }
void Grid::move(Direction direction) void Grid::move(Direction direction)
{ {
switch (direction) switch (direction)
{ {
case UP: case UP:
break; break;
case DOWN: case DOWN:
break; break;
case LEFT: case LEFT:
break; break;
case RIGHT: case RIGHT:
break; break;
default: default:
break; break;
} }
} }
void Grid::moveDown() void Grid::moveDown()
{/* {/*
for (int i = 0 ; i < m_size; i++) for (int i = 0 ; i < m_size; i++)
{ {
// If the column is full, check the next column // If the column is full, check the next column
bool columnIsFull = true; bool columnIsFull = true;
for (int j = 0; j < m_size; j++) for (int j = 0; j < m_size; j++)
{ {
if (m_table[j][i]->isEmpty()) if (m_table[j][i]->isEmpty())
{ {
columnIsFull = false; columnIsFull = false;
break; break;
} }
} }
while (!columnIsFull) while (!columnIsFull)
{ {
// Calculate the first line to merge // Calculate the first line to merge
int firstLine = m_size - 1; int firstLine = m_size - 1;
while (m_table[firstLine][i]->isEmpty() && firstLine > 0) while (m_table[firstLine][i]->isEmpty() && firstLine > 0)
firstLine--; firstLine--;
if (firstLine == 0) if (firstLine == 0)
break; break;
// Calculate the second line to merge // Calculate the second line to merge
int secondLine = firstLine - 1; int secondLine = firstLine - 1;
while (m_table[secondLine][i]->isEmpty() && secondLine > 0) while (m_table[secondLine][i]->isEmpty() && secondLine > 0)
secondLine--; secondLine--;
// If there is only one element, pull it down // If there is only one element, pull it down
if (m_table[secondLine][i]->isEmpty() && firstLine != m_size - 1) if (m_table[secondLine][i]->isEmpty() && firstLine != m_size - 1)
{ {
Cell * originalCell = m_table[firstLine][i]; Cell * originalCell = m_table[firstLine][i];
Cell * finalCell = m_table[m_size-1][i]; Cell * finalCell = m_table[m_size-1][i];
m_table[firstLine][i] = finalCell; m_table[firstLine][i] = finalCell;
m_table[m_size-1][i] = originalCell; m_table[m_size-1][i] = originalCell;
break; break;
} }
// If there is only one element which is at the full bottom, break the loop // If there is only one element which is at the full bottom, break the loop
if (m_table[secondLine][i]->isEmpty()) if (m_table[secondLine][i]->isEmpty())
break; break;
// If there are two "good" elements, begin the merge process // If there are two "good" elements, begin the merge process
Cell * cell1 = m_table[firstLine][i]; Cell * cell1 = m_table[firstLine][i];
Cell * cell2 = m_table[secondLine][i]; Cell * cell2 = m_table[secondLine][i];
Cell * mergedCell = NULL; Cell * mergedCell = NULL;
// If the two cells are the same, merge them // If the two cells are the same, merge them
if (cell1->equals(cell2)) if (cell1->equals(cell2))
{ {
int value = std::stoi(cell1->getValue()); int value = std::stoi(cell1->getValue());
mergedCell = new Cell(std::to_string(value)); mergedCell = new Cell(std::to_string(value));
} }
} }
}*/ }*/
} }