in reply to Wizardv1.1

I have a problem. I'm trying to make the damage a monster inflicts on you random within a certain range (bigger range for more powerful monsters.) But, the values are never random; they are always zero. Here's what a portion of the code looks like:
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; } }
If anyone could give me some advice as to why this isn't working, I would be very grateful.

Replies are listed 'Best First'.
Re^2: Wizardv1.1
by eric256 (Parson) on May 10, 2005 at 17:50 UTC

    You put my $dmg inside the if block. The my part tells it that when that block ends at the next } to destroy your $dmg variable. In other words teh my part tells it that the variable $dmg should only be available for the current block. To fix it add my $dmg; outside the if and remove the my from inside the block.

    my $dmg; if($room==6){ $dmg=int (rand 5)+1; }elsif($room==3){ $dmg=int (rand 4)+1; }elsif($room==2){ $dmg=int (rand 3)+1; }elsif($room==1){ $dmg=int (rand 2)+1; }

    That might be better written as

    my $dmg = int (rand $room + 1) + 1;

    ___________
    Eric Hodges