-
Notifications
You must be signed in to change notification settings - Fork 1
/
Grid.cpp
208 lines (191 loc) · 6.43 KB
/
Grid.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "pch.h"
#include "Grid.h"
#include <iostream>
int Grid::getNeighbours(int x, int z)
{
int neighbourCount = 0;
if (cellMatrix[(x - 1)][z].GetState() == 1 && x != 0) // middle left
neighbourCount++;
if (cellMatrix[(x - 1)][(z - 1)].GetState() == 1 && x != 0 && z != 0) // top left
neighbourCount++;
if (cellMatrix[(x - 1)][(z + 1)].GetState() == 1 && x != 0 && z != gridSize) // bottom left
neighbourCount++;
if (cellMatrix[(x)][(z - 1)].GetState() == 1 && z != 0) // middle top
neighbourCount++;
if (cellMatrix[(x)][(z + 1)].GetState() == 1 && z != gridSize) // middle bottom
neighbourCount++;
if (cellMatrix[(x + 1)][z].GetState() == 1 && x != gridSize) // middle right
neighbourCount++;
if (cellMatrix[(x + 1)][(z + 1)].GetState() == 1 && x != gridSize && z != gridSize) // top right
neighbourCount++;
if (cellMatrix[(x + 1)][(z - 1)].GetState() == 1 && x != gridSize && z != 0) // bottom right
neighbourCount++;
return neighbourCount;
}
void Grid::initializeGrid()
{
int border = 1;
m_isPlayerSet = false;
m_isTreasureSet = false;
m_LevelSolvable = false;
m_distance = false;
m_searchResult = -1;
Clear();
m_aStar.ResetDistance();
// i is x, j is z
while (!m_isPlayerSet && !m_isTreasureSet && !m_LevelSolvable)
{
for (int i = 0; i <= gridSize - 1; i++)
{
for (int j = 0; j <= gridSize - 1; j++)
{
if (i == 0 || i == gridSize - 1 || j == 0 || j == gridSize - 1) {
cellMatrix[i][j].SetState(border);
stateMatrix[j][i] = border;
}
else {
if (!m_isPlayerSet)
{
int randomValue = (rand() % 100 + 1);
if (randomValue > 70 - probabilityToBeAlive)
{
cellMatrix[i][j].SetState(2); // player
m_cellPlayer = cellMatrix[i][j];
stateMatrix[j][i] = 2;
m_playerIndex = make_pair(j, i);
m_isPlayerSet = true;
continue;
}
}
if (!m_isTreasureSet)
{
int randomValue = (rand() % 100 + 1);
if(randomValue % probabilityToBeAlive == 0)
{
cellMatrix[i][j].SetState(3); // treasurebox
m_cellTreasure = cellMatrix[i][j];
stateMatrix[j][i] = 3;
m_treasureIndex = make_pair(j, i);
m_isTreasureSet = true;
continue;
}
}
//else {
int randomValue = (rand() % 100 + 1);
bool aliveState = randomValue > 100 - probabilityToBeAlive ? 1 : 0;
cellMatrix[i][j].SetState(aliveState);
stateMatrix[j][i] = aliveState;
//}
}
}
}
// Once ready from initalisation, check solvability and distance
m_searchResult = m_aStar.aStarSearch(stateMatrix, m_playerIndex, m_treasureIndex);
if (*m_aStar.GetDistance() > 10)
{
m_distance = true;
}
else {
m_distance = false;
}
if (m_searchResult == 1 && m_distance)
{
m_LevelSolvable = true;
}
}
}
int* Grid::GetDistance()
{
int distance = *m_aStar.GetDistance();
int* distancePointer = &distance;
return distancePointer;
}
void Grid::ResetPlayerInStateMatrix(int r, int c)
{
for (int z = 0; z < gridSize - 1; z++)
{
for (int x = 0; x < gridSize - 1; x++)
{
if (cellMatrix[z][x].GetState() == 2)
{
stateMatrix[r][c] = 2;
m_playerIndex = make_pair(r, c);
}
}
}
}
void Grid::nextGeneration()
{
gridInitialised = false; // collisions
int nextGrid[101][101];
for (int z = 0; z < gridSize - 1; z++)
for (int x = 0; x < gridSize - 1; x++)
if (z == 0 || z == gridSize - 1 || x == 0 || x == gridSize - 1) {
// border, do nothing
}
else {
nextGrid[x][z] = getNeighbours(x, z);
}
for (int z = 0; z < gridSize - 1; z++)
for (int x = 0; x < gridSize - 1; x++)
{
if (z == 0 || z == gridSize - 1 || x == 0 || x == gridSize - 1) {
// border, do nothing
}
else {
if (cellMatrix[x][z].GetState() == 1)
{
if (nextGrid[x][z] > 3)
{
cellMatrix[x][z].SetState(0);
haveChanged.push_back(CellPoint(x, z, 0));
}
if (nextGrid[x][z] < 2)
{
cellMatrix[x][z].SetState(0);
haveChanged.push_back(CellPoint(x, z, 0));
}
}
else if (cellMatrix[x][z].GetState() == 3)
{
// do nothing to treasurebox
}
else
{
if (nextGrid[x][z] == 3) {
cellMatrix[x][z].SetState(1);
haveChanged.push_back(CellPoint(x, z, 1));
}
}
}
}
}
//Clear grid and stateMatrix
void Grid::Clear()
{
for (int z = 0; z < gridSize; z++)
{
for (int x = 0; x < gridSize; x++)
{
stateMatrix[z][x] = -1;
cellMatrix[x][z].SetState(-1);
}
}
}
//Return grid size
int Grid::Size() {
return gridSize;
}
//GridInitialisation
bool Grid::GetInitialised() {
return gridInitialised;
}
void Grid::SetInitialised(bool state) {
gridInitialised = state;
}
Cell Grid::GetTreasureCell() {
return m_cellTreasure;
}
Cell Grid::GetPlayerCell() {
return m_cellPlayer;
}