Backgammon/Bot.pm

filter: Statementstmt 100% 79 / 79 Branchbran 96.6% 29 / 30 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 99.2% 140 / 141 CC 26 SCAR 16.5
CC
26
Cov
99%
choose
9.0
blot_exposure
8.0
score
4.0
CRAP 5.2
« Backgammon/Result.pm ↑ Summary Backgammon/Dice.pm »
1 package Game::Backgammon::Bot;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Game::Backgammon::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Game::Backgammon::Rules ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Game::Backgammon::Shots qw(shots_at);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 our $VERSION = '0.04';
13  
14 has level => (
15 is => 'ro',
16 isa => Int,
17 default => 3
18 8 );
19  
20 212280 ▶sub knows_blots { $_[0]->level >= 3 }
Subroutine knows_blots: called CC=1 SCAR=0.0
Pod documented
21 212280 ▶sub knows_shape { $_[0]->level >= 2 }
Subroutine knows_shape: called CC=1 SCAR=0.0
Pod documented
22  
23 13908 ▶sub choose {
Subroutine choose: called CC=9 SCAR=22.0
Pod documented
24 13908 my ($self, $game) = @_;
25 13908 my $turns = $game->legal_turns;
26 13908 ▶ return undef unless @$turns;
Branch: 1 branch
BranchTrueFalseTotal
unless @$turns 0 13908 13908
27 13908 ▶ return $turns->[0] if @$turns == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$turns == 1 1339 12569 13908
28  
29 12569 my $player = $game->turn;
30 12569 my ($best, $best_score);
31 12569 for my $turn (@$turns) {
32 212275 my $board = $game->board;
33 212275 $board = Game::Backgammon::Rules::apply_move($board, $_) for @{ $turn->moves };
34 212275 my $score = $self->score($board, $player);
35 212275 ▶ next if defined $best_score
Branch: 1 branch
BranchTrueFalseTotal
if defined $best_score and $score < $best_score || $score == $best_score && $turn->key ge $best->key 183595 28680 212275
Condition100%9/9
defined $best_score and $score < $best_score || $score == $best_score && $turn->key ge $best->key
!ll&&!rl&&r
1256916111183595
$score < $best_score or $score == $best_score and $turn->key ge $best->key
l!l&&r!l&&!r
920129158316111
$score == $best_score and $turn->key ge $best->key
!ll&&!rl&&r
7140897191583
MC/DC100%4/4
defined $best_score and $score < $best_score || $score == $best_score && $turn->key ge $best->key
defined $best_score $score < $best_score $score == $best_score $turn->key ge $best->key
Truth table100%5/5
A and B or C and D
A= defined $best_scoreB= $score < $best_scoreC= $score == $best_scoreD= $turn->key ge $best->key
ABCDresult
0XXX0
100X0
10100
11XX1
10111
36 && ($score < $best_score
37 || ($score == $best_score && $turn->key ge $best->key));
38 28680 ($best, $best_score) = ($turn, $score);
39 }
40 12569 return $best;
41 }
42  
43 212277 ▶sub score {
Subroutine score: called CC=4 SCAR=13.9
Pod documented
44 212277 my ($self, $board, $player) = @_;
45 212277 my $them = Game::Backgammon::Board::other($player);
46  
47 212277 my $score = $board->pip_count($them) - $board->pip_count($player);
48  
49 212277 $score += 4 * ($board->off($player) - $board->off($them));
50 212277 $score += 12 * ($board->bar($them) - $board->bar($player));
51  
52 212277 ▶ if ($self->knows_shape) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->knows_shape) 26950 185327 212277
53 26950 $score += 2 * $self->_made_points($board, $player);
54 26950 $score += 3 * $self->_longest_prime($board, $player) ** 2;
55 26950 ▶ $score += 8 if $self->_has_anchor($board, $player);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_anchor($board, $player) 12270 14680 26950
56 }
57  
58 212277 ▶ if ($self->knows_blots) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->knows_blots) 25017 187260 212277
59 25017 $score -= $self->blot_exposure($board, $player);
60 }
61 212277 return $score;
62 }
63  
64 25020 ▶sub blot_exposure {
Subroutine blot_exposure: called CC=8 SCAR=20.8
Pod documented
65 25020 my ($self, $board, $player) = @_;
66 25020 my $them = Game::Backgammon::Board::other($player);
67 25020 my $risk = 0;
68  
69 25020 for my $n (1 .. 24) {
70 600480 ▶ next unless $board->mine_on($player, $n) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $board->mine_on($player, $n) == 1 538051 62429 600480
71  
72 62429 my $worst = 0;
73 62429 for my $k (1 .. $n - 1) {
74 633479 ▶ next unless $board->theirs_on($player, $k);
Branch: 1 branch
BranchTrueFalseTotal
unless $board->theirs_on($player, $k) 504348 129131 633479
75 129131 my $shots = shots_at($n - $k);
76 129131 ▶ $worst = $shots if $shots > $worst;
Branch: 1 branch
BranchTrueFalseTotal
if $shots > $worst 81289 47842 129131
77 }
78 62429 ▶ if ($board->bar($them)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($board->bar($them)) 29174 33255 62429
79 29174 my $shots = shots_at($n);
80 29174 ▶ $worst = $shots if $shots > $worst;
Branch: 1 branch
BranchTrueFalseTotal
if $shots > $worst 10595 18579 29174
81 }
82 62429 $risk += $worst;
83 }
84 25020 return $risk;
85 }
86  
87 26950 ▶sub _made_points {
Subroutine _made_points: called CC=3 SCAR=11.0
88 26950 my ($self, $board, $player) = @_;
89 26950 my $n = 0;
90 26950 ▶ for my $p (1 .. 24) { $n++ if $board->mine_on($player, $p) >= 2 }
Branch: 1 branch
BranchTrueFalseTotal
if $board->mine_on($player, $p) >= 2 116886 529914 646800
91 26950 return $n;
92 }
93  
94 26950 ▶sub _longest_prime {
Subroutine _longest_prime: called CC=4 SCAR=13.9
95 26950 my ($self, $board, $player) = @_;
96 26950 my ($best, $run) = (0, 0);
97 26950 for my $p (1 .. 24) {
98 646800 ▶ $run = $board->mine_on($player, $p) >= 2 ? $run + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$board->mine_on($player, $p) >= 2 ? : 116886 529914 646800
99 646800 ▶ $best = $run if $run > $best;
Branch: 1 branch
BranchTrueFalseTotal
if $run > $best 60162 586638 646800
100 }
101 26950 return $best;
102 }
103  
104 26950 ▶sub _has_anchor {
Subroutine _has_anchor: called CC=3 SCAR=11.0
105 26950 my ($self, $board, $player) = @_;
106 26950 ▶ for my $p (19 .. 24) { return 1 if $board->mine_on($player, $p) >= 2 }
Branch: 1 branch
BranchTrueFalseTotal
if $board->mine_on($player, $p) >= 2 12270 138940 151210
107 14680 return 0;
108 }
109  
110 8 1;
111  
112 __END__
« Backgammon/Result.pm ↑ Summary Backgammon/Dice.pm »