Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://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":



  • 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 cooling their heels in the Monastery: (2)
As of 2024-03-19 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found