blob: ca93955801162ad2d8439382c74a5593c80312cf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include "Grid.hpp"
Grid::Grid(int size)
{
m_size = size;
m_table = std::vector<std::vector<Cell*> >(size);
for(int i = 0 ; i < size ; i++)
{
m_table[i] = std::vector<Cell*>(size);
for (int j = 0 ; j < size ; j++)
{
Cell * cell = new Cell();
m_table[i][j] = cell;
}
}
}
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()
{
for(int i = 0 ; i < m_size ; i++)
{
for(int j = 0 ; j < m_size ; j++)
{
std::cout << m_table[i][j]->description();
}
std::cout << std::endl;
}
}
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 UP:
break;
case DOWN:
break;
case LEFT:
break;
case 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));
}
}
}*/
}
|