Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Easy Text Adventures in Perl

by Ovid (Cardinal)
on Feb 13, 2005 at 23:14 UTC ( #430652=perlmeditation: print w/replies, xml ) Need Help??

There always seems to be a bit of interest in games with programmers. japhy writes about CRPGs, Perl, Gtk2, and my vision, Tiefling picked up quite a few ++ votes and tons of responses for Daft text adventure project and dragonchild writes about Zork (but not much about Perl) in Testing Zork-style game. Well, while continuing to try to inspire interest in logic programming, I've bundled a very tiny (7 room) adventure game with the latest AI::Prolog. AI::Prolog, if you've not heard of it, is a pure Perl Prolog compiler. I've written about this at Bringing Logic Programming to Perl.

First, you have to use the aiprolog shell that now comes with the distribution. The game is also included in the distribution in the data/ directory. The aiprolog shell can be installed automatically when you install AI::Prolog or you can choose to run it separately, if you prefer. You will find it in the bin/ dir.

Assuming that "" is in the current directory, the beginning of a simple session might look something like this ("?- " is the command prompt):

[ovid@tomis data]$ aiprolog Welcome to AI::Prolog v 0.5 ?- % no more ?- start. Enter commands using standard Prolog syntax. Available commands are: start. -- to start the game. n. s. e. w. u. d. -- to go in that direction. take(Object). -- to pick up an object. drop(Object). -- to put down an object. kill. -- to attack an enemy. look. -- to look around you again. instructions. -- to see this message again. halt. -- to end the game and quit. You are in a meadow. To the north is the dark mouth of a cave; to the south is a small building. Your assignment, should you decide to accept it, is to recover the famed Bar-Abzad ruby and return it to this meadow. start ?- n. Go into that dark cave without a light? Are you crazy? You cannot go that way.n ?-

Shell commands in the aiprolog shell start with a percent sign '%' (which isn't too surprising as this is how you begin comments in Prolog.) The first command, % no more, tells the shell to not issue the More? (y/N) command after you issue queries. Usually you would want those, but when you're playing the game, it's quite annoying.

The second command, start., is a Prolog predicate defined in the program The commands that you issue to play the game are actually simple Prolog statements. You're learning logic programming and playing a game at the same time! Playing around with the shell, reading the docs and working through an easy guide to Prolog should be enough to get you started learning Prolog, though you really don't have to know it to play the game.

Just to give you an idea of how easy this is, here's the code to pick up (take) an object:

take(Object) :- i_am_at(Place), at(Object, Place), retract(at(Object, Place)), assert(at(Object, in_hand)), print('OK.'), nl.

The only thing that might be mysterious about that is "nl" and that's just a command to print a newline.

If you want to write your own games (or other sample programs), feel free to send them to me and they might make it into the next distribution. Also, patches, suggestions and requests welcome! I expect to do a lot more with AI::Prolog in the future and the more involvement, the merrier.

Note that the latest information about AI::Prolog is usually posted to my use.perl journal.


New address of my CGI Course.

Replies are listed 'Best First'.
Re: Easy Text Adventures in Perl
by thor (Priest) on Feb 14, 2005 at 01:09 UTC
    While I agree that this is pretty cool, it's a bit of a misnomer to say that you're writing text adventures in Perl. You're writing them in Prolog, for which there happens to be an Inline-style module.


    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      Yeah, it's pretty hard for me to disagree with that. Still, I think more Perl children should learn logic programming and so I toss out candy to lure 'em in.

      Update: though when I stop to think about it, if I lied and just said "I've created a Perl program that lets you use simple commands to make text adventures really easy to write", no one would have thought anything of it :/


      New address of my CGI Course.

        Still, I think more Perl children should learn logic programming and so I toss out candy to lure 'em in.
        I've heard this statement and other similar statements any number of other times, but I'm afraid I've yet to fully grasp the reasoning behind them, so I was wondering if I might impose upon your good nature and inquire as to exactly why you feel people should learn Logic Programming in general (and prolog in specific?)? Is it just a general notion that the more paradigms we're exposed to, the more options you have when devising solutions? Is there perchance a specific example of a "real world" (for some value of real, obviously) problem that is vastly easier to solve in prolog and you could present here, hopefully with contrasting examples from multiple languages?
Re: Easy Text Adventures in Perl
by Courage (Parson) on Feb 14, 2005 at 16:34 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://430652]
Approved by chromatic
Front-paged by grinder
and the sunlight beams...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2017-02-22 15:51 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (331 votes). Check out past polls.