PDA

View Full Version : Text adventure game... in Prolog.


Kraetos
2009-12-15, 20:24
I think I get the honor of making the first Programmer's Nook thread about Prolog. Hooray!

Anyways, it's finals time. And I've been a little distracted. So now I have to pound out this program with far less time than I'd like. It's a text adventure, which my professor already wrote most of the important code for, and it's my job to extend it and make something substantial with it.

I've got a bug that's bothering the shit out of me.


% interactive adventure game.

% hall(Current, Dir, Next) - defines a connection from the Current room to the Next room
% in direction Dir.

% start
hall(start,n,living).
hall(start,s,empty).

% living
hall(living,s,start).

% empty
hall(empty,n,start).

% tool(Room, Item) - defines what item (if any) is in which room.

tool(start,knife).
tool(living,key).
tool(empty,none).

% descript(Room) - outputs a description of Room.

% start
descript(start) :- write('You are standing in the entrance hall.'),nl,
write('The front door is locked. You need the key.'),nl,
write('It\'s anyone\'s guess as to how you got in here *without* the key, but no use crying over spilt milk.'),nl.

%living
descript(living) :- write('This is the living room. Where a Mudkip lives, anyways.'),nl.

%empty
descript(empty) :- write('This room is empty.'),nl,
write('What kind of dungeon designer makes a chamber with nothing in it?'),nl,
write('He must\'ve been getting paid by the room.'),nl.

% getdir( Room, List ) - defines List of valid directions to go from a given Room.

getdir(start,[n,s]).
getdir(living, [s]).
getdir(empty, [n]).

% addtool(Bool, Object, List, NewList) - if Bool='y', Object is added to List to form NewList.

addtool(n,_,H,NH) :- NH=H.
addtool(y,Objct,H,NH) :- NH=[Objct|H].

% toolbelt(Room, List, NewList) - When in Room with List (tool belt), define NewList, which
% may contain a new object found in Room.

toolbelt(Rm, H, NH) :-
tool(Rm, Tool),
member( Tool, H ),
NH = H.
toolbelt(Rm, H, NH) :-
tool(Rm, Tool), Tool = none, NH = H.

toolbelt( Rm, H, NH ) :-
tool(Rm, Tool),
%Tool is not a member of the toolbelt
write('There is a '), write(Tool), write('. Do you want to pick it up (y/n)?'), nl,
read(C),
addtool(C, Tool, H, NH).

% danger( Room, List ) - When in Room with List (tool belt), player may gain from having the
% right tool, or may die from not having the right tool.

% start
danger(start,_) :-
write('The exit is also here, but you can\'t open the door without a key.'),nl.
danger(start,H) :-
member(key,H),!,write('You have the key to exit the game!'),nl,
write('The tool belt holds: '), write(H),nl,exit.

% living
danger(living,H) :-
member(knife,H),!,
write('You just killed the Mudkip. Was that really necessary? A Mudkip never harmed anyone.'),nl,
write('Except other Pokémon, I suppose.'),nl.
danger(living,_) :- write('You were just killed by a Mudkip. How embarrasing!'),nl,exit.

% empty
danger(empty,_) :-
write('What, you think something is going to happen in an empty room?'),nl,
write('TANSTAAFL, friend.'),nl.

% moveFrom( CurrentRoom, ToolBelt ) - Player arrives in CurrentRoom with ToolBelt. We see what happens
% in this room, then figure out where to go next.

moveFrom(Here,Holding) :-
descript(Here),
danger(Here,Holding),
toolbelt(Here,Holding,NewHolding),
repeat,
getdir(Here,L),
write('You can go: '), write(L),nl,
read(Dir), hall(Here, Dir, NewRoom),
moveFrom(NewRoom,NewHolding).

exit :- writeln('You have finished the game'), !, halt.

:- moveFrom(start, []).


Here's the bug. If you enter start when you have the key, the game should end.


danger(start,H) :-
member(key,H),!,write('You have the key to exit the game!'),nl,
write('The tool belt holds: '), write(H),nl,exit.


This is the predicate that's supposed to end the game. H is the list of inventory. If 'key' is in H, then the game is over. You win. But right now, it doesn't seem to recognize if you have the key or not. (The key is in the living room.)

Any idea as to why this predicate isn't working?

Partial
2009-12-20, 22:13
Just want to say this.. I feel for you man, Prolog sucks. I took a semester of logical programming and it was lisp and prolog... not fun. Best of luck with finals dude!