-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.cpp
More file actions
139 lines (119 loc) · 3.56 KB
/
grid.cpp
File metadata and controls
139 lines (119 loc) · 3.56 KB
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
#include <cstdlib>
#include <QDebug>
#include "grid.h"
Grid::Grid(QWidget* parent)
: fParent(parent), fWidth(0), fHeight(0),
fStart(-1),
fFinish(std::vector<int>(1,-1))
{}
Grid::Grid(QWidget* parent, int width, int height)
: fParent(parent), fWidth(width), fHeight(height),
fStart(-1),
fFinish(std::vector<int>(1,-1))
{}
QPoint Grid::GetStartPoint()
{
return QPoint(fStart%fWidth,trunc(fStart/fWidth));
}
void Grid::SetStart(int value)
{
if (fStart >= 0 && fStart < fWidth*fHeight)
GetNode(fStart)->SetIsGoal(false);
//remove old start node if within grid
if (value >= 0 && value < fWidth*fHeight)
{
//check if new start is already a finish, delete finish;
for (int i = 0; i < GetNFinish(); i++)
{
if (value == GetFinish(i)) DeleteFinish(value);
}
//Set to start
fStart = value;
GetNode(fStart)->SetIsStart();
}
else
{
DeleteStart();
}
}
void Grid::DeleteStart()
{
if (fStart >= 0 && fStart < fWidth*fHeight)
GetNode(fStart)->SetIsGoal(false);
//remove old start node if within grid
fStart = -1;
}
QPoint Grid::GetFinishPoint(int n)
{
int f = GetFinish(n);
return QPoint(f%fWidth,trunc(f/fWidth));
}
void Grid::AddFinish(int value)
{
if (value >= 0 && value < fWidth*fHeight)
{
//replace start if necessary
if (value == fStart) DeleteStart();
//check if finish already exists
bool flag = false;
for (int i = 0; i < GetNFinish(); i++)
if (value == GetFinish(i)) flag = true;
if (!flag) fFinish.push_back(value);
//Modufy node accordingly
GetNode(value)->SetIsGoal();
}
}
void Grid::DeleteFinish(int value)
{
//Check value exists within fFinish vector
bool flag = false;
for (int i = 0; i < GetNFinish(); i++)
if (GetFinish(i) == value) flag = true;
//if it does, get rid of it
if (flag)
{
GetNode(value)->SetIsGoal(false);
if (GetNFinish() > 1) fFinish.erase(
std::remove(fFinish.begin(),fFinish.end(),value),
fFinish.begin()
);
//removes element == value, and reorders remaining elements
else if (GetNFinish() == 1)
fFinish[0] = -1;
}
}
void Grid::Randomise(bool blocks, bool goal, bool start, int goals, int density)
{
if (!(blocks || goal || start)) return;
double blockChance = blocks ? density/100. : 0;
int numGoals = goal ? goals : 0;
if (start) SetStart(fWidth*fHeight*rand()/RAND_MAX);
for (int i = 0; i < numGoals; i++)
{
int p;
while (true)
{
p = fWidth*fHeight*rand()/RAND_MAX;
if (!(GetNode(p)->IsGoal() || GetNode(p)->IsStart())) break;
}
AddFinish(p);
}
for (int i= 0; i < fWidth*fHeight; i++)
{
Node* n = GetNode(i);
if (!(n->IsGoal() || n->IsStart()))
if (double(rand())/RAND_MAX < blockChance)
n->SetIsSolid();
}
}
QBrush Grid::GetBrush(int x, int y)
{
if (GetNode(GetWidth()*y + x)->IsSolid())
return QBrush(QColor(120,120,120), Qt::SolidPattern);
else if (GetNode(GetWidth()*y + x)->IsGoal())
return QBrush(Qt::green, Qt::SolidPattern);
else if (GetNode(GetWidth()*y + x)->IsStart())
return QBrush(Qt::red, Qt::SolidPattern);
else
return QBrush(Qt::white, Qt::SolidPattern);
}