| 1 | package Game::Mahjong::Result; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 34 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 34 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 34 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 34 | ▶ | use constant BASE => 8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 34 | ▶ | use constant SEATS => 4; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 34 | ▶ | use constant HANDS => 16; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 34 | ▶ | use constant ROUNDS => 4; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 180 | ▶ | sub settle { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
settle: called CC=18 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 180 | my (%o) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 180 | my @deltas = (0) x SEATS; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 180 | ▶ | my $by = $o{by} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{"by"} // ""
MC/DC0%0/1
$o{"by"} // ""
$o{"by"}
Truth table50%1/2
A
A= $o{"by"}
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 180 | ▶ | return \@deltas if $by eq 'exhausted'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 179 | my $winner = $o{winner}; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 179 | ▶ | die 'Game::Mahjong::Result: the winner is a seat 0 to 3' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $winner and $winner =~ /\A[0-3]\z/
MC/DC50%1/2
defined $winner and $winner =~ /\A[0-3]\z/
defined $winner
$winner =~ /\A[0-3]\z/
Truth table66%2/3
A and B
A= defined $winnerB= $winner =~ /\A[0-3]\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | unless defined $winner && $winner =~ /\A[0-3]\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 178 | my $points = $o{points}; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 178 | ▶ | die 'Game::Mahjong::Result: points are a whole number' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $points and $points =~ /\A\d+\z/
MC/DC50%1/2
defined $points and $points =~ /\A\d+\z/
defined $points
$points =~ /\A\d+\z/
Truth table66%2/3
A and B
A= defined $pointsB= $points =~ /\A\d+\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | unless defined $points && $points =~ /\A\d+\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 177 | ▶ | if ($by eq 'self') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 52 | for my $seat (0 .. SEATS - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 29 | 208 | ▶ | next if $seat == $winner; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 156 | $deltas[$seat] -= BASE + $points; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 156 | $deltas[$winner] += BASE + $points; | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 52 | return \@deltas; | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 125 | ▶ | if ($by eq 'discard' || $by eq 'rob') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$by eq "discard" or $by eq "rob"
MC/DC100%2/2
$by eq "discard" or $by eq "rob"
$by eq "discard"
$by eq "rob"
Truth table100%3/3
A or B
A= $by eq "discard"B= $by eq "rob"
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 124 | my $from = $o{from}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 124 | ▶ | die 'Game::Mahjong::Result: a win by discard names the discarder' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $from and $from =~ /\A[0-3]\z/ and $from != $winner
defined $from and $from =~ /\A[0-3]\z/
MC/DC66%2/3
defined $from and $from =~ /\A[0-3]\z/ and $from != $winner
defined $from
$from =~ /\A[0-3]\z/
$from != $winner
Truth table75%3/4
A and B and C
A= defined $fromB= $from =~ /\A[0-3]\z/C= $from != $winner
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | unless defined $from && $from =~ /\A[0-3]\z/ && $from != $winner; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 122 | for my $seat (0 .. SEATS - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 40 | 488 | ▶ | next if $seat == $winner; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 366 | ▶ | my $gives = $seat == $from ? BASE + $points : BASE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 366 | $deltas[$seat] -= $gives; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 366 | $deltas[$winner] += $gives; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 122 | return \@deltas; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | die "Game::Mahjong::Result: no such way to win '$by'"; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 33 | ▶ | sub places { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
places: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 33 | my ($totals) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 33 | ▶ | my @seats = sort { $totals->[$b] <=> $totals->[$a] || $a <=> $b } 0 .. $#$totals; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$totals->[$b] <=> $totals->[$a] || $a <=> $b
MC/DC0%0/2
$totals->[$b] <=> $totals->[$a] || $a <=> $b
$totals->[$b] <=> $totals->[$a]
$a <=> $b
Truth table66%2/3
A || B
A= $totals->[$b] <=> $totals->[$a]B= $a <=> $b
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 33 | my (@places, $place, $last); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 33 | for my $i (0 .. $#seats) { | |||||||||||||||||||||||||||||||||||||||||
| 55 | 132 | my $seat = $seats[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 132 | ▶ | if (!defined $last || $totals->[$seat] != $last) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $last or $totals->[$seat] != $last
MC/DC100%2/2
not defined $last or $totals->[$seat] != $last
not defined $last
$totals->[$seat] != $last
Truth table100%3/3
not(A) or B
A= not defined $lastB= $totals->[$seat] != $last
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 115 | $place = $i + 1; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 115 | $last = $totals->[$seat]; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 132 | $places[$seat] = $place; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 33 | return \@places; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 16 | ▶ | sub winner { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
winner: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 16 | my ($totals) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 16 | my $places = places($totals); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 16 | my @first = grep { $places->[$_] == 1 } 0 .. $#$places; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 16 | ▶ | return @first == 1 ? $first[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 235 | ▶ | sub finished { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
finished: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 235 | my ($hands_played) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 235 | ▶ | return $hands_played >= HANDS ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 34 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||||||||||||||||||||