FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pathfinder_example/models/PathFinder.cpp at master · Hackjaku/pathfinder_example · GitHub
Hackjaku
/
pathfinder_example
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pathfinder_example
/
models
/
PathFinder.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
217 lines (196 loc) · 6.03 KB
Breadcrumbs
pathfinder_example
/
models
/
PathFinder.cpp
Copy path
File metadata and controls
217 lines (196 loc) · 6.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
209
210
211
212
213
214
215
216
217
#
include
"
PathFinder.h
"
using
namespace
std
;
//
? CONTAINS
bool
operator
<= (vector<Vertex> vec, Vertex element) {
for
(
const
auto
&value : vec) {
if
(value.
coords
== element.
coords
) {
return
true
;
}
}
return
false
;
}
PathFinder::PathFinder
(vector<vector<
char
>> mazeVec) {
this
->
maze
= mazeVec;
}
void
PathFinder::addVisited
() {
for
(
const
auto
adj :
this
->
adjacent
) {
if
(
this
->
visited
<= adj) {
continue
;
}
else
{
if
(
this
->
maze
[adj.
coords
.
first
][adj.
coords
.
second
] ==
'
.
'
) {
this
->
visited
.
push_back
(adj);
}
}
}
}
int
PathFinder::getVisitedDistanceFromCoords
(
int
x,
int
y) {
for
(
auto
&v :
this
->
visited
) {
if
(v.
coords
.
first
== x && v.
coords
.
second
== y) {
return
v.
distance
;
}
}
return
-
1
;
}
void
PathFinder::printShortestPath
() {
if
(
this
->
maze
.
empty
()) {
cout <<
"
\033
[1;34mno maze found
\033
[0m
\n\n
"
;
return
;
}
this
->
visited
.
push_back
(
this
->
start
);
for
(
int
i =
0
; i <
this
->
visited
.
size
(); ++i) {
this
->
addAdjacents
(
this
->
visited
[i]);
this
->
addVisited
();
this
->
adjacent
.
clear
();
}
//
Vertex destination = Vertex(this->maze.size() - 1, this->maze[0].size() - 1);
//
return (this->visitedV <= destination);
vector<Vertex> shortestPath;
if
(
this
->
visited
<=
this
->
destination
) {
int
distance =
0
;
for
(
const
auto
&val :
this
->
visited
) {
if
(val.
coords
==
this
->
destination
.
coords
) {
distance = val.
distance
-
1
;
shortestPath.
push_back
(val);
break
;
}
}
auto
dest =
this
->
destination
;
while
(distance >=
0
) {
auto
adj =
getAdjacents
(dest);
for
(
const
auto
&val : adj) {
if
(val.
distance
== distance) {
shortestPath.
push_back
(val);
dest = val;
--distance;
adj.
clear
();
break
;
}
}
}
}
else
{
cout <<
"
\033
[1;34munreachable
\033
[0m
\n\n
"
;
}
for
(
int
i =
0
; i <
this
->
maze
.
size
(); ++i) {
for
(
int
j =
0
; j <
this
->
maze
[
0
].
size
(); ++j) {
if
(
this
->
maze
[i][j] ==
'
.
'
) {
Vertex v =
Vertex
(i, j,
0
);
if
(v.
coords
==
this
->
destination
.
coords
) {
cout <<
"
\033
[1;36mX
\033
[0m
"
;
}
else
if
(v.
coords
==
this
->
start
.
coords
) {
cout <<
"
\033
[1;33mO
\033
[0m
"
;
}
else
if
(shortestPath <= v) {
cout <<
"
\033
[1;32m.
\033
[0m
"
;
}
else
{
cout <<
"
\033
[1;34m.
\033
[0m
"
;
}
}
else
{
cout <<
"
\033
[1;31m+
\033
[0m
"
;
}
}
cout << endl;
}
cout << endl << endl;
}
vector<Vertex>
PathFinder::getAdjacents
(Vertex v) {
vector<Vertex> adj;
for
(
auto
&val :
this
->
visited
) {
if
(val.
coords
.
first
== v.
coords
.
first
) {
if
(val.
coords
.
second
== v.
coords
.
second
+
1
|| val.
coords
.
second
== v.
coords
.
second
-
1
) {
adj.
push_back
(val);
continue
;
}
}
if
(val.
coords
.
second
== v.
coords
.
second
) {
if
(val.
coords
.
first
== v.
coords
.
first
+
1
|| val.
coords
.
first
== v.
coords
.
first
-
1
) {
adj.
push_back
(val);
continue
;
}
}
if
(adj.
size
() ==
4
) {
break
;
}
}
return
adj;
}
Vertex::Vertex
(
int
row,
int
col,
int
dist) {
this
->
coords
.
first
= row;
this
->
coords
.
second
= col;
this
->
distance
= dist;
}
Vertex::Vertex
() {
this
->
coords
.
first
=
0
;
this
->
coords
.
second
=
0
;
this
->
distance
=
0
;
}
void
PathFinder::addAdjacents
(Vertex v) {
Vertex vt;
if
(v.
coords
.
first
!=
0
) {
vt.
coords
.
first
= v.
coords
.
first
-
1
;
vt.
coords
.
second
= v.
coords
.
second
;
vt.
distance
= v.
distance
+
1
;
this
->
adjacent
.
push_back
(vt);
}
if
(v.
coords
.
second
!=
0
) {
vt.
coords
.
first
= v.
coords
.
first
;
vt.
coords
.
second
= v.
coords
.
second
-
1
;
vt.
distance
= v.
distance
+
1
;
this
->
adjacent
.
push_back
(vt);
}
if
(v.
coords
.
first
<
this
->
maze
.
size
() -
1
) {
vt.
coords
.
first
= v.
coords
.
first
+
1
;
vt.
coords
.
second
= v.
coords
.
second
;
vt.
distance
= v.
distance
+
1
;
this
->
adjacent
.
push_back
(vt);
}
if
(v.
coords
.
second
<
this
->
maze
[
0
].
size
() -
1
) {
vt.
coords
.
first
= v.
coords
.
first
;
vt.
coords
.
second
= v.
coords
.
second
+
1
;
vt.
distance
= v.
distance
+
1
;
this
->
adjacent
.
push_back
(vt);
}
}
void
PathFinder::printMaze
() {
if
(
this
->
maze
.
empty
()) {
cout <<
"
\033
[1;34mno maze found
\033
[0m
\n\n
"
;
return
;
}
for
(
int
i =
0
; i <
this
->
maze
.
size
() +
1
; ++i) {
for
(
int
j =
0
; j <
this
->
maze
[
0
].
size
() +
1
; ++j) {
if
(i ==
0
&& j >
0
) {
cout <<
"
\033
[1;35m
"
<< ((j -
1
) %
10
) <<
"
\033
[0m
"
;
continue
;
}
else
if
(j ==
0
&& i >
0
) {
cout <<
"
\033
[1;35m
"
<< ((i -
1
) %
10
) <<
"
\033
[0m
"
;
continue
;
}
else
if
(j ==
0
&& i ==
0
) {
cout <<
"
"
;
}
else
if
(maze[i -
1
][j -
1
] ==
'
.
'
) {
cout <<
"
\033
[1;34m.
\033
[0m
"
;
}
else
if
(maze[i -
1
][j -
1
] ==
'
+
'
) {
cout <<
"
\033
[1;31m+
\033
[0m
"
;
}
}
cout << endl;
}
cout << endl;
}
void
PathFinder::setStart
(
int
row,
int
col) {
this
->
adjacent
.
clear
();
this
->
visited
.
clear
();
this
->
start
=
Vertex
(row, col,
0
);
}
void
PathFinder::setDestination
(
int
row,
int
col) {
this
->
adjacent
.
clear
();
this
->
visited
.
clear
();
this
->
destination
=
Vertex
(row, col,
0
);
}
PathFinder::PathFinder
() {
vector<vector<
char
>> v;
this
->
maze
= v;
}
bool
PathFinder::hasMazeSet
() {
return
!
this
->
maze
.
empty
();
}
void
PathFinder::setMaze
(vector<vector<
char
>> maze) {
this
->
maze
= maze;
}
Back
|
FazBrowse Home
|
New Git URL