2048/Game.pm

filter: Statementstmt 75.7% 72 / 95 Branchbran 60.0% 18 / 30 Conditioncond 80.0% 12 / 15 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 73.3% 11 / 15 Podpod 0.0% 0 / 9 totaltotal 68.7% 119 / 173 SCAR 36.8
CC
26
Cov
73%
move
12.0
_game_file
12.0
move_tiles
11.2
CRAP 39.5
« 2048.pm ↑ Summary 2048/Input.pm »
1 package Games::2048::Game;
2 4 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # increment this whenever we break compat with older game objects
6 4 our $VERSION = '0.02';
7  
8 4 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 extends 'Games::2048::Board';
13  
14 4 has won => is => 'rw', default => 0;
15 4 has version => is => 'rw', default => __PACKAGE__->VERSION;
16  
17 sub insert_start_tiles {
18 2 ▶ my ($self, $start_tiles) = @_;
Subroutine insert_start_tiles: called CC=2 SCAR=6.9
Pod undocumented
19 2 $self->insert_random_tile for 1..$start_tiles;
20 }
21  
22 sub insert_random_tile {
23 30 ▶ my $self = shift;
Subroutine insert_random_tile: called CC=3 SCAR=11.0
Pod undocumented
24 30 my @available_cells = $self->available_cells;
25 30 ▶ return if !@available_cells;
Branch: 1 branch
BranchTrueFalseTotal
unless @available_cells 10 20 30
26 20 my $cell = $available_cells[rand @available_cells];
27 20 ▶ my $value = rand() < 0.9 ? 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
rand() < 0.9 ? : 20 0 20
28 20 $self->insert_tile($cell, $value);
29 }
30  
31 sub move_tiles {
32 38 ▶ my ($self, $vec) = @_;
Subroutine move_tiles: called CC=11 SCAR=24.1
Pod undocumented
33 38 my $moved;
34  
35 38 ▶ my $reverse = $vec->[0] > 0 || $vec->[1] > 0;
Condition100%3/3
$vec->[0] > 0 || $vec->[1] > 0
l!l&&r!l&&!r
13619
MC/DC100%2/2
$vec->[0] > 0 || $vec->[1] > 0
$vec->[0] > 0 $vec->[1] > 0
Truth table100%3/3
A || B
A= $vec->[0] > 0B= $vec->[1] > 0
ABresult
1X1
011
000
36  
37 38 for my $cell (sort { $reverse } $self->tile_cells) {
38 400 my $tile = $self->tile($cell);
39 400 my $next = $cell;
40 400 my $farthest;
41 400 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$self->within_bounds($next) and not $self->tile($next) and do { do { $farthest = $next; $next = [map(($next->[$_] + $vec->[$_]), (0, 1))] } } 171 400 571
Condition100%3/3
$self->within_bounds($next) and not $self->tile($next)
!ll&&!rl&&r
131269171
MC/DC100%2/2
$self->within_bounds($next) and not $self->tile($next)
$self->within_bounds($next) not $self->tile($next)
Truth table100%3/3
A and not(B)
A= $self->within_bounds($next)B= not $self->tile($next)
ABresult
0X0
100
111
42 571 $farthest = $next;
43 571 $next = [ map $next->[$_] + $vec->[$_], 0..1 ];
44 } while ($self->within_bounds($next)
45 and !$self->tile($next));
46  
47 400 ▶ if ($self->cells_can_merge($cell, $next)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->cells_can_merge($cell, $next)) { } 60 340 400
elsif (not $self->tile($farthest)) { } 102 238 340
48 # merge
49 60 my $next_tile = $self->tile($next);
50  
51 60 $tile->moving_from($cell);
52  
53 60 $tile->merging_tiles(undef);
54 60 $tile->appear(undef);
55 60 $next_tile->merging_tiles(undef);
56 60 $next_tile->appear(undef);
57  
58 my $merged_tile = Games::2048::Tile->new(
59 value => $tile->value + $next_tile->value,
60 60 merging_tiles => [ sort { $reverse } $tile, $next_tile ],
61 merged => 1,
62 );
63  
64 60 $self->clear_tile($cell);
65 60 $self->set_tile($next, $merged_tile);
66  
67 60 $self->score($self->score + $merged_tile->value);
68 60 ▶ $self->best_score($self->score) if $self->score > $self->best_score;
Branch: 1 branch
BranchTrueFalseTotal
if $self->score > $self->best_score 60 0 60
69 60 ▶ if ($merged_tile->value >= 2048 and !$self->won) {
Branch: 1 branch
BranchTrueFalseTotal
if ($merged_tile->value >= 2048 and not $self->won) 0 60 60
Condition33%1/3
$merged_tile->value >= 2048 and not $self->won
!ll&&!rl&&r
6000
MC/DC0%0/2
$merged_tile->value >= 2048 and not $self->won
$merged_tile->value >= 2048 not $self->won
Truth table33%1/3
A and not(B)
A= $merged_tile->value >= 2048B= not $self->won
ABresult
0X0
100
111
70 0 $self->win(1);
71 0 $self->won(1);
72 }
73 60 $moved = 1;
74 }
75 elsif (!$self->tile($farthest)) {
76 # slide
77 102 $tile->moving_from($cell);
78 102 $tile->merging_tiles(undef);
79 102 $tile->appear(undef);
80  
81 102 $self->clear_tile($cell);
82 102 $self->set_tile($farthest, $tile);
83 102 $moved = 1;
84 }
85 }
86  
87 38 $_->merged(0) for $self->each_tile;
88  
89 38 return $moved;
90 }
91  
92 sub move {
93 0 ▶ my ($self, $vec) = @_;
Subroutine move: not called CC=3 SCAR=24.8
Pod undocumented
94 0 ▶ if ($self->move_tiles($vec)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->move_tiles($vec)) 0 0 0
95 0 $self->insert_random_tile;
96  
97 0 $self->needs_redraw(1);
98 0 $self->moving_vec($vec);
99 0 $self->moving(Games::2048::Animation->new(
100 duration => 0.2,
101 ));
102  
103 0 ▶ if (!$self->has_moves_remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_moves_remaining) 0 0 0
104 0 $self->lose(1);
105 }
106 }
107 }
108  
109 sub cells_can_merge {
110 473 ▶ my ($self, $cell, $next) = @_;
Subroutine cells_can_merge: called CC=4 SCAR=13.9
Pod undocumented
111 473 my $tile = $self->tile($cell);
112 473 my $next_tile = $self->tile($next);
113 473 ▶ $tile and $next_tile and !$next_tile->merged and $next_tile->value == $tile->value;
Branch: 1 branch
BranchTrueFalseTotal
$tile and $next_tile and not $next_tile->merged and $next_tile->value == $tile->value 288 185 473
Condition83%5/6
$tile and $next_tile and not $next_tile->merged
!ll&&!rl&&r
15134288
$tile and $next_tile
!ll&&!rl&&r
0151322
MC/DC66%2/3
$tile and $next_tile and not $next_tile->merged
$tile $next_tile not $next_tile->merged
Truth table75%3/4
A and B and not(C)
A= $tileB= $next_tileC= not $next_tile->merged
ABCresult
0XX0
10X0
1100
1111
114 }
115  
116 sub has_moves_remaining {
117 6 ▶ my $self = shift;
Subroutine has_moves_remaining: called CC=5 SCAR=16.1
Pod undocumented
118 6 ▶ return 1 if $self->has_available_cells;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_available_cells 3 3 6
119 3 for my $vec ([0, -1], [-1, 0]) {
120 5 for my $cell ($self->each_cell) {
121 73 my $next = [ map $cell->[$_] + $vec->[$_], 0..1 ];
122 73 ▶ return 1 if $self->cells_can_merge($cell, $next);
Branch: 1 branch
BranchTrueFalseTotal
if $self->cells_can_merge($cell, $next) 1 72 73
123 }
124 }
125 2 return;
126 }
127  
128 sub _game_file {
129 0 ▶ state $dir = eval {
Subroutine _game_file: not called CC=3 SCAR=24.8
130 0 ▶ my $my_dist_method = "my_dist_" . ($^O eq "MSWin32" ? "data" : "config");
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
131 0 File::HomeDir->$my_dist_method("Games-2048", {create => 1});
132 };
133 0 ▶ return if !defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 0 0
134 0 return catfile($dir, "game.dat");
135 }
136  
137 sub save {
138 0 ▶ my $self = shift;
Subroutine save: not called CC=1 SCAR=6.9
Pod undocumented
139 0 $self->version(__PACKAGE__->VERSION);
140 0 eval { store($self, _game_file); 1 };
141 }
142  
143 sub restore {
144 0 ▶ my $self = eval { retrieve(_game_file) };
Subroutine restore: not called CC=1 SCAR=6.9
Pod undocumented
145 0 $self;
146 }
147  
148 sub is_valid {
149 1 ▶ my $self = shift;
Subroutine is_valid: called CC=2 SCAR=7.2
Pod undocumented
150 1 ▶ defined $self->version and $self->version >= __PACKAGE__->VERSION;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->version and $self->version >= __PACKAGE__->VERSION 1 0 1
151 }
152  
153 4 1;
« 2048.pm ↑ Summary 2048/Input.pm »