% 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.
% Row 6
wall([X,6]) :- X =\= 2.
% Start / end
start([2,1]).
end([2,6]).
% Maze Size
mazeSize([6,6]).

%Checks location of walls, maze bounds, makes sure we cant move there
badSpot([X,Y]) :- wall([X,Y]).
badSpot([X,Y]) :- X < 1; Y < 1.
badSpot([X,Y]) :- mazeSize([W, H]), (X > W; Y > H).

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

%Prefer to move in general direction of maze exit
toExit([CurrX, CurrY], [NextX, NextY]) :- end([EndX, EndY]), 
        sqrt((EndX-CurrX)^2 + (EndY-CurrY)^2) > sqrt((EndX-NextX)^2 + (EndY-NextY)^2).

% 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) :- 
        move(CurrentLocation, NewLocation),
        toExit(CurrentLocation, NewLocation),
        \+ badSpot(NewLocation),
        \+ member(NewLocation, BeenThere),
        solve(NewLocation, [NewLocation | BeenThere], RestOfPath), 
        Path = [CurrentLocation | RestOfPath].

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