Here it is. Okay so it isn't tommorow. I shortened it a bit, but its the
same really.
Joel:
I've gone through and suggested some simple changes up through the first major block. You've already been given a good look at how you might rework the entire state of the game, so this is more simple. Still, maybe you'll find some useful suggestions in it.
Good luck.
James
P.S. Can't wait to play the sequel.
(warning: long section of code ahead)
#!/usr/bin/perl
use warnings; use strict;
# Yours:
# my $lightbulb = 0;
# Mine:
my $lightbulb = 'on'; # I changed tests to look for 'on' or 'off' below
# Remembering magic numbers hurts my brain, so I changed them to something easier.
# It also allows a fancy trick with the messages later...
my $plate = 0;
# Yours:
# my $dowhat ="\nWhat will you do?\n";
# my $bridge = "\nYou are on the bridge. There is a red light flashing on the main console.\nThere are doorways to the north, south, and west.\n";
# my $bridge2 = "\nYou are on the bridge. You can see out the window that the ship has landed on an unknown planet.\nThere are doorways to the north, south and west\n\n";
# my $light = "\nThe light is labeled \"Smoke alert in engineering\"\n\n";
# Mine:
my %messages = ( do_what => "\nWhat will you do?\n",
bridge_light_on => "\nYou are on the bridge. There is a red light flashing on the main console.\nThere are doorways to the north, south, and west.\n",
bridge_light_off => "\nYou are on the bridge. You can see out the window that the ship has landed on an unknown planet.\nThere are doorways to the north, south and west\n\n",
light => "\nThe light is labeled \"Smoke alert in engineering\"\n\n" );
# I like sticking all the messages in a single variable, a hash.
# When I need them, I can just ask for them by name.
my $engine = "\nYou are in the engineering room. There seems to be some smoke coming from\nan open panel.\nThere is a wall panel on the floor. The bridge is to the north.\n\n";
my $engine2 = "\nYou are in the engineering room. There seems to be smoke coming from an open panel.\nThe bridge is to the north\n\n";
my $engine3 = "\nYou are in engineering. The bridge is to the north\n\n";
my $crew = "\nYou enter the crew quarters. Of course, since the crew evacuated, you have them all to yourself. The bridge is to the south\n\n";
my $airlock2 = "\nYou are in the airlock. The ship has landed on a habitable planet, and\nyou may leave if you want to. The bridge is to the east\n";
my $airlock = "\nYou are in the airlock. You are currently travelling through hyperspace,\nand cannot leave the ship. The bridge is to the east\n";
print qq(Welcome to "Spaceship"\n); print "What is your name? \n\n"; print "My name is:";
# Yours: # my $name=<>; # chomp $name; # Mine: chomp( my $name = <> ); # You can do both in one step, if you like.
print "Welcome to the bridge, $name\n\n", "Uh Oh, it looks like something is wrong. You will need to make an emergency\nlanding... If you can fix the ship first.\n\n";
# Mine;
bridge();
# Have to call my subroutine myself, instead of just running into a goto() block.
# Yours: # ENGINE: { # Mine; sub bridge { # Create a subroutine, a reusable block of code.
# Yours:
# if ($lightbulb eq 'on') { # changed from == 1 to eq 'on'
# print "$bridge"; # don't quote variables unless you need to add to them
# }
# else {
# print "$bridge2";
# }
# Mine;
print $messages{"bridge_light_$lightbulb"};
# Since $lightbuld has my state in it and I changed the messages to include the state,
# I can just let Perl figure out which one I want.
# Yours:
# BRIDGE2: {
# Mine;
while (1) { # infinite loop
# You just want to repeat this until we match.
# Yours: # print "$do_what"; # my $do = <>; # chomp $do; # Mine; my $do = do_what(); # Call me new subroutine (see below) and store results. if ($do eq "examine light") { print $messages{light}; # Yours: # redo BRIDGE2; # Mine; redo; # We don't need a label. redo() defaults to the enclosing loop. }
# Yours: # elsif ($do eq "north") { # Mine; elsif ($do =~ /^n(?:orth)?$/) { goto CREW; } # Use a Regular Expression to match n or north. I'm a lazy typist. ;) # if ($do eq "west") { elsif ($do =~ /^w(?:est)?$/) { goto AIRLOCK; } elsif ($do eq "look") { # Yours: # goto BRIDGE; # Mine; bridge(); # Call subroutine, instead of goto(). } # elsif ($do eq "south") { elsif ($do =~ /^s(?:outh)?$/) { goto ENGINE; } # Yours: # goto BRIDGE2; # No longer needed. Loops repeat automatically.
} }
# Mine; sub do_what { print $messages{do_what}; # ask for action chomp( my $input = <> ); # read one in and clean it up return $input; # send it back to where sub was called } # A simple get a line of input subroutine we can call 1,003 times. :D
# Changing the rest is left as an exercise to the reader...
ENGINE: {
if ($plate == 0) {
print "$engine";
}
elsif ($plate == 1) {
print "$engine2";
}
elsif ($plate == 2) {
print "$engine3";
}
ENGINE2: {
print $messages{do_what};
my $do = <>;
chomp $do;
if ($do eq "take panel") {
print "You take the metal panel";
$plate=1;
}
elsif ($do eq "north") {
bridge();
}
elsif ($do eq "use panel") {
if ($plate == 1) {
print "You replace the plate over the hole in the wall, and\nsinglehandedly save yourself from smoke inhalation.\n";
$plate = 2;
$lightbulb = 'off';
}
else {
print "\nYou have no plate to use\n";
}
}
elsif ($do eq "look") {
goto ENGINE;
}
}
goto ENGINE2;
}
CREW: { print $crew; CREW2: { print $messages{do_what}; my $do = <>; chomp $do; if ($do eq "south") { bridge(); } elsif ($do eq "look") { goto CREW; } goto CREW2; } }
AIRLOCK: {
if ($lightbulb eq 'off') {
print "$airlock2";
}
else {
print "$airlock";
}
AIRLOCK2: {
print $messages{do_what};
my $do = <>;
chomp $do;
if ($lightbulb eq 'off') {
if ($do eq "leave ship") {
goto WIN;
}
else {
goto AIRLOCK2
}
}
if ($lightbulb eq 'on') {
if ($do eq "leave ship") {
print "\nYou can't leave yet, you have to fix the ship first so it can land\n";
}
}
if ($do eq "east") {
bridge();
}
else {
goto AIRLOCK2;
}
}
}
WIN: {
print "Congratulations! You fixed the ship. You have left to explore the alien\nlandscape. To continue this storyline, wait for me to get bored and make a\nsequal.\n";
goto PLAY;
}
PLAY: { print 'Play again? [Y/N]'; my $playagain=<>; chomp $playagain; if ($playagain eq "y") { $plate=0; $lightbulb = 'on'; print "\n\n\n\n\n"; bridge(); } if ($playagain eq "n") { print "Exiting..."; exit 0; } redo PLAY }
__END__
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>