/
neighbors.cpp
113 lines (96 loc) · 2.74 KB
/
neighbors.cpp
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
#include "neighbors.h"
Neighbors::Neighbors(QObject *parent, int rows, int cols, int slices) :
QObject(parent)
{
tableRows = rows;
tableCols = cols;
tableSlices = slices;
sliceSize = tableRows*tableCols;
tableSize = sliceSize*tableSlices;
}
void Neighbors::setCubeList(QList<Cube> list)
{
cubeList = list;
}
QList<Cube> Neighbors::getCubeList()
{
return cubeList;
}
void Neighbors::processRules()
{
return;
}
void Neighbors::cleartable()
{
for (int i = 0; i < cubeList.size(); ++i)
table.append(false);
}
void Neighbors::processNeighbors(int id)
{
neighborsList.clear();
countNeighborsHiteds = 0;
sliceNeighbors(id); // Fatia Atual
if (id + sliceSize < tableSize) // Fatia de Trás
sliceNeighbors(id + sliceSize);
else if (tableSlices > 1)
sliceNeighbors((id + sliceSize) % sliceSize);
if (id - sliceSize > 0) // Fatia da Frente
sliceNeighbors(id - sliceSize);
else if (tableSlices > 1)
sliceNeighbors((id + (tableSize) - sliceSize) % (tableSize));
}
static int normalizeIndex(int index)
{
if (index < 0)
return index * -1;
else
return index;
}
void Neighbors::sliceNeighbors(int id)
{
rowNeighbors(id, true); // linha atual
if (id % sliceSize >= tableRows) // linha acima
rowNeighbors(id - tableRows, false);
else
rowNeighbors(sliceSize - tableRows + id, false);
if (id % sliceSize < (sliceSize - tableRows)) // linha abaixo
rowNeighbors(id + tableRows, false);
else
rowNeighbors(normalizeIndex(sliceSize - tableRows - id), false);
}
void Neighbors::rowNeighbors(int id, bool slideCurrent)
{
if (!slideCurrent)
if (cubeList[id].isHited())
countNeighborsHiteds++; // Adiciono o vizinho do meio apenas se não estiver na fatia atual
neighborsList.append(cubeList[id]);
if (id % tableCols > 0)
{
neighborsList.append(cubeList[id-1]);
if (cubeList[id-1].isHited())
countNeighborsHiteds++; //Adiciono o vizinho na esquerda
}
else
{
neighborsList.append(cubeList[id+tableCols-1]);
if (cubeList[id+tableCols-1].isHited())
countNeighborsHiteds++; // Adiciono o vizinho do outro lado
}
if ((id+1) % tableCols > 0)
{
neighborsList.append(cubeList[id+1]);
if (cubeList[id+1].isHited())
countNeighborsHiteds++; // Adiciono o vizinho na direita
}
else
{
neighborsList.append(cubeList[id-tableCols+1]);
if (cubeList[id-tableCols+1].isHited())
countNeighborsHiteds++; // Adiciono o vizinho do outro lado
}
}
void Neighbors::tableToCubeList()
{
for (int i = 0; i < cubeList.size(); ++i)
cubeList[i].setCubeAsHited(table[i]);
}