<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">% Row 1
wall([X,1]) :- X =\= 2.
% Row 2
wall([X,2]) :- X = 1; X = 4; X = 6.
% Row 3
wall([X,3]) :- X = 1; X = 2; X = 6.
% Row 4
wall([X,4]) :- X =\= 3, X =\= 5.
% Row 5
wall([X,5]) :- X = 1; X = 4; X = 6. %remove X = 4 to show that the program follows the left wall
% Row 6
wall([X,6]) :- X =\= 2.
% Start / end
start([2,1]).
end([2,6]).
% Maze Size
mazeSize([6,6]).

badSpot([X,Y]) :- wall([X,Y]).
badSpot([X,Y]) :- X &lt; 1; Y &lt; 1.
badSpot([X,Y]) :- mazeSize([W, H]), (X &gt; W; Y &gt; H).


direction([CurX, CurY], ResultingDir) :- distances([CurX, CurY], _, Directions),
    	sort(Directions, [MinDist|_]),
    	%MinDist is min(Up, min(Down, min(Left, Right))),
    	minDir(MinDist, Directions, ResultingDir).

direction([CurX, CurY], ResultingDir) :- distances([CurX, CurY], _, Directions),
    	sort(Directions, [_, SecondMin|_]),
    	%MinDist is min(Up, min(Down, min(Left, Right))),
    	minDir(SecondMin, Directions, ResultingDir).

direction([CurX, CurY], ResultingDir) :- distances([CurX, CurY], _, Directions),
    	sort(Directions, [_, _, ThirdMin|_]),
    	%MinDist is min(Up, min(Down, min(Left, Right))),
    	minDir(ThirdMin, Directions, ResultingDir).

direction([CurX, CurY], ResultingDir) :- distances([CurX, CurY], _, Directions),
    	sort(Directions, [_, _, _, Worst]),
    	%MinDist is min(Up, min(Down, min(Left, Right))),
    	minDir(Worst, Directions, ResultingDir).

%map the smallest distances to an actual direction for the move
minDir(MinDist, [Up, Down, Left, Right], ResultingDir) :- 
            (   MinDist = Up, ResultingDir = up);
        	(   MinDist = Down, ResultingDir = down);
        	(   MinDist = Left, ResultingDir = left);
        	(   MinDist = Right, ResultingDir = right).

%direction([CurX, CurY], ResultingDir) :- distances([CurX, CurY], _, [Up, Down, Left, Right]),
%    	%MinElement = list_min(Distances),
%    	ResultingDist is min(Up, min(Down, min(Left, Right))),
%    	write(ResultingDist),
%            (   (   once(member(ResutlingDist,[Up])), ResultingDir = up);
%        	(   once(member(ResutlingDist,[Down])), ResultingDir = down);
%        	(   once(member(ResutlingDist,[Left])), ResultingDir = left);
%        	(   once(member(ResutlingDist,[Right])), ResultingDir = right)).
%


distances([CurX, CurY], [EndX, EndY], [UpDist, DownDist, LeftDist, RightDist]) :- 
    	end([EndX,EndY]),
    	UpDist is sqrt((CurX-EndX)*(CurX-EndX)+(CurY-1-EndY)*(CurY-1-EndY)),
    	DownDist is sqrt((CurX-EndX)*(CurX-EndX)+(CurY+1-EndY)*(CurY+1-EndY)),
   		LeftDist is sqrt((CurX-1-EndX)*(CurX-1-EndX)+(CurY-EndY)*(CurY-EndY)),
    	RightDist is sqrt((CurX+1-EndX)*(CurX+1-EndX)+(CurY-EndY)*(CurY-EndY)).


%up
move([CurrX, CurrY], [CurrX, NextY], up) :- NextY is CurrY - 1.
%down
move([CurrX, CurrY], [CurrX, NextY], down) :- NextY is CurrY + 1.
%left
move([CurrX, CurrY], [NextX, CurrY], left) :- NextX is CurrX - 1.
%right
move([CurrX, CurrY], [NextX, CurrY], right) :- NextX is CurrX + 1.



% A solution of the maze from our current location is [] if our current location is the end.
solve(CurrentLocation, _, Path, _) :- end(CurrentLocation), Path = [CurrentLocation].


solve(CurrentLocation, BeenThere, Path, MoveDir) :- 
    	direction(CurrentLocation, MoveDir),
    	move(CurrentLocation, NewLocation, MoveDir), 
        \+badSpot(NewLocation),
        \+member(NewLocation, BeenThere),
        solve(NewLocation, [NewLocation | BeenThere], RestOfPath, _), 
        Path = [CurrentLocation | RestOfPath].
		
% Convenience rule
solve(Path) :- start(Start), solve(Start, [Start], Path, _).


drawCell(Column, Row, _) :- wall([Column, Row]), write("X"), !.
drawCell(Column, Row, _) :- start([Column, Row]), write("S"), !.
drawCell(Column, Row, _) :- end([Column, Row]), write("E"), !.
drawCell(Column, Row, Path) :- member([Column, Row], Path), write("P"), !.
drawCell(_, _, _) :- write("O").

drawRow(Row, Path) :- drawCell(1, Row, Path), tab(1), drawCell(2, Row, Path), tab(1), 
        drawCell(3, Row, Path), tab(1), drawCell(4, Row, Path), tab(1), 
        drawCell(5, Row, Path), tab(1), drawCell(6, Row, Path), nl.
                
draw :- drawRow(1, []), drawRow(2, []), drawRow(3, []), drawRow(4, []), drawRow(5, []), 
        drawRow(6, []).
draw(Path) :- drawRow(1, Path), drawRow(2, Path), drawRow(3, Path), drawRow(4, Path),
        drawRow(5, Path), drawRow(6, Path).</pre></body></html>