| 1 | package Game::Gin::Scoring; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Game::Gin::Card qw(deadwood_of); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Game::Gin::Meld qw(is_meld); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use Game::Gin::Deadwood qw(best); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 11 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||
| 13 | 11 | our @EXPORT_OK = qw(best_layoff settle match_result | |||||||||||||||||||||||||||||||
| 14 | GIN_BONUS UNDERCUT_BONUS BIG_GIN_BONUS | ||||||||||||||||||||||||||||||||
| 15 | TARGET BOX_BONUS GAME_BONUS); | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use constant GIN_BONUS => 25; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 11 | ▶ | use constant UNDERCUT_BONUS => 25; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 11 | ▶ | use constant BIG_GIN_BONUS => 31; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 11 | ▶ | use constant TARGET => 100; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | 11 | ▶ | use constant BOX_BONUS => 25; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | 11 | ▶ | use constant GAME_BONUS => 100; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 1944 | ▶ | sub best_layoff { | ||||||||||||||||||||||||||||||
|
Subroutine
best_layoff: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 26 | 1944 | my ($unmatched, $melds) = @_; | |||||||||||||||||||||||||||||||
| 27 | 1944 | ▶ | $unmatched ||= []; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$unmatched ||= []
MC/DC0%0/1
$unmatched ||= []
$unmatched
Truth table50%1/2
A
A= $unmatched
| |||||||||||||||||||||||||||||||||
| 28 | 1944 | ▶ | $melds ||= []; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$melds ||= []
MC/DC0%0/1
$melds ||= []
$melds
Truth table50%1/2
A
A= $melds
| |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 1944 | my $total = 0; | |||||||||||||||||||||||||||||||
| 31 | 1944 | $total += deadwood_of($_) for @$unmatched; | |||||||||||||||||||||||||||||||
| 32 | 1944 | ▶ | return { laid => [], melds => [ map { [@$_] } @$melds ], deadwood => $total } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@$unmatched and @$melds
MC/DC50%1/2
@$unmatched and @$melds
@$unmatched
@$melds
Truth table66%2/3
A and B
A= @$unmatchedB= @$melds
| |||||||||||||||||||||||||||||||||
| 33 | unless @$unmatched && @$melds; | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 1943 | my (@best_laid, $best_value, @best_melds); | |||||||||||||||||||||||||||||||
| 36 | 1943 | $best_value = 0; | |||||||||||||||||||||||||||||||
| 37 | 1943 | @best_melds = map { [@$_] } @$melds; | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 1943 | my $walk; | |||||||||||||||||||||||||||||||
| 40 | 17435 | ▶ | $walk = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 41 | 17435 | my ($i, $piles, $laid, $value) = @_; | |||||||||||||||||||||||||||||||
| 42 | 17435 | ▶ | if ($value > $best_value) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 1171 | $best_value = $value; | |||||||||||||||||||||||||||||||
| 44 | 1171 | @best_laid = @$laid; | |||||||||||||||||||||||||||||||
| 45 | 1171 | @best_melds = map { [@$_] } @$piles; | |||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | 17435 | ▶ | return if $i > $#$unmatched; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 14114 | my $card = $unmatched->[$i]; | |||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 14114 | $walk->($i + 1, $piles, $laid, $value); | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 14114 | for my $m (0 .. $#$piles) { | |||||||||||||||||||||||||||||||
| 54 | 31545 | my @grown = (@{ $piles->[$m] }, $card); | |||||||||||||||||||||||||||||||
| 55 | 31545 | ▶ | next unless is_meld(\@grown); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | 1378 | my @next = map { [@$_] } @$piles; | |||||||||||||||||||||||||||||||
| 57 | 1378 | $next[$m] = \@grown; | |||||||||||||||||||||||||||||||
| 58 | 1378 | $walk->($i + 1, \@next, [ @$laid, $card ], $value + deadwood_of($card)); | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | 14114 | return; | |||||||||||||||||||||||||||||||
| 61 | 1943 | }; | |||||||||||||||||||||||||||||||
| 62 | 1943 | $walk->(0, [ map { [@$_] } @$melds ], [], 0); | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | return { | ||||||||||||||||||||||||||||||||
| 65 | 1943 | laid => \@best_laid, | |||||||||||||||||||||||||||||||
| 66 | melds => \@best_melds, | ||||||||||||||||||||||||||||||||
| 67 | deadwood => $total - $best_value, | ||||||||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 2027 | ▶ | sub settle { | ||||||||||||||||||||||||||||||
|
Subroutine
settle: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 72 | 2027 | my (%o) = @_; | |||||||||||||||||||||||||||||||
| 73 | 2027 | my ($knocker, $defender) = @o{qw(knocker defender)}; | |||||||||||||||||||||||||||||||
| 74 | 2027 | ▶ | my $k_dead = $o{knocker_deadwood} // 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{"knocker_deadwood"} // 0
MC/DC0%0/1
$o{"knocker_deadwood"} // 0
$o{"knocker_deadwood"}
Truth table50%1/2
A
A= $o{"knocker_deadwood"}
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 2027 | ▶ | my $d_best = best($o{defender_cards} || []); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'defender_cards'} || []
MC/DC0%0/1
$o{'defender_cards'} || []
$o{'defender_cards'}
Truth table50%1/2
A
A= $o{'defender_cards'}
| |||||||||||||||||||||||||||||||||
| 77 | 2027 | my $d_dead = $d_best->{deadwood}; | |||||||||||||||||||||||||||||||
| 78 | 2027 | my @laid; | |||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 2027 | ▶ | unless ($o{gin}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | 1940 | ▶ | my $lay = best_layoff($d_best->{unmatched}, $o{knocker_melds} || []); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'knocker_melds'} || []
MC/DC0%0/1
$o{'knocker_melds'} || []
$o{'knocker_melds'}
Truth table50%1/2
A
A= $o{'knocker_melds'}
| |||||||||||||||||||||||||||||||||
| 82 | 1940 | $d_dead = $lay->{deadwood}; | |||||||||||||||||||||||||||||||
| 83 | 1940 | @laid = @{ $lay->{laid} }; | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 2027 | ▶ | if ($o{big_gin}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | 7 | return { winner => $knocker, loser => $defender, kind => 'big_gin', | |||||||||||||||||||||||||||||||
| 88 | points => BIG_GIN_BONUS + $d_dead, | ||||||||||||||||||||||||||||||||
| 89 | knocker_deadwood => 0, defender_deadwood => $d_dead, laid_off => [] }; | ||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | 2020 | ▶ | if ($o{gin}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 80 | return { winner => $knocker, loser => $defender, kind => 'gin', | |||||||||||||||||||||||||||||||
| 93 | points => GIN_BONUS + $d_dead, | ||||||||||||||||||||||||||||||||
| 94 | knocker_deadwood => 0, defender_deadwood => $d_dead, laid_off => [] }; | ||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 1940 | ▶ | if ($d_dead <= $k_dead) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | 136 | return { winner => $defender, loser => $knocker, kind => 'undercut', | |||||||||||||||||||||||||||||||
| 99 | points => UNDERCUT_BONUS + ($k_dead - $d_dead), | ||||||||||||||||||||||||||||||||
| 100 | knocker_deadwood => $k_dead, defender_deadwood => $d_dead, | ||||||||||||||||||||||||||||||||
| 101 | laid_off => \@laid }; | ||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 1804 | return { winner => $knocker, loser => $defender, kind => 'knock', | |||||||||||||||||||||||||||||||
| 105 | points => $d_dead - $k_dead, | ||||||||||||||||||||||||||||||||
| 106 | knocker_deadwood => $k_dead, defender_deadwood => $d_dead, | ||||||||||||||||||||||||||||||||
| 107 | laid_off => \@laid }; | ||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 204 | ▶ | sub match_result { | ||||||||||||||||||||||||||||||
|
Subroutine
match_result: called CC=19 SCAR=29.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 111 | 204 | my (%o) = @_; | |||||||||||||||||||||||||||||||
| 112 | 204 | ▶ | my @hands = @{ $o{hands} || [] }; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'hands'} || []
MC/DC0%0/1
$o{'hands'} || []
$o{'hands'}
Truth table50%1/2
A
A= $o{'hands'}
| |||||||||||||||||||||||||||||||||
| 113 | 204 | ▶ | my $target = $o{target} // TARGET; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$o{"target"} // 100
MC/DC100%1/1
$o{"target"} // 100
$o{"target"}
Truth table100%2/2
A
A= $o{"target"}
| |||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 204 | my (%points, %won, $winner); | |||||||||||||||||||||||||||||||
| 116 | 204 | for my $h (@hands) { | |||||||||||||||||||||||||||||||
| 117 | 2063 | ▶ | my $seat = $h->{winner} or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | 2044 | $points{$seat} += $h->{points}; | |||||||||||||||||||||||||||||||
| 119 | 2044 | $won{$seat}++; | |||||||||||||||||||||||||||||||
| 120 | 2044 | ▶ | $winner = $seat if !$winner && $points{$seat} >= $target; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $winner and $points{$seat} >= $target
MC/DC50%1/2
not $winner and $points{$seat} >= $target
not $winner
$points{$seat} >= $target
Truth table66%2/3
not(A) and B
A= not $winnerB= $points{$seat} >= $target
| |||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 204 | my @seats = sort keys %{ { map { $_ => 1 } (keys %points, keys %won, 'p1', 'p2') } }; | |||||||||||||||||||||||||||||||
| 124 | 204 | ▶ | my $loser = $winner ? ($winner eq 'p1' ? 'p2' : 'p1') : undef; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 204 | ▶ | my $shutout = ($winner && !($won{$loser} || 0)) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
$winner and not $won{$loser} || 0
$won{$loser} || 0
MC/DC100%2/2
$winner and not $won{$loser} || 0
$winner
$won{$loser}
Truth table100%3/3
A and not(B)
A= $winnerB= $won{$loser}
| |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 204 | my %totals; | |||||||||||||||||||||||||||||||
| 129 | 204 | for my $seat (@seats) { | |||||||||||||||||||||||||||||||
| 130 | 408 | ▶ | my $p = $points{$seat} || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$points{$seat} || 0
MC/DC100%1/1
$points{$seat} || 0
$points{$seat}
Truth table100%2/2
A
A= $points{$seat}
| |||||||||||||||||||||||||||||||||
| 131 | 408 | ▶ | $p *= 2 if $shutout; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | 408 | ▶ | $totals{$seat} = $p + BOX_BONUS * ($won{$seat} || 0); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$won{$seat} || 0
MC/DC100%1/1
$won{$seat} || 0
$won{$seat}
Truth table100%2/2
A
A= $won{$seat}
| |||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | 204 | ▶ | $totals{$winner} += GAME_BONUS if $winner; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | return { | ||||||||||||||||||||||||||||||||
| 137 | winner => $winner, | ||||||||||||||||||||||||||||||||
| 138 | loser => $loser, | ||||||||||||||||||||||||||||||||
| 139 | 408 | ▶ | hand_points => { map { $_ => ($points{$_} || 0) } @seats }, | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$points{$_} || 0
MC/DC100%1/1
$points{$_} || 0
$points{$_}
Truth table100%2/2
A
A= $points{$_}
| |||||||||||||||||||||||||||||||||
| 140 | 204 | ▶ | hands_won => { map { $_ => ($won{$_} || 0) } @seats }, | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$won{$_} || 0
MC/DC100%1/1
$won{$_} || 0
$won{$_}
Truth table100%2/2
A
A= $won{$_}
| |||||||||||||||||||||||||||||||||
| 141 | shutout => $shutout, | ||||||||||||||||||||||||||||||||
| 142 | totals => \%totals, | ||||||||||||||||||||||||||||||||
| 143 | target => $target, | ||||||||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 11 | 1; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||