| 1 | package Game::Backgammon::Dice; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 7 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 10 | 7 | our @EXPORT_OK = qw(roll_for opening_for); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 8553 | ▶ | sub _faces { | ||||||||||||||||||||||||||
|
Subroutine
_faces: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 13 | 8553 | my ($seed, $n) = @_; | |||||||||||||||||||||||||||
| 14 | 8553 | my @out; | |||||||||||||||||||||||||||
| 15 | 8553 | my $counter = 0; | |||||||||||||||||||||||||||
| 16 | 8553 | ▶ | while (@out < 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 8553 | my $bytes = Digest::SHA::sha256($seed . "roll:$n:$counter"); | |||||||||||||||||||||||||||
| 18 | 8553 | for my $byte (unpack 'C*', $bytes) { | |||||||||||||||||||||||||||
| 19 | 17382 | ▶ | next if $byte >= 252; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 17106 | push @out, ($byte % 6) + 1; | |||||||||||||||||||||||||||
| 21 | 17106 | ▶ | last if @out == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 8553 | $counter++; | |||||||||||||||||||||||||||
| 24 | 8553 | ▶ | die 'Game::Backgammon::Dice: no usable bytes in 256 rounds' if $counter > 256; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 8553 | return @out; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 8553 | ▶ | sub roll_for { | ||||||||||||||||||||||||||
|
Subroutine
roll_for: called CC=5 SCAR=19.4
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 8553 | my ($seed, $n) = @_; | |||||||||||||||||||||||||||
| 31 | 8553 | ▶ | die 'Game::Backgammon::Dice: roll_for wants a seed' unless defined $seed && length $seed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $seed and length $seed
MC/DC0%0/2
defined $seed and length $seed
defined $seed
length $seed
Truth table33%1/3
A and B
A= defined $seedB= length $seed
| |||||||||||||||||||||||||||||
| 32 | 8553 | ▶ | die 'Game::Backgammon::Dice: a roll number is a non-negative integer' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $n and $n =~ /\A\d+\z/
MC/DC0%0/2
defined $n and $n =~ /\A\d+\z/
defined $n
$n =~ /\A\d+\z/
Truth table33%1/3
A and B
A= defined $nB= $n =~ /\A\d+\z/
| |||||||||||||||||||||||||||||
| 33 | unless defined $n && $n =~ /\A\d+\z/; | ||||||||||||||||||||||||||||
| 34 | 8553 | return _faces($seed, $n); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4848 | ▶ | sub dice_for { | ||||||||||||||||||||||||||
|
Subroutine
dice_for: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 4848 | my ($seed, $n) = @_; | |||||||||||||||||||||||||||
| 39 | 4848 | my ($a, $b) = roll_for($seed, $n); | |||||||||||||||||||||||||||
| 40 | 4848 | ▶ | return $a == $b ? ($a) x 4 : ($a, $b); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 294 | ▶ | sub opening_for { | ||||||||||||||||||||||||||
|
Subroutine
opening_for: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 294 | my ($seed) = @_; | |||||||||||||||||||||||||||
| 45 | 294 | my $n = 0; | |||||||||||||||||||||||||||
| 46 | 294 | ▶ | while ($n < 64) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 354 | my ($white, $black) = roll_for($seed, $n); | |||||||||||||||||||||||||||
| 48 | 354 | $n++; | |||||||||||||||||||||||||||
| 49 | 354 | ▶ | next if $white == $black; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 294 | ▶ | return ($white > $black ? 'white' : 'black'), [ $white, $black ], $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 0 | die 'Game::Backgammon::Dice: the opening roll tied 64 times, which is not chance'; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 7 | 1; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | __END__ | ||||||||||||||||||||||||||||