blib/lib/Game/Floppy.pm

filter: Statementstmt 100% 158 / 158 Branchbran 97.7% 43 / 44 Conditioncond 86.6% 13 / 15 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 27 / 27 Podpod 92.8% 13 / 14 totaltotal 97.7% 262 / 268 CC 31 SCAR 17.9
CC
31
Cov
99%
tick
12.0
BUILD
11.1
circle_hits_rectangle
6.0
CRAP 6.0
« blib/lib/Game/Floppy/Terminal.pm ↑ Summary blib/lib/Game/Floppy/Result.pm »
1 package Game::Floppy;
2  
3 16 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 ▶use Game::Floppy::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Game::Floppy::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 16 our $VERSION = '0.10';
15  
16 has width => (
17 is => 'ro',
18 isa => Int,
19 default => 516
20 16 );
21  
22 has height => (
23 is => 'ro',
24 isa => Int,
25 default => 400
26 16 );
27  
28 has seed => (
29 is => 'ro',
30 isa => Str,
31 default => 'Game::Floppy'
32 16 );
33  
34 has bird_x => (
35 is => 'ro',
36 isa => Int,
37 default => 80
38 16 );
39  
40 has bird_radius => (
41 is => 'ro',
42 isa => Int,
43 default => 24
44 16 );
45  
46 has gravity => (
47 is => 'ro',
48 isa => Int,
49 default => 3
50 16 );
51  
52 has lift => (
53 is => 'ro',
54 isa => Int,
55 default => 30
56 16 );
57  
58 has tube_every => (
59 is => 'ro',
60 isa => Int,
61 default => 100
62 16 );
63  
64 has tube_speed => (
65 is => 'ro',
66 isa => Int,
67 default => 3
68 16 );
69  
70 has tube_width => (
71 is => 'ro',
72 isa => Int,
73 default => 52
74 16 );
75  
76 has gap => (
77 is => 'ro',
78 isa => Int,
79 default => 140
80 16 );
81  
82 has tube_score => (
83 is => 'ro',
84 isa => Int,
85 default => 100
86 16 );
87  
88 has _tubes => (
89 is => 'rw',
90 isa => ArrayRef,
91 239 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
92 private => 1
93 16 );
94  
95 16 has _y => (is => 'rw', private => 1);
96 16 has _cause => (is => 'rw', private => 1);
97 16 has _error => (is => 'rw', private => 1);
98  
99 has _flap => (
100 is => 'rw',
101 isa => Int,
102 default => 0,
103 private => 1
104 16 );
105  
106 has _tilt => (
107 is => 'rw',
108 isa => Int,
109 default => 0,
110 private => 1
111 16 );
112  
113 has _spawned => (
114 is => 'rw',
115 isa => Int,
116 default => 0,
117 private => 1
118 16 );
119  
120 has _score => (
121 is => 'rw',
122 isa => Int,
123 default => 0,
124 private => 1
125 16 );
126  
127 has _ticks => (
128 is => 'rw',
129 isa => Int,
130 default => 0,
131 private => 1
132 16 );
133  
134 has _status => (
135 is => 'rw',
136 isa => Str,
137 default => 'ready',
138 private => 1
139 16 );
140  
141 2462 ▶my $spawn = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 2462 my ($self) = @_;
143 2462 my $k = $self->_spawned;
144 2462 my $hash = Digest::SHA::sha256($self->seed . "floppy:tube:$k");
145 2462 my $range = $self->height - 100;
146 2462 my $h = unpack('N', substr($hash, 0, 4)) % $range;
147 2462 my $top = $range - $h;
148 2462 push @{ $self->_tubes }, {
149 x => $self->width,
150 gap_top => $top,
151 gap_bottom => $top + $self->gap,
152 k => $k,
153 };
154 2462 $self->_spawned($k + 1);
155 2462 return;
156 16 };
157  
158 237 ▶my $lay = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 237 my ($self) = @_;
160 237 $self->_y(int($self->height / 2) - $self->bird_radius);
161 237 $self->_tubes([]);
162 237 $self->_flap(0);
163 237 $self->_tilt(0);
164 237 $self->_spawned(0);
165 237 $self->_score(0);
166 237 $self->_ticks(0);
167 237 $self->_status('ready');
168 237 $self->_cause(undef);
169 237 $self->_error(undef);
170 237 return $self;
171 16 };
172  
173 4 ▶my $refuse = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
174 4 my ($self, $code) = @_;
175 4 $self->_error(Game::Floppy::Error->of($code));
176 4 return 0;
177 16 };
178  
179 79 ▶my $finish = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
180 79 my ($self, $cause, $flapped) = @_;
181 79 $self->_status('finished');
182 79 $self->_cause($cause);
183 79 $self->_flap(0);
184 79 return { moved => 1, flapped => $flapped, passed => 0, died => $cause };
185 16 };
186  
187 239 ▶sub BUILD {
Subroutine BUILD: called CC=11 SCAR=24.1
Pod undocumented
188 239 my ($self) = @_;
189  
190 239 for my $name (qw/ width height bird_x bird_radius gravity lift tube_every tube_speed tube_width gap tube_score /) {
191 2619 my $value = $self->$name;
192 2619 ▶ Carp::croak("Game::Floppy: $name must be a whole number, not "
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value and $value =~ /\A[0-9]+\z/u 1 2618 2619
defined $value ? : 1 0 1
Condition66%2/3
defined $value and $value =~ /\A[0-9]+\z/u
!ll&&!rl&&r
012618
MC/DC50%1/2
defined $value and $value =~ /\A[0-9]+\z/u
defined $value $value =~ /\A[0-9]+\z/u
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
193 . (defined $value ? "'$value'" : 'undef'))
194 unless defined $value && $value =~ /\A[0-9]+\z/;
195 }
196  
197 238 ▶ Carp::croak('Game::Floppy: a field is at least 200 high') if $self->height < 200;
Branch: 1 branch
BranchTrueFalseTotal
if $self->height < 200 1 237 238
198 237 ▶ Carp::croak('Game::Floppy: a field ' . $self->width . ' wide has no room for a tube')
Branch: 1 branch
BranchTrueFalseTotal
if $self->width < $self->bird_x + $self->tube_width + 1 1 236 237
199 if $self->width < $self->bird_x + $self->tube_width + 1;
200 236 for my $name (qw/ bird_radius gravity lift tube_every tube_speed tube_width gap tube_score /) {
201 1881 ▶ Carp::croak("Game::Floppy: $name is 1 or more") if $self->$name < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$name < 1 2 1879 1881
202 }
203 234 ▶ Carp::croak('Game::Floppy: a seed is a string with something in it')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->seed and length $self->seed 1 233 234
Condition66%2/3
defined $self->seed and length $self->seed
!ll&&!rl&&r
01233
MC/DC50%1/2
defined $self->seed and length $self->seed
defined $self->seed length $self->seed
Truth table66%2/3
A and B
A= defined $self->seedB= length $self->seed
ABresult
0X0
100
111
204 unless defined $self->seed && length $self->seed;
205  
206 233 $self->$lay;
207 233 return;
208 }
209  
210 736976 ▶sub circle_hits_rectangle {
Subroutine circle_hits_rectangle: called CC=6 SCAR=17.9
Pod documented
211 736976 my ($class, $cx, $cy, $r, $x, $y, $w, $h) = @_;
212 736976 my ($hw, $hh) = ($w / 2, $h / 2);
213 736976 my $dx = abs($cx - ($x + $hw));
214 736976 my $dy = abs($cy - ($y + $hh));
215 736976 ▶ return 0 if $dx > $hw + $r || $dy > $hh + $r;
Branch: 1 branch
BranchTrueFalseTotal
if $dx > $hw + $r or $dy > $hh + $r 736857 119 736976
Condition100%3/3
$dx > $hw + $r or $dy > $hh + $r
l!l&&r!l&&!r
624165112692119
MC/DC100%2/2
$dx > $hw + $r or $dy > $hh + $r
$dx > $hw + $r $dy > $hh + $r
Truth table100%3/3
A or B
A= $dx > $hw + $rB= $dy > $hh + $r
ABresult
1X1
011
000
216 119 ▶ return 1 if $dx <= $hw || $dy <= $hh;
Branch: 1 branch
BranchTrueFalseTotal
if $dx <= $hw or $dy <= $hh 26 93 119
Condition100%3/3
$dx <= $hw or $dy <= $hh
l!l&&r!l&&!r
12593
MC/DC100%2/2
$dx <= $hw or $dy <= $hh
$dx <= $hw $dy <= $hh
Truth table100%3/3
A or B
A= $dx <= $hwB= $dy <= $hh
ABresult
1X1
011
000
217 93 my ($ex, $ey) = ($dx - $hw, $dy - $hh);
218 93 ▶ return $ex * $ex + $ey * $ey <= $r * $r ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ex * $ex + $ey * $ey <= $r * $r ? : 22 71 93
219 }
220  
221 24606 ▶sub flap {
Subroutine flap: called CC=3 SCAR=11.0
Pod documented
222 24606 my ($self) = @_;
223 24606 ▶ return $self->$refuse('not_active') if $self->_status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_status eq "finished" 1 24605 24606
224 24605 ▶ return $self->$refuse('already_flapped') if $self->_flap;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_flap 3 24602 24605
225 24602 $self->_flap(1);
226 24602 $self->_error(undef);
227 24602 return 1;
228 }
229  
230 248807 ▶sub tick {
Subroutine tick: called CC=12 SCAR=24.8
Pod documented
231 248807 my ($self) = @_;
232  
233 248807 ▶ return { moved => 0 } if $self->_status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_status eq "finished" 2 248805 248807
234  
235 248805 $self->_status('active');
236 248805 $self->_ticks($self->_ticks + 1);
237  
238 248805 my $y = $self->_y + $self->gravity;
239 248805 my $flapped = 0;
240 248805 ▶ if ($self->_flap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_flap) 24597 224208 248805
241 24597 $y -= $self->lift;
242 24597 $self->_flap(0);
243 24597 $self->_tilt(-45);
244 24597 $flapped = 1;
245 }
246 248805 ▶ $self->_tilt($self->_tilt + 1) if $self->_tilt < 45;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_tilt < 45 248462 343 248805
247 248805 $self->_y($y);
248  
249 248805 ▶ return $self->$finish('sky', $flapped) if $y < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $y < 0 3 248802 248805
250 248802 ▶ return $self->$finish('ground', $flapped) if $y > $self->height;
Branch: 1 branch
BranchTrueFalseTotal
if $y > $self->height 6 248796 248802
251  
252 248796 my $tubes = $self->_tubes;
253 248796 my ($cx, $r, $w) = ($self->bird_x, $self->bird_radius, $self->tube_width);
254 248796 my $passed = 0;
255 248796 my @kept;
256 248796 for my $tube (@$tubes) {
257 368505 $tube->{x} -= $self->tube_speed;
258 return $self->$finish('tube', $flapped)
259 if $self->circle_hits_rectangle($cx, $y, $r, $tube->{x}, 0, $w, $tube->{gap_top})
260 368505 ▶ || $self->circle_hits_rectangle($cx, $y, $r, $tube->{x}, $tube->{gap_bottom}, $w, $self->height);
Branch: 1 branch
BranchTrueFalseTotal
if $self->circle_hits_rectangle($cx, $y, $r, $tube->{"x"}, 0, $w, $tube->{"gap_top"}) or $self->circle_hits_rectangle($cx, $y, $r, $tube->{"x"}, $tube->{"gap_bottom"}, $w, $self->height) 44 368461 368505
Condition100%3/3
$self->circle_hits_rectangle($cx, $y, $r, $tube->{"x"}, 0, $w, $tube->{"gap_top"}) or $self->circle_hits_rectangle($cx, $y, $r, $tube->{"x"}, $tube->{"gap_bottom"}, $w, $self->height)
l!l&&r!l&&!r
422368461
MC/DC100%2/2
$self->circle_hits_rectangle($cx, $y, $r, $tube->{"x"}, 0, $w, $tube->{"gap_top"}) or $self->circle_hits_rectangle($cx, $y, $r, $tube->{"x"}, $tube->{"gap_bottom"}, $w, $self->height)
$self->circle_hits_rectangle($cx, $y, $r, $tube->{"x"}, 0, $w, $tube->{"gap_top"}) $self->circle_hits_rectangle($cx, $y, $r, $tube->{"x"}, $tube->{"gap_bottom"}, $w, $self->height)
Truth table100%3/3
A or B
A= $self->circle_hits_rectangle($cx, $y, $r, $tube->{"x"}, 0, $w, $tube->{"gap_top"})B= $self->circle_hits_rectangle($cx, $y, $r, $tube->{"x"}, $tube->{"gap_bottom"}, $w, $self->height)
ABresult
1X1
011
000
261 368461 ▶ if ($tube->{x} + $w < $cx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tube->{"x"} + $w < $cx) 2168 366293 368461
262 2168 $self->_score($self->_score + $self->tube_score);
263 2168 $passed = 1;
264 2168 next;
265 }
266 366293 push @kept, $tube;
267 }
268 248752 ▶ $self->_tubes(\@kept) if $passed;
Branch: 1 branch
BranchTrueFalseTotal
if $passed 2168 246584 248752
269  
270 248752 ▶ $self->$spawn if $self->_ticks % $self->tube_every == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ticks % $self->tube_every == 0 2462 246290 248752
271  
272 248752 return { moved => 1, flapped => $flapped, passed => $passed, died => undef };
273 }
274  
275 27 ▶sub quit {
Subroutine quit: called CC=2 SCAR=6.9
Pod documented
276 27 my ($self) = @_;
277 27 ▶ return 0 if $self->_status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_status eq "finished" 1 26 27
278 26 $self->$finish('quit', 0);
279 26 return 1;
280 }
281  
282 4 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
283 4 my ($self) = @_;
284 4 return $self->$lay;
285 }
286  
287 248834 ▶sub bird {
Subroutine bird: called CC=1 SCAR=0.0
Pod documented
288 248834 my ($self) = @_;
289 248834 return { x => $self->bird_x, y => $self->_y, radius => $self->bird_radius, tilt => $self->_tilt };
290 }
291  
292 243616 ▶sub tubes {
Subroutine tubes: called CC=1 SCAR=0.0
Pod documented
293 243616 my ($self) = @_;
294 243616 return [ map { +{ %$_ } } @{ $self->_tubes } ];
295 }
296  
297 55 ▶sub passed { my ($self) = @_; return int($self->_score / $self->tube_score) }
Subroutine passed: called CC=1 SCAR=0.0
Pod documented
298 7504 ▶sub score { return $_[0]->_score }
Subroutine score: called CC=1 SCAR=0.0
Pod documented
299 244284 ▶sub ticks { return $_[0]->_ticks }
Subroutine ticks: called CC=1 SCAR=0.0
Pod documented
300 478730 ▶sub status { return $_[0]->_status }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
301 6 ▶sub error { return $_[0]->_error }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
302  
303 54 ▶sub result {
Subroutine result: called CC=2 SCAR=6.9
Pod documented
304 54 my ($self) = @_;
305 54 ▶ return undef unless $self->_status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_status eq "finished" 3 51 54
306 51 return Game::Floppy::Result->new(
307 cause => $self->_cause,
308 score => $self->_score,
309 tubes => $self->passed,
310 ticks => $self->_ticks,
311 );
312 }
313  
314 16 1;
315  
316 __END__
« blib/lib/Game/Floppy/Terminal.pm ↑ Summary blib/lib/Game/Floppy/Result.pm »