DEV Community

Discussion on: Daily Challenge #28 - Kill the Monster!

Collapse
 
choroba profile image
E. Choroba

I'm not sure I understand the assignment correctly. The damage I give to the monster is always 1, right? So if the monster's health is 3, it gets killed before it can attack back. It needs at least 4 hit points to be able to bite.

#!/usr/bin/perl
use warnings;
use strict;

use Function::Parameters;

fun kill_monsters($h, $m, $dm) {
    my $hits   = int(($m - 1) / 3);
    my $damage = $dm * $hits;
    $h -= $damage;
    return $h > 0
        ? "hits: $hits, damage: $damage, health: $h"
        : 'hero died'
}

use Test::More tests => 3;

is kill_monsters(100, 3, 33), 'hits: 0, damage: 0, health: 100';
is kill_monsters( 50, 7, 10), 'hits: 2, damage: 20, health: 30';
is kill_monsters(  2, 7,  1), 'hero died';