% 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).

%up
move([CurrX, CurrY], [CurrX, NextY], Orientation) :- NextY is CurrY - 1, Orientation = orientation(up).
%down
move([CurrX, CurrY], [NextX, NextY], Orientation) :- NextY is CurrY + 1, NextX = CurrX, Orientation = orientation(down).
%left
move([CurrX, CurrY], [NextX, NextY], Orientation) :- NextY = CurrY, NextX is CurrX - 1, Orientation = orientation(left).
%right
move([CurrX, CurrY], [NextX, NextY], Orientation) :- NextY = CurrY, NextX is CurrX + 1, Orientation = orientation(right).

% individual directions specified for convenience; absolute directions later mapped to relative orientations
up([CurrX, CurrY], [CurrX, NextY]) :- NextY is CurrY - 1.
down([CurrX, CurrY], [NextX, NextY]) :- NextY is CurrY + 1, NextX = CurrX.
left([CurrX, CurrY], [NextX, NextY]) :- NextY = CurrY, NextX is CurrX - 1.
right([CurrX, CurrY], [NextX, NextY]) :- NextY = CurrY, NextX is CurrX + 1.

checkLeftWall([X, Y], Orientation) :- 
        (Orientation = orientation(down),
        NextX is X + 1,
        NextY is Y,
        badSpot([NextX, NextY]));

        (Orientation = orientation(left),
        NextX is X,
        NextY is Y + 1,
        badSpot([NextX, NextY]));
        
        (Orientation = orientation(up),
        NextX is X - 1,
        NextY is Y,
        badSpot([NextX, NextY]));

        (Orientation = orientation(right),
        NextX is X,
        NextY is Y - 1,
        badSpot([NextX, NextY])).


rotateClockWise(Orientation, NewOrientation) :-
        (Orientation = orientation(down),
        NewOrientation = orientation(left));
        (Orientation = orientation(left),
        NewOrientation = orientation(up));
        (Orientation = orientation(up),
        NewOrientation = orientation(right));
        (Orientation = orientation(right),
        NewOrientation = orientation(down)).


% 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, Orientation, Path) :- 
        % first block contains instructions for moving left (highest priority movement)
        ((Orientation = orientation(down),
        \+checkLeftWall(CurrentLocation, Orientation),
        right(CurrentLocation, NewLocation),
        \+badSpot(NewLocation),
        solve(NewLocation, orientation(right), RestOfPath), 
        Path = [CurrentLocation | RestOfPath],
        !);

        (Orientation = orientation(left),
        \+checkLeftWall(CurrentLocation, Orientation),
        down(CurrentLocation, NewLocation),
        \+badSpot(NewLocation),
        solve(NewLocation, orientation(down), RestOfPath), 
        Path = [CurrentLocation | RestOfPath],
        !);

        (Orientation = orientation(right),
        \+checkLeftWall(CurrentLocation, Orientation),
        up(CurrentLocation, NewLocation),
        \+badSpot(NewLocation),
        solve(NewLocation, orientation(up), RestOfPath), 
        Path = [CurrentLocation | RestOfPath],
        !);

        (Orientation = orientation(up),
        \+checkLeftWall(CurrentLocation, Orientation),
        left(CurrentLocation, NewLocation),
        \+badSpot(NewLocation),
        solve(NewLocation, orientation(left), RestOfPath), 
        Path = [CurrentLocation | RestOfPath],
        !));

        % second block contains instructions for moving forward if unable to move left

        ((Orientation = orientation(down),
        checkLeftWall(CurrentLocation, Orientation),
        down(CurrentLocation, NewLocation),
        \+badSpot(NewLocation),
        solve(NewLocation, Orientation, RestOfPath), 
        Path = [CurrentLocation | RestOfPath],
        !);

        (Orientation = orientation(left),
        checkLeftWall(CurrentLocation, Orientation),
        left(CurrentLocation, NewLocation),
        \+badSpot(NewLocation),
        solve(NewLocation, Orientation, RestOfPath), 
        Path = [CurrentLocation | RestOfPath],
        !);

        (Orientation = orientation(right),
        checkLeftWall(CurrentLocation, Orientation),
        right(CurrentLocation, NewLocation),
        \+badSpot(NewLocation),
        solve(NewLocation, Orientation, RestOfPath), 
        Path = [CurrentLocation | RestOfPath],
        !);

        (Orientation = orientation(up),
        checkLeftWall(CurrentLocation, Orientation),
        up(CurrentLocation, NewLocation),
        \+badSpot(NewLocation),
        solve(NewLocation, Orientation, RestOfPath), 
        Path = [CurrentLocation | RestOfPath],
        !));


        % rotate orientation to the right if all else fail
        
        (rotateClockWise(Orientation, X),
        solve(CurrentLocation, X, Path),
        !).


% Convenience rule
solve(Path) :- start(Start), solve(Start, orientation(down), 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).