AI-Prolog

 view release on metacpan or  search on metacpan

data/sleepy.pro  view on Meta::CPAN

    print('The lights are already off.'), nl.

sleep :-
    not(i_am_at(bed)),
    print('You find it hard to sleep standing up.'), nl,
    !, fail.

sleep :-
    lit(bedroom),
    print('You can not get to sleep with the light on.'), nl,
    !, fail.

sleep :-
    lit(den),
    print('The light from the den is keeping you awake.'), nl,
    !, fail.

sleep :- 
    or(i_am_holding(flyswatter), at(flyswatter, bed)),
    print('What? Sleep with a dirty old flyswatter?'), nl,
    !, fail.

sleep :-
    alive(fly),
    print('As soon as you start to doze off, a fly lands'), nl,
    print('on your face and wakes you up again.'), nl,
    make_visible(fly),
    make_visible(flyswatter),
    !, fail.

sleep :-
    print('Ahhh...you (yawn) made...it...zzzzzzzz.'), nl, nl,
    finish.

swat(fly) :-
    swat.

swat :-
    i_am_at(Place),
    not(lit(Place)),
    print('You flail aimlessly in the dark!'), nl.

swat :-
    not(i_am_holding(flyswatter)),
    print('You are not holding the flyswatter.'), nl,
    !, fail.

swat :-
    not(alive(fly)),
    print('He is dead, Jim.'), nl.

swat :-
    i_am_at(Place),
    not(at(fly, Place)),
    print('You swish the flyswatter through the air.'), nl.

    /* Have flyswatter, room is lit, fly is here and alive. */

swat :-
    buzz_off,
    print('The fly escapes into the other room.'), nl.

swat :-
    print('Success! You killed that pesky fly!'), nl,
    retract(alive(fly)).

swat :- /* For debugging... */
    print('You must have forgotten a case!', nl).

make_visible(X) :-
    visible_object(X).

make_visible(X) :-
    assert(visible_object(X)).

buzz_off :-
    at(fly, bedroom),
    lit(den),
    retract(at(fly, bedroom)),
    assert(at(fly, den)).

buzz_off :-
    at(fly, den),
    lit(bedroom),
    retract(at(fly, den)),
    assert(at(fly, bedroom)).

optional_buzz_off :-
    buzz_off.

optional_buzz_off.


/* Under UNIX, the "halt." command quits Prolog but does not
   remove the output window. On a PC, however, the window
   disappears before the final output can be seen. Hence this
   routine requests the user to perform the final "halt." */

finish :-
        nl,
        print('The game is over. Please enter the "halt." command.'),
        nl.


/* This rule just prints out game instructions. */

instructions :-
        nl,
        print('Enter commands using standard Prolog syntax.'), nl,
        print('Available commands are:'), nl,
        print('start.                   -- to start the game.'), nl,
        print('n.  s.  e.  w.  u.  d.   -- to go in that direction.'), nl,
        print('take(Object).            -- to pick up an object.'), nl,
        print('drop(Object).            -- to put down an object.'), nl,
        print('use(Object).             -- to manipulate an object.'), nl,
        print('look.                    -- to look around you again.'), nl,
        print('on.  off.                -- to control the room lights.'), nl,
        print('sleep.                   -- to try to go to sleep.'), nl,
        print('instructions.            -- to see this message again.'), nl,
        print('halt.                    -- to end the game and quit.'), nl,
        nl.



( run in 2.400 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )