Calin said:
I asked for general supervision.
Trying to read your code gives me eyestrain, but it looks what you try to do is similar to this:
https://www.geeksforgeeks.org/boundary-fill-algorithm/
When drawing a new pixel, add to that pixel the information which pixel has drawn it. Then, when the first pixel hits the goal, track back this information to get a path to the initial source.
But this won't be the shortest path. To get that, you must ensure you draw pixels in order of increasing path length from the source. Priority queue is one option to achieve this (but not a very good one):
https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-using-priority_queue-stl/?ref=rp
Calin said:
The approach to make less code is optional.
If you work on something you do not yet know how it should work, badly cluttered code will slow you down much more than if you knew.
I wrote some shortest paths code in a grid for another thread here a while back, but not sure if it's free of bugs or good at all:
int data [8*8] = {
1,1,1,1,1,1,1,1,
1,0,0,0,0,7,0,1,
1,0,0,0,0,0,0,1,
1,0,0,1,1,1,1,1,
1,0,0,1,0,9,0,1,
1,0,0,1,0,0,0,1,
1,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1};
int start = 0, target = 0;
for (int i=0; i<64; i++)
{
int x = data[i];
if (x==7) start = i;
if (x==9) target = i;
data[i] = (x == 1 ? 1000 : 999);
}
int previous[64] = {};
int stack[64];
stack[0] = target;
data[target] = 0;
int tail = 1;
for (int i=0; i<tail; i++)
{
int c = stack[i];
int distC = data[c];
int distN = distC + 1;
//if (c==start) break; // optional early out if we want only one path
int x = c&7;
int y = (c>>3);
for (int j=0; j<4; j++)
{
int n = c;
switch (j)
{
case 0: n++; break;
case 1: n--; break;
case 2: n+=8; break;
default: n-=8;
}
if (data[n] == 1000) continue;
if (distN < data[n])
{
data[n] = distN;
previous[n] = c;
stack[tail++] = n;
}
}
}
char output[8][9] = {};
for (int y=0; y<8; y++) for (int x=0; x<8; x++)
{
output[y][x] = data[y*8+x] == 1000 ? 'X' : ' ';
}
int path = start;
while (path)
{
output[path>>3][path&7] = '@';
path = previous[path];
}
for (int y=0; y<8; y++)
{
SystemTools::Log(output[y]);
SystemTools::Log("\n");
}
ouput:
XXXXXXXX
X @ X
X @@@@ X
X @XXXXX
X @X@@ X
X @X@ X
X @@@ X
XXXXXXXX