I am coding an adventure game in swi-prolog and I am fairly new to it, but I understand the basics. I am trying to figure out how to set a time limit to give the user around 5 minutes to find objects before they disappear. For example, there is a secret key that takes you right to the enemy and if you dont find it in 5 minutes it disappears.
This is what I have so far...
/*
--> Diamond Cookie -- an adventure game
--> You must find the diamond cookie in order to save your sweet land
from being taken over by the healthy enemies.
--> Type start. to begin the story/game
*/
:- dynamic at/2, i_am_at/1, alive/1. /* Needed by SWI-Prolog. */
:- retractall(at(_, _)), retractall(i_am_at(_)), retractall(alive(_)).
/* defines the starting location. */
i_am_at(village).
/* shows how each of the rooms/areas are connected. */
path(snake, d, cave).
path(village, e, box).
path(box, w, village).
path(village, w, secret).
path(secret, e, village).
path(cave, u, snake).
path(cave, w, cave_entrance).
path(cave_entrance, e, cave).
path(cave_entrance, s, village).
path(ghouse, n, village).
path(ghouse, w, fridge).
path(fridge, e, ghouse).
path(room1, w, ghouse).
path(room1, e, room2).
path(secret, n, tree1).
path(tree1, s, secret).
path(secret, w, tree2).
path(tree2, e, secret).
path(secret, s, tree3).
path(tree3, n, secret).
path(village, n, cave_entrance) :- at(popsiclelight, in_hand).
path(village, n, cave_entrance) :-
write('You can''t go into that dark, fudgey cave without the popsiclelight and gumdrop batteries! Are you insane!?'), nl,
!, fail.
path(village, s, ghouse) :- at(gumdrop, in_hand).
path(village, s, ghouse) :-
write('Find the gumdrops to come inside!'), nl,
!, fail.
path(ghouse, e, room1) :- at(lolipopkey, in_hand).
path(ghouse, e, room1) :-
write('The unfrosted door is locked. A lolipop key is needed...'), nl,
fail.
/* where the various objects in the game are located. */
at(diamondcookie, snake).
at(lolipopkey, cave_entrance).
at(popsiclelight, ghouse).
at(candycane, room1).
at(gumdrop, box).
at(key, tree2).
/* snake is alive. */
alive(snake).
/* picking up objects. */
take(X) :-
at(X, in_hand),
write('You are already holding it!'),
nl, !.
take(X) :-
i_am_at(Place),
at(X, Place),
retract(at(X, Place)),
assert(at(X, in_hand)),
write('done.'),
nl, !.
take(_) :-
write('I don''t see that item here.'),
nl.
/* dropping unwanted object. */
drop(X) :-
at(X, in_hand),
i_am_at(Place),
retract(at(X, in_hand)),
assert(at(X, Place)),
write('done.'),
nl, !.
drop(_) :-
write('You are not holding it!'),
nl.
/* the six direction letters to move around */
n :- go(n).
s :- go(s).
e :- go(e).
w :- go(w).
u :- go(u).
d :- go(d).
/* how to move in a given direction. */
go(Direction) :-
i_am_at(Here),
path(Here, Direction, There),
retract(i_am_at(Here)),
assert(i_am_at(There)),
look, !.
go(_) :-
write('You cannot go that way...'), nl.
/* how to look around the area. */
look :-
i_am_at(Place),
describe(Place),
nl,
notice_objects_at(Place),
nl.
/* mention all the objects in the vicinity. */
notice_objects_at(Place) :-
at(X, Place),
write('There is a '), write(X), write(' here.'), nl,
fail.
notice_objects_at(_).
/* inventory to list all possesions */
inventory :-
write(' Inventory: '),nl,
list_items.
list_items :-
at(X, in_hand),
write(X), tab(10),
fail.
list_items.
/* getting killed and killing the celery and the snake. */
kill :-
i_am_at(fridge),
write('Horrible idea! You have just been turned into a vegetable by the celery...'), nl,
!, die.
kill :-
i_am_at(cave),
write('This is not working. The snake is very heavy and'), nl,
write('sticky, how does it even slither...').
kill :-
i_am_at(snake),
at(candycane, in_hand),
retract(alive(snake)),
write('You stab the snake repeatedly. Slimy juices'), nl,
write('ooze out of the spider''s back, and get all over you.'), nl,
write('You have killed it!!'),
nl, !.
kill :-
i_am_at(snake),
write('Punching the snake with your fists does no'), nl,
write('damage to it. '), nl, !.
kill :-
write('I do not see anything dangerous here.'), nl.
/* when you die. */
die :-
!, finish.
/* requests the user to perform the final halt after the game is over. */
finish :-
nl,
write('The game is over. Please enter the halt. command.'),
nl, !.
/* game instructions. */
instructions :-
nl,
write('Enter commands using standard Prolog syntax.'), nl,
write('Available commands are:'), nl,
write('start. -- to start the game.'), nl,
write('n. s. e. w. u. d. -- to go in that direction.'), nl,
write('take(Object). -- to pick up an object.'), nl,
write('drop(Object). -- to put down an object.'), nl,
write('inventory. -- to check your inventory.'), nl,
write('kill. -- to attack an enemy.'), nl,
write('look. -- to look around you again.'), nl,
write('instructions. -- to see this message again.'), nl,
write('halt. -- to end the game and quit.'), nl,
nl.
/* prints out instructions and tells where you are starting. */
start :-
instructions,
look.
/* describe the various rooms. Depending on
circumstances, a room may have more than one description. */
describe(village) :-
at(diamondcookie, in_hand),
write('Congratulations! You have recovered the diamond cookie'), nl,
write('and saved the land! You won.'), nl,
finish, !.
describe(village) :-
write('You are in the Sour Patch Village. To the north is the chocolate'), nl,
write('lava cave enterence; to the south is a small gingerbread house.'), nl,
write('To the east there is a small box that may have something in it.'),nl,
write('Your mission, if you accept, is to recover the diamond cookie'), nl,
write('and return it to this village to save the land from the healthy enemies!.'), nl,
write('There is also a secret in the map that if found in time, .'), nl.
describe(secret) :-
write('WOW... a gummy worm jungle! There are three trees. One tree is to the north and is pink/blue'), nl,
write('gummy worms. To the west there is a yellow/pink gummy worm tree. To the south there is an orange/green'), nl,
write('gummy worm tree. Maybe there is something important here...'), nl.
describe(tree1) :-
write('...just a tree.'), nl.
describe(tree2) :-
write('There is a hole in this tree with seems to be a chocoloate key.'), nl,
write('It must open something in a chocolatey place...'), nl.
describe(tree3) :-
write('Just a bunch of gummy frogs...'), nl.
describe(box) :-
write('Inside the box there are gumdrops, the kind that would go perfect in'), nl,
write('a popsiclelight to emit bright light.'), nl.
describe(ghouse) :-
write('You are in a small gingerbread house. The exit is to the north.'), nl,
write('There is a frosted door to the west, and it cracked opened.'), nl,
write('It smells a bit weird over there. There is an unfrosted door to the east.'), nl.
describe(fridge) :-
write('You are in the angry celery stick''s fridge! The celery sticks are'), nl,
write('angry and want to turn you to a vegetable. Get out while you can!'), nl.
describe(room1) :-
write('This minty smelling room has a sharp candycane, this would make a great weapon!'), nl.
describe(cave_entrance) :-
write('You are in the opening of a fudgey cave. The exit is to'), nl,
write('the south; there is a dark, round sprinkle-covered passage to'), nl,
write('the east.'), nl.
describe(cave) :-
alive(snake),
at(diamondcookie, in_hand),
write('The twizzler snake sees you with the diamond cookie and poisons you with ranch!!'), nl,
write(' ...you die within seconds....'), nl,
die.
describe(cave) :-
alive(snake),
write('There is a huge twizzler snake here! He poisons with ranch dressing,'), nl,
write('and it is directly in front of you!'), nl,
write('You should leave quietly, ASAP!'), nl, !.
describe(cave) :-
write('eghhh! There is a huge twizzler snake here, spewing ranch.'), nl,
write('It is going in circles around something sparkly.'), nl.
describe(snake) :-
alive(snake),
write('You are on top of a huge twizzler snake, standing on its sticky'), nl,
write('red skin. The smell of its poisonous ranch is awful.'), nl.
describe(snake) :-
write('YUCK! You''re on top of a giant dead snake!'), nl.
How would I add a time limit for the user to find an object within a five-minute time limit. Additionally, if they don't, how would I make it so that the object disappears after the five minutes and the user would need to find the objects the long way?
We were asked to write a menu based calculator on an exam where we were to return control to the menu once the desired calculation had been performed.
I wrote the following code as my solution and while the professor deemed it as correct I still think there must be a better way to return control to the menu after the the first clause to table(A,N) returns false.
Please note that I've redacted quite a bit of my original code that was irrelevant to my question.
menu :-
write('Enter a choice: '),
read(C),
choice(C).
choice(1) :-
table(5).
table(N) :-
A is 1,
start(A,N).
table(A,N) :-
K is A*N,
write(K),
nl,
A1 is A+1,
A1=<10,
table(A1, N)
;
menu.
I'm very new to prolog so the question might not be appropriately worded. Please let me know if that's the case.
You can use repeat/0 to loop forever.
menu :-
repeat, % add this line
write('Enter a choice: '),
read(C),
choice(C).
choice(1) :-
table(5).
table(N) :-
A is 1,
start(A,N).
table(A,N) :-
K is A*N,
write(K),
nl,
A1 is A+1,
A1=<10,
table(A1, N).
% ; % delete this line
% menu. % delete this line
I am trying to create a prolog program to print out the truthtable of a statement. It works fine for the example truthtable(A,B,and(A,B)),
but if I try truthable(A,B,or(A,and(A,B))) it doesn't work and shows everything as false.
and(true,true):- true.
and(false,true):-false.
and(true,false):-false.
and(false,false):-false.
or(true,true):-true.
or(false,true):-true.
or(true,false):-true.
or(false,false):-false.
non(true):-false.
non(false):-true.
evaluate(E, true) :- E, !.
evaluate(E, false).
bool(true).
bool(false).
truthtable(A,B,E):-
bool(A),
bool(B),
write(A),
write(' \t '),
write(B),
write(' \t '),
evaluate(E, R),
write(R),
nl,
fail.
Also what can I do if I want the user to add any number of inputs for example A,B and C not only A and B.
I am using SWI Prolog for windows 7 and one of my assignments is a basic logic puzzle. We have been given a sample solution to a seperate problem - including its source code.
Its the "Its a tie" problem. However I do not know how to get results using the Solve:- predicate. After consulting the .pl file do I have to input a specific command to the console or anything like that?
Thanks for any help.
Source Code (I did not write this code) :
% Problem #1, "It's a tie", Dell Logic Puzzles, October 1999
% Each man (mr so-and-so) got a tie from a relative.
tie(cupids).
tie(happy_faces).
tie(leprechauns).
tie(reindeer).
mr(crow).
mr(evans).
mr(hurley).
mr(speigler).
relative(daughter).
relative(father_in_law).
relative(sister).
relative(uncle).
solve :-
tie(CrowTie), tie(EvansTie), tie(HurleyTie), tie(SpeiglerTie),
all_different([CrowTie, EvansTie, HurleyTie, SpeiglerTie]),
relative(CrowRelative), relative(EvansRelative),
relative(HurleyRelative), relative(SpeiglerRelative),
all_different([CrowRelative, EvansRelative, HurleyRelative, SpeiglerRelative]),
Triples = [ [crow, CrowTie, CrowRelative],
[evans, EvansTie, EvansRelative],
[hurley, HurleyTie, HurleyRelative],
[speigler, SpeiglerTie, SpeiglerRelative] ],
% 1. The tie with the grinning leprechauns wasn't a present from a daughter.
\+ member([_, leprechauns, daughter], Triples),
% 2. Mr. Crow's tie features neither the dancing reindeer nor the yellow happy faces.
\+ member([crow, reindeer, _], Triples),
\+ member([crow, happy_faces, _], Triples),
% 3. Mr. Speigler's tie wasn't a present from his uncle.
\+ member([speigler, _, uncle], Triples),
% 4. The tie with the yellow happy faces wasn't a gift from a sister.
\+ member([_, happy_faces, sister], Triples),
% 5. Mr Evans and Mr. Speigler own the tie with the grinning leprechauns
% and the tie that was a present from a father-in-law, in some order.
( (member([evans, leprechauns, _], Triples),
member([speigler, _, father_in_law], Triples)) ;
(member([speigler, leprechauns, _], Triples),
member([evans, _, father_in_law], Triples)) ),
% 6. Mr. Hurley received his flamboyant tie from his sister.
member([hurley, _, sister], Triples),
tell(crow, CrowTie, CrowRelative),
tell(evans, EvansTie, EvansRelative),
tell(hurley, HurleyTie, HurleyRelative),
tell(speigler, SpeiglerTie, SpeiglerRelative).
% Succeeds if all elements of the argument list are bound and different.
% Fails if any elements are unbound or equal to some other element.
all_different([H | T]) :- member(H, T), !, fail.
all_different([_ | T]) :- all_different(T).
all_different([_]).
tell(X, Y, Z) :-
write('Mr. '), write(X), write(' got the '), write(Y),
write(' tie from his '), write(Z), write('.'), nl.
After consulting the file, just type the name of the predicate, with the arguments in parentheses (if there are any), followed by a period. If you have more than one predicate in your query, separate them by commas. In your case (I called the file solve.pl):
?- [solve].
% solve compiled 0.00 sec, 18 clauses
true.
?- solve.
Mr. crow got the cupids tie from his daughter.
Mr. evans got the leprechauns tie from his uncle.
Mr. hurley got the reindeer tie from his sister.
Mr. speigler got the happy_faces tie from his father_in_law.
true ;
false.
?-