void_Anthony() has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get a random number and assign it to a variable (may seem stupid, but I'm still having trouble.) The code I'm having a problem with:
do{ $goblin=$goblin-$damage; if($goblin<0){ $goblin=0; }if($wizard<0){ $wizard=0; } if($room==6){ my $dmg=int (rand 5)+1; }elsif($room==3){ my $dmg=int (rand 4)+1; }elsif($room==2){ my $dmg=int (rand 3)+1; }elsif($room==1){ my $dmg=int (rand 2)+1; } $wizard-=$dmg; print "wizard health: $wizard\t"; print "$monster health: $goblin\n"; }until($goblin<=0 || $wizard<=0); if($wizard>0){ print "good job you got $newgold gold!\n"; $gold+=$newgold; &freedale; }elsif($wizard<=0){ &die; } }else{ print "Please enter 1, 2, or q...\n"; &battle; }
I'm trying to get a random value for $dmg (depending on the room you're in) and then subtract that from $wizard. Unfortunately, every time i run the program, $wizard doesn't change (originally 50.) If someone could offer some advice as to what I'm doing wrong, I would appreciate it.

Replies are listed 'Best First'.
Re: Random Numbers?
by dragonchild (Archbishop) on May 10, 2005 at 19:50 UTC
    Add the following to the top of your script.
    use strict; use warnings;
    The problem will be self-evident.

    For those who can't wait ...


    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: Random Numbers?
by Forsaken (Friar) on May 10, 2005 at 21:07 UTC
    Completely off-topic, but have you ever considered using spaces in your code? Especially when things get more complicated I personally tend to find $wizard -= 1 a lot easier to read than $wizard-=1. Same thing for the if conditions tbqh, if($x == 1) seems easier to read than if($x==1). Perhaps tis just a matter of taste, don't know how others feel about it.

    update(suggested by Animator ;-)
    You might want to take a look at the Perlstyle POD for some very good insights on the subject.

    Remember rule one...

Re: Random Numbers?
by polettix (Vicar) on May 10, 2005 at 23:08 UTC
    The topic has been discussed in depth but I'd like to share a couple of notes.

    Follow Forsaken's advice about formatting. You can also find the perltidy program quite useful, it should be in the basic distribution and basically does the work. I was particularly curious about the following lines:

    if($goblin<0){ $goblin=0; }if($wizard<0){ $wizard=0; }
    The second if comes immediately after the closing block associated to the previous one, which makes it less readable - it may be taken for an elsif at first glance.

    Another consideration deals with the way you eval the damage with the if/elsif/... checks. I'd rather use some kind of data structure here:

    # Early in the code, i.e. in some configuration section my @max_damages = (0) x $number_of_rooms; @max_damages[ 1, 2, 3, 6 ] = ( 2, 3, 4, 5 ); # ... then $wizard -= 1 + int(rand($max_damages[$room]));
    This should save you the bore to add new elsif branches when you add rooms.

    One final question: what does it happen in rooms 4 and 5? Or is the test for room "6" just a typo, and you meant "4"? If so, note that there's even a simpler solution using rooms as numbers:

    $wizard -= 1 + int(rand($room + 1));

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
Re: Random Numbers?
by trammell (Priest) on May 10, 2005 at 20:09 UTC
    }elsif($wizard<=0){ &die; }
    You might want to use a different function name, since die() is already a Perl builtin. Unless you really meant to call Perl's die().

    You also have $dmg and $damage variables. Are they supposed to be different?

    Update: Thanks, [id://Animator]. Spending time here in PM is great for clearing up my little "pockets of ignorance".

      You cannot prefix a perl-built-in function with an ampersand. All functions prefixed by an ampersand are user-defined functions... which means he isn't calling Perl's die()
Re: Random Numbers?
by dynamo (Chaplain) on May 10, 2005 at 21:28 UTC
    Sorry for the off-topic but I just wanted to say it's really cool looking at code with variable names like $wizard and $goblin, and dealing with algorithms to do combat resolution in a fantasy RPG type setting..

    I wrote a lot of that kind of stuff when learning to program. Please feel invited to post more along these lines.

      You'll find more (and various version of this code) at Wizardv1.1... Hey, please join us if you wish :)
Re: Random Numbers?
by DrHyde (Prior) on May 11, 2005 at 09:27 UTC