Cellulo/Game/Particle.pm

filter: Statementstmt 35.6% 36 / 101 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 24.0% 6 / 25 Podpod 0.0% 0 / 8 totaltotal 25.0% 42 / 168 SCAR 51.8
CC
20
Cov
27%
_build_char
30.0
_build_xdir
20.0
_build_ydir
20.0
CRAP 177.7
↑ Summary Cellulo/Game.pm »
1 package Games::Cellulo::Game::Particle;
2 3 $Games::Cellulo::Game::Particle::VERSION = '0.2_01';
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use List::Util qw[ min ];
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use constant {
11 3 R => 1,
12 B => 2,
13 G => 3,
14 Y => 4,
15 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 has x => ( is => 'rw', required => 1 );
17 3 has y => ( is => 'rw', required => 1 );
18 3 has type => ( is => 'rw', required => 1 );
19  
20 3 has xdir => ( is => 'lazy', clearer => 1 );
21 3 has ydir => ( is => 'lazy', clearer => 1 );
22 3 has char => ( is => 'lazy', clearer => 1 );
23  
24 sub _build_xdir {
25 0 ▶ for( $_[0]->type ) {
Subroutine _build_xdir: not called CC=4 SCAR=30.0
26 0 ▶ return -1 if $_ eq R;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq 1 0 0 0
27 0 ▶ return 1 if $_ eq B;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq 2 0 0 0
28 0 return 0;
29 }
30 }
31  
32 sub _build_ydir {
33 0 ▶ for ( $_[0]->type ) {
Subroutine _build_ydir: not called CC=4 SCAR=30.0
34 0 ▶ return -1 if $_ eq G;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq 3 0 0 0
35 0 ▶ return 1 if $_ eq Y;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq 4 0 0 0
36 0 return 0;
37 }
38 }
39  
40 3 has dir => (
41 is => 'lazy',
42 );
43  
44 sub _build_dir {
45 0 ▶ return [ $_[0]->xdir,$_->[0]->ydir ];
Subroutine _build_dir: not called CC=1 SCAR=6.9
46 }
47 sub _cc {
48 0 ▶ my($color,$str) = @_;
Subroutine _cc: not called CC=1 SCAR=6.9
49 0 colored($str,$color);
50 }
51  
52 sub _build_char {
53 0 ▶ my $self = shift;
Subroutine _build_char: not called CC=5 SCAR=34.0
54 0 ▶ return _cc( 'blue', 'o' ) if $self->type eq B;
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq 2 0 0 0
55 0 ▶ return _cc( 'red', 'o' ) if $self->type eq R;
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq 1 0 0 0
56 0 ▶ return _cc( 'green', 'o' ) if $self->type eq G;
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq 3 0 0 0
57 0 ▶ return _cc( 'yellow', 'o' ) if $self->type eq Y;
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq 4 0 0 0
58 }
59  
60  
61  
62  
63 sub move {
64 0 ▶ my $self = shift;
Subroutine move: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my $wantx = $self->xpos( $self->x + $self->xdir );
66 0 my $wanty = $self->ypos( $self->y + $self->ydir );
67 0 $self->x( $wantx );
68 0 $self->y( $wanty );
69 }
70  
71 3 my @possible_directions = (
72 "-1,-1", "-1,0", "-1,1",
73 "0,-1", "0,1", #skip 0,0, its a noop
74 "1,-1", "1,0", "1,1",
75 );
76  
77 3 my @possible_direction_refs = map { [ split ',', $_ ] } @possible_directions;
78 has tries_in_direction => (
79 is => 'ro',
80 default => sub { +{
81 0 ▶ map { $_ => 0 } @possible_directions
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 };
83 }
84 3 );
85  
86 has successes_in_direction => (
87 is => 'ro',
88 default => sub { +{
89 0 ▶ map { $_ => 0 } @possible_directions
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 };
91 }
92 3 );
93  
94 has num_avoid_tries => (
95 is => 'rw',
96 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 3 );
98  
99 has num_successes => (
100 is => 'rw',
101 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 3 );
103  
104 sub p_found_free_path {
105 0 ▶ my $self = shift;
Subroutine p_found_free_path: not called CC=2 SCAR=17.9
Pod undocumented
106 0 my $num_tries = $self->num_avoid_tries;
107 0 my $num_successes = $self->num_successes;
108 0 ▶ return .5 unless $num_successes;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_successes 0 0 0
109 0 return $num_tries / $num_successes;
110 }
111  
112 sub p_went_in_direction {
113 0 ▶ my( $self, $direction ) = @_;
Subroutine p_went_in_direction: not called CC=2 SCAR=17.9
Pod undocumented
114 0 my $num_tries_in_direction = $self->tries_in_direction->{$direction};
115 0 my $num_tries = $self->num_avoid_tries;
116 0 ▶ return .5 unless $num_tries;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_tries 0 0 0
117 0 return $num_tries_in_direction / $num_tries;
118 }
119  
120 sub p_found_free_path_went_in_direction_x {
121 0 ▶ my( $self, $direction ) = @_;
Subroutine p_found_free_path_went_in_direction_x: not called CC=2 SCAR=17.9
Pod undocumented
122 0 my $num_avoid_successes = $self->num_successes;
123 0 my $num_avoid_successes_in_direction = $self->successes_in_direction->{$direction};
124 0 ▶ return .5 unless $num_avoid_successes;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_avoid_successes 0 0 0
125 0 return $num_avoid_successes_in_direction / $num_avoid_successes;
126 }
127 sub p_went_in_direction_x_found_free_path {
128 0 ▶ my( $self, $direction ) = @_;
Subroutine p_went_in_direction_x_found_free_path: not called CC=4 SCAR=30.0
Pod undocumented
129 0 my $p_found_free_path = $self->p_found_free_path;
130 0 my $p_went_in_direction = $self->p_went_in_direction( $direction );
131 0 my $p_found_free_path_went_in_direction_x = $self->p_found_free_path_went_in_direction_x( $direction );
132 0 ▶ return .5 unless $p_found_free_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $p_found_free_path 0 0 0
133 0 ▶ return .5 unless $p_found_free_path_went_in_direction_x;
Branch: 1 branch
BranchTrueFalseTotal
unless $p_found_free_path_went_in_direction_x 0 0 0
134 0 ▶ return .5 unless $p_went_in_direction;
Branch: 1 branch
BranchTrueFalseTotal
unless $p_went_in_direction 0 0 0
135 0 return ( $p_found_free_path_went_in_direction_x * $p_went_in_direction ) / $p_found_free_path;
136  
137 }
138 sub avoidx {
139 0 ▶ my $self = shift;
Subroutine avoidx: not called CC=1 SCAR=6.9
Pod undocumented
140 # return 0 if $self->xdir;
141 0 int( rand(3) ) - 1;
142 }
143  
144 sub avoidy {
145 0 ▶ my $self = shift;
Subroutine avoidy: not called CC=1 SCAR=6.9
Pod undocumented
146 # return 0 if $self->ydir;
147 0 int( rand(3) ) - 1;
148 }
149  
150 3 has initial_avoidx => ( is => 'lazy' );
151 3 has initial_avoidy => ( is => 'lazy' );
152  
153 0 ▶sub _build_initial_avoidx { shift->avoidy }
Subroutine _build_initial_avoidx: not called CC=1 SCAR=6.9
154 0 ▶sub _build_initial_avoidy { shift->avoidx }
Subroutine _build_initial_avoidy: not called CC=1 SCAR=6.9
155  
156 sub avoid_dir {
157 0 ▶ my $self = shift;
Subroutine avoid_dir: not called CC=4 SCAR=30.0
Pod undocumented
158 # return [ $_->avoidx, $_->avoidy ];
159 0 my @p = map { $self->p_went_in_direction_x_found_free_path( $_ ) } @possible_directions;
160 0 my $min = min @p;
161 0 my @possibles;
162 0 ▶ for( my $i = 0; $i < @p; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @p) 0 0 0
163 0 ▶ push @possibles, $possible_direction_refs[$i] if $p[$i] == $min;
Branch: 1 branch
BranchTrueFalseTotal
if $p[$i] == $min 0 0 0
164 }
165 0 ▶ return $possibles[ int( rand( @possibles ) ) ] if @possibles;
Branch: 1 branch
BranchTrueFalseTotal
if @possibles 0 0 0
166 0 return;
167 }
168  
169 3 1;
170 __END__
↑ Summary Cellulo/Game.pm »