% Prolog Challenge 2 Part 1 - Kaitlyn Jesmonth

%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]).
% Direction
dir(up).
dir(down).
dir(left).
dir(right).

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], up) :- NextY is CurrY - 1.
%down
move([CurrX, CurrY], [NextX, NextY], down) :- NextY is CurrY + 1, NextX = CurrX.
%left
move([CurrX, CurrY], [NextX, NextY], left) :- NextY = CurrY, NextX is CurrX - 1.
%right
move([CurrX, CurrY], [NextX, NextY], right) :- NextY = CurrY, NextX is CurrX + 1.

% select direction to move
select(CurrentLocation, NewLocation, A, _, _, _, A) :- move(CurrentLocation, NewLocation, A),
	\+badSpot(NewLocation).
select(CurrentLocation, NewLocation, _, B, _, _, B) :- move(CurrentLocation, NewLocation, B),
	\+badSpot(NewLocation).
select(CurrentLocation, NewLocation, _, _, C, _, C) :- move(CurrentLocation, NewLocation, C),
	\+badSpot(NewLocation).
select(CurrentLocation, NewLocation, _, _, _, D, D) :- move(CurrentLocation, NewLocation, D),
	\+badSpot(NewLocation).

% A solution of the maze from our current location is [] if our current location is the end.
solve(CurrentLocation, _, Path) :- end(CurrentLocation), Path = [CurrentLocation].


% Convenience rule
solve(Path) :- start(Start), solve(Start, down, Path).

% solve based on direction you are facing
solve(CurrentLocation, up, Path) :- select(CurrentLocation, NewLocation, left, up, right, down, Dir), solve(NewLocation, Dir, NewPath), Path = [CurrentLocation | NewPath].
solve(CurrentLocation, down, Path) :- select(CurrentLocation, NewLocation, right, down, left, up, Dir),solve(NewLocation, Dir, NewPath), Path = [CurrentLocation | NewPath].
solve(CurrentLocation, left, Path) :- select(CurrentLocation, NewLocation, down, left, up, right, Dir), solve(NewLocation, Dir, NewPath), Path = [CurrentLocation | NewPath].
solve(CurrentLocation, right, Path) :- select(CurrentLocation, NewLocation, up, right, down, left, Dir), solve(NewLocation, Dir, NewPath), Path = [CurrentLocation | NewPath].
