Backgammon/Rules.pm

filter: Statementstmt 100% 89 / 89 Branchbran 97.5% 39 / 40 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 98.0% 148 / 151 CC 28 SCAR 20.6
CC
28
Cov
99%
legal_turns
11.0
single_moves
11.0
_walk
5.0
CRAP 7.8
« Backgammon/Notation.pm ↑ Summary Backgammon/Result.pm »
1 package Game::Backgammon::Rules;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Game::Backgammon::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Game::Backgammon::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Game::Backgammon::Turn;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 our $VERSION = '0.03';
12 7 our @EXPORT_OK = qw(legal_turns apply_move single_moves);
13  
14 167707 ▶sub single_moves {
Subroutine single_moves: called CC=11 SCAR=24.0
Pod documented
15 167707 my ($board, $player, $die) = @_;
16 167707 my @out;
17  
18 167707 ▶ if ($board->bar($player)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($board->bar($player)) 6856 160851 167707
19 6856 my $to = 25 - $die;
20 6856 ▶ return () if $board->is_blocked($player, $to);
Branch: 1 branch
BranchTrueFalseTotal
if $board->is_blocked($player, $to) 2765 4091 6856
21 4091 return Game::Backgammon::Move->new(
22 player => $player, from => 'bar', to => $to, die => $die,
23 hit => $board->is_blot($player, $to));
24 }
25  
26 160851 my $home = $board->all_home($player);
27 160851 ▶ my $highest = $home ? $board->highest_occupied($player) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$home ? : 7839 153012 160851
28  
29 160851 for my $n (1 .. 24) {
30 3860424 ▶ next unless $board->mine_on($player, $n);
Branch: 1 branch
BranchTrueFalseTotal
unless $board->mine_on($player, $n) 2754438 1105986 3860424
31 1105986 my $to = $n - $die;
32  
33 1105986 ▶ if ($to >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to >= 1) 865771 240215 1105986
34 865771 ▶ next if $board->is_blocked($player, $to);
Branch: 1 branch
BranchTrueFalseTotal
if $board->is_blocked($player, $to) 91150 774621 865771
35 774621 push @out, Game::Backgammon::Move->new(
36 player => $player, from => $n, to => $to, die => $die,
37 hit => $board->is_blot($player, $to));
38 774621 next;
39 }
40  
41 240215 ▶ next unless $home;
Branch: 1 branch
BranchTrueFalseTotal
unless $home 225240 14975 240215
42 14975 ▶ if ($to == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($to == 0) { } 4429 10546 14975
elsif ($n == $highest) { } 2664 7882 10546
43 4429 push @out, Game::Backgammon::Move->new(
44 player => $player, from => $n, to => 'off', die => $die);
45 }
46 elsif ($n == $highest) {
47 2664 push @out, Game::Backgammon::Move->new(
48 player => $player, from => $n, to => 'off', die => $die);
49 }
50 }
51 160851 return @out;
52 }
53  
54 929290 ▶sub apply_move {
Subroutine apply_move: called CC=4 SCAR=13.9
Pod documented
55 929290 my ($board, $move) = @_;
56 929290 my $next = $board->clone;
57 929290 my $player = $move->player;
58 929290 my $them = Game::Backgammon::Board::other($player);
59  
60 929290 ▶ if ($move->is_bar) { $next->to_bar($player, -1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($move->is_bar) { } 11560 917730 929290
61 917730 else { $next->add($player, $move->from, -1) }
62  
63 929290 ▶ if ($move->is_off) { $next->to_off($player) }
Branch: 1 branch
BranchTrueFalseTotal
if ($move->is_off) { } 9175 920115 929290
64 else {
65 920115 ▶ if ($next->is_blot($player, $move->to)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->is_blot($player, $move->to)) 69236 850879 920115
66 69236 $next->set_point($them, 25 - $move->to, 0);
67 69236 $next->to_bar($them);
68 }
69 920115 $next->add($player, $move->to, 1);
70 }
71 929290 return $next;
72 }
73  
74 10295 ▶sub legal_turns {
Subroutine legal_turns: called CC=11 SCAR=24.0
Pod documented
75 10295 my ($board, $player, @dice) = @_;
76  
77 10295 my @seqs;
78 10295 _walk($board, $player, \@dice, [], \@seqs);
79  
80 10295 my $most = 0;
81 10295 ▶ for my $s (@seqs) { $most = @{ $s->{moves} } if @{ $s->{moves} } > $most }
Branch: 1 branch
BranchTrueFalseTotal
if @{$s->{"moves"};} > $most 21356 774736 796092
82 10295 @seqs = grep { @{ $_->{moves} } == $most } @seqs;
83  
84 10295 ▶ if ($most == 1 && @dice == 2 && $dice[0] != $dice[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($most == 1 and @dice == 2 and $dice[0] != $dice[1]) 462 9833 10295
Condition83%5/6
$most == 1 and @dice == 2 and $dice[0] != $dice[1]
!ll&&!rl&&r
98330462
$most == 1 and @dice == 2
!ll&&!rl&&r
981320462
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 462 ▶ my $larger = $dice[0] > $dice[1] ? $dice[0] : $dice[1];
Branch: 1 branch
BranchTrueFalseTotal
$dice[0] > $dice[1] ? : 239 223 462
86 462 my @big = grep { $_->{moves}[0]->die == $larger } @seqs;
87 462 ▶ @seqs = @big if @big;
Branch: 1 branch
BranchTrueFalseTotal
if @big 257 205 462
88 }
89  
90 10295 ▶ return [ Game::Backgammon::Turn->new(player => $player, dice => \@dice, moves => []) ]
Branch: 1 branch
BranchTrueFalseTotal
unless @seqs 0 10295 10295
91 unless @seqs;
92  
93 10295 my (%seen, @turns);
94 10295 for my $s (@seqs) {
95 my $turn = Game::Backgammon::Turn->new(
96 638381 player => $player, dice => \@dice, moves => $s->{moves});
97 638381 ▶ next if $seen{ $turn->key }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$turn->key}++ 487273 151108 638381
98 151108 push @turns, $turn;
99 }
100 10295 return \@turns;
101 }
102  
103 796092 ▶sub _walk {
Subroutine _walk: called CC=5 SCAR=16.1
104 796092 my ($board, $player, $dice, $sofar, $out) = @_;
105 796092 push @$out, { moves => [ @$sofar ] };
106 796092 ▶ return unless @$dice;
Branch: 1 branch
BranchTrueFalseTotal
unless @$dice 637086 159006 796092
107  
108 159006 my %tried;
109 159006 for my $i (0 .. $#$dice) {
110 197345 ▶ next if $tried{ $dice->[$i] }++;
Branch: 1 branch
BranchTrueFalseTotal
if $tried{$dice->[$i]}++ 29647 167698 197345
111 167698 my @rest = @$dice;
112 167698 my ($die) = splice @rest, $i, 1;
113 167698 for my $move (single_moves($board, $player, $die)) {
114 785797 _walk(apply_move($board, $move), $player, \@rest,
115 [ @$sofar, $move ], $out);
116 }
117 }
118 159006 return;
119 }
120  
121 7 1;
122  
123 __END__
« Backgammon/Notation.pm ↑ Summary Backgammon/Result.pm »