DEV Community

loading...

Discussion on: Daily Challenge #43 - Boardgame Fight Resolver

Collapse
choroba profile image
E. Choroba

Perl solution, using a hash of known fight results.

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

{   my %stronger = (
        archers   => 'swordsmen',
        swordsmen => 'pikemen',
        pikemen   => 'cavalry',
        cavalry   => 'archers');
    sub fight_resolve {
        my ($attacker, $defender) = @_;
        ($stronger{$defender} // "") eq $attacker ? $defender : $attacker
    }
}

use Test::More tests => 16;

is fight_resolve('archers', 'archers'    ), 'archers';
is fight_resolve('archers', 'swordsmen'  ), 'archers';
is fight_resolve('archers', 'pikemen'    ), 'archers';
is fight_resolve('archers', 'cavalry'    ), 'cavalry';
is fight_resolve('swordsmen', 'archers'  ), 'archers';
is fight_resolve('swordsmen', 'swordsmen'), 'swordsmen';
is fight_resolve('swordsmen', 'pikemen'  ), 'swordsmen';
is fight_resolve('swordsmen', 'cavalry'  ), 'swordsmen';
is fight_resolve('pikemen', 'archers'    ), 'pikemen';
is fight_resolve('pikemen', 'swordsmen'  ), 'swordsmen';
is fight_resolve('pikemen', 'pikemen'    ), 'pikemen';
is fight_resolve('pikemen', 'cavalry'    ), 'pikemen';
is fight_resolve('cavalry', 'archers'    ), 'cavalry';
is fight_resolve('cavalry', 'swordsmen'  ), 'cavalry';
is fight_resolve('cavalry', 'pikemen'    ), 'pikemen';
is fight_resolve('cavalry', 'cavalry'    ), 'cavalry';