Prolog Code
{{{% Hunt the Wumpus % A Logic Puzzle-Inspired Textgame % Katie Rivard, 6/28/04 % Map: %%%%%%%%%%%%% %a %b %c %dW% %%%%%%%%%%%%% %e %f@%g %h$% %%%%%%%%%%%%% %i %j %k %l % %%%%%%%%%%%%% %m %n %o %p % %%%%%%%%%%%%%
%% Figure out a way to make this less horrid: northof(a,e). northof(b,f). northof(c,g). northof(d,h). northof(e,i). northof(f,j). northof(g,k). northof(h,l). northof(i,m). northof(j,n). northof(k,o). northof(l,p).
westof(a,b). westof(b,c). westof(c,d). westof(e,f). westof(f,g). westof(g,h). westof(i,j). westof(j,k). westof(k,l). westof(m,n). westof(n,o). westof(o,p).
southof(A,B):-
- northof(B,A).
eastof(A,B):-
- westof(B,A).
adj(A,B):-
- northof(A,B); southof(A,B); eastof(A,B); westof(A,B).
:- dynamic(gold/1). :- dynamic(you/1).
pit(f). wumpus(d). gold(h). you(i).
goto(X):-
- you(L), adj(X,L), retract(you(L)), assertz(you(X)), write('You are in room '), write(X), nl.
look:-
- you(X), write('You are in room '), write(X), nl, !, tell_north(X),tell_south(X),tell_east(X),tell_west(X), tell_breezy(X), tell_smelly(X).
climb:-
- gold(you), you(i), retract(you(i)), assertz(you(out)).
climb:-
- write('You cannot climb out!'), nl.
quit:- !, fail.
help:-
write('Commands:\n\tlook, goto(room), climb, quit, help.\n\tEnd each command with a period.'),nl.
%% This one's more learning-ful, but less useful to the game. %tell_adj(X,T):- % adj(X,Y), % \+(member(Y,T)), % write(Y),write(' is adjacent.'),nl, % tell_adj(X, [Y|T]).
tell_north(X):-
- northof(Y,X), write(Y), write(' is to the north.'),nl.
tell_north(_).
tell_south(X):-
- southof(Y,X), write(Y), write(' is to the south.'),nl.
tell_south(_).
tell_east(X):-
- eastof(Y,X), write(Y), write(' is to the east.'),nl.
tell_east(_).
tell_west(X):-
- westof(Y,X), write(Y),write(' is to the west.'),nl.
tell_west(_).
breezy(X):-
- adj(X, Y), pit(Y).
tell_breezy(X):-
- breezy(X), write('You feel a breeze.'),nl.
tell_breezy(_).
smelly(X):-
- adj(X, Y), wumpus(Y).
tell_smelly(X):-
- smelly(X), write('Something smells horrible... '), nl.
tell_smelly(_).
goldcheck:-
- gold(X), you(X), retract(gold(X)), assertz(gold(you)), write('You found the gold!'), nl.
goldcheck.
death:-
- you(L), (
- (pit(L),write('You fell into a pit!'),nl); (wumpus(L), write('You are wumpus food!'),nl)
done:-
- death; you(out).
go:- done,
- write('Thanks for playing...'), nl.
go:-
write('>> '), read(X), call(X), goldcheck, go.}}}