| 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.02'; | |||||||||||||||||||||||||||||||||
| 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
points_of($a) <=> points_of($b) or power_of($a) <=> power_of($b)
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
| |||||||||||||||||||||||||||||||||||
| 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
points_of($b) <=> points_of($a) or power_of($b) <=> power_of($a)
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 40 | 101564 | ▶ | $worth += 3 if power_of($card) == 5; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition50%2/4
$o{"my_points"} // 0
$o{"target"} // 66
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"}
Truth table50%1/2
A
A= $o{"target"}
| |||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$o{"level"} // 1
MC/DC0%0/1
$o{"level"} // 1
$o{"level"}
Truth table50%1/2
A
A= $o{"level"}
| |||||||||||||||||||||||||||||||||||
| 54 | 18948 | ▶ | return 0 unless $o{talon_left}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | 18948 | ▶ | my $target = $o{target} // 66; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{"target"} // 66
MC/DC0%0/1
$o{"target"} // 66
$o{"target"}
Truth table50%1/2
A
A= $o{"target"}
| |||||||||||||||||||||||||||||||||||
| 57 | 18948 | ▶ | my $need = $target - ($o{my_points} // 0); | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{"my_points"} // 0
MC/DC0%0/1
$o{"my_points"} // 0
$o{"my_points"}
Truth table50%1/2
A
A= $o{"my_points"}
| |||||||||||||||||||||||||||||||||||
| 58 | 18948 | ▶ | return 0 if $need <= 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$trumps >= 2 and $reach >= $need + 20
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
| |||||||||||||||||||||||||||||||||||
| 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'}
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'}
| |||||||||||||||||||||||||||||||||||
| 68 | 2861 | ▶ | @{ $o{marriages} || [] }; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'marriages'} || []
MC/DC0%0/1
$o{'marriages'} || []
$o{'marriages'}
Truth table50%1/2
A
A= $o{'marriages'}
| |||||||||||||||||||||||||||||||||||
| 69 | 2861 | ▶ | return @m ? $m[0]{suit} : undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 76 | 23441 | ▶ | return $cards->[0] if @$cards == 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 85 | 14834 | my @plain = grep { suit_of($_) ne $trump } @win; | |||||||||||||||||||||||||||||||||
| 86 | 14834 | ▶ | return _cheapest(@plain ? \@plain : \@win); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 96 | 23386 | ▶ | return $cards->[0] if @$cards == 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | 22881 | my $trump = $o{trump}; | |||||||||||||||||||||||||||||||||
| 99 | 22881 | ▶ | my $level = $o{level} // 1; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{"level"} // 1
MC/DC0%0/1
$o{"level"} // 1
$o{"level"}
Truth table50%1/2
A
A= $o{"level"}
| |||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | 22881 | ▶ | return _cheapest($cards) if $level < 2; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | 20006 | my @plain = grep { suit_of($_) ne $trump } @$cards; | |||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | 20006 | ▶ | if (($o{phase} // 1) == 2) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$o{"phase"} // 1
MC/DC0%0/1
$o{"phase"} // 1
$o{"phase"}
Truth table50%1/2
A
A= $o{"phase"}
| |||||||||||||||||||||||||||||||||||
| 106 | 8234 | my @high = grep { power_of($_) == 5 } @$cards; | |||||||||||||||||||||||||||||||||
| 107 | 8234 | ▶ | return _dearest(\@high) if @high; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 108 | 3165 | ▶ | return _dearest(\@plain) if @plain; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 109 | 551 | return _dearest($cards); | |||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 11772 | ▶ | return _cheapest(\@plain) if @plain; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 113 | 179 | return _cheapest($cards); | |||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | 9 | 1; | |||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||||||||