# Issues with a simple pathfinder

Hi! I decided to remake a simple pathfinder algorithm (every “found” slot tries to find slots adjacent to itself that have not been found and are not “blocked” (it is meant to be used as a wall)

The issue is the path generated. Every `Slot` has an array list of “moves”. Every possible move is set defined in the movement[] PVector array (its shorter and can be modified). Once you try to move from a “found” slot and find an adjacent slot, it will add a PVector id (movement index) to the new [moves] array list. I previously made it with Strings, so I would use something like:

``````void displayPath() {
String moves[] = split(path,",");
//decoding the movement direction
line(x,y,x+mX,y+mY);
x+= mX;
y+= mY;
}
``````

I can’t find the previous program, so yeah : P

Here is my code.

``````int w = 10, h = 10, scl = 60, dx=5, dy=5; //dx && dy are coords of the slot you want to display path from
Slot slots[][] = new Slot[w][h];
PVector movement[] = {new PVector(0,0), new PVector(1, 0), new PVector(-1, 0), new PVector(0, 1), new PVector(0, -1)}; //possible moves, movement[0] is ignored in the code
void setup() {
size(600, 600);
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) slots[i][j] = new Slot(i, j, false, false);
}
void mousePressed() {
int mx = constrain(floor(map(mouseX, 0, width, 0, w)), 0, w-1), my = constrain(floor(map(mouseY, 0, height, 0, h)), 0, h-1);
if(mouseButton == LEFT) slots[mx][my].blocked = !slots[mx][my].blocked;
if(mouseButton == RIGHT) { dx = mx; dy = my; }
}
void draw() {
background(0);
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
slots[i][j].display();
}
slots[dx][dy].displayPath();
//println(slots[dx][dy].path);
}
class Slot {
int x, y;
boolean found, blocked, origin;
ArrayList<Integer> moves = new ArrayList<Integer>(); //moves array
Slot(int x_, int y_, boolean found_, boolean blocked_) { //defining a slot
x = x_;
y = y_;
found = found_;
blocked = blocked_;
}
void display() {
fill(255);
if (blocked) fill(0);
if (found) fill(255, 0, 0);
if (origin) fill(0,0,255);
rect(x*scl, y*scl, scl, scl);
}
void displayPath() { //ISSUE IS HERE! I GET A WEIRD SEQUENCE OF MOVES!!!!
int nx = x, ny = y, px = x, py = y;
stroke(0,0,255);
for(int i = 0; i < moves.size(); i++) {
nx += movement[moves.get(i)].x;
ny += movement[moves.get(i)].y;
line((nx+0.5)*scl,(ny+0.5)*scl,(px+0.5)*scl,(py+0.5)*scl);
px = nx;
py = ny;
}
noStroke();
}
void found(ArrayList<Integer> pMoves, int move) { //called when the slot is found
moves = pMoves;
found = true;
}
void search() {
for (int i = 0; i < movement.length; i++) {
int px = int(x+movement[i].x), py = int(y + movement[i].y);
if (inBounds(px, py )) {
if (slots[px][py].blocked == false) {
if (slots[px][py].found == false) {
slots[px][py].found(moves,i);
}
}
}
}
}
}
void keyPressed() {
int mx = constrain(floor(map(mouseX, 0, width, 0, w)), 0, w-1), my = constrain(floor(map(mouseY, 0, height, 0, h)), 0, h-1);
if (key != ' ') {
slots[mx][my].found(new ArrayList<Integer>(), 0);
slots[mx][my].origin = true;
} else if(key == ' ') {
for(int i = 0; i < w; i++) for(int j = 0; j < h; j++) if(slots[j][i].found == true) slots[i][j].search();
}
}
boolean inBounds(int x, int y) {
return(x >= 0 && x < w && y >= 0 && y < h);
}

``````