Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Wizardv1.1

by void_Anthony() (Acolyte)
on May 05, 2005 at 23:42 UTC ( [id://454537]=sourcecode: print w/replies, xml ) Need Help??
Category: Fun Stuff
Author/Contact Info void_Anthony()
Description: Wizardv1.0 with some minor modifications to the 'shop' sub, a new level, a new enemy, and a little bit of small error catching. -credit for Wizard v1.0 goes to 'legolas'
$wizard=50;
$wizardl=1;
$gold=10;
$damage=3;
print"Welcome to Wizard!  Type your wizard's name...";
$name=<STDIN>;
chomp ($name);
print "Welcome $name to Wizard.  You are now in the Merlins           
+   castle.  You are surrounded  by other mages and disscussion    flo
+ws back\n";
print "and forth in the room.   Suddenly Merlin stands up in front of 
+all and says, I need a powerful man to go out and slay all the monste
+rs that have been plauging my land.\n";
print "Which one of you can raise to the challenge.  Slowly all those 
+in the room rise, including yourself.\n\n";
print "Good, good merlin utters.  Then be off.\n\n\n";
print "(note: the higher the number you choose, the more often you wil
+l encouter enemies!)\n\n\n";
sub die{
if($wizard==0){
print "Thanks for playing";
die;
}
sub toad {
 if($room!=6){
 my $toad=int (rand 4)+1;
  if($toad<=$wizardl){
  print "The goblins have been zapped into toads!\n";
  $gold=$gold+3;
  print "good job you got 3 gold!\n";
  &freedale
 }else{
  print "your spell has failed! -2 health\n";
  $wizard=$wizard-2;
  &battle;
 }
}else{
 my $toad=int (rand 7)+1;
 if($toad<=$wizardl){
  print "The demons have been zapped into toads!\n";
  $gold=$gold+6;
  print "great job you got 6 gold!\n";
  &freedale
 }elsif($toad>$wizardl){
  print "your spell has failed! -2 health\n";
  $wizard=$wizard-2;
  &battle2;
   }
  }
 }
sub battle {
 my $goblin=12;
 print "What do you want to do...press 1 to use spells or 2 for combat
+\n";
 $choice=<STDIN>;
 if($choice==1){
   &toad;
 }elsif($choice=="q"){
   &die;
 }elsif($choice==2){
 do{
  $goblin=$goblin-$damage;
  $wizard=$wizard-2;
  print "wizard health: $wizard\t";
  print "goblin health: $goblin\n";
 }until($goblin==0 || $wizard==0);
  if($wizard>0){
   print "good job you got 3 gold!\n";
   $gold=$gold+3;
   &freedale;
  }elsif($wizard<=0){
  &die;
}
    }
        }
sub battle2 {
 my $demon=18;
 print "What do you want to do...press 1 to use magic or 2   for comba
+t\n";
 $choice=<STDIN>;
 if($choice==1){
  &toad;
 }elsif($choice=="q"){
  &die;
  }elsif($choice==2){
   do{
    $demon=$demon-$damage;
    $wizard=$wizard-2;
    print "wizard health: $wizard\t";
    print "demon health: $demon\n";
   }until($demon==0 || $wizard==0);
   if(wizard>0){
    print "good job, you got 6 gold!\n";
    $gold=$gold+6;
    &freedale;
   }elsif($wizard<=0){
    &die;
   }
  }
 }
sub forest {
 print "The forest is dark and smells of old mold and rotton fish. 
+ The foul waters have turned this once peaceful place in to a\n";    
+ print "dark evil place.\n";
 my $gobbo=int (rand 6)+1;
 if ($gobbo>=5){
  print "goblins attack!\n";
  &battle;
 }elsif($gobbo!=5 || $gobbo!=6){
  print "There are no monsters here now.\n";
  &freedale
 }
}

sub mountain {
 print "Its pretty cold up here, but from this altitude you can see
+ the whole town below you.\n"; 
 print "Although it is clear that you are not the first one here!\n
+";
 my $gobbo=int (rand 6)+1;
 if ($gobbo>=4){
  print "goblins attack\n";
  &battle;
 }elsif($gobbo!=4 || $gobbo!=5 || $gobbo!=6){
   print "There are no monsters here now.\n";
   &freedale
 }
}

sub dungeon {
 print "There is a heavy feeling in the air, it's very dark and mus
+ty down here.\n"; 
 print "You can see clear signs that goblins have been here, and ma
+ybe worse things!\n";
 my $gobbo=int (rand 6)+1;
 if ($gobbo>=3){
  print "goblins attack\n";
  &battle;
 }elsif($gobbo!=3 || $gobbo!=4 || $gobbo!=5 || $gobbo!=6){
  print "There are no monsters here now.\n";
  &freedale
 }
}
sub splane {
 print "This is a mysterious place full of horribly powerful monsters.
+..\n";
 print "You see tracks unlike any other you have encountered before.\n
+";
 my $gobbo=int (rand 8)+1;
 if ($gobbo>=2){
  print "Demons attack!\n";
   &battle2
  }elsif($gobbo!=2 || $gobbo!=3 || $gobbo!=4 || $gobbo!=5 ||       $go
+bbo!=6 || $gobbo!=7 || $gobbo!=8){
    print "There are no demons here now.\n";
    &freedale
 }
}
sub shop {
    print "Hello welcome to freedale market may I help you?\n";
    print "type 1 for 30 more health..................................
+.......
+.....10 gold\n";
    print "type 2 for daggers (increases damage per round by one).....
+....................................
+....15 gold\n";
    print "type 3 for magic wands(increases magic level by one).......
+..............................
+30 gold\n";
    print "type 4 to leave the shop\n";
$shop=<STDIN>;
if($shop==1){
 if($gold<10){
  print "you don't have enough gold\n";
  &freedale;
 }else{
  print "Thank you\n";
  $gold=$gold-10;
  $wizard=$wizard +30;
  &freedale;
 }
}elsif($shop==2){
 if($gold<15){
  print "you don't have enough gold\n";
  &freedale;
  }else{
 print "thank you\n";
 $gold=$gold-15;
 $damage=$damage + 1;
 &freedale;
 }
}elsif($shop==3){
 if($gold<15){
  print "you don't have enough gold\n";
  &freedale;
}else{
 print "thank you\n";
 $gold=$gold-30;
 $wizardl=$wizardl+1;
 &freedale
 }
}elsif($shop==4){
 print "Thank you for coming!\n";
 &freedale;
}else{
 print "Please enter a number between 1 and 4!\n";
 &freedale
 }
}
sub cheat{
 $wizardl = $wizardl + 2;
 $wizard = 100;
 $gold = 60;
 &freedale
}
sub error{
 &freedale
}
sub stat{
    print "health:\t$wizard\n";
    print "magic level:\t$wizardl\n";
    print "gold:\t$gold\n";
    &freedale
}
sub freedale{
 print "you are in freedale\n";
 print "where do you want to go\n";
 print "type 1 for the forest, 2 for the mountain, or 3  for  the dung
+eon, 4 for the shop, 5 to view stats, 6 for the  Shadow Plane, or q t
+o quit.\n ";
 print "press q at anytime to quit.\n";
$room=<STDIN>;
if($room==1){
 &forest;
}elsif($room==2){
 &mountain;
}elsif($room==3){

 &dungeon;
}elsif($room==4){
 &shop
}elsif($room==5){
 &stat
}elsif($room==6){
 if($wizardl < 3){
    print "You're level isn't high enough! You must have a magic lvl o
+f 3 to come here.\n";
    &freedale
    }elsif($wizardl => 3){
     &splane
    }
}elsif($room==7){
 &cheat
}elsif($room=="q"){
 &die
}else{
 print "Please enter a number between 1 and 6 or q to  quit!\n";
 &error
}
    }
        }
&freedale
Replies are listed 'Best First'.
Re: Wizard v1.2
by wazoox (Prior) on May 06, 2005 at 11:59 UTC
    Not bad, but it's a bit rough :) It let me have -12 golds and -2 health :) I corrected these, added use strict and warnings, and added some punctuation, indented the file (thanks to perltidy) and corrected a mistake : don't use "die" as a sub name, it's dangerous, and BTW you made a big mistake with the curly braces putting all the following code inside the "die" sub, luckily it worked anyway :)
    I don't understand the warning on line 297, looks weird, if anyone comes to an idea... Enjoy!
    update: I corrected a bug that caused the program to exit if you entered anything but 1,2 or q during combat. Have fun!
    update 2: I made a single "battle" sub for better coding practice! Next, I'll create a hash of various monsters to allow some variety in monsters types.
    update 3: I forgot to remove one of the "&die". BTW, I should remove the "&" next, because it's...ugly :)
    update 4: removed the "&", work in progress, beware V 1.2 :)
    update 5:OK, I guess we hit the 1.2 mark : 6 different random monsters! :)
      A suggestion for the next update: put all the "places" where you have to fight in one subroutine and the variable things in a hash (similar as you have doen with the fighting routines).

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        Sure. Actually I was thinking about how to give some more depth to the game without making the code any longer... more places, perhaps a "maze", harder monsters (when you reach level 6 you basically never lose anymore)... And what about various "quests"? The first one would be "kill the monsters around Merlin's place", then you'll switch to another...
        I'm afraid it may turn into a real app soon :)
      How come when I kill Demons in combat, I only get 3 gold, but when I zap them with a spell, I get 6?

      Also, I hit a "deep recursion" at one point, which I thought was odd, but it didn't affect gameplay, which is, of course, smooth and realistic. :-)


      Caution: Contents may have been coded under pressure.
        Hmmm, looks like a bug :) For the deep recursion problem, I didn't really understand what happened, but I didn't care too much because it works fine anyway :)
        BTW I'm surprised to see that there so few text-based adventure games on perlmonks, and I started working on it more seriously :)
      I've modified it a bit, adding a third number for the number of coins for each monster:

      Adding a variable damage could also be a good idea.
        Fine :) Actually I'd like to implement a more complete combat system with damage, armor, etc. Also I had started a system to build "places" such as a forest, a castle, a cave so you can wander around a little, find some tresures, etc. Then of course you'll need to be able to save the game state; then why not make it CGI-able, and what about multiplayer game? Mmmmh, i'm afraid it's about to become a real project :)

        Here's what I'm working out for the "place generator" :

        Forest, mountain, village, castle, cavern maze.
        • a forest is a group of connected "rooms" with very little "walls". You can almost always go whichever direction you want. There are some obstacles though : rivers, big trees, rocks.
        • mountain is a group of connected "rooms" with walls on a prefered axis (you can easily travel from north to south, but not from east to west) and a top (from which you can go all directions), some rocks and pits. Summit is the preferred place to finc treasures of course.
        • village is a village :), with some streets, some houses, with people, treasures or monsters inside, and perhaps shops.
        • castle is made of groups of rooms (towers) with several floors (some possibly underground), connected through corridors, with a dungeon (a bigger tower) where the monsters and treasures mostly are :)
        • a cavern maze is a maze, haunted by terrible monsters, perhaps even dragons. Wow, big treasures too!
        The game would generate a random "world" to be explored. I'd create a set of basic "quests", you'll end the quest by killing a group of defined monsters or finding a set of defined treasures; then it would generate another world, harder, with a new quest, etc.
        Yes, As I thought about it, it becames quite clear that this would be easily extended to aclient-server mode, so you can play with a command line, a browser (cgi) interface (possibly graphical) or even a graphic display, and why not, make it multiplayer.
        OK, be enthusiast enough and I'll make it :)
