blib/lib/Game/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
« blib/lib/Game/Backgammon.pm ↑ Summary blib/lib/Game/Backgammon/Result.pm »
1 package Game::Backgammon::Rules;
2  
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 ▶use Game::Backgammon::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Game::Backgammon::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Game::Backgammon::Turn;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 28 our $VERSION = '0.05';
12 28 our @EXPORT_OK = qw(legal_turns apply_move single_moves);
13  
14 411883 ▶sub single_moves {
Subroutine single_moves: called CC=11 SCAR=24.0
Pod documented
15 411883 my ($board, $player, $die) = @_;
16 411883 my @out;
17  
18 411883 ▶ if ($board->bar($player)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($board->bar($player)) 39782 372101 411883
19 39782 my $to = 25 - $die;
20 39782 ▶ return () if $board->is_blocked($player, $to);
Branch: 1 branch
BranchTrueFalseTotal
if $board->is_blocked($player, $to) 3024 36758 39782
21 36758 return Game::Backgammon::Move->new(
22 player => $player, from => 'bar', to => $to, die => $die,
23 hit => $board->is_blot($player, $to));
24 }
25  
26 372101 my $home = $board->all_home($player);
27 372101 ▶ my $highest = $home ? $board->highest_occupied($player) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$home ? : 7708 364393 372101
28  
29 372101 for my $n (1 .. 24) {
30 8930424 ▶ next unless $board->mine_on($player, $n);
Branch: 1 branch
BranchTrueFalseTotal
unless $board->mine_on($player, $n) 7047746 1882678 8930424
31 1882678 my $to = $n - $die;
32  
33 1882678 ▶ if ($to >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to >= 1) 1699450 183228 1882678
34 1699450 ▶ next if $board->is_blocked($player, $to);
Branch: 1 branch
BranchTrueFalseTotal
if $board->is_blocked($player, $to) 92783 1606667 1699450
35 1606667 push @out, Game::Backgammon::Move->new(
36 player => $player, from => $n, to => $to, die => $die,
37 hit => $board->is_blot($player, $to));
38 1606667 next;
39 }
40  
41 183228 ▶ next unless $home;
Branch: 1 branch
BranchTrueFalseTotal
unless $home 168211 15017 183228
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 372101 return @out;
52 }
53  
54 2256654 ▶sub apply_move {
Subroutine apply_move: called CC=4 SCAR=13.9
Pod documented
55 2256654 my ($board, $move) = @_;
56 2256654 my $next = $board->clone;
57 2256654 my $player = $move->player;
58 2256654 my $them = Game::Backgammon::Board::other($player);
59  
60 2256654 ▶ if ($move->is_bar) { $next->to_bar($player, -1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($move->is_bar) { } 97895 2158759 2256654
61 2158759 else { $next->add($player, $move->from, -1) }
62  
63 2256654 ▶ if ($move->is_off) { $next->to_off($player) }
Branch: 1 branch
BranchTrueFalseTotal
if ($move->is_off) { } 9248 2247406 2256654
64 else {
65 2247406 ▶ if ($next->is_blot($player, $move->to)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->is_blot($player, $move->to)) 158538 2088868 2247406
66 158538 $next->set_point($them, 25 - $move->to, 0);
67 158538 $next->to_bar($them);
68 }
69 2247406 $next->add($player, $move->to, 1);
70 }
71 2256654 return $next;
72 }
73  
74 41868 ▶sub legal_turns {
Subroutine legal_turns: called CC=11 SCAR=24.0
Pod documented
75 41868 my ($board, $player, @dice) = @_;
76  
77 41868 my @seqs;
78 41868 _walk($board, $player, \@dice, [], \@seqs, {});
79  
80 41868 my $most = 0;
81 41868 ▶ for my $s (@seqs) { $most = @{ $s->{moves} } if @{ $s->{moves} } > $most }
Branch: 1 branch
BranchTrueFalseTotal
if @{$s->{"moves"};} > $most 94389 934397 1028786
82 41868 @seqs = grep { @{ $_->{moves} } == $most } @seqs;
83  
84 41868 ▶ 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 41334 41868
Condition83%5/6
$most == 1 and @dice == 2 and $dice[0] != $dice[1]
!ll&&!rl&&r
413340534
$most == 1 and @dice == 2
!ll&&!rl&&r
4131420534
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 41868 ▶ return [ Game::Backgammon::Turn->new(player => $player, dice => \@dice, moves => []) ]
Branch: 1 branch
BranchTrueFalseTotal
unless @seqs 0 41868 41868
91 unless @seqs;
92  
93 41868 my (%seen, @turns);
94 41868 for my $s (@seqs) {
95 my $turn = Game::Backgammon::Turn->new(
96 653543 player => $player, dice => \@dice, moves => $s->{moves});
97 653543 ▶ next if $seen{ $turn->key }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$turn->key}++ 0 653543 653543
98 653543 push @turns, $turn;
99 }
100 41868 return \@turns;
101 }
102  
103 1692196 ▶sub _walk {
Subroutine _walk: called CC=6 SCAR=17.9
104 1692196 my ($board, $player, $dice, $sofar, $out, $seen) = @_;
105 1692196 ▶ 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))}++ 663410 1028786 1692196
106 1028786 push @$out, { moves => [ @$sofar ] };
107 1028786 ▶ return unless @$dice;
Branch: 1 branch
BranchTrueFalseTotal
unless @$dice 652081 376705 1028786
108  
109 376705 my %tried;
110 376705 for my $i (0 .. $#$dice) {
111 509575 ▶ next if $tried{ $dice->[$i] }++;
Branch: 1 branch
BranchTrueFalseTotal
if $tried{$dice->[$i]}++ 97701 411874 509575
112 411874 my @rest = @$dice;
113 411874 my ($die) = splice @rest, $i, 1;
114 411874 for my $move (single_moves($board, $player, $die)) {
115 1650328 _walk(apply_move($board, $move), $player, \@rest,
116 [ @$sofar, $move ], $out, $seen);
117 }
118 }
119 376705 return;
120 }
121  
122 28 1;
123  
124 __END__
« blib/lib/Game/Backgammon.pm ↑ Summary blib/lib/Game/Backgammon/Result.pm »