#!/usr/bin/perl
my $fred = gen_char();
my $barney = gen_char();
print "fred:\n";
print "strength: $fred->{'strength'} +$fred->{'dam_bonus'} dam\n";
print "dexterity: $fred->{'dexterity'} +$fred->{'hit_bonus'} to hit\n"
+;
print "endurance: $fred->{'endurance'}\n";
print "armor class: $fred->{'armor_class'}\n";
print "\nbarney:\n";
print "strength: $barney->{'strength'} +$barney->{'dam_bonus'} dam\n";
print "dexterity: $barney->{'dexterity'} +$barney->{'hit_bonus'} to hi
+t\n";
print "endurance: $barney->{'endurance'}\n";
print "armor_class: $barney->{'armor_class'}\n";
print "\nlet the battle begin...\n";
until($fred->{'endurance'} <= 0 || $barney->{'endurance'} <= 0 ){
sleep(1);
$round++;
print"round:$round\n";
fight();
print "fred: $fred->{'endurance'}\n";
print "barney: $barney->{'endurance'}\n\n";
}
if ($fred->{'endurance'} > $barney->{'endurance'}){
print "fred wins!!!\n";
}elsif($barney->{'endurance'} > $fred->{'endurance'}){
print "barney wins!!!\n";
}else{
print "tie!\n";
}
print "\ntotal rounds = $round\n";
sub gen_char{
my %char;
$char{'strength'} = roll(3, 6, 3);
$char{'dexterity'} = roll(3, 6, 3);
$char{'endurance'} = roll(3, 20, 6);
$char{'armor_class'} = roll(1, 4, 3);
if($char{'strength'} > 19){ $char{'dam_bonus'} = 3;
}elsif($char{'strength'} <= 19 && $char{'strength'} > 17){
$char{'dam_bonus'} = 2;
}elsif($char{'strength'} <= 17 && $char{'strength'} > 15){
$char{'dam_bonus'} = 1;
}else{
$char{'dam_bonus'} = 0;
}
if($char{'dexterity'} > 19){
$char{'hit_bonus'} = 3;
}elsif($char{'dexterity'} <= 19 && $char{'dexterity'} > 17){
$char{'hit_bonus'} = 2;
}elsif($char{'dexterity'} <= 17 && $char{'dexterity'} > 15){
$char{'hit_bonus'} = 1;
}else{
$char{'hit_bonus'} = 0;
}
return \%char;
}
sub roll {
my $number = $_[0];
my $sides = $_[1];
my $modifier = $_[2];
my $total = 0;
for($i = 0; $i < $number; $i++){
$total += int(rand($sides)+1);
}
$total += $modifier;
return $total;
}
sub fight {
$barney_attack = roll($fred->{'armor_class'}, 6, $barney->{'hi
+t_bonus'});
$fred_attack = roll($barney->{'armor_class'}, 6, $fred->{'hit_
+bonus'});
my $damage = 0;
if ($fred_attack > $barney->{'dexterity'}){
$damage = roll(1, 6, $fred->{'dam_bonus'});
print "fred rolls $fred_attack, hits for $damage damage!\n
+";
$barney->{'endurance'} = $barney->{'endurance'} - $damage
+;
}else{
print "fred rolls $fred_attack, misses\n";
}
if ($barney_attack > $fred->{'dexterity'}){
$damage = roll(1, 6, $barney->{'dam_bonus'});
print "barney rolls $barney_attack, hits for $damage damag
+e!\n";
$fred->{'endurance'} = $fred->{'endurance'} - $damage;
}else{
print "barney rolls $barney_attack, misses\n";
}
}
In reply to battle.pl
by dshahin
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.