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

% Directions
direction(n).
direction(e).
direction(s).
direction(w).
right_turn(n,e).
right_turn(e,s).
right_turn(s,w).
right_turn(w,n).
left_turn(n,w).
left_turn(e,n).
left_turn(s,e).
left_turn(w,s).

% Left wall check - direction matters
has_left_wall([CurrX, CurrY, e]) :- WallY is CurrY - 1, wall([CurrX, WallY]).
has_left_wall([CurrX, CurrY, s]) :- WallX is CurrX + 1, wall([WallX, CurrY]).
has_left_wall([CurrX, CurrY, w]) :- WallY is CurrY + 1, wall([CurrX, WallY]).
has_left_wall([CurrX, CurrY, n]) :- WallX is CurrX - 1, wall([WallX, CurrY]).

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, n], [CurrX, NextY, n]) :- NextY is CurrY - 1.
%down
move([CurrX, CurrY, s], [CurrX, NextY, s]) :- NextY is CurrY + 1.
%left
move([CurrX, CurrY, w], [NextX, CurrY, w]) :- NextX is CurrX - 1.
%right
move([CurrX, CurrY, e], [NextX, CurrY, e]) :- NextX is CurrX + 1.
%turn left
move([CurrX, CurrY, CurrDir], [CurrX, CurrY, NextDir]) :- left_turn(CurrDir, NextDir).
%turn right
move([CurrX, CurrY, CurrDir], [CurrX, CurrY, NextDir]) :- right_turn(CurrDir, NextDir).




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

% if there is a wall to our left
solve([CurrX, CurrY, CurrDir], BeenThere, Path) :- move([CurrX, CurrY, CurrDir], [NextX, NextY, CurrDir]), 
        \+badSpot([NextX, NextY]),
        %\+member([NextX, NextY, CurrDir], BeenThere),
		has_left_wall([CurrX, CurrY, CurrDir]),
        solve([NextX, NextY, CurrDir], [[NextX, NextY, CurrDir] | BeenThere], RestOfPath), 
        Path = [[CurrX, CurrY] | RestOfPath].

% if there is no wall to our left -> gotta turn left and go forward
solve([CurrX, CurrY, CurrDir], BeenThere, Path) :- move([CurrX, CurrY, CurrDir], [CurrX, CurrY, NextDir]), 
		left_turn(CurrDir, NextDir), 
    	\+has_left_wall([CurrX, CurrY, CurrDir]),
        %\+member([CurrX, CurrY, NextDir], BeenThere),
		move([CurrX, CurrY, NextDir], [NextX, NextY, NextDir]),
        \+badSpot([NextX, NextY]),
        %\+member([NextX, NextY, NextDir], BeenThere),
        solve([NextX, NextY, NextDir], [[NextX, NextY, NextDir], [CurrX, CurrY, NextDir] | BeenThere], RestOfPath), 
        Path = [[CurrX, CurrY]| RestOfPath].

% if there is a wall to our left but we can't go forward -> turn around
solve([CurrX, CurrY, CurrDir], BeenThere, Path) :- move([CurrX, CurrY, CurrDir], [BadX, BadY, CurrDir]),
    	badSpot([BadX, BadY]),
    	move([CurrX, CurrY, CurrDir], [CurrX, CurrY, NextDir]), 
        right_turn(CurrDir, NextDir),
        %\+member([CurrX, CurrY, NextDir], BeenThere),
		has_left_wall([CurrX, CurrY, CurrDir]),
        solve([CurrX, CurrY, NextDir], [[CurrX, CurrY, NextDir] | BeenThere], RestOfPath), 
        Path = [[CurrX, CurrY] | RestOfPath].
		
% Convenience rule
solve(Path) :- start([StartX, StartY]), solve([StartX, StartY, s], [[StartX, StartY, s]], 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).