1/*
    2or_holds([],W,[_|Z]) <=> or_holds(W,Z).
    3elementary_action(enter).
    4elementary_action(exit).
    5elementary_action(turn).
    6elementary_action(grab).
    7elementary_action(shoot).
    8elementary_action(go).
    9compound_action(do(_,_))
   10compound_action(go_to(_,_)).
   11*/
   12
   13:- use_module( library(random)).   14
   15:- dynamic current_state/1.   16
   17init_simulator :- init_scenario,
   18                  retractall(current_state(_)), assertz(current_state([])).
   19
   20:- dynamic wumpus/2,pit/2,gold/2.   21
   22init_scenario :-
   23
   24   retractall(wumpus(_,_)), retractall(pit(_,_)), retractall(gold(_,_)),
   25
   26   xdim(XD), ydim(YD),
   27
   28   random(0,4294967296,N1), random(0,4294967296,N2), XW is N1 mod XD + 1, YW is N2 mod YD + 1,
   29   ( XW=1, YW=1 -> true ; assertz(wumpus(XW,YW)), write(wumpus(XW,YW)) ),
   30
   31   random(0,4294967296,N3), random(0,4294967296,N4), XG is N3 mod XD + 1, YG is N4 mod YD + 1,
   32   assertz(gold(XG,YG)), write(gold(XG,YG)),
   33
   34   no_of_random_pits(P), create_pits(P).
   35
   36create_pits(0) :- !.
   37create_pits(M) :-
   38   xdim(XD), ydim(YD),
   39   random(0,4294967296,N1), random(0,4294967296,N2), XP is N1 mod XD + 1, YP is N2 mod YD + 1,
   40   ( XP+YP < 4 -> create_pits(M) ; assertz(pit(XP,YP)), write(pit(XP,YP)) ),
   41   M1 is M-1, create_pits(M1).
   42
   43
   44perform(turn, []) :-
   45        write('turn'), nl,
   46        current_state([at(X,Y),facing(D)]),
   47        retract(current_state([at(X,Y),facing(D)])),
   48        ( D < 4 -> D1 is D+1 ; D1 is 1 ),
   49        assertz(current_state([at(X,Y),facing(D1)])).
   50
   51perform(enter, [Breeze,Stench,Glitter]) :-
   52        write('enter'), nl,
   53        current_state(Z),
   54        retract(current_state(Z)),
   55        assertz(current_state([at(1,1),facing(1)])),
   56        ( gold(1,1) -> Glitter = true ; Glitter = false ),
   57        ( (wumpus(1,2) ; wumpus(2,1)) -> Stench = true ;
   58            Stench = false ),
   59        ( (pit(2,1) ; pit(1,2)) -> Breeze = true ;
   60            Breeze = false ).
   61
   62perform(exit, []) :-
   63        write('exit'), nl,
   64        current_state([at(X,Y),facing(D)]),
   65        retract(current_state([at(X,Y),facing(D)])),
   66        assertz(current_state([])).
   67
   68perform(grab, []) :-
   69        write('grab'), nl.
   70
   71perform(shoot, [Scream]) :-
   72        write('shoot'), nl,
   73        current_state([at(X,Y),facing(D)]),
   74        wumpus(WX, WY),
   75        ( in_direction(X, Y, D, WX, WY), Scream = true ; Scream = false ).
   76
   77perform(go, [Breeze,Stench,Glitter]) :-
   78        write('go'), nl,
   79        current_state([at(X,Y),facing(D)]),
   80        retract(current_state([at(X,Y),facing(D)])),
   81        ( D = 1 -> X1 is X, Y1 is Y+1 ;
   82          D = 3 -> X1 is X, Y1 is Y-1 ;
   83          D = 2 -> X1 is X+1, Y1 is Y ;
   84          D = 4 -> X1 is X-1, Y1 is Y ),
   85        assertz(current_state([at(X1,Y1),facing(D)])),
   86        ( gold(X1,Y1) -> Glitter = true ; Glitter = false ),
   87        X_east is X1+1, X_west is X1-1, Y_north is Y1+1, Y_south is Y1-1,
   88        ( (wumpus(X_east,Y1) ; wumpus(X1,Y_north) ;
   89           wumpus(X_west,Y1) ; wumpus(X1,Y_south)) -> Stench = true ;
   90            Stench = false ),
   91        ( (pit(X_east,Y1) ; pit(X1,Y_north) ;
   92           pit(X_west,Y1) ; pit(X1,Y_south)) -> Breeze = true ;
   93            Breeze = false )