| 1 | package Game::RoyalUr::Dice; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 63 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 63 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 63 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 63 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 63 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 63 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 63 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 63 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 63 | our @EXPORT_OK = qw(throw_for marked roll_of roll_for opening_for); | |||||||||||||||||||||||||||||||
| 15 | 63 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 520446 | ▶ | my $field = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 18 | 520446 | my ($variant, $name) = @_; | |||||||||||||||||||||||||||||||
| 19 | 520446 | my $value; | |||||||||||||||||||||||||||||||
| 20 | 520446 | ▶ | if (Scalar::Util::blessed($variant) && $variant->can($name)) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($variant) and $variant->can($name)
MC/DC50%1/2
builtin::blessed($variant) and $variant->can($name)
builtin::blessed($variant)
$variant->can($name)
Truth table66%2/3
A and B
A= builtin::blessed($variant)B= $variant->can($name)
| |||||||||||||||||||||||||||||||||
| 21 | 251810 | $value = $variant->$name; | |||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||
| 23 | elsif (ref $variant eq 'HASH') { | ||||||||||||||||||||||||||||||||
| 24 | 268635 | $value = $variant->{$name}; | |||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||
| 26 | else { | ||||||||||||||||||||||||||||||||
| 27 | 1 | Carp::croak("Game::RoyalUr::Dice: a rule set is a hash reference or an object with $name"); | |||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||
| 29 | 520445 | ▶ | Carp::croak("Game::RoyalUr::Dice: the rule set has no $name") unless defined $value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | 520443 | return $value; | |||||||||||||||||||||||||||||||
| 31 | 63 | }; | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 398893 | ▶ | sub throw_for { | ||||||||||||||||||||||||||||||
|
Subroutine
throw_for: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 34 | 398893 | my ($seed, $n, $dice) = @_; | |||||||||||||||||||||||||||||||
| 35 | 398893 | ▶ | Carp::croak('Game::RoyalUr::Dice: throw_for wants a seed') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $seed and length $seed
MC/DC100%2/2
defined $seed and length $seed
defined $seed
length $seed
Truth table100%3/3
A and B
A= defined $seedB= length $seed
| |||||||||||||||||||||||||||||||||
| 36 | unless defined $seed && length $seed; | ||||||||||||||||||||||||||||||||
| 37 | 398891 | ▶ | Carp::croak('Game::RoyalUr::Dice: a throw number is a non-negative integer') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $n and $n =~ /\A\d+\z/
MC/DC100%2/2
defined $n and $n =~ /\A\d+\z/
defined $n
$n =~ /\A\d+\z/
Truth table100%3/3
A and B
A= defined $nB= $n =~ /\A\d+\z/
| |||||||||||||||||||||||||||||||||
| 38 | unless defined $n && $n =~ /\A\d+\z/; | ||||||||||||||||||||||||||||||||
| 39 | 398888 | ▶ | Carp::croak('Game::RoyalUr::Dice: there are three dice or four') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $dice and $dice =~ /\A[34]\z/
MC/DC100%2/2
defined $dice and $dice =~ /\A[34]\z/
defined $dice
$dice =~ /\A[34]\z/
Truth table100%3/3
A and B
A= defined $diceB= $dice =~ /\A[34]\z/
| |||||||||||||||||||||||||||||||||
| 40 | unless defined $dice && $dice =~ /\A[34]\z/; | ||||||||||||||||||||||||||||||||
| 41 | 398885 | my $bytes = "$seed"; | |||||||||||||||||||||||||||||||
| 42 | 398885 | ▶ | Carp::croak('Game::RoyalUr::Dice: a seed is bytes, and that one holds a character above 255') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | unless utf8::downgrade($bytes, 1); | ||||||||||||||||||||||||||||||||
| 44 | 398884 | my $byte = unpack 'C', Digest::SHA::sha256($bytes . "roll:$n"); | |||||||||||||||||||||||||||||||
| 45 | 398884 | return [ map { ($byte >> $_) & 1 } 0 .. $dice - 1 ]; | |||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 421218 | ▶ | sub marked { | ||||||||||||||||||||||||||||||
|
Subroutine
marked: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 49 | 421218 | my ($throw) = @_; | |||||||||||||||||||||||||||||||
| 50 | 421218 | my $count = 0; | |||||||||||||||||||||||||||||||
| 51 | 421218 | $count += $_ for @$throw; | |||||||||||||||||||||||||||||||
| 52 | 421218 | return $count; | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 476444 | ▶ | sub roll_of { | ||||||||||||||||||||||||||||||
|
Subroutine
roll_of: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 56 | 476444 | my ($marked, $variant) = @_; | |||||||||||||||||||||||||||||||
| 57 | 476444 | ▶ | Carp::croak('Game::RoyalUr::Dice: a count of marked dice is 0 to 4') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $marked and $marked =~ /\A[0-4]\z/
MC/DC50%1/2
defined $marked and $marked =~ /\A[0-4]\z/
defined $marked
$marked =~ /\A[0-4]\z/
Truth table66%2/3
A and B
A= defined $markedB= $marked =~ /\A[0-4]\z/
| |||||||||||||||||||||||||||||||||
| 58 | unless defined $marked && $marked =~ /\A[0-4]\z/; | ||||||||||||||||||||||||||||||||
| 59 | 476443 | my $zero = $field->($variant, 'zero_rolls'); | |||||||||||||||||||||||||||||||
| 60 | 476441 | ▶ | Carp::croak('Game::RoyalUr::Dice: zero_rolls is 0 or 4') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | unless $zero =~ /\A[04]\z/; | ||||||||||||||||||||||||||||||||
| 62 | 476440 | ▶ | return $marked == 0 ? $zero + 0 : $marked + 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 44003 | ▶ | sub roll_for { | ||||||||||||||||||||||||||||||
|
Subroutine
roll_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 66 | 44003 | my ($seed, $n, $variant) = @_; | |||||||||||||||||||||||||||||||
| 67 | 44003 | my $throw = throw_for($seed, $n, $field->($variant, 'dice')); | |||||||||||||||||||||||||||||||
| 68 | 44002 | return roll_of(marked($throw), $variant); | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 745 | ▶ | sub opening_for { | ||||||||||||||||||||||||||||||
|
Subroutine
opening_for: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 72 | 745 | my ($seed, $dice) = @_; | |||||||||||||||||||||||||||||||
| 73 | 745 | my ($n, @throws) = (0); | |||||||||||||||||||||||||||||||
| 74 | 745 | ▶ | while ($n < 128) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 1030 | my $light = throw_for($seed, $n++, $dice); | |||||||||||||||||||||||||||||||
| 76 | 1030 | my $dark = throw_for($seed, $n++, $dice); | |||||||||||||||||||||||||||||||
| 77 | 1030 | push @throws, $light, $dark; | |||||||||||||||||||||||||||||||
| 78 | 1030 | my ($l, $d) = (marked($light), marked($dark)); | |||||||||||||||||||||||||||||||
| 79 | 1030 | ▶ | next if $l == $d; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | 745 | ▶ | return (($l > $d ? 'light' : 'dark'), $n, \@throws); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | 0 | Carp::croak('Game::RoyalUr::Dice: the opening throw tied 64 times, which is not chance'); | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 63 | 1; | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | __END__ | ||||||||||||||||||||||||||||||||