| 1 | package Game::Backgammon::Bot; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Game::Backgammon::Board; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Game::Backgammon::Rules (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Game::Backgammon::Shots qw(shots_at); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | has level => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | default => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 50269 | ▶ | sub knows_blots { $_[0]->level >= 3 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
knows_blots: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 50269 | ▶ | sub knows_shape { $_[0]->level >= 2 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
knows_shape: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3737 | ▶ | sub choose { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
choose: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 3737 | my ($self, $game) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3737 | my $turns = $game->legal_turns; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 3737 | ▶ | return undef unless @$turns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 3737 | ▶ | return $turns->[0] if @$turns == 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 2868 | my $player = $game->turn; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 2868 | my ($best, $best_score); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 2868 | for my $turn (@$turns) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 50264 | my $board = $game->board; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 50264 | $board = Game::Backgammon::Rules::apply_move($board, $_) for @{ $turn->moves }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 50264 | my $score = $self->score($board, $player); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 50264 | ▶ | next if defined $best_score | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
defined $best_score and $score < $best_score || $score == $best_score && $turn->key ge $best->key
$score < $best_score or $score == $best_score and $turn->key ge $best->key
$score == $best_score and $turn->key ge $best->key
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | && ($score < $best_score | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | || ($score == $best_score && $turn->key ge $best->key)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 8596 | ($best, $best_score) = ($turn, $score); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 2868 | return $best; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 50266 | ▶ | sub score { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
score: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 50266 | my ($self, $board, $player) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 50266 | my $them = Game::Backgammon::Board::other($player); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 50266 | my $score = $board->pip_count($them) - $board->pip_count($player); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 50266 | $score += 4 * ($board->off($player) - $board->off($them)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 50266 | $score += 12 * ($board->bar($them) - $board->bar($player)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 50266 | ▶ | if ($self->knows_shape) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 25927 | $score += 2 * $self->_made_points($board, $player); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 25927 | $score += 3 * $self->_longest_prime($board, $player) ** 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 25927 | ▶ | $score += 8 if $self->_has_anchor($board, $player); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 50266 | ▶ | if ($self->knows_blots) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 23994 | $score -= $self->blot_exposure($board, $player); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 50266 | return $score; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 23997 | ▶ | sub blot_exposure { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
blot_exposure: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 23997 | my ($self, $board, $player) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 23997 | my $them = Game::Backgammon::Board::other($player); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 23997 | my $risk = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 23997 | for my $n (1 .. 24) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 575928 | ▶ | next unless $board->mine_on($player, $n) == 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 59946 | my $worst = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 59946 | for my $k (1 .. $n - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 608586 | ▶ | next unless $board->theirs_on($player, $k); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 124192 | my $shots = shots_at($n - $k); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 124192 | ▶ | $worst = $shots if $shots > $worst; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 59946 | ▶ | if ($board->bar($them)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 28553 | my $shots = shots_at($n); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 28553 | ▶ | $worst = $shots if $shots > $worst; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 59946 | $risk += $worst; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 23997 | return $risk; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 25927 | ▶ | sub _made_points { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_made_points: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 25927 | my ($self, $board, $player) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 25927 | my $n = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 25927 | ▶ | for my $p (1 .. 24) { $n++ if $board->mine_on($player, $p) >= 2 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 25927 | return $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 25927 | ▶ | sub _longest_prime { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_longest_prime: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 25927 | my ($self, $board, $player) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 25927 | my ($best, $run) = (0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 25927 | for my $p (1 .. 24) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 622248 | ▶ | $run = $board->mine_on($player, $p) >= 2 ? $run + 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 622248 | ▶ | $best = $run if $run > $best; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 25927 | return $best; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 25927 | ▶ | sub _has_anchor { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_has_anchor: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 25927 | my ($self, $board, $player) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 25927 | ▶ | for my $p (19 .. 24) { return 1 if $board->mine_on($player, $p) >= 2 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 14213 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||