blib/lib/Game/Schnapsen/Search.pm

filter: Statementstmt 100% 89 / 89 Branchbran 89.4% 34 / 38 Conditioncond 61.7% 21 / 34 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 83.4% 166 / 199 CC 35 SCAR 17.8
CC
35
Cov
89%
best_lead
10.1
should_close
9.5
best_follow
5.0
CRAP 5.9
« blib/lib/Game/Schnapsen/Deal.pm ↑ Summary blib/lib/Game/Schnapsen/Deck.pm »
1 package Game::Schnapsen::Search;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use Game::Schnapsen::Card qw(suit_of power_of points_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Game::Schnapsen::Trick qw(winner_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 our $VERSION = '0.03';
12 9 our @EXPORT_OK = qw(best_lead best_follow best_marriage should_close should_claim
13 hand_strength LEVELS);
14  
15 9 ▶use constant LEVELS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 32868 ▶sub _cheapest {
Subroutine _cheapest: called CC=3 SCAR=11.2
18 32868 my ($cards) = @_;
19 32868 ▶ my ($c) = sort { points_of($a) <=> points_of($b)
Condition66%4/6
points_of($a) <=> points_of($b) || power_of($a) <=> power_of($b) || $a <=> $b
l!l&&r!l&&!r
82873156650
points_of($a) <=> points_of($b) or power_of($a) <=> power_of($b)
l!l&&r!l&&!r
82873015665
MC/DC0%0/3
points_of($a) <=> points_of($b) || power_of($a) <=> power_of($b) || $a <=> $b
points_of($a) <=> points_of($b) power_of($a) <=> power_of($b) $a <=> $b
Truth table50%2/4
A or B || C
A= points_of($a) <=> points_of($b)B= power_of($a) <=> power_of($b)C= $a <=> $b
ABCresult
1XX1
01X1
0011
0000
20 || power_of($a) <=> power_of($b)
21 || $a <=> $b } @$cards;
22 32868 return $c;
23 }
24  
25 8234 ▶sub _dearest {
Subroutine _dearest: called CC=3 SCAR=11.2
26 8234 my ($cards) = @_;
27 8234 ▶ my ($c) = sort { points_of($b) <=> points_of($a)
Condition66%4/6
points_of($b) <=> points_of($a) || power_of($b) <=> power_of($a) || $a <=> $b
l!l&&r!l&&!r
413839310
points_of($b) <=> points_of($a) or power_of($b) <=> power_of($a)
l!l&&r!l&&!r
413803931
MC/DC0%0/3
points_of($b) <=> points_of($a) || power_of($b) <=> power_of($a) || $a <=> $b
points_of($b) <=> points_of($a) power_of($b) <=> power_of($a) $a <=> $b
Truth table50%2/4
A or B || C
A= points_of($b) <=> points_of($a)B= power_of($b) <=> power_of($a)C= $a <=> $b
ABCresult
1XX1
01X1
0011
0000
28 || power_of($b) <=> power_of($a)
29 || $a <=> $b } @$cards;
30 8234 return $c;
31 }
32  
33 18953 ▶sub hand_strength {
Subroutine hand_strength: called CC=4 SCAR=13.9
Pod documented
34 18953 my (%o) = @_;
35 18953 my $trump = $o{trump};
36 18953 my $total = 0;
37 18953 for my $card (@{ $o{cards} }) {
38 101564 my $worth = points_of($card);
39 101564 ▶ $worth += 5 if suit_of($card) eq $trump;
Branch: 1 branch
BranchTrueFalseTotal
if suit_of($card) eq $trump 24763 76801 101564
40 101564 ▶ $worth += 3 if power_of($card) == 5;
Branch: 1 branch
BranchTrueFalseTotal
if power_of($card) == 5 22926 78638 101564
41 101564 $total += $worth;
42 }
43 18953 return $total;
44 }
45  
46 31661 ▶sub should_claim {
Subroutine should_claim: called CC=4 SCAR=14.5
Pod documented
47 31661 my (%o) = @_;
48 31661 ▶ return ($o{my_points} // 0) >= ($o{target} // 66) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($o{'my_points'} // 0) >= ($o{'target'} // 66) ? : 2425 29236 31661
Condition50%2/4
$o{"my_points"} // 0
l!l
316610
$o{"target"} // 66
l!l
316610
MC/DC0%0/1
$o{"my_points"} // 0
$o{"my_points"}
MC/DC0%0/1
$o{"target"} // 66
$o{"target"}
Truth table50%1/2
A
A= $o{"my_points"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"target"}
Aresult
11
00
49 }
50  
51 21865 ▶sub should_close {
Subroutine should_close: called CC=9 SCAR=22.5
Pod documented
52 21865 my (%o) = @_;
53 21865 ▶ return 0 if ($o{level} // 1) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if ($o{"level"} // 1) < 2 2917 18948 21865
Condition50%1/2
$o{"level"} // 1
l!l
218650
MC/DC0%0/1
$o{"level"} // 1
$o{"level"}
Truth table50%1/2
A
A= $o{"level"}
Aresult
11
00
54 18948 ▶ return 0 unless $o{talon_left};
Branch: 1 branch
BranchTrueFalseTotal
unless $o{"talon_left"} 0 18948 18948
55  
56 18948 ▶ my $target = $o{target} // 66;
Condition50%1/2
$o{"target"} // 66
l!l
189480
MC/DC0%0/1
$o{"target"} // 66
$o{"target"}
Truth table50%1/2
A
A= $o{"target"}
Aresult
11
00
57 18948 ▶ my $need = $target - ($o{my_points} // 0);
Condition50%1/2
$o{"my_points"} // 0
l!l
189480
MC/DC0%0/1
$o{"my_points"} // 0
$o{"my_points"}
Truth table50%1/2
A
A= $o{"my_points"}
Aresult
11
00
58 18948 ▶ return 0 if $need <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $need <= 0 0 18948 18948
59  
60 18948 my $trumps = scalar grep { suit_of($_) eq $o{trump} } @{ $o{cards} };
61 18948 my $reach = hand_strength(%o);
62 18948 ▶ return ($trumps >= 2 && $reach >= $need + 20) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$trumps >= 2 && $reach >= $need + 20 ? : 972 17976 18948
Condition100%3/3
$trumps >= 2 and $reach >= $need + 20
!ll&&!rl&&r
114206556972
MC/DC100%2/2
$trumps >= 2 and $reach >= $need + 20
$trumps >= 2 $reach >= $need + 20
Truth table100%3/3
A and B
A= $trumps >= 2B= $reach >= $need + 20
ABresult
0X0
100
111
63 }
64  
65 2861 ▶sub best_marriage {
Subroutine best_marriage: called CC=4 SCAR=14.0
Pod documented
66 2861 my (%o) = @_;
67 84 ▶ my @m = sort { $b->{value} <=> $a->{value} || $a->{suit} cmp $b->{suit} }
Condition66%2/3
$Game::Schnapsen::Search::b->{'value'} <=> $Game::Schnapsen::Search::a->{'value'} || $Game::Schnapsen::Search::a->{'suit'} cmp $Game::Schnapsen::Search::b->{'suit'}
l!l&&r!l&&!r
38460
MC/DC0%0/2
$Game::Schnapsen::Search::b->{'value'} <=> $Game::Schnapsen::Search::a->{'value'} || $Game::Schnapsen::Search::a->{'suit'} cmp $Game::Schnapsen::Search::b->{'suit'}
$Game::Schnapsen::Search::b->{'value'} <=> $Game::Schnapsen::Search::a->{'value'} $Game::Schnapsen::Search::a->{'suit'} cmp $Game::Schnapsen::Search::b->{'suit'}
Truth table66%2/3
A || B
A= $Game::Schnapsen::Search::b->{'value'} <=> $Game::Schnapsen::Search::a->{'value'}B= $Game::Schnapsen::Search::a->{'suit'} cmp $Game::Schnapsen::Search::b->{'suit'}
ABresult
1X1
011
000
68 2861 ▶ @{ $o{marriages} || [] };
Condition50%1/2
$o{'marriages'} || []
l!l
28610
MC/DC0%0/1
$o{'marriages'} || []
$o{'marriages'}
Truth table50%1/2
A
A= $o{'marriages'}
Aresult
11
00
69 2861 ▶ return @m ? $m[0]{suit} : undef;
Branch: 1 branch
BranchTrueFalseTotal
@m ? : 2860 1 2861
70 }
71  
72 23441 ▶sub best_follow {
Subroutine best_follow: called CC=5 SCAR=16.1
Pod documented
73 23441 my (%o) = @_;
74 23441 my $cards = $o{cards};
75 23441 ▶ return undef unless @$cards;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cards 0 23441 23441
76 23441 ▶ return $cards->[0] if @$cards == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$cards == 1 5220 18221 23441
77  
78 18221 my $led = $o{led};
79 18221 my $trump = $o{trump};
80  
81 18221 my @win = grep { winner_of($led, $_, $trump) == $_ } @$cards;
82 18221 my @lose = grep { winner_of($led, $_, $trump) != $_ } @$cards;
83  
84 18221 ▶ if (@win) {
Branch: 1 branch
BranchTrueFalseTotal
if (@win) 14834 3387 18221
85 14834 my @plain = grep { suit_of($_) ne $trump } @win;
86 14834 ▶ return _cheapest(@plain ? \@plain : \@win);
Branch: 1 branch
BranchTrueFalseTotal
@plain ? : 9704 5130 14834
87 }
88  
89 3387 return _cheapest(\@lose);
90 }
91  
92 23386 ▶sub best_lead {
Subroutine best_lead: called CC=10 SCAR=23.1
Pod documented
93 23386 my (%o) = @_;
94 23386 my $cards = $o{cards};
95 23386 ▶ return undef unless @$cards;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cards 0 23386 23386
96 23386 ▶ return $cards->[0] if @$cards == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$cards == 1 505 22881 23386
97  
98 22881 my $trump = $o{trump};
99 22881 ▶ my $level = $o{level} // 1;
Condition50%1/2
$o{"level"} // 1
l!l
228810
MC/DC0%0/1
$o{"level"} // 1
$o{"level"}
Truth table50%1/2
A
A= $o{"level"}
Aresult
11
00
100  
101 22881 ▶ return _cheapest($cards) if $level < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $level < 2 2875 20006 22881
102  
103 20006 my @plain = grep { suit_of($_) ne $trump } @$cards;
104  
105 20006 ▶ if (($o{phase} // 1) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (($o{"phase"} // 1) == 2) 8234 11772 20006
Condition50%1/2
$o{"phase"} // 1
l!l
200060
MC/DC0%0/1
$o{"phase"} // 1
$o{"phase"}
Truth table50%1/2
A
A= $o{"phase"}
Aresult
11
00
106 8234 my @high = grep { power_of($_) == 5 } @$cards;
107 8234 ▶ return _dearest(\@high) if @high;
Branch: 1 branch
BranchTrueFalseTotal
if @high 5069 3165 8234
108 3165 ▶ return _dearest(\@plain) if @plain;
Branch: 1 branch
BranchTrueFalseTotal
if @plain 2614 551 3165
109 551 return _dearest($cards);
110 }
111  
112 11772 ▶ return _cheapest(\@plain) if @plain;
Branch: 1 branch
BranchTrueFalseTotal
if @plain 11593 179 11772
113 179 return _cheapest($cards);
114 }
115  
116 9 1;
117  
118 __END__
« blib/lib/Game/Schnapsen/Deal.pm ↑ Summary blib/lib/Game/Schnapsen/Deck.pm »