My input: A list of lists with 0s and 1s. 0s being walkable blocks and 1s being walls. My starting point (1,1) and my ending point (for now we will use (2,2)).
My output: The pathway taken to get from 1,1 to 2,2.
My issue: I am accessing something out of index and I do not understand why.
Here is my code:
maze([[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 1],
[0, 0, 0, 0]]).
checkfor(X,Y,Maze) :-
nth1(Y, Maze, R),
nth1(X, R, 0),
assertz(w(X,Y)).
findadjacentsquare(OldX,OldY,X,Y,Maze) :-
nextmove(OldX,OldY,X,Y),
checkfor(X,Y,Maze).
nextmove(OldX,OldY,OldX,Y) :-
Y is OldY+1.
%Y >= 0,
%Y < 5.
nextmove(OldX,OldY,X,OldY) :-
X is OldX+1.
%X >= 0,
%X < 5.
nextmove(OldX,OldY,OldX,Y) :-
Y is OldY-1.
%Y >= 0,
%Y < 5.
nextmove(OldX,OldY,X,OldY) :-
X is OldX-1.
%X >= 0,
%X < 5.
citypath(X,Y,X,Y,Maze,Path) :-
write(Path).
citypath(OldX,OldY,X,Y,Maze,Path) :-
findadjacentsquare(OldX,OldY,X1,Y1,Maze),
citypath(X1,Y1,X,Y,Maze,[w(X1,Y1)|Path]).
So when calling it here is what I'm doing:
maze(M1), citypath(1,1,2,2,M1,Path)
Now I don't have any of the logic in here to save the path yet, but that isn't the issue. My issue is when I run this it is getting the out of local stack error and I can't seem to figure out why. I tried to hard-code constraints in the nextmove statements but that doesn't seem to be working either.