Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

While this is technically Perl code, you might want to stop reading AMIGA BASIC books ;-)

First of all, you should use strict; and use warnings;, so perl can help you find obvious errors.

Second, please stop using goto for normal code flow (there are special cases where goto is an obvious choice, but this isn't one of those). Use subs.

Third, instead of a multitide of named variables, look into using a hash. This will make further processing easier without too much work when you are expanding your inventory system.

Fourth, from your program flow, you never enter a value for the difficulty before setting the other values

Now that i have rambled about your code quality, let's try to improve it, shall we?

First of all, we have to make it work in the first place. So let's reorder stuff and fix the numerous basic problems other monks already explained. And since we don't need all the flow control just yet, we'll temporarly remove it. We also remove the system() calls, because they are OS dependant and might not even work in every terminal.:

#!/usr/bin/perl use strict; use warnings; my ($diff, $wood, $food); my $maxRNG = 6; my $RNG = rand($maxRNG); print "May I have your name please?\n"; chomp(my $name = <>); print "Thank you $name.\n"; print "Please enter a difficulty level: easy, or hard.\n"; chomp($diff = <>); print "you chose $diff.\n"; if ($diff eq "easy"){ $wood = 120; } elsif ($diff eq "hard"){ $wood = 75; } if ($diff eq "easy"){ $food = 50; } elsif ($diff eq "hard"){ $food = 20; } print "You have crash landed on a strange alien planet and must find a + way to get back to earth.\n"; print "\n"; print "You have $wood wood and $food food.\n"; print "You can:\n"; if($wood >= 10){ print "BUILD a HUT with 10 wood\n"; }

Ok, that works. More or less. The inputs are not checked, it's still spagetti code and you have a lot of named variables that should be part of a system. Oh, and yes, you do the easy/hard IF operation twice. Let's tackle the two IF blocks first, we'll just turn them into a single (formatted) one:

if ($diff eq "easy"){ $wood = 120; $food = 50; } elsif ($diff eq "hard"){ $wood = 75; $food = 20; }

Much better, isn't it? Next, we'll put the inventory (wood and food) into a single hash. We'll also check the inputs of both questions:

#!/usr/bin/perl use strict; use warnings; my %inventory; my $maxRNG = 6; my $RNG = rand($maxRNG); my $name = ""; while($name eq "") { print "May I have your name please? "; chomp($name = <>); } print "Thank you $name.\n"; while(1) { print "Please enter a difficulty level: easy or hard? "; chomp(my $diff = <>); if ($diff eq "easy"){ $inventory{wood} = 120; $inventory{food} = 50; last; } elsif ($diff eq "hard"){ $inventory{wood} = 75; $inventory{food} = 20; last; } else { next; } print "You chose $diff.\n"; } print "You have crash landed on a strange alien planet and must find a + way to get back to earth.\n"; print "\n"; print "You have:\n"; foreach my $item (sort keys %inventory) { print " $inventory{$item} $item\n"; } print "You can:\n"; if($inventory{wood} >= 10){ print "BUILD a HUT with 10 wood\n"; }

Now, as a final cleanup step, let's take the two initialization functions (playername and difficulty) out of the bulk of the program and into a separate file (a perl module). No object orientation, just to show you how to separate functions so you wont end a with a hundred-thousand-lines file.

After this final edit, we have two files. The main program (i called it alien.pl) reads:

#!/usr/bin/perl use strict; use warnings; use AlienInit; my $maxRNG = 6; my $RNG = int(rand($maxRNG)+1); my $name = AlienInit::getUsername(); my ($diff, %inventory) = AlienInit::getInventory(); print "You have crash landed on a strange alien planet and must find a + way to get back to earth.\n"; print "\n"; print "You have:\n"; foreach my $item (sort keys %inventory) { print " $inventory{$item} $item\n"; } print "You can:\n"; if($inventory{wood} >= 10){ print "BUILD a HUT with 10 wood\n"; }
and the file with the initialization functions called AlienInit.pm:
package AlienInit; use strict; use warnings; sub getUsername { my $name = ""; while($name eq "") { print "May I have your name please? "; chomp($name = <>); } print "Thank you $name.\n"; return $name; } sub getInventory { my ($diff, %inventory); while(1) { print "Please enter a difficulty level: easy or hard? "; chomp($diff = <>); if ($diff eq "easy"){ $inventory{wood} = 120; $inventory{food} = 50; last; } elsif ($diff eq "hard"){ $inventory{wood} = 75; $inventory{food} = 20; last; } else { next; } print "You chose $diff.\n"; } return ($diff, %inventory); } 1;

That's it. If we ever meet in person, i expect you to buy me a coffee (i use the same system with all my apprentices ;-)

"You have reached the Monastery. All our helpdesk monks are busy at the moment. Please press "1" to instantly donate 10 currency units for a good cause or press "2" to hang up. Or you can dial "12" to get connected directly to second level support."

In reply to Re: SImulation of an Inventory. by cavac
in thread SImulation of an Inventory. by Dipseydoodle

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (2)
    As of 2014-09-21 20:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (176 votes), past polls