--As of Thursday, February 19, 2004 6:31 PM -0500, Joel is alleged to have said:

Here it is. Okay so it isn't tommorow. I shortened it a bit, but its the
same really.

--As for the rest, it is mine.


Here's a first approximation of a rewrite. ;-) (Major problem: it always asks what you want to do twice...)

This version keeps track of the state of the game, and uses that to guide the flow. Note that I actually did put commands in a variable...

However, now each location you can go to is entirely independent of any other.

I'm sure this can be simplified. It's just the first treatment I thought of.

Daniel T. Staal

---------------------------------------------------------------
This email copyright the author.  Unless otherwise noted, you
are expressly allowed to retransmit, quote, or otherwise use
the contents for non-commercial purposes.  This copyright will
expire 5 years after the author's death, or in 30 years,
whichever is longer, unless such a period is in excess of
local copyright law.
---------------------------------------------------------------
#!/usr/bin/perl



use warnings;

use strict;



# Game strings.

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 $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 $light = "\nThe light is labeled \"Smoke alert in engineering\"\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";



# Game locations

my %places = (

        BRIDGE          => \&bridge,

        AIRLOCK                 => \&airlock,

        ENGINE          => \&engine,

        CREW                    => \&crew,

);



# Game variables.

my %gamestate;



# Actually start the program.



# Main game loop.

my $playtime = 'y';

while ($playtime eq 'y')

{

        # Game variables.

        %gamestate = ( 

                location                => 'BRIDGE',

                plate           => 0,

                lightbulb       => 0,

                previous                => 'NONE',

                command         => 'NONE',

                );



        print qq(Welcome to "Spaceship"\n);

        print "What is your name? \n\n";

        print "My name is: ";

        chomp ($gamestate{'name'}=<>);

        print "Welcome to the bridge, $gamestate{'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";



        while ($gamestate{'location'} ne 'WINNER')

        {

                $gamestate{'command'} = getcommand();

                &{$places{"$gamestate{'location'}"}}();

        }



        print 'Play again? [y/n]';

        chomp ($playtime=<>);

        print "\n\n\n\n\n";

}       # End of game loop.



# End the game.

print "Exiting...";





#

# End of program.  Start of subroutines.

#



# Get a command.

sub getcommand {

        print "$dowhat";

        chomp (my $command = <>);

        return $command;

}



# They're on the bridge.

sub bridge {

        if ($gamestate{'previous'} ne 'BRIDGE') {

                if ($gamestate{'lightbulb'} == 0) {

                        print "$bridge";

                } else {

                        print "$bridge2";

                }

        }

        $gamestate{'previous'} = $gamestate{'location'};

        if ($gamestate{'command'} eq "examine light") {

                print $light;

        } elsif ($gamestate{'command'} eq "north") {

                $gamestate{'location'} = 'CREW';

        } if ($gamestate{'command'} eq "west") {

                $gamestate{'location'} = 'AIRLOCK';

        } elsif ($gamestate{'command'} eq "look") {

                if ($gamestate{'lightbulb'} == 0) {

                        print "$bridge";

                } else {

                        print "$bridge2";

                }

        } elsif ($gamestate{'command'} eq "south") {

                $gamestate{'location'} = 'ENGINE';

        }

}



# They're in the airlock.

sub airlock {

        my $command = $_[0];

        if ($gamestate{'previous'} ne 'airlock') {

                if ($gamestate{'lightbulb'} == 1) {

                        print "$airlock2";

                } else {

                        print "$airlock";

                }

        }

        $gamestate{'previous'} = $gamestate{'location'};

        if ($gamestate{'command'} eq "leave ship"){

                if ($gamestate{'lightbulb'} == 1) {

                        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";

                        $gamestate{'location'} = 'WINNER';

                } elsif ($gamestate{'lightbulb'} == 0) {

                        print "\nYou can't leave yet, you have to fix the ship first 
so it can land\n";

                }

        }

        $gamestate{'location'} = 'BRIDGE' if ($gamestate{'command'} eq "east");

}



# They're in the engine room.

sub engine {

        if ($gamestate{'previous'} ne 'ENGINE') {

                if ($gamestate{'plate'} == 0) {

                        print "$engine";

                } elsif ($gamestate{'plate'} == 1) {

                        print "$engine2";

                } elsif ($gamestate{'plate'} == 2) {

                        print "$engine3";

                }

        }

        $gamestate{'previous'} = $gamestate{'location'};

        if ($gamestate{'command'} eq "take panel") {

                print "You take the metal panel";

                $gamestate{'plate'}=1;

        } elsif ($gamestate{'command'} eq "north") {

                $gamestate{'location'} = 'BRIDGE';

        } elsif ($gamestate{'command'} eq "use panel") {

                if ($gamestate{'plate'} == 1) {

                        print "You replace the plate over the hole in the wall, 
and\nsinglehandedly save yourself from smoke inhalation.\n";

                        $gamestate{'plate'} = 2;

                        $gamestate{'lightbulb'} = 1; 

                } else {

                        print "\nYou have no plate to use\n";

                }

        }

}



# They're in the crew's quarters.

sub crew {

        print $crew if ($gamestate{'previous'} ne 'CREW');

        $gamestate{'previous'} = $gamestate{'location'};

        if ($gamestate{'command'} eq "south") {

                $gamestate{'location'} = 'BRIDGE';

        }

        elsif ($gamestate{'command'} eq "look") {

                print $crew;

        }

}




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to