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 spider.pro 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 "spider.pro" is in the current directory, the beginning of a simple session might look something like this ("?- " is the command prompt):[ovid@tomis data]$ aiprolog spider.pro 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 spider.pro. 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.
Cheers,
Ovid
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 | |
by Ovid (Cardinal) on Feb 14, 2005 at 01:21 UTC | |
by BUU (Prior) on Feb 14, 2005 at 10:44 UTC | |
by halley (Prior) on Feb 14, 2005 at 14:38 UTC | |
by Ovid (Cardinal) on Feb 14, 2005 at 17:02 UTC | |
by dimar (Curate) on Feb 14, 2005 at 17:23 UTC | |
| |
by BUU (Prior) on Feb 14, 2005 at 19:37 UTC | |
| |
|
Re: Easy Text Adventures in Perl
by Courage (Parson) on Feb 14, 2005 at 16:34 UTC |