Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

spoonman:

The biggest item I'd like to point out is that your flow control is flawed. You're using subroutines to call themselves and each other without ever returning, causing the stack to get deeper and deeper. I'd suggest instead that you have a control loop like this:

sub process_command { print "\n\n:"; chomp ($command = <STDIO>); $command =~ tr/A-Z/a-z/; if ($currentRoom == HALLWAY) { hallwayCommand(); } elsif ($currentRoom == BEDROOM) { bedroomCommand(); } ... etcetera ... }

Then, in the cases where you're calling xxxxxxxCommand(), you'd instead just use:

$currentRoom = HALLWAY; return;

Obviously, you can skip the assignment if you're already *in* the hallway.

It looks like your game will be fun when you get it ironed out! But you're working too hard right now: there's duplicated code in various places. Whenever you see code that's duplicated, or is *similar* to code in other places, you should think about methods to turn it into a subroutine.

For example:

if ($uniform == HAVE) { print "\n\nOne slightly uncomfortable uniform."; } if ($keyCard == HAVE) { print "\n\nYour access keycard."; } if ($lighter == HAVE) { print "\n\nOne old lighter."; } if ($closetKey == HAVE) { print "\n\nThe key to the utility closet."; }

In many (?all?) room, you have similar code. The problem as I see it is that you're individually managing each item by name (i.e., with a separate variable), which is pretty much *forcing* you to write all those duplicated lines of code.

I'd suggest making a hash hold all your objects, so you can write a subroutine to display what you see, kinda like this:

# At start of program, set up the starting location and description of + each object my $Objects = ( light => { location=>BEDROOM, description=>'The chair in front of your desk has your uniform s +lung over it', }, keycard => { location=>BEDROOM, description=>'Your keycard sits on the desk', } ... etcetera ... }; sub display_items_in_location { my $loc_to_display = shift; for my $item (keys %Objects) { if ($Objects{$item}{location} == $loc_to_display) { print $Objects{$item}{location}; } } }

Then in your various rooms, you can display the items with a single subroutine call:

sub bedroomCommand { ... elsif ($light == TRUE) { print "The sleeping quarters are quite small.... display_items_in_location(BEDROOM); print "There are no paintings or posters on the wall... sleep 5; }

And when you want to pick something up, you just reference the object in the hash:

elsif ($command =~ "keycard" or $command =~ "take keycard" ... ... elsif ($light == TRUE) { if ($Objects{keycard}{location} == HAVE) { print "You already have it.\n\n"; sleep 2; return; # from my first comment } else { print "You pick up the keycard.\n..... sleep 2; $Objects{keycard}{location} = HAVE; $Objects{keycard}{description} = 'Your access keycard. +';

Notice that you may want to change the description as you do things with it, so your inventory doesn't look like it has a desk in it! Also, it can simplify your inventory, since HAVE is just another location.

There are other things you'll eventually want to tune up, but other monks have caught many of them, so I'll leave them out for now.

Note: I find that when working to improve on a task, like writing, I try to learn how to fix a couple of things at a time. It's too difficult to learn 10 things at once. So when I was learning to be a writer, I had my editors give me a "top five" list of faults in my writing after they review each article. I'd review my next article with those points in mind, and eventually, I'd learn one well enough that it would fall off the list to be replaced with something else.

That's when I learned another thing: You'll never be perfect at it, as someone can always come up with a "top five" list of things for you to improve on. Though, when you get really good, they may have to review a *lot* of material to find all five!

One more point about the "top five" list: Sometimes people will add an item to the list that isn't really a fact but an opinion. If you ever question an item on the list, double-check it with others to check. For example: code indentation and braces are a hotly contended topic. But for most languages, there isn't a "best" way to do it, usually it's just style and opinion. A few languages have relatively firm conventions that you may want to adhere to, such as those primarily used in a single IDE that insists on reformatting to it's convention. While opinions differ on where your braces go, I believe that *consistency* is quite important. Luckily, you're doing quite well on that. It makes the code easier to read and maintain.

Every once in a while, I get a bit long winded, and that obviously happened today. So to wrap it up, I'd task you with a "top two items":

  • Think about ways to use subroutines to save yourself effort.
  • Practice your program flow handling.

Keep it up, it looks like you're doing well and having fun. I'm looking forward to updates and future questions.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Text Based Perl Game by roboticus
in thread Text Based Perl Game by spoonman2525

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found