:- module(grid, [ query/2, shoot/1, revive/0 ]). :- (dynamic shot/2). :- (dynamic failed/2). :- use_module(library(lists)). revive :- retractall(shot(_, _)), retractall(failed(_, _)). shoot((_, _)) :- shot(_, _), !, write("\nAlready shot!\n"), fail. shoot((X, Y)) :- format('~nPang! You shot at ~q~n', [(X, Y)]), assert(shot(X, Y)). query((_, _), []) :- failed(X, Y), format('~nAgent has been lost at ~q, (`revive.` to continue)~n', [(X, Y)]), !, fail. query((X, Y), Out2) :- integer(X), integer(Y), %DB=[[[], [], [], [stench]], [[], [], [breeze, stench], [wumpus]], [[], [breeze], [pit], [breeze, stench]], [[], [], [glitter], []]], %DB=[[[], [], ['stench'], ['wumpus']], [[], [], [], ['stench']], [[], [], ['breeze'], ['glitter']], [[], ['breeze'], ['pit'], ['breeze']]], %DB=[[[], ['breeze'], [], []], [['stench', 'breeze'], ['pit'], ['breeze'], []], [['wumpus'], ['stench', 'breeze'], [], []], [['stench', 'glitter'], [], [], []]], % not found %DB=[[[], ['glitter'], [], []], [[], ['stench'], ['breeze'], []], [['stench'], ['wumpus', 'breeze'], ['stench', 'pit'], ['breeze']], [[], ['stench'], ['breeze'], []]], DB=[[[], [], [], []], [[], ['stench'], ['breeze'], []], [['stench'], ['wumpus', 'breeze'], ['stench', 'pit'], ['breeze']], [['glitter'], ['stench'], ['breeze'], []]], nth0(Y, DB, Row), nth0(X, Row, Found), format('~nInvestigating ~q,~nfound: ~q~n', [(X, Y), Found]), ( ( member(wumpus, Found), \+ shot(X, Y) ; member(pit, Found) ) -> write("FAIL"), assert(failed(X, Y)), fail ; true ), clean_query(Found, Out), maplist({X, Y}/[I, O]>>(O=..[I, (X, Y)]), Out, Out2), !. clean_query(In, Out) :- clean_breeze(In, Out1), clean_stench(Out1, Out2), clean_glitter(Out2, Out). clean_breeze(In, In) :- member(breeze, In). clean_breeze(In, [no_breeze|In]) :- \+ member(breeze, In). clean_stench(In, In) :- member(stench, In). clean_stench(In, [no_stench|In]) :- \+ member(stench, In). clean_glitter(In, In) :- member(glitter, In). clean_glitter(In, [no_glitter|In]) :- \+ member(glitter, In).