| 1 | package Game::Snake::Result; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 15 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 15 | our @CAUSES = qw/ wall body full quit /; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 15 | my %IS_CAUSE = map { $_ => 1 } @CAUSES; | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 15 | my %SENTENCE = ( | |||||||||||||||||||||||||||||||
| 17 | wall => 'hit the wall', | ||||||||||||||||||||||||||||||||
| 18 | body => 'ran into yourself', | ||||||||||||||||||||||||||||||||
| 19 | full => 'filled the board', | ||||||||||||||||||||||||||||||||
| 20 | quit => 'quit', | ||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | has cause => ( | ||||||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 25 | isa => Str | ||||||||||||||||||||||||||||||||
| 26 | 15 | ); | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | has score => ( | ||||||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 30 | isa => Int, | ||||||||||||||||||||||||||||||||
| 31 | default => 0 | ||||||||||||||||||||||||||||||||
| 32 | 15 | ); | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | has length => ( | ||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 36 | isa => Int, | ||||||||||||||||||||||||||||||||
| 37 | default => 0 | ||||||||||||||||||||||||||||||||
| 38 | 15 | ); | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | has ticks => ( | ||||||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 42 | isa => Int, | ||||||||||||||||||||||||||||||||
| 43 | default => 0 | ||||||||||||||||||||||||||||||||
| 44 | 15 | ); | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | has speed => ( | ||||||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 48 | isa => Int, | ||||||||||||||||||||||||||||||||
| 49 | default => 0 | ||||||||||||||||||||||||||||||||
| 50 | 15 | ); | |||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 51 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 53 | 51 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 51 | my $cause = $self->cause; | |||||||||||||||||||||||||||||||
| 56 | Carp::croak("Game::Snake::Result: '" . (defined $cause ? $cause : 'undef') | ||||||||||||||||||||||||||||||||
| 57 | . "' is not a cause") | ||||||||||||||||||||||||||||||||
| 58 | 51 | ▶ | unless defined $cause && $IS_CAUSE{$cause}; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $cause and $IS_CAUSE{$cause}
MC/DC100%2/2
defined $cause and $IS_CAUSE{$cause}
defined $cause
$IS_CAUSE{$cause}
Truth table100%3/3
A and B
A= defined $causeB= $IS_CAUSE{$cause}
| |||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 49 | for my $name (qw/ score length ticks speed /) { | |||||||||||||||||||||||||||||||
| 61 | 193 | my $value = $self->$name; | |||||||||||||||||||||||||||||||
| 62 | 193 | ▶ | Carp::croak("Game::Snake::Result: $name must be a whole number, not " | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $value and $value =~ /\A[0-9]+\z/u
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
| |||||||||||||||||||||||||||||||||
| 63 | . (defined $value ? "'$value'" : 'undef')) | ||||||||||||||||||||||||||||||||
| 64 | unless defined $value && $value =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 48 | return; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | sub causes { return [@CAUSES] } | ||||||||||||||||||||||||||||||
|
Subroutine
causes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 37 | ▶ | sub died { | ||||||||||||||||||||||||||||||
|
Subroutine
died: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 73 | 37 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 74 | 37 | ▶ | return $self->cause eq 'quit' ? 0 : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 34 | ▶ | sub stringify { | ||||||||||||||||||||||||||||||
|
Subroutine
stringify: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 78 | 34 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 79 | 34 | my $tail = sprintf '%s at %d points, %d long', $SENTENCE{ $self->cause }, | |||||||||||||||||||||||||||||||
| 80 | $self->score, $self->length; | ||||||||||||||||||||||||||||||||
| 81 | 34 | ▶ | return $self->died ? "Game over: $tail" : ucfirst $tail; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 15 | 1; | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||||||