:- use_module('pl-man-game/main'). %% Predicate for controlling the state :- dynamic state/1. state(up). :- dynamic result/1. result(0). :- dynamic line/1. line(0). %% Symplifying rules hO(OBJ) :- havingObject(appearance(OBJ)). s(DIR, OBJ) :- see(normal, DIR, OBJ). change(EST) :- retractall(state(_)), assert(state(EST)). setresult(Z) :- retractall(result(_)), assert(result(Z)). setline(L) :- retractall(line(_)), assert(line(L)), writeln(L). %% doE(EST, ACT): %% Being in state EST, do action ACT %% % Priority directions dir(left). dir(up). dir(right). dir(down). %% Sub-Rules for doing the sum below me doE(up, move(left)) :- line(0),s(down, '+'), s(down-left,X),number(X), s(down-right,Y),number(Y), Z is X+Y, setresult(Z), setline(1),writeln(Z). doE(up, move(left)) :- line(1),s(left,'.'). doE(up, move(left)) :- line(1),s(left,Z), number(Z), result(W), not(Z=W). doE(up, get(DIR)) :- dir(DIR),line(1),s(DIR,Z), number(Z), result(W), Z=W, writeln(Z). doE(up, move(up)) :- line(1), s(left,'#'),setline(2). doE(up, move(right)) :- line(2),s(right,'.'). doE(up, move(right)) :- line(2),s(right,Z), number(Z), result(W), not(Z=W). doE(up, get(DIR)) :- dir(DIR),line(2),s(DIR,Z), number(Z), result(W), Z=W, writeln(Z). doE(up, move(up)) :- line(2), s(right,'#'),setline(3). doE(up, move(left)) :- line(3),s(left,'.'). doE(up, move(left)) :- line(3),s(left,Z), number(Z), result(W), not(Z=W). doE(up, get(DIR)) :- dir(DIR),line(3),s(DIR,Z), number(Z), result(W), Z=W, writeln(Z). doE(up, move(down)) :- line(3),s(left,'#'),setline(down). doE(up, move(down)) :- line(down), s(down,' '). doE(up, move(down)) :- line(down),s(down,Z), number(Z), result(W), not(Z=W). doE(up, get(DIR)) :- dir(DIR),line(down),s(DIR,Z), number(Z), result(W), Z=W, writeln(Z). doE(up, move(right)) :- line(down),s(down,'#'),s(up-left,'#'),setline(right). doE(up, move(right)) :- line(right),s(right,Z), number(Z), result(W), not(Z=W). doE(up, get(right)) :- line(right),s(right,Z), number(Z), result(W), Z=W, writeln(Z). doE(up, move(right)) :- line(right), s(right,' '). doE(up, move(right)) :- line(right), s(down,'+'). doE(up, move(left)) :- line(right), s(down-left,'+'), change(down). % Rules to open and go down doE(down, use(down)) :- line(right), s(down,'+'). doE(down, drop(DIR)) :- hO(Z), number(Z), dir(DIR), line(right), s(DIR, ' '), writeln('dropped'). doE(down, move(down)) :- line(right), s(down,' '), setline(a). doE(down, move(down)) :- line(a), s(left, X), s(right, Y), number(X), number(Y). doE(down, get(down)) :- line(a), s(left, X), s(right, Y), number(X), number(Y), s(down, 'l'). doE(down, move(left)) :- line(a), s(left,' '). doE(down, get(DIR)) :- dir(DIR),line(a), s(DIR,'l'). doE(down, move(down)) :- line(a), s(left,'#'), setline(b). doE(down, move(right)) :- line(b), s(right,' '). doE(down, get(DIR)) :- dir(DIR),line(b), s(DIR,'l'). doE(down, move(down)) :- line(b), s(right,'#'), setline(c). doE(down, move(left)) :- line(c), s(left,' '). doE(down, get(DIR)) :- dir(DIR),line(c), s(DIR,'l'). doE(down, move(down)) :- line(c), s(left,'#'), setline(d). doE(down, move(down)) :- line(d), s(down,'.'), setline(e). doE(down, move(right)) :- line(e), s(right,'.'). doE(down, use(right)) :- line(e), s(right,'E'). %% Main rule for launching sub-rules %% do(ACT) :- state(EST), doE(EST, ACT).