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



badSpot([X,Y]) :- wall([X,Y]).

badSpot([X,Y]) :-
	X < 1; Y < 1.

badSpot([X,Y]) :-
	mazeSize([W, H]), (X > W; Y > H).


iscloser([X1,Y1],[X2,Y2]):- A is ((2-X1)^2 + (6-Y1)^2),B is ((2-X2)^2+(6-Y2)^2), A<B.


%right:
move([CurrX, CurrY], [NextX, NextY]) :-
	NextY = CurrY, NextX is CurrX + 1.
%left:
move([CurrX, CurrY], [NextX, NextY]) :-
	NextY = CurrY, NextX is CurrX - 1.
%down:
move([CurrX, CurrY], [NextX, NextY]) :-
	NextY is CurrY + 1, NextX = CurrX.
%up:
move([CurrX, CurrY], [CurrX, NextY]) :-
	NextY is CurrY - 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) :-

	move(CurrentLocation, NewLocation),
	\+iscloser(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).


