Prolog101(06)

动态修改全局数据

asserta(X) :把子句X当作此子句的谓词的第一个子句加入到动态数据库中,不可回溯。
assert(X)或assertz(X) :把子句X当作此子句的谓词的最后一个子句加入到动态数据库中,不可回溯。
retract(X) :把子句X从动态数据库中删除,不可回溯。

%swipl -s room.pl
%Hansen

:-dynamic here/1. 
:-dynamic location/2. 
:-dynamic bag/1.

%房间定义
room(kitchen).
room(office).
room(hall). 
room(diningroom).
room(cellar). 

%门定义
door(office, hall).
door(kitchen, office).
door(hall, diningroom).
door(kitchen, cellar).
door(diningroom, kitchen).

%规则:有门的两个房间是相通的
connect(X,Y) :- door(X,Y).
connect(X,Y) :- door(Y,X).

%物品在哪个房间
location(desk, office).
location(apple, kitchen). 
location(flashlight, desk). 
location(washingmachine, cellar).
location(nani, washingmachine).
location(broccoli, kitchen).
location(crackers, kitchen).
location(computer, office).

%背包
bag(tourch).

%哪些物品可以吃
edible(apple).
edible(crackers).
edible(broccoli).

%当前位置
here(hall).

%移动
move(Place):- can_go(Place),retract(here(X)), asserta(here(Place)).
can_go(Place):- here(X), connect(X, Place).  
can_go(Place):- write('You can''t go to '),write(Place),write(' from here.'), nl, fail.

%获取物品
take(X):- can_take(X), retract(location(X,_)), asserta(bag(X)), write(X), write(' taken.'), nl.
can_take(Thing):- here(Place), location(Thing, Place). 
can_take(Thing):- write('There is no '), write(Thing), write(' here.'), nl, fail. 

%放下物品
put(X):- can_put(X), here(Place), retract(bag(X)), asserta(location(X,Place)), write(X), write(' put.'), nl.
can_put(Thing):- bag(Thing). 
can_put(Thing):- write('There is no '), write(Thing), write(' in your bag.'), nl, fail. 

%房间物品列表
list_things(Place):- location(X, Place),tab(2),write(X),nl,fail.
list_things(_).

%相连的
list_connections(Place):- connect(Place, X),tab(2),write(X),nl,fail.
list_connections(_).

%持有物品列表
list_bag(Thing):- bag(X),tab(2),write(X),nl,fail.
list_bag(_).

%吃东西
eat(Thing):- can_eat(Thing), retract(bag(X)), write(Thing), write(' eaten. Yummy!').
can_eat(Thing):- bag(Thing), edible(Thing).
can_eat(Thing):- not(bag(Thing)), write('There is no '), write(Thing), write(' in your bag.'), nl, fail. 
can_eat(Thing):- bag(Thing), write('You can''t eat the '), write(Thing), write('.'), nl, fail. 

%查看房间情况
look :-
here(Place), write('You are in the '), write(Place), nl,
write('You can see:'),nl,list_things(Place),  
write('You can go to:'), nl, list_connections(Place),
write('You have:'),nl,list_bag(Thing).

%帮助
game :-
write('Look around: look/0'),nl,
write('Move around: move/1'),nl,
write('Take something: take/1'),nl,
write('Eat something: eat/1').
1 ?- game.
Look around: look/0
Move around: move/1
Take something: take/1
Eat something: eat/1
true.

2 ?- look.
You are in the hall
You can see:
You can go to:
  diningroom
  office
You have:
  tourch
true.

3 ?- move(diningroom).
true .

4 ?- look.
You are in the diningroom
You can see:
You can go to:
  kitchen
  hall
You have:
  tourch
true.

5 ?- move(kitchen).
true .

6 ?- look.
You are in the kitchen
You can see:
  apple
  broccoli
  crackers
You can go to:
  office
  cellar
  diningroom
You have:
  tourch
true.
7 ?- take(apple).
apple taken.
true .

8 ?- look.
You are in the kitchen
You can see:
  broccoli
  crackers
You can go to:
  office
  cellar
  diningroom
You have:
  apple
  tourch
true.

9 ?- eat(apple).
apple eaten. Yummy!
true .

Leave a Reply

Your email address will not be published. Required fields are marked *

*