0

I have a house with rooms that are defined with connections for when you can go from one room to another eg.

connection(garage,sidehall).
connection(sidehall,kitchen).
connection(kitchen,diningroom).

canget(X,Y):-connection(X,Y).
canget(X,Y):-connection(X,_),
            write('player goes from '),write(X),write(' to     '),write(Y),nl,
            canget(_,Y).

Im trying to figure out how make it so the player can only get from one room to another when they have a specific item, such as you can only be in the kitchen when items = gloves.

canget(X,Y,Item):-connection(X,Y,Item),canbein(Y,Item).
canget(X,Y,Item):-connection(X,Somewhere,Item),canbein(Somewhere,Item),canget(Somewhere,Y,Item).

tried defining canbein with:

canbein(kitchen):- item(sword).
canbein(sidehall):- item(hat).      

but that doesnt work!

Have defined my items as such, not sure if this is right either:

item(gloves,sword,helm,cheese).

Basically, have i declared my item values correctly? How can i use the specific item value to make canget x to y false?

Thank you!

Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
JJ1
  • 17
  • 2
  • 5

1 Answers1

0

Well, I see a few problems with your code. Firstly, you call canbein with two arguments (from canget predicate). However, canbein is defined as single-argument predicate. Therefore, the call always fails as no canbein/2 predicate exists.

I suggest the following modification:

canbein(kitchen, sword).
canbein(sidehall, hat).

Than, the item definition is not required. Let's think about what happens during the unification of

canget(X,Y,Item) :- connection(X,Y,Item), canbein(Y,Item).

Let's assume the following setting X=sidehall, Y=kitchen, Item==sword. This predicate should be OK. Assuming the conection predicate is OK, prolog tries to find canbein(Y, Item) i.e. canbein(kitchen, sword) and it succeeds.

On the contrary, if the Item is different the unification fails, hence it works as expected.

The second problem is the item predicate. By your definition, it expects 4 arguments. That's nonsense, of course. You should declare it like

item(gloves).
item(sword).
item(helm).
item(cheese).

However, I don't think this predicate is necessary at all. Just to be clear, try to call item(X) and obtain all results (the four declared). Try it with the prior definition - what should you even ask for?

I hope it helps :)

petrbel
  • 2,428
  • 5
  • 29
  • 49
  • thanks so much mate, appreciate it. think i understand it a bit better now :) i suck it programming! – JJ1 May 18 '15 at 10:54
  • @JJ1 Please consider marking the answer if the it helped you :) it's a good common at stackoverflow since the other users will know that it solves the problem. You can vote it up if you like the style etc. – petrbel May 18 '15 at 15:42