Dominoes/Set.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Dominoes.pm ↑ Summary
1 package Game::Dominoes::Set;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶use Game::Dominoes::Tile;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 our $VERSION = '0.04';
12 our @EXPORT_OK = qw(tiles tile_of order_for PIPS SIZE);
13  
14 use constant SIZE => 28;
15 use constant PIPS => 168;
16  
17 my @TILE;
18 $TILE[$_] = Game::Dominoes::Tile->from_id($_) for 1 .. SIZE;
19  
20 sub tiles {
21 return [ @TILE[ 1 .. SIZE ] ];
22 }
23  
24 sub tile_of {
25 my ($id) = @_;
26 die 'Game::Dominoes::Set: no tile has id ' . (defined $id ? $id : 'undef')
27 unless defined $id && $id =~ /\A\d+\z/ && $id >= 1 && $id <= SIZE;
28 return $TILE[$id];
29 }
30  
31 sub order_for {
32 my ($seed, $hand) = @_;
33 die 'order_for wants a 32-byte seed'
34 unless defined $seed && length $seed == 32;
35 die 'order_for wants a hand number from 1'
36 unless defined $hand && $hand =~ /\A[1-9]\d*\z/;
37  
38 my ($counter, @words) = (0);
39 my $next = sub {
40 unless (@words) {
41 my $digest = Digest::SHA::sha256($seed . "hand:$hand:" . $counter++);
42 @words = unpack 'N8', $digest;
43 }
44 return shift @words;
45 };
46  
47 my @order = (1 .. SIZE);
48 for (my $i = SIZE - 1; $i > 0; $i--) {
49 my $n = $i + 1;
50 my $limit = int(4294967296 / $n) * $n;
51 my $word;
52 do { $word = $next->() } while $word >= $limit;
53 my $j = $word % $n;
54 @order[$i, $j] = @order[$j, $i];
55 }
56 return \@order;
57 }
58  
59 1;
60  
61 __END__
« Dominoes.pm ↑ Summary