Backgammon/Rules.pm

filter: Statementstmt 100% 91 / 91 Branchbran 95.2% 40 / 42 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 97.4% 151 / 155 CC 29 SCAR 20.7
CC
29
Cov
98%
legal_turns
11.0
single_moves
11.0
_walk
6.0
CRAP 7.9
« Backgammon.pm ↑ Summary Backgammon/Result.pm »
1 package Game::Backgammon::Rules;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Game::Backgammon::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Game::Backgammon::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Game::Backgammon::Turn;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 our $VERSION = '0.04';
12 10 our @EXPORT_OK = qw(legal_turns apply_move single_moves);
13  
14 409932 ▶sub single_moves {
Subroutine single_moves: called CC=11 SCAR=24.0
Pod documented
15 409932 my ($board, $player, $die) = @_;
16 409932 my @out;
17  
18 409932 ▶ if ($board->bar($player)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($board->bar($player)) 39765 370167 409932
19 39765 my $to = 25 - $die;
20 39765 ▶ return () if $board->is_blocked($player, $to);
Branch: 1 branch
BranchTrueFalseTotal
if $board->is_blocked($player, $to) 3023 36742 39765
21 36742 return Game::Backgammon::Move->new(
22 player => $player, from => 'bar', to => $to, die => $die,
23 hit => $board->is_blot($player, $to));
24 }
25  
26 370167 my $home = $board->all_home($player);
27 370167 ▶ my $highest = $home ? $board->highest_occupied($player) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$home ? : 7708 362459 370167
28  
29 370167 for my $n (1 .. 24) {
30 8884008 ▶ next unless $board->mine_on($player, $n);
Branch: 1 branch
BranchTrueFalseTotal
unless $board->mine_on($player, $n) 7012408 1871600 8884008
31 1871600 my $to = $n - $die;
32  
33 1871600 ▶ if ($to >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to >= 1) 1689644 181956 1871600
34 1689644 ▶ next if $board->is_blocked($player, $to);
Branch: 1 branch
BranchTrueFalseTotal
if $board->is_blocked($player, $to) 92394 1597250 1689644
35 1597250 push @out, Game::Backgammon::Move->new(
36 player => $player, from => $n, to => $to, die => $die,
37 hit => $board->is_blot($player, $to));
38 1597250 next;
39 }
40  
41 181956 ▶ next unless $home;
Branch: 1 branch
BranchTrueFalseTotal
unless $home 166939 15017 181956
42 15017 ▶ if ($to == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($to == 0) { } 4024 10993 15017
elsif ($n == $highest) { } 2887 8106 10993
43 4024 push @out, Game::Backgammon::Move->new(
44 player => $player, from => $n, to => 'off', die => $die);
45 }
46 elsif ($n == $highest) {
47 2887 push @out, Game::Backgammon::Move->new(
48 player => $player, from => $n, to => 'off', die => $die);
49 }
50 }
51 370167 return @out;
52 }
53  
54 2245681 ▶sub apply_move {
Subroutine apply_move: called CC=4 SCAR=13.9
Pod documented
55 2245681 my ($board, $move) = @_;
56 2245681 my $next = $board->clone;
57 2245681 my $player = $move->player;
58 2245681 my $them = Game::Backgammon::Board::other($player);
59  
60 2245681 ▶ if ($move->is_bar) { $next->to_bar($player, -1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($move->is_bar) { } 97796 2147885 2245681
61 2147885 else { $next->add($player, $move->from, -1) }
62  
63 2245681 ▶ if ($move->is_off) { $next->to_off($player) }
Branch: 1 branch
BranchTrueFalseTotal
if ($move->is_off) { } 9248 2236433 2245681
64 else {
65 2236433 ▶ if ($next->is_blot($player, $move->to)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->is_blot($player, $move->to)) 158086 2078347 2236433
66 158086 $next->set_point($them, 25 - $move->to, 0);
67 158086 $next->to_bar($them);
68 }
69 2236433 $next->add($player, $move->to, 1);
70 }
71 2245681 return $next;
72 }
73  
74 41767 ▶sub legal_turns {
Subroutine legal_turns: called CC=11 SCAR=24.0
Pod documented
75 41767 my ($board, $player, @dice) = @_;
76  
77 41767 my @seqs;
78 41767 _walk($board, $player, \@dice, [], \@seqs, {});
79  
80 41767 my $most = 0;
81 41767 ▶ for my $s (@seqs) { $most = @{ $s->{moves} } if @{ $s->{moves} } > $most }
Branch: 1 branch
BranchTrueFalseTotal
if @{$s->{"moves"};} > $most 94133 929688 1023821
82 41767 @seqs = grep { @{ $_->{moves} } == $most } @seqs;
83  
84 41767 ▶ if ($most == 1 && @dice == 2 && $dice[0] != $dice[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($most == 1 and @dice == 2 and $dice[0] != $dice[1]) 534 41233 41767
Condition83%5/6
$most == 1 and @dice == 2 and $dice[0] != $dice[1]
!ll&&!rl&&r
412330534
$most == 1 and @dice == 2
!ll&&!rl&&r
4121320534
MC/DC66%2/3
$most == 1 and @dice == 2 and $dice[0] != $dice[1]
$most == 1 @dice == 2 $dice[0] != $dice[1]
Truth table75%3/4
A and B and C
A= $most == 1B= @dice == 2C= $dice[0] != $dice[1]
ABCresult
0XX0
10X0
1100
1111
85 534 ▶ my $larger = $dice[0] > $dice[1] ? $dice[0] : $dice[1];
Branch: 1 branch
BranchTrueFalseTotal
$dice[0] > $dice[1] ? : 277 257 534
86 534 my @big = grep { $_->{moves}[0]->die == $larger } @seqs;
87 534 ▶ @seqs = @big if @big;
Branch: 1 branch
BranchTrueFalseTotal
if @big 293 241 534
88 }
89  
90 41767 ▶ return [ Game::Backgammon::Turn->new(player => $player, dice => \@dice, moves => []) ]
Branch: 1 branch
BranchTrueFalseTotal
unless @seqs 0 41767 41767
91 unless @seqs;
92  
93 41767 my (%seen, @turns);
94 41767 for my $s (@seqs) {
95 my $turn = Game::Backgammon::Turn->new(
96 650455 player => $player, dice => \@dice, moves => $s->{moves});
97 650455 ▶ next if $seen{ $turn->key }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$turn->key}++ 0 650455 650455
98 650455 push @turns, $turn;
99 }
100 41767 return \@turns;
101 }
102  
103 1682662 ▶sub _walk {
Subroutine _walk: called CC=6 SCAR=17.9
104 1682662 my ($board, $player, $dice, $sofar, $out, $seen) = @_;
105 1682662 ▶ return if $seen->{ join(',', sort map { $_->notation } @$sofar) . '|' . join(',', sort @$dice) }++;
Branch: 1 branch
BranchTrueFalseTotal
if $$seen{join(",", sort(map({$_->notation;} @$sofar))) . '|' . join(",", sort(@$dice))}++ 658841 1023821 1682662
106 1023821 push @$out, { moves => [ @$sofar ] };
107 1023821 ▶ return unless @$dice;
Branch: 1 branch
BranchTrueFalseTotal
unless @$dice 648993 374828 1023821
108  
109 374828 my %tried;
110 374828 for my $i (0 .. $#$dice) {
111 507019 ▶ next if $tried{ $dice->[$i] }++;
Branch: 1 branch
BranchTrueFalseTotal
if $tried{$dice->[$i]}++ 97096 409923 507019
112 409923 my @rest = @$dice;
113 409923 my ($die) = splice @rest, $i, 1;
114 409923 for my $move (single_moves($board, $player, $die)) {
115 1640895 _walk(apply_move($board, $move), $player, \@rest,
116 [ @$sofar, $move ], $out, $seen);
117 }
118 }
119 374828 return;
120 }
121  
122 10 1;
123  
124 __END__
« Backgammon.pm ↑ Summary Backgammon/Result.pm »