good chemistry is complicated,
and a little bit messy -LW
Re: SImulation of an Inventory.by cavac (Deacon)
|on Jun 24, 2012 at 20:16 UTC||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.:
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:
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:
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:
and the file with the initialization functions called AlienInit.pm:
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."