Gin/Bot.pm

filter: Statementstmt 94.8% 55 / 58 Branchbran 76.9% 20 / 26 Conditioncond 61.7% 21 / 34 MC/DCmcdc 42.1% 8 / 19 Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 76.8% 113 / 147 CC 27 SCAR 31.0
CC
27
Cov
81%
choose
28.5
seed_for
12.0
BEGIN
1.0
CRAP 22.2
« Gin/Terminal.pm ↑ Summary Gin/Scoring.pm »
1 package Game::Gin::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 Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Game::Gin::Deadwood qw(deadwood);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Game::Gin::Search qw(worth_taking best_discard knock_now LEVELS);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 our $VERSION = '0.03';
14  
15 5 has level => (is => 'ro', isa => Int);
16 5 has seed => (is => 'ro', isa => Str);
17  
18 41355 ▶sub choose {
Subroutine choose: called CC=25 SCAR=33.5
Pod documented
19 41355 my ($self, $game, $seat) = @_;
20 41355 ▶ my $deal = $game->can('deal') ? $game->deal : $game;
Branch: 1 branch
BranchTrueFalseTotal
$game->can('deal') ? : 41355 0 41355
21 41355 ▶ return undef unless $deal && !$deal->over;
Branch: 1 branch
BranchTrueFalseTotal
unless $deal and not $deal->over 0 41355 41355
Condition33%1/3
$deal and not $deal->over
!ll&&!rl&&r
0041355
MC/DC0%0/2
$deal and not $deal->over
$deal not $deal->over
Truth table33%1/3
A and not(B)
A= $dealB= not $deal->over
ABresult
0X0
100
111
22 41355 ▶ return undef unless ($deal->turn // '') eq $seat;
Branch: 1 branch
BranchTrueFalseTotal
unless ($deal->turn // "") eq $seat 1 41354 41355
Condition50%1/2
$deal->turn // ""
l!l
413550
MC/DC0%0/1
$deal->turn // ""
$deal->turn
Truth table50%1/2
A
A= $deal->turn
Aresult
11
00
23  
24 41354 my $legal = $deal->legal($seat);
25 41354 ▶ return undef unless @$legal;
Branch: 1 branch
BranchTrueFalseTotal
unless @$legal 0 41354 41354
26  
27 41354 ▶ my $level = $self->level || LEVELS;
Condition50%1/2
$self->level || 2
l!l
413540
MC/DC0%0/1
$self->level || 2
$self->level
Truth table50%1/2
A
A= $self->level
Aresult
11
00
28 41354 my $hand = $deal->hand_of($seat)->cards;
29 41354 my $phase = $deal->phase;
30  
31 41354 ▶ if ($phase eq 'upcard' || $phase eq 'draw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq "upcard" or $phase eq "draw") 20711 20643 41354
Condition100%3/3
$phase eq "upcard" or $phase eq "draw"
l!l&&r!l&&!r
18731883820643
MC/DC100%2/2
$phase eq "upcard" or $phase eq "draw"
$phase eq "upcard" $phase eq "draw"
Truth table100%3/3
A or B
A= $phase eq "upcard"B= $phase eq "draw"
ABresult
1X1
011
000
32 20711 my $take = worth_taking(hand => $hand, upcard => $deal->upcard, level => $level);
33 20711 ▶ return { kind => 'take' } if $take && grep { $_->{kind} eq 'take' } @$legal;
Branch: 1 branch
BranchTrueFalseTotal
if $take and grep {$_->{"kind"} eq "take";} @$legal 6101 14610 20711
Condition66%2/3
$take and grep {$_->{"kind"} eq "take";} @$legal
!ll&&!rl&&r
1461006101
MC/DC50%1/2
$take and grep {$_->{"kind"} eq "take";} @$legal
$take grep {$_->{"kind"} eq "take";} @$legal
Truth table66%2/3
A and B
A= $takeB= grep {$_->{"kind"} eq "take";} @$legal
ABresult
0X0
100
111
34 14610 ▶ return { kind => 'draw' } if grep { $_->{kind} eq 'draw' } @$legal;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->{"kind"} eq "draw";} @$legal 13881 729 14610
35 729 ▶ return { kind => 'pass' } if grep { $_->{kind} eq 'pass' } @$legal;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->{"kind"} eq "pass";} @$legal 729 0 729
36 0 return $legal->[0];
37 }
38 20643 ▶ return { kind => 'draw' } if $phase eq 'forced_draw';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "forced_draw" 331 20312 20643
39  
40 20312 my ($big) = grep { $_->{kind} eq 'big_gin' } @$legal;
41 20312 ▶ return $big if $big;
Branch: 1 branch
BranchTrueFalseTotal
if $big 5 20307 20312
42  
43 20307 ▶ my $card = best_discard(
Condition100%2/2
$deal->taken || undef
l!l
609914208
MC/DC100%1/1
$deal->taken || undef
$deal->taken
Truth table100%2/2
A
A= $deal->taken
Aresult
11
00
44 hand => $hand,
45 level => $level,
46 just_taken => ($deal->taken || undef),
47 );
48 20307 ▶ return $legal->[0] unless defined $card;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $card 0 20307 20307
49  
50 20307 my $left = deadwood([ grep { $_ != $card } @$hand ]);
51 20307 my $knock = knock_now(deadwood => $left, stock_left => $deal->stock_left, level => $level);
52  
53 20307 ▶ if ($knock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knock) 1456 18851 20307
54 1456 ▶ my ($move) = grep { $_->{kind} eq 'discard' && $_->{card} == $card && $_->{knock} } @$legal;
Condition83%5/6
$_->{'kind'} eq 'discard' && $_->{'card'} == $card && $_->{'knock'}
!ll&&!rl&&r
1824814561456
$_->{"kind"} eq "discard" and $_->{"card"} == $card
!ll&&!rl&&r
0182482912
MC/DC66%2/3
$_->{'kind'} eq 'discard' && $_->{'card'} == $card && $_->{'knock'}
$_->{"kind"} eq "discard" $_->{"card"} == $card $_->{'knock'}
Truth table75%3/4
A and B && C
A= $_->{"kind"} eq "discard"B= $_->{"card"} == $cardC= $_->{'knock'}
ABCresult
0XX0
10X0
1100
1111
55 1456 ▶ return $move if $move;
Branch: 1 branch
BranchTrueFalseTotal
if $move 1456 0 1456
56 }
57 18851 ▶ my ($move) = grep { $_->{kind} eq 'discard' && $_->{card} == $card && !$_->{knock} } @$legal;
Condition83%5/6
$_->{'kind'} eq 'discard' && $_->{'card'} == $card && !$_->{'knock'}
!ll&&!rl&&r
183835131718851
$_->{"kind"} eq "discard" and $_->{"card"} == $card
!ll&&!rl&&r
018383520168
MC/DC66%2/3
$_->{'kind'} eq 'discard' && $_->{'card'} == $card && !$_->{'knock'}
$_->{"kind"} eq "discard" $_->{"card"} == $card !$_->{'knock'}
Truth table75%3/4
A and B && not(C)
A= $_->{"kind"} eq "discard"B= $_->{"card"} == $cardC= !$_->{'knock'}
ABCresult
0XX0
10X0
1100
1111
58 18851 ▶ return $move || $legal->[0];
Condition33%1/3
$move || $legal->[0]
l!l&&r!l&&!r
1885100
MC/DC0%0/2
$move || $legal->[0]
$move $legal->[0]
Truth table33%1/3
A || B
A= $moveB= $legal->[0]
ABresult
1X1
011
000
59 }
60  
61 0 ▶sub seed_for {
Subroutine seed_for: not called CC=3 SCAR=24.8
Pod documented
62 0 my ($class, $game_seed, $seat) = @_;
63 0 ▶ return unpack 'H16', Digest::SHA::sha256('gin-bot:' . ($game_seed // '') . ':' . ($seat // '?'));
Condition0%0/4
$game_seed // ""
l!l
00
$seat // "?"
l!l
00
MC/DC0%0/1
$game_seed // ""
$game_seed
MC/DC0%0/1
$seat // "?"
$seat
Truth table0%0/2
A
A= $game_seed
Aresult
11
00
Truth table0%0/2
A
A= $seat
Aresult
11
00
64 }
65  
66 5 1;
67  
68 __END__
« Gin/Terminal.pm ↑ Summary Gin/Scoring.pm »