Battleship/Player.pm

filter: Statementstmt 91.6% 66 / 72 Branchbran 75.0% 18 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 75.0% 3 / 4 totaltotal 85.3% 99 / 116 CC 16 SCAR 16.9
CC
16
Cov
86%
strike
10.6
craft
4.1
matrix
2.2
CRAP 5.4
« Battleship.pm ↑ Summary Battleship/Craft.pm »
1 package Game::Battleship::Player;
2 3 $Game::Battleship::Player::VERSION = '0.0602';
3 3 our $AUTHORITY = 'cpan:GENE';
4  
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Game::Battleship::Craft;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Game::Battleship::Grid;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Types::Standard qw( ArrayRef Int Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 has id => (
12 is => 'ro',
13 isa => Str,
14 );
15  
16 3 has name => (
17 is => 'ro',
18 isa => Str,
19 );
20  
21 3 has life => (
22 is => 'ro',
23 isa => Int,
24 );
25  
26 has fleet => (
27 is => 'ro',
28 isa => ArrayRef,
29 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 [
31 3 Game::Battleship::Craft->new(
32 name => 'aircraft carrier',
33 points => 5,
34 ),
35 Game::Battleship::Craft->new(
36 name => 'battleship',
37 points => 4,
38 ),
39 Game::Battleship::Craft->new(
40 name => 'cruiser',
41 points => 3,
42 ),
43 Game::Battleship::Craft->new(
44 name => 'submarine',
45 points => 3,
46 ),
47 Game::Battleship::Craft->new(
48 name => 'destroyer',
49 points => 2,
50 ),
51 ]
52 },
53 3 );
54  
55 3 has dimensions => (
56 is => 'ro',
57 isa => ArrayRef[Int],
58 );
59  
60 3 has grid => (
61 is => 'ro',
62 isa => ArrayRef,
63 );
64  
65 3 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
66 3 my $self = shift;
67  
68 $self->{grid} =
69 3 Game::Battleship::Grid->new(
70 fleet => $self->fleet,
71 dimensions => $self->dimensions,
72 );
73  
74 # Compute the life points for this player.
75 3 $self->{life} += $_->points for @{ $self->fleet };
76 }
77  
78 # The enemy must be a G::B::Player object.
79 4 ▶sub matrix {
Subroutine matrix: called CC=2 SCAR=7.9
Pod documented
80 4 my ($self, $enemy) = @_;
81 return $enemy
82 ? join "\n",
83 0 map { "@$_" } @{ $self->{$enemy->name}{matrix} }
84 : join "\n",
85 4 ▶ map { "@$_" } @{ $self->{grid}{matrix} };
Branch: 1 branch
BranchTrueFalseTotal
$enemy ? : 0 4 4
86 }
87  
88 # The enemy must be a G::B::Player object.
89 101 ▶sub strike {
Subroutine strike: called CC=10 SCAR=23.6
Pod documented
90 101 my ($self, $enemy, $x, $y) = @_;
91  
92 101 ▶ croak "No opponent to strike.\n" unless $enemy;
Branch: 1 branch
BranchTrueFalseTotal
unless $enemy 0 101 101
93 101 ▶ croak "No coordinate at which to strike.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x and defined $y 0 101 101
Condition33%1/3
defined $x and defined $y
!ll&&!rl&&r
00101
MC/DC0%0/2
defined $x and defined $y
defined $x defined $y
Truth table33%1/3
A and B
A= defined $xB= defined $y
ABresult
0X0
100
111
94 unless defined $x && defined $y;
95  
96 101 ▶ if ($enemy->life > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enemy->life > 0) { } 101 0 101
97 # Initialize the enemy grid map if we need to.
98 $self->{$enemy->name} = Game::Battleship::Grid->new
99 101 ▶ unless exists $self->{$enemy->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$self{$enemy->name} 2 99 101
100  
101 101 my $enemy_pos = \$enemy->{grid}{matrix}[$x][$y];
102 101 my $map_pos = \$self->{$enemy->name}{matrix}[$x][$y];
103  
104 101 ▶ if ($$map_pos ne '.') {
Branch: 2 branches
BranchTrueFalseTotal
if ($$map_pos ne '.') { } 1 100 101
elsif ($enemy->_is_a_hit($x, $y)) { } 18 82 100
105 1 warn "Duplicate strike on ", $enemy->name, ' by ', $self->name, " at $x, $y.\n";
106 1 return -1;
107 }
108 elsif ($enemy->_is_a_hit($x, $y)) { # Set the enemy grid map coordinate char to 'hit'.
109 18 $$map_pos = 'x';
110  
111 # What craft was hit?
112 18 my $craft = $self->craft(id => $$enemy_pos);
113  
114 18 warn $self->name, ' hit ', $enemy->name, "'s ", $craft->name, "!\n";
115  
116 # How much is left on this craft?
117 18 my $remainder = $craft->hit;
118  
119 # Tally the hit in the craft object, itself and emit a happy
120 # warning if it was totally destroyed.
121 18 ▶ warn $self->name, ' sunk ', $enemy->name, "'s ", $craft->name, "!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $remainder 2 16 18
122 unless $remainder;
123  
124 # Indicate the hit on the enemy grid by lowercasing the craft
125 # id.
126 18 $$enemy_pos = lc $$enemy_pos;
127  
128 # Increment the player's score.
129 18 $self->{score}++;
130  
131 # Decrement the opponent's life.
132 warn $enemy->name, " is out of the game.\n"
133 18 ▶ if --$enemy->{life} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$enemy->{"life"} <= 0 0 18 18
134  
135 18 return 1;
136 }
137 else {
138 # Set the enemy grid map coordinate char to 'miss'.
139 82 warn $self->name, ' missed ', $enemy->name, " at $x, $y.\n";
140 82 $$map_pos = 'o';
141 82 return 0;
142 }
143 }
144 else {
145 0 warn $enemy->name, " is already out of the game. Strike another opponent.\n";
146 0 return -1;
147 }
148 }
149  
150 100 ▶sub _is_a_hit {
Subroutine _is_a_hit: called CC=2 SCAR=6.9
151 100 my ($self, $x, $y) = @_;
152 100 ▶ return $self->{grid}{matrix}[$x][$y] ne '.'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'grid'}{'matrix'}[$x][$y] ne '.' ? : 18 82 100
153 ? 1 : 0;
154 }
155  
156 20 ▶sub craft {
Subroutine craft: called CC=4 SCAR=14.2
Pod documented
157 20 my ($self, $key, $val) = @_;
158  
159 # If the key is not defined, assume it's supposed to be the id.
160 20 ▶ unless (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 20 20
161 0 $val = $key;
162 0 $key = 'id';
163 }
164  
165 20 my $craft;
166  
167 20 for (@{ $self->{fleet} }) {
168 44 ▶ if ($val eq $_->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq $_->{$key}) 20 24 44
169 20 $craft = $_;
170 20 last;
171 }
172 }
173  
174 20 return $craft;
175 }
176  
177 3 1;
178  
179 __END__
« Battleship.pm ↑ Summary Battleship/Craft.pm »