diff options
-rw-r--r-- | agent.prolog | 114 |
1 files changed, 64 insertions, 50 deletions
diff --git a/agent.prolog b/agent.prolog index 3a189bb..ba59a43 100644 --- a/agent.prolog +++ b/agent.prolog @@ -19,10 +19,10 @@ init :- initPos :- - roomList(R), - random_member(X,R), retractall(here(_)), - not(somethingAt(X)), + roomList(Rooms), + filter(Rooms, somethingAt, Emptyrooms), + random_member(X,Emptyrooms), asserta(here(X)). @@ -30,10 +30,10 @@ initPits :- true. initWumpus :- - roomList(R), - random_member(X,R), retractall(wumpusAt(_)), - not(somethingAt(X)), + roomList(Rooms), + filter(Rooms, somethingAt, Emptyrooms), + random_member(X,Emptyrooms), asserta(wumpusAt(X)). @@ -48,26 +48,26 @@ initBats :- true. % command functions look :- - here(L), - writeSenses(L), - writeExits(L). - - -move(D) :- - here(L), - connects(L,N,D), - retract(here(L)), - asserta(here(N)), - join(['You move to the ',D,' .\n\n'],M), - write(M), !. - - -take(T) :- - here(L), - itemAt(T,L), - canTake(T), - retract(itemAt(T,L)), - asserta(holding(T)), + here(Location), + writeSenses(Location), + writeExits(Location). + + +move(Direction) :- + here(Location), + connects(Location, New, Direction), + retract(here(Location)), + asserta(here(New)), + join(['You move to the ',Direction,' .\n\n'],W), + write(W), !. + + +take(Item) :- + here(Location), + itemAt(Item,Location), + canTake(Item), + retract(itemAt(Item,Location)), + asserta(holding(Item)), write('Taken.\n\n'), !. @@ -75,43 +75,47 @@ take(T) :- % letting the player know what's going on -writeSenses(L) :- - glitter(L), - breeze(L), - bats(L), - stench(L). +writeSenses(Location) :- + glitter(Location), + breeze(Location), + bats(Location), + stench(Location). -glitter(_) :- not(current_predicate(goldAt/1)). -glitter(L) :- - goldAt(L), +glitter(Location) :- + current_predicate(goldAt/1), + goldAt(Location), write('You see a glitter along the sandy floor of the cave.\n'). +glitter(_) :- true. -breeze(_) :- not(current_predicate(pitAt/1)). -breeze(L) :- - connects(L,X,_), - pitAt(X), +breeze(Location) :- + current_predicate(pitAt/1), + connects(Location, ConnectedRoom, _), + pitAt(ConnectedRoom), write('A cold breeze blows through the room, making you shiver slightly.\n'). +breeze(_) :- true. -bats(_) :- not(current_predicate(batsAt/1)). -bats(L) :- - connects(L,X,_), - batsAt(X), +bats(Location) :- + current_predicate(batsAt/1), + connects(Location, ConnectedRoom, _), + batsAt(ConnectedRoom), write('All available surfaces are covered in guano. How unsanitary.\n'). +bats(_) :- true. -stench(_) :- not(current_predicate(wumpusAt/1)). -stench(L) :- - connects(L,X,_), - wumpusAt(X), +stench(Location) :- + current_predicate(wumpusAt/1), + connects(Location, ConnectedRoom, _), + wumpusAt(ConnectedRoom), write('An overpowering stench fills your nose.\n'). +stench(_) :- true. -writeExits(L) :- - findall(X, connects(L,_,X), E), - intercalate(E, ', ', O), +writeExits(Location) :- + findall(X, connects(Location,_,X), Exits), + intercalate(Exits, ', ', O), join(['There are exits to the ',O,'.\n'],W), write(W). @@ -127,5 +131,15 @@ somethingAt(X) :- current_predicate(goldAt/1), goldAt(X). somethingAt(X) :- current_predicate(batsAt/1), batsAt(X). -intercalate(I,S,O) :- O is I. +intercalate([X|Y], Spacer, Result) :- + intercalate(Y, Spacer, Tail), + atom_concat(X, Spacer, Head), + atom_concat(Head, Tail, Result). +intercalate([X], _, Result) :- Result = X. + + +filter(List, Predicate, Result) :- + Test =.. [Predicate,X], + findall(X, Test, No), + subtract(List, No, Result). |