/* UNIVERSIDADE FEDERAL DE PERNAMBUCO * * DEPARTAMENTO DE INFORMATICA * * * * PROJETO DE PROCESSAMENTO DE CONHECIMENTO * * * * Profs: Geber Ramalho * * Jacques Robin * * * * AGENTES INTELIGENTES * * WUMPUS WORLD * * * * Desenvolvido por: * * Clarissa Vasconcelos Martins * * Fernanda Bezerra Cavalcanti Marques * * Simone Antunes Correia * * Suzana Abreu Accioly Canuto * * * ******************************************************************/ loop(T, Side_length) :- not dead(agent, T1), not won(T1), env_percept(T), percept_knowledge(T), S2 is Side_length + 1, S1 is Side_length + 2, loc(gold, Lg, T), loc(agent, La, T), orient(agent, O,T), list_of_pits(Pl), loc(wumpus, Lw, T), world(S1, S1, La, O, Lg, Lw, Pl, [0,S2], T), draw_world(S1, S1, [0, S2], T), knowledge_action(T, T1), action_env(T, T1, Side_length), loop(T1, Side_length). loop(T, Side_length) :- abort. w1 :- wumpus_world(4, [[3,2], [2,1], [4,4]] , [3,3], [2,3]). w2 :- wumpus_world(4, [[4,2], [1,2]] , [2,1], [3,3]). w3 :- wumpus_world(4, [[1,3], [2,4], [4,4]], [1,4], [2,2]). w4 :- wumpus_world(4, [[2,3], [3,3], [3,2]], [1,4], [1,3]). w5 :- wumpus_world(4, [[2,2], [3,4]], [3,3], [1,2]). w6 :- wumpus_world(4, [[3,2], [2,1]], [3,3], [2,4]). w7 :- wumpus_world(4, [[2,2], [4,4]], [3,3], [4,1]). w8 :- wumpus_world(4, [[4,2], [1,2]] , [2,1], [3,3]). w9 :- wumpus_world(4, [[2,2], [2,1]] , [1,3], [3,3]). /************** INICIALIZACAO *****************/ wumpus_world(Side_length, Pit_list, Wumpus_loc, Gold_loc) :- build_walls(Side_length), place_pits(Pit_list), assert(list_of_pits(Pit_list)), asserta(loc(wumpus, Wumpus_loc, X)), asserta(loc(gold, Gold_loc, 0)), asserta(loc(agent, [1,1], 0)), asserta(orient(agent, 0, 0)), /* S1 is Side_length + 2, S2 is Side_length + 1, world(S1, S1, [1,1], 0, Gold_loc, Wumpus_loc, Pit_list, [0,S2], 0), draw_world(S1, S1, [0, S2], 0), */ loop(0, Side_length). build_walls(Side_length) :- S1 is Side_length + 1, assert(loc(wall, [0, Y], T)), assert(loc(wall, [S1, Y], T)), assert(loc(wall, [X, 0], T)), assert(loc(wall, [X, S1], T)). place_pits([]). place_pits([Head|Tail]) :- assert(loc(pit, Head, T)), place_pits(Tail). /***** PREDICADOS GENERICOS ******/ /* MORRER E VENCER */ adjacent([X, Y], [X1, Y]) :- X1 is X + 1. adjacent([X, Y], [X1, Y]) :- X1 is X - 1. adjacent([X, Y], [X, Y1]) :- Y1 is Y + 1. adjacent([X, Y], [X, Y1]) :- Y1 is Y - 1. /* Serve apenas para o agente Semantica: infront(Algo, Agente) Algo e a localizacao do que se deseja saber se esta na frente do agente. Agente e' a localizacao do agente. */ infront([X, Y1], [X, Y], T) :- orient(agent, 0, T), Y1 is Y + 1. infront([X1, Y], [X, Y], T) :- orient(agent, 90, T), X1 is X + 1. infront([X, Y1], [X, Y], T) :- orient(agent, 180, T), Y1 is Y - 1. infront([X1, Y], [X, Y], T) :- orient(agent, 270, T), X1 is X - 1. behind([X, Y1], [X, Y], T) :- orient(agent, 0, T), Y1 is Y - 1. behind([X1, Y], [X, Y], T) :- orient(agent, 90, T), X1 is X - 1. behind([X, Y1], [X, Y], T) :- orient(agent, 180, T), Y1 is Y + 1. behind([X1, Y], [X, Y], T) :- orient(agent, 270, T), X1 is X + 1. atright([X1, Y], [X, Y], T) :- orient(agent, 0, T), X1 is X + 1. atright([X, Y1], [X, Y], T) :- orient(agent, 90, T), Y1 is Y - 1. atright([X1, Y], [X, Y], T) :- orient(agent, 180, T), X1 is X - 1. atright([X, Y1], [X, Y], T) :- orient(agent, 270, T), Y1 is Y + 1. atleft([X1, Y], [X, Y], T) :- orient(agent, 0, T), X1 is X - 1. atleft([X, Y1], [X, Y], T) :- orient(agent, 90, T), Y1 is Y + 1. atleft([X1, Y], [X, Y], T) :- orient(agent, 180, T), X1 is X + 1. atleft([X, Y1], [X, Y], T) :- orient(agent, 270, T), Y1 is Y - 1. dead(agent, T) :- loc(agent,L, T), loc(wumpus, L, T), nl, write("Wumpus: UOOOOOORRRRRRRR"), nl, write("Agent: NOOOOOOOOOOOOOOO"), nl, write("Wumpus: (glup)"), nl. dead(agent, T) :- loc(agent,L, T), loc(pit, L, T), nl, write("Agent: I'm falliiiiiiiiiiiing!"), nl, write("fiiiiiiiiiiiiiu BUMMMMMMMMMMM"), nl. dead(wumpus, T) :- shoot(agent, T1), T1 is T - 1, orient(agent, 0, T1), loc(agent, [X, Y], T1), loc(wumpus, [X, Y1], T1), Y1 > Y. dead(wumpus, T) :- shoot(agent, T1), T1 is T - 1, orient(agent,90, T1), loc(agent, [X, Y], T1), loc(wumpus, [X1, Y], T1), X1 > X. dead(wumpus, T) :- shoot(agent, T1), T1 is T - 1, orient(agent, 180, T1), loc(agent, [X, Y], T1), loc(wumpus, [X, Y1], T1), Y1 < Y. dead(wumpus, T) :- shoot(agent, T1), T1 is T - 1, orient(agent, 270, T1), loc(agent, [X, Y], T1), loc(wumpus, [X1, Y], T1), X1 < X. won(T) :- arrived(T1), nl, write("YESSSSS!!!"), tab(2), write("I WON! I'M A RICH MAN, NOW!"), nl. /***** 1 - AMBIENTE -> PERCEPCOES *****/ env_percept(T) :- percept_breeze(T), percept_stench(T), percept_glitter(T), percept_pain(T), percept_scream(T). percept_breeze(T) :- loc(agent, L, T), adjacent(L, L1), loc(pit, L1, T), asserta(percept(breeze, loc(agent, L, T))). percept_breeze(T) :- loc(agent, L, T), asserta(percept(not_breeze, loc(agent, L, T))). percept_stench(T) :- loc(agent, L, T), adjacent(L, L1), loc(wumpus, L1, T), asserta(percept(stench, loc(agent, L, T))). percept_stench(T) :- loc(agent, L, T), asserta(percept(not_stench, loc(agent, L, T))). percept_glitter(T) :- loc(agent, L, T), loc(gold, L, T), asserta(percept(glitter, loc(agent, L, T))). percept_glitter(T) :- loc(agent, L, T), asserta(percept(not_glitter, loc(agent, L, T))). percept_pain(T) :- bump(agent,T), loc(agent, L, T), asserta(percept(pain,loc(agent, L, T))). percept_pain(T) :- loc(agent, L, T), loc(agent, L, T), asserta(percept(not_pain, loc(agent, L, T))). percept_scream(T) :- dead(wumpus,T), T1 is T-1, not dead(wumpus,T1), asserta(percept(scream,X)). percept_scream(T) :- asserta(percept(not_scream,X)). /**** 2 - PERCEPCOES --->CONHECIMENTO ****/ percept_knowledge(T) :- know_locWall(T), suspect_locPit(T), know_locPit(T), suspect_locWumpus(T), retract_minorSuspections(T), know_locWumpus(T), know_locGold(T), know_deadWumpus(T), safe_place(T), asserta_visited(L). asserta_visited(T) :- loc(agent, L, T), not visited(L), asserta(visited(L)). asserta_visited(T). safe_place(T) :- not dead(agent, T), loc(agent, L, T), asserta(know(loc(safe, L, T))), retract_suspPit(L), retract_suspWumpus(L), fail. safe_place(T) :- know(loc(not_pit, L, T1)), T1 =< T, know(loc(not_wumpus, L, T2)), T2 =< T, asserta(know(loc(safe, L, T))), retract_suspPitWumpus(L), fail. safe_place(T) :- loc(agent, L, T), dead(agent,T), asserta(know(loc(not_safe, L, T))). safe_place(T). retract_suspPit(L) :- suspect(loc(pit, L, Y)), retract(suspect(loc(pit, L,Y))), fail. retract_suspPit(L). retract_suspWumpus(L) :- suspect(loc(wumpus, L, Y)), retract(suspect(loc(wumpus, L,Y))), fail. retract_suspWumpus(L). know_locWall(T) :- percept(pain, loc(agent, [X,Y], T)), assertKnowWall(loc(agent, [X,Y], T)), !. know_locWall(T) :- loc(agent, L, T), percept(not_pain, loc(agent, L, T)), assert(know(loc(not_wall, L, T))), !. assertKnowWall(loc(agent, [X,Y], T)) :- orient(agent, 0, T), Y1 is Y + 1, asserta(know(loc(wall, [X, Y1], T))), !. assertKnowWall(loc(agent, [X,Y], T)) :- orient(agent, 90, T), X1 is X + 1, asserta(know(loc(wall, [X1, Y], T))), !. assertKnowWall(loc(agent, [X,Y], T)) :- orient(agent, 180, T), Y1 is Y - 1, asserta(know(loc(wall, [X, Y1], T))), !. assertKnowWall(loc(agent, [X,Y], T)) :- orient(agent, 270, T), X1 is X - 1, asserta(know(loc(wall, [X1, Y], T))), !. know_locGold(T) :- percept(glitter, loc(agent, L, T)), asserta(know(loc(gold, L, T))), !. know_locGold(T) :- percept(not_glitter, loc(agent, L, T)), asserta(know(loc(not_gold, L, T))), !. know_deadWumpus(T) :- percept(scream, loc(agent, L, T)), asserta(know(dead(wumpus, T))), !. know_deadWumpus(T) :- percept(not_scream, loc(agent, L, T)), !. suspect_locPit(T) :- percept(breeze, loc(agent, L, T)), assertSuspect_Pit(L, T), !. suspect_locPit(T) :- percept(not_breeze, loc(agent, L, T)), assertKnowNot_Pit(L, T), !. suspect_locPit(T). assertSuspect_Pit(L,T) :- adjacent(L,L1), not know(loc(not_pit, L1, T2)), not know(loc(not_wall, L1, T2)), asserta(suspect(loc(pit, L1, T))), fail. assertKnowNot_Pit(L,T) :- adjacent(L,L1), asserta(know(loc(not_pit, L1, T))), fail. know_locPit(T) :- not dead(agent, T), loc(agent, L, T), asserta(know(loc(not_pit, L, T))). know_locPit(T) :- loc(agent, L, T), dead(agent, T), loc(pit, L, T), asserta(know(loc(pit, L, T))). know_locPit(T). suspect_locWumpus(T) :- percept(stench, loc(agent, L, T)), assertSuspect_Wumpus(L, T), !. suspect_locWumpus(T) :- percept(not_stench, loc(agent, L, T)), assertKnowNot_Wumpus(L, T), !. suspect_locWumpus(T). assertSuspect_Wumpus(L,T) :- adjacent(L,L1), not visited(L), not know(loc(not_wumpus, L1, T2)), not know(loc(not_wall, L1, T2)), asserta(suspect(loc(wumpus, L1, T))), fail. assertKnowNot_Wumpus(L,T) :- adjacent(L,L1), asserta(know(loc(not_wumpus, L1, T))), fail. retract_minorSuspections(T) :- suspect(loc(wumpus, L, T1)), T1 =< T, suspect(loc(wumpus, L, T2)), T2 \= T1, T2 < T, suspect(loc(wumpus, L1, T3)), T3 =< T, L1 \= L, not suspect(loc(wumpus, L1, T4)),T3 \= T4, T4 < T, retract(suspect(loc(wumpus, L1, T3))), asserta(know(loc(not_wumpus,L1,T))). retract_minorSuspections(T) . know_locWumpus(T) :- suspect(loc(wumpus, L, T1)),T1 =< T, suspect(loc(wumpus, L, T2)), T2 \= T1, T2 < T, suspect(loc(wumpus, L, T3)), T3 \= T2, T3 \= T1, T3 < T, asserta(know(loc(wumpus, L, T))). know_locWumpus(T) :- not dead(agent, T), loc(agent, L, T), asserta(know(loc(not_wumpus, L, T))). know_locWumpus(T) :- loc(agent, L, T), dead(agent, T), loc(wumpus, L, T), asserta(know(loc(wumpus, L, T))). know_locWumpus(T). /****** 3 - CONHECIMENTOS -> ACOES *******/ knowledge_action(T, T1) :- grab(agent, gold, T2), T2 is T - 1, goback(T, T1). knowledge_action(T, T1) :- verifyloop(T, T1). knowledge_action(T, T1) :- return(agent, T2), actionreturn(T, T1). knowledge_action(T, T1) :- action(T, T1). goback(T, T1) :- T1 is T + 3, asserta(return(agent, T)). verifyloop(T, T1) :- loc(agent, L, T), orient(agent, O, T), loc(agent, L, T2), orient(agent, O, T2), T2 < T - 1, return(agent, T3), T > T3, T2 > T3, avoidloop(T, T1, T2). verifyloop(T, T1) :- loc(agent, L, T), orient(agent, O, T), loc(agent, L, T2), orient(agent, O, T2), T2 < T - 1, not return(agent, T3), avoidloop(T, T1, T2). action(T, T1) :- great(T, T1). action(T, T1) :- good(T, T1). action(T, T1) :- medium(T, T1). action(T, T1) :- risky(T, T1). actionreturn(T, T1) :- greatreturn(T, T1). actionreturn(T, T1) :- goodreturn(T, T1). actionreturn(T, T1) :- mediumreturn(T, T1). actionreturn(T, T1) :- riskyreturn(T, T1). greatreturn(T, T1) :- action_shoot(T, T1). goodreturn(T, T1) :- loc(agent, L, T), orient(agent, 0, T), infront(L1, L, T), know(loc(wall, L1, T2)), asserta(turn(agent, left, T)), T1 is T + 1. goodreturn(T, T1) :- loc(agent, L, T), orient(agent, 270, T), infront(L1, L, T), know(loc(wall, L1, T2)), asserta(turn(agent, left, T)), T1 is T + 1. goodreturn(T, T1) :- loc(agent, L, T), infront(L1, L, T), know(loc(wall, L1, T2)), asserta(turn(agent, right, T)), T1 is T + 1. goodreturn(T, T1) :- orient(agent, 0, T), loc(agent, L, T), know(loc(safe, L1, T2)), atleft(L1, L, T), T1 is T + 1, asserta(turn(agent, left, T2)). goodreturn(T, T1) :- orient(agent, 90, T), loc(agent, L, T), know(loc(safe, L1, T2)), atright(L1, L, T), T1 is T + 1, asserta(turn(agent, right, T)). goodreturn(T, T1) :- orient(agent, 180, T), loc(agent, L, T), know(loc(safe, L1, T2)), infront(L1, L, T), T1 is T + 1, asserta(forward(agent, T)). goodreturn(T, T1) :- orient(agent, 270, T), loc(agent, L, T), know(loc(safe, L1, T2)), infront(L1, L, T), T1 is T + 1, asserta(forward(agent, T)). mediumreturn(T, T1) :- loc(agent, L, T), know(loc(safe, L1, T2)), infront(L1, L, T), T1 is T + 1, asserta(forward(agent, T)). mediumreturn(T, T1) :- loc(agent, L, T), know(loc(safe, L1, T2)), atright(L1, L, T), T1 is T + 1, asserta(turn(agent, right, T)). mediumreturn(T, T1) :- loc(agent, L, T), know(loc(safe, L1, T2)), atleft(L1, L, T), T1 is T + 1, asserta(turn(agent, left, T)). riskyreturn(T, T1) :- orient(agent, 0, T), T1 is T + 1, asserta(turn(agent, left, T)). riskyreturn(T, T1) :- orient(agent, 90, T), T1 is T + 1, asserta(turn(agent, right, T)). riskyreturn(T, T1) :- T1 is T + 1, asserta(forward(agent, T)). great(T, T1) :- action_get(T, T1). great(T, T1) :- action_shoot(T, T1). good(T, T1) :- action_forward(T, T1). good(T, T1) :- action_turn(T, T1). medium(T, T1) :- action_backwards(T, T1). action_get(T, T1) :- know(loc(gold, L, T)), loc(agent, L, T), not carry(agent, gold, T), asserta(grab(agent, gold, T)), T1 is T + 1. action_shoot(T, T1) :- know(loc(wumpus, [X, Y], T)), loc(agent, [X, Y1], T), Y1 < Y, orient(agent, 0, T), asserta(shoot(agent, T)), T1 is T + 1. action_shoot(T, T1) :- know(loc(wumpus, [X, Y], T)), loc(agent, [X, Y1], T), Y1 > Y, orient(agent, 180, T), asserta(shoot(agent, T)), T1 is T + 1. action_shoot(T, T1) :- know(loc(wumpus, [X, Y], T)), loc(agent, [X1, Y], T), X1 < X, orient(agent, 90, T), asserta(shoot(agent, T)), T1 is T + 1. action_shoot(T, T1) :- know(loc(wumpus, [X, Y], T)), loc(agent, [X1, Y], T), X1 > X, orient(agent, 270, T), asserta(shoot(agent, T)), T1 is T + 1. action_forward(T, T1) :- loc(agent, L, T), infront(L1, L, T), know(loc(safe, L1, T2)), not know(loc(wall, L1, T3)), asserta(forward(agent, T)), T1 is T + 1. action_forward(T, T1) :- loc(agent, L, T), infront(L1, L, T), not suspect(loc(wumpus, L1, T2)), not suspect(loc(pit, L1, T3)), not know(loc(wall, L1, T4)), asserta(forward(agent, T)), T1 is T + 1. action_backwards(T, T1) :- T \= 0, loc(agent, [X, Y], T), orient(agent, 0, T), X1 is X + 1, Y1 is Y - 1, not suspect(loc(pit, [X1, Y1], T2)), not suspect(loc(wumpus, [X1, Y1], T3)), not know(loc(wall, [X1, Y1], T4)), asserta(backwards(agent, T)), T1 is T + 5. action_backwards(T, T1) :- T \= 0, loc(agent, [X, Y], T), orient(agent, 90, T), X1 is X - 1, Y1 is Y - 1, not suspect(loc(pit, [X1, Y1], T2)), not suspect(loc(wumpus, [X1, Y1], T3)), not know(loc(wall, [X1, Y1], T4)), asserta(backwards(agent, T)), T1 is T + 5. action_backwards(T, T1) :- T \= 0, loc(agent, [X, Y], T), orient(agent, 180, T), X1 is X - 1, Y1 is Y + 1, not suspect(loc(pit, [X1, Y1], T2)), not suspect(loc(wumpus, [X1, Y1], T3)), not know(loc(wall, [X1, Y1], T4)), asserta(backwards(agent, T)), T1 is T + 5. action_backwards(T, T1) :- T \= 0, loc(agent, [X, Y], T), orient(agent, 270, T), X1 is X + 1, Y1 is Y + 1, not suspect(loc(pit, [X1, Y1], T2)), not suspect(loc(wumpus, [X1, Y1], T3)), not know(loc(wall, [X1, Y1], T4)), asserta(backwards(agent, T)), T1 is T + 5. avoidloop(T, T1, T2) :- forward(agent, T2), T2 < T, T1 is T + 1, asserta(turn(agent, right, T)). avoidloop(T, T1, T2) :- turn(agent, right, T2), T2 < T, T1 is T + 1, asserta(turn(agent, left, T)). avoidloop(T, T1, T2) :- turn(agent, left, T2), T2 < T, T1 is T + 1, asserta(turn(agent, right, T)). avoidloop(T, T1, T2) :- backwards(agent, T), T2 < T, T1 is T + 1, asserta(forward(agent, T)). avoidloopreturn(T, T1, T2) :- forward(agent, T2), T2 < T, T1 is T + 1, orient(agent, 0, T), asserta(turn(agent, left, T)). avoidloopreturn(T, T1, T2) :- forward(agent, T2), T2 < T, T1 is T + 1, orient(agent, 270, T), asserta(turn(agent, left, T)). avoidloopreturn(T, T1, T2) :- forward(agent, T2), T2 < T, T1 is T + 1, asserta(turn(agent, right, T)). avoidloopreturn(T, T1, T2) :- avoidloop(T, T1, T2). risky(T, T1) :- T1 is T + 1, bump(agent, T), asserta(turn(agent, right, T)). risky(T, T1) :- T1 is T + 1, asserta(forward(agent, T)). action_turn(T, T1) :- loc(agent, L, T), atright(L1, L, T), know(loc(safe, L1, T2)), not know(loc(wall, L1, T3)), asserta(turn(agent, right, T)), T1 is T + 1. action_turn(T, T1) :- loc(agent, L, T), atleft(L1, L, T), know(loc(safe, L1, T2)), not know(loc(wall, L1, T3)), asserta(turn(agent, left, T)), T1 is T + 1. action_turn(T, T1) :- loc(agent, L, T), atright(L1, L, T), not suspect(loc(wumpus, L1, T2)), not suspect(loc(pit, L1, T3)), not know(loc(wall, L1, T4)), asserta(turn(agent, right, T)), T1 is T + 1. action_turn(T, T1) :- loc(agent, L, T), atleft(L1, L, T), not suspect(loc(wumpus, L1, T2)), not suspect(loc(pit, L1, T3)), not know(loc(wall, L1, T4)), asserta(turn(agent, left, T)), T1 is T + 1. /************** 4 - ACOES -> AMBIENTE ****************/ action_env(T, T1, S) :- loc_agent(T, T1),loc(agent,La,T1),orient_agent(T, T1), loc_gold(T, T1), loc_wumpus(T, T1), /*orient(agent, O, T1), loc(gold, Lg, T1), list_of_pits(Pl), loc(wumpus, Lw, T1), S1 is S + 2, S2 is S + 1, world(S1, S1, La, O, Lg, Lw, Pl, [0,S2], T1), */ go_on. loc_agent(T, T1) :- T1 is T + 1, forward(agent, T), loc(agent, [X, Y], T), orient(agent, 0, T), Y1 is Y + 1, loc(wall, [X, Y1], T), asserta(bump(agent, T1)), asserta(loc(agent, [X, Y], T1)), asserta(orient(agent, 0, T1)), !. loc_agent(T, T1) :- T1 is T + 1, forward(agent, T), loc(agent, [X, Y], T), orient(agent, 90, T), X1 is X + 1, loc(wall, [X1, Y], T), asserta(bump(agent, T1)), asserta(loc(agent, [X, Y], T1)), asserta(orient(agent, 90, T1)), !. loc_agent(T, T1) :- T1 is T + 1, forward(agent, T), loc(agent, [X, Y], T), orient(agent, 180, T), Y1 is Y - 1, loc(wall, [X, Y1], T), asserta(bump(agent, T1)), asserta(loc(agent, [X, Y], T1)), asserta(orient(agent, 180, T1)), !. loc_agent(T, T1) :- T1 is T + 1, forward(agent, T), loc(agent, [X, Y], T), orient(agent, 270, T), X1 is X - 1, loc(wall, [X1, Y], T), asserta(bump(agent, T1)), asserta(loc(agent, [X, Y], T1)), asserta(orient(agent, 270, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards(agent, T), loc(agent, [X,Y], T), orient(agent, 0, T), X1 is X + 1, Y1 is Y - 1, loc(wall, [X1, Y1], T), asserta(bump(agent, T1)), asserta(loc(agent, [X, Y1], T1)), asserta(orient(agent, 90, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards(agent, T), loc(agent, [X,Y], T), orient(agent, 90, T), X1 is X - 1, Y1 is Y - 1, loc(wall, [X1, Y1], T), asserta(bump(agent, T1)), asserta(loc(agent, [X1, Y], T1)), asserta(orient(agent, 180, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards(agent, T), loc(agent, [X,Y], T), orient(agent, 180, T), X1 is X - 1, Y1 is Y + 1, loc(wall, [X1, Y1], T), asserta(bump(agent, T1)), asserta(loc(agent, [X, Y1], T1)), asserta(orient(agent, 270, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards(agent, T), loc(agent, [X,Y], T), orient(agent, 270, T), X1 is X + 1, Y1 is Y - 1, loc(wall, [X1, Y1], T), asserta(bump(agent, T1)), asserta(loc(agent, [X1, Y], T1)), asserta(orient(agent, 0, T1)), !. loc_agent(T, T1) :- T1 is T + 3, return(agent, T), loc(agent, [X,Y], T), Y1 is Y - 1, orient(agent,0,T), asserta(loc(agent,[X,Y1], T1)), asserta(orient(agent, 180, T1)). loc_agent(T, T1) :- T1 is T + 3, return(agent, T), loc(agent, [X,Y], T), X1 is X - 1, orient(agent,90,T), asserta(loc(agent,[X1,Y], T1)), asserta(orient(agent, 270, T1)). loc_agent(T, T1) :- T1 is T + 3, return(agent, T), loc(agent, [X,Y], T), Y1 is Y + 1, orient(agent, 180,T), asserta(loc(agent,[X,Y1], T1)), asserta(orient(agent, 0, T1)). loc_agent(T, T1) :- T1 is T + 3, return(agent, T), loc(agent, [X,Y], T), X1 is X + 1, orient(agent,270,T), asserta(loc(agent,[X1,Y], T1)), asserta(orient(agent, 90, T1)). loc_agent(T, T1) :- T1 is T + 1, forward(agent, T), loc(agent, [X,Y], T), Y1 is Y + 1, orient(agent,0,T), asserta(loc(agent,[X,Y1], T1)), !. loc_agent(T, T1) :- T1 is T + 1, forward(agent, T), loc(agent, [X,Y], T), X1 is X + 1, orient(agent,90,T), asserta(loc(agent,[X1,Y], T1)), !. loc_agent(T, T1) :- T1 is T + 1, forward(agent, T), loc(agent, [X,Y], T), Y1 is Y - 1, orient(agent, 180,T), asserta(loc(agent,[X,Y1], T1)), !. loc_agent(T, T1) :- T1 is T + 1, forward(agent, T), loc(agent, [X,Y], T), X1 is X - 1, orient(agent,270,T), asserta(loc(agent,[X1,Y], T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards(agent, T), loc(agent, [X,Y], T), orient(agent, 0, T), X1 is X + 1, Y1 is Y - 1, asserta(loc(agent, [X1, Y1], T1)), asserta(orient(agent, 90, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards(agent, T), loc(agent, [X,Y], T), orient(agent, 90, T), X1 is X - 1, Y1 is Y - 1, asserta(loc(agent, [X1, Y1], T1)), asserta(orient(agent, 180, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards(agent, T), loc(agent, [X,Y], T), orient(agent, 180, T), X1 is X - 1, Y1 is Y + 1, asserta(loc(agent, [X1, Y1], T1)), asserta(orient(agent, 270, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards(agent, T), loc(agent, [X,Y], T), orient(agent, 270, T), X1 is X + 1, Y1 is Y + 1, asserta(loc(agent, [X1, Y1], T1)), asserta(orient(agent, 0, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards2(agent, T), loc(agent, [X,Y], T), orient(agent, 90, T), X1 is X - 1, Y1 is Y + 1, asserta(loc(agent, [X1, Y1], T1)), asserta(orient(agent, 0, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards2(agent, T), loc(agent, [X,Y], T), orient(agent, 180, T), X1 is X + 1, Y1 is Y + 1, asserta(loc(agent, [X1, Y1], T1)), asserta(orient(agent, 90, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards2(agent, T), loc(agent, [X,Y], T), orient(agent, 270, T), X1 is X + 1, Y1 is Y - 1, asserta(loc(agent, [X1, Y1], T1)), asserta(orient(agent, 180, T1)), !. loc_agent(T, T1) :- T1 is T + 5, backwards2(agent, T), loc(agent, [X,Y], T), orient(agent, 0, T), X1 is X - 1, Y1 is Y - 1, asserta(loc(agent, [X1, Y1], T1)), asserta(orient(agent, 270, T1)), !. loc_agent(T, T1) :- loc(agent, L, T), asserta(loc(agent, L, T1)). loc_wumpus(T, T1) :- loc(wumpus, L, T), asserta(loc(wumpus, L, T1)). carry(agent, gold, T) :- grab(agent, gold, T1), T1 < T. carry(agent, arrow, T) :- not shot(agent,T1), T1 < T. loc_gold(T, T1) :- loc(agent, L, T1), carry(agent, gold, T1), asserta(loc(gold, L, T1)). loc_gold(T, T1) :- loc(gold, L, T), not carry(agent, gold, T1), asserta(loc(gold, L, T1)). loc_gold(T, T1) :- loc(gold, L, T), asserta(loc(gold, L, T1)). orient_agent(T, T1) :- T1 is T + 1, orient(agent, 270, T), turn(agent, right, T), asserta(orient(agent, 0, T1)). orient_agent(T, T1) :- T1 is T + 1, orient(agent, O1, T), turn(agent, right, T), O is O1 + 90, asserta(orient(agent, O, T1)). orient_agent(T, T1) :- T1 is T + 1, orient(agent, 0, T), turn(agent, left, T), asserta(orient(agent, 270, T1)). orient_agent(T, T1) :- T1 is T + 1, orient(agent, O1, T), turn(agent, left, T), O is O1 - 90, asserta(orient(agent, O , T1)). orient_agent(T, T1) :- backwards(agent, T). /* quando anda pra tras, a orientacao ja e' assertada no loc_agent */ orient_agent(T, T1) :- bump(agent, T1). /* se ja fez o assert do bump, tambem ja fez da orientacao */ orient_agent(T, T1) :- return(agent, T). orient_agent(T, T1) :- orient(agent, O, T), asserta(orient(agent, O, T1)). /************** INTERFACE *******************/ world(Rows, Cols, L, O, G, W, P, [X1, Y1], T) :- verifywon(T), nl,write("***************** "), write(wumpus), write(" **************"), write(tempo), write(T), nl,nl, showWorld(Rows, Cols, L, O, G, W, P, [X1, Y1]). showWorld(Rows, Cols, L, O, G, W, P, [X1, Y1]) :- Cols > 0 , row(Rows),nl, picture(Rows, L, O, G, W, P,[X1,Y1]), Y2 is Y1 - 1, C is Cols - 1, nl, showWorld(Rows,C , L, O, G, W, P, [X1,Y2]). verifywon(T) :- return(agent, T2), loc(agent, [1,1], T), asserta(arrived(T)). verifywon(T). showWorld(Rows, Cols, L, O, G, W, P, [X1,Y1]):- row(Rows). row(R) :- R > 0, write(l), write(---------------), R1 is R - 1, row(R1). row(R) :- write(l). picture(R, [X,Y], O, [XG,YG], W, P, [X1,Y1]) :- R > 0, write(l), draw_ag([X,Y], O, [X1,Y1]), draw_wall1([X1,Y1]), drawgold([XG,YG],[X1,Y1]), draw_wumpus(W,[X1,Y1]), draw_pits(P, [X1,Y1]), X2 is X1 + 1, R1 is R - 1, picture(R1, [X,Y], O, [XG,YG],W, P, [X2,Y1]). picture(R, [X,Y], O, [XG,YG], W, P, [X1,Y1]) :- write(l). draw_ag([X,Y], 0 ,[X,Y]) :- tab(1), write(a), write("^"),draw_arrow(X) . draw_ag([X,Y], 90 ,[X,Y]) :- tab(1), write(a), write(">"),draw_arrow(X) . draw_ag([X,Y], 180 ,[X,Y]) :- tab(1), write(a), write("v"),draw_arrow(X) . draw_ag([X,Y], 270 ,[X,Y]) :- tab(1), write(a), write("<"),draw_arrow(X) . draw_ag([X,Y], O, [X1,Y1]) :-tab(4). draw_arrow(X) :- shoot(agent,_), tab(1). draw_arrow(X) :- write(f). drawgold([X,Y],[X,Y]) :- write(g), tab(1). drawgold([X,Y],[X1,Y1]) :- tab(2). draw_wumpus([X,Y],[X,Y]) :- write(w). draw_wumpus([X,Y],[X1,Y1]) :-tab(1). draw_pits([], _) :- tab(5). draw_pits([[X,Y]|Tail] , [X,Y]) :- write(p), tab(4). draw_pits([H|Tail] , [X,Y]) :- draw_pits( Tail , [X,Y]). draw_wall1(X) :- loc(wall,X,_), write(wa), tab(1). draw_wall1(X) :- tab(3). go_on :- get(C), C = 115, abort. go_on. draw_world(Rows, Cols, I, T) :- nl,write("******************** Suspects/Knwoledge *************"), tab(2), write(tempo), tab(2), write(T),nl, nl, world2(Rows, Cols, I). draw_row(R) :- R > 0, write(l), write(---------------), R1 is R - 1, draw_row(R1). draw_row(R) :- write(l). world2(Rows, Cols,[X1,Y1]) :- Rows >= 0 , draw_row(Cols),nl, susp(Cols,[X1,Y1]), Y2 is Y1 - 1, R is Rows- 1, nl, world2(R ,Cols,[X1,Y2]). world2(Rows, Cols,[X1,Y1]) :- draw_row(Rows). susp(R, [X1,Y1]) :- R > 0, write(l), draw_suspect([X1,Y1]), X2 is X1 + 1, R1 is R - 1, susp(R1,[X2,Y1]). susp(R, [X1,Y1]) :- write(l). draw_suspect([X1,Y1]) :- draw_wall([X1,Y1]), draw2_wumpus([X1,Y1]), draw_safe([X1,Y1]), draw_notsafe([X1,Y1]), draw_visit([X1,Y1]), draw_suspect_wumpus([X1,Y1]), draw_suspect_pit([X1,Y1]). draw_wall(X) :- know(loc(wall, X,_)), write(wa), write("!"), !. draw_wall(X) :- tab(3). draw2_wumpus(X) :- know(loc(wumpus, X,_)), write(w),write("!") , !. draw2_wumpus(X) :- tab(2). draw_safe(X) :- know(loc(safe, X,_)), write(s),write("!") , !. draw_safe(X) :-tab(2). draw_notsafe(X) :- know(loc(not_safe, X,_)), write(n),write("!") , !. draw_notsafe(X) :-tab(2). draw_visit(X) :- loc(agent,X,_), write(v), write("!"),!. draw_visit(X) :- tab(2). draw_suspect_wumpus([X,Y]) :- suspect(loc(wumpus, [X,Y],_)), write(w), write("?"),!. draw_suspect_wumpus([X,Y]) :- tab(2). draw_suspect_pit([X,Y]) :- suspect(loc(pit, [X,Y],_)), write(p), write("?"),!. draw_suspect_pit([X,Y]) :- tab(2).