Re: Wizardv1.1
by cbrandtbuffalo (Deacon) on May 06, 2005 at 21:40 UTC
    Hmmm, maybe another bug somewhere...
    What do you want to do...press 1 to use spells or 2 for combat 2 wizard health: 18 goblin health: 7 wizard health: 16 goblin health: 2 wizard health: 14 goblin health: -3 wizard health: 12 goblin health: -8 wizard health: 10 goblin health: -13 wizard health: 8 goblin health: -18 wizard health: 6 goblin health: -23 wizard health: 4 goblin health: -28 wizard health: 2 goblin health: -33 wizard health: 0 goblin health: -38 Died at wizard.pl line 16, <STDIN> line 102.

    That's one tough, undead goblin! :)

      It's a bug in wizard v1.1 (the combat ends ONLY if the goblin's health falls to exactly 0). Try 1.2 :)
        I have fixed that 'invincible goblin bug'(including the wizard one as well.) Also took out the extra battle sequence and made it specific for each area you fought in. I liked wazoox's idea of including more enemies but I didn't like the random idea. You might die if you encountered a demon in the forest! So I assigned a specific enemy to each area. Harder the area, harder the monster is to kill (and less success depending on your level with the toad spell.) But, you get more gold for the harder enemies. I like that quest idea, but I think it would be very long, drawn out code to accomplish this. If you have any bright ideas as to how I might get started (initiate here on the forums and novice at perl) I would gladly appreciate the advice. (I have been thinking about incorporating items in the game too. Any feedback is welcome.) See my notepad for the current code. 1.2 won't be released until I'm sure there will be many more things to offer.
Re: Wizardv1.1
by void_Anthony() (Acolyte) on May 10, 2005 at 14:53 UTC
    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.

      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
Re: Wizardv1.1
by void_Anthony() (Acolyte) on May 12, 2005 at 14:34 UTC
    Wizard1.2 will be releasedvery soon. If anyone has some last-minute suggestions or debugging tips, I'd like to hear them now. See my scratchpad for the most current version of code.
      Please, PLEASE, please PlEaSe, pL34Z3, add use strict and perhaps use warnings to your script! That will save you LOTS of headaches, so get used to ALWAYS use them!
        I will look into use strict for the next version (I want to have some ideas for 1.3. I'm running low as it is.) But if that's all, this will be it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-20 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found