There are a lot of ways to attack that problem -- the question is are you more interested in writing the program, or in writing the story? I ask because you will probably be making a pretty serious commitment to learning Perl and coding to get to the point where you're writing the story.
That said, here's a skeleton for a program:
use strict;
use warnings;
use IO::Prompt;
$|++;
# Map:
# foo <-> baz
# | ^
# | |
# | V
# +----> bar
# You can get from foo to bar, but you have to return via baz.
# You can also go from foo to baz to bar.
my %locations = (
foo => [ qw(bar baz) ],
bar => [ qw(baz) ],
baz => [ qw(foo bar) ],
);
my $current_location = 'foo';
while(1) {
print_location($current_location);
$current_location = read_destination($current_location);
if ($current_location eq 'quit') {
print "Bye!\n";
exit;
}
}
sub print_location {
my ($where) = @_;
if (-f "$where.txt") {
if (open my $fh, "<", "$where.txt") {
while(defined($_ = <$fh>)) {
print;
}
close $fh;
}
}
else {
print "There's nothing interesting here.\n";
}
}
sub read_destination {
my ($where) = shift;
my %next;
@next{ @{$locations{$where}} } = ();
print "You can go to: ", (join ', ', sort keys %next),"\n";
my $next_place;
while(! $next_place or !exists($locations{$next_place})) {
prompt "Pick a place: ";
$next_place = $_;
last if $next_place eq 'quit';
}
return $next_place;
}
Notice that the %locations data structure defines the locations and their relationships; in a really flexible program, you'd define a file format and read this in from it.
The print_location routine looks for a static description file corresponding to the name of the location and prints it, or a generic "nothing to see" message if there's no description file.
In my test story I created foo.txtwhich contained
It's beautiful beyond belief here at foo!
and bar.txt which contained
It's cold, dark, and scary here at bar.
You wish you were back at foo.
This gets you custom descriptions for those locations, and the generic one for baz.
read_destination shows the list of possible places you can go, and prompts you to enter one, making you keep entering places until you pick a good one or say 'quit'.
Obviously this only lets you walk around without doing anything. If you wanted to add that, you'd need to expand read_destination to understand verbs as well as locations; you'd need to add objects (as in 'things', not code entities) and what room they were in, or if they were being carried, and then you'd need to handle verbs (or at least 'use') for the objects ,,, and pretty soon you've rewritten TADS.
I encourage you to keep playing with this anyway; it's a great way to learn more Perl. I've used a few advanced techniques in here - a hash of arrays, and a hash slice to set up the "where can you go next" test, and I've installed a CPAN module (IO::Prompt), because that was easier than writing it myself.
This is a jumping-off point that may be useful to you. I can't give you much more advice other than to say this is a great way to learn Perl, because you have an itch you want to scratch. Also, if you ever turn this into a generic program for reading a story file and creating an adventure, you've got a great CPAN module on your hands.
Good luck, and happy programming! |