% 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).

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

%if count is greater than 3, change next location
% if Start > End, fail.
count(Start, End, Answer) :- Start > End, fail.
% if Start = End, Answer is [Start]
count(Start, End, Answer) :- Start = End, Answer = [Start].
% if Start < End
count(Start, End, Answer) :- Start < End, Next is Start + 1, 
	Answer = [Start | TheRest], count(Next, End, TheRest).


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


%My idea was to implement a heuristic that will move in one direction 3 times, then choose a
%different random direction to move in until the end of the maze is found.
%For example, if we move southward to get into the maze, we would navigate south two more 
%times before choosing one of the other 4 cardianl directions.
%
%This definitely does not work and I cannot figure out how to make it work properly
%but I still have thoughts about it anyway. In our tiny in-class maze, a heuristic like this
%does not really impact the ability to solve the maze effectively but for a much larger
%maze it is almost a guarantee that this is not an ideal way to solve a maze.
