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

Re: SImulation of an Inventory.

by cavac (Chaplain)
on Jun 24, 2012 at 20:16 UTC ( #978081=note: print w/ replies, xml ) Need Help??


in reply to SImulation of an Inventory.

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


Comment on Re: SImulation of an Inventory.
Select or Download Code
Re^2: SImulation of an Inventory.
by Dipseydoodle (Acolyte) on Jun 25, 2012 at 18:00 UTC

    Thanks. I think you solved two problems now.

    Oh and you totally got my by the way, I do love my BASIC languages, I guess I was trying to achive the same goal with this, thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://978081]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-09-20 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (151 votes), past polls