| 1 | package Game::Dominoes::Notation; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Game::Dominoes::Tile; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use Game::Dominoes::Layout; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 13 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||
| 12 | 13 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||
| 13 | tile_text parse_tile | ||||||||||||||||||||||||||||||||
| 14 | move_text parse_move | ||||||||||||||||||||||||||||||||
| 15 | to_text from_text to_layout | ||||||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | sub tile_text { | ||||||||||||||||||||||||||||||
|
Subroutine
tile_text: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 19 | 1 | my ($tile) = @_; | |||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | die 'Game::Dominoes::Notation: tile_text wants a tile' unless ref $tile; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 21 | 1 | return $tile->stringify; | |||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 46 | ▶ | sub parse_tile { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_tile: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 25 | 46 | my ($text) = @_; | |||||||||||||||||||||||||||||||
| 26 | 46 | ▶ | die 'Game::Dominoes::Notation: not a tile: ' | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $text and $text =~ /\A([0-6])-([0-6])\z/
MC/DC50%1/2
defined $text and $text =~ /\A([0-6])-([0-6])\z/
defined $text
$text =~ /\A([0-6])-([0-6])\z/
Truth table66%2/3
A and B
A= defined $textB= $text =~ /\A([0-6])-([0-6])\z/
| |||||||||||||||||||||||||||||||||
| 27 | . (defined $text ? "'$text'" : 'undef') | ||||||||||||||||||||||||||||||||
| 28 | unless defined $text && $text =~ /\A([0-6])-([0-6])\z/; | ||||||||||||||||||||||||||||||||
| 29 | 44 | return Game::Dominoes::Tile->of($1, $2); | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 149 | ▶ | sub move_text { | ||||||||||||||||||||||||||||||
|
Subroutine
move_text: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 33 | 149 | my ($move) = @_; | |||||||||||||||||||||||||||||||
| 34 | 149 | ▶ | die 'Game::Dominoes::Notation: move_text wants a move' unless ref $move; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 149 | ▶ | if (ref $move ne 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 93 | my $played = $move->tile->stringify . '@' . $move->arm; | |||||||||||||||||||||||||||||||
| 38 | 93 | ▶ | $played .= '*' if $move->spinner; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | 93 | return $played; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 56 | ▶ | my $kind = $move->{kind} || 'play'; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$move->{'kind'} || 'play'
MC/DC100%1/1
$move->{'kind'} || 'play'
$move->{'kind'}
Truth table100%2/2
A
A= $move->{'kind'}
| |||||||||||||||||||||||||||||||||
| 43 | 56 | ▶ | return 'P' if $kind eq 'pass'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | 48 | ▶ | return '|' if $kind eq 'hand_end'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 45 | 40 | ▶ | if ($kind eq 'draw') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 22 | ▶ | my $n = $move->{count} || 1; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$move->{'count'} || 1
MC/DC100%1/1
$move->{'count'} || 1
$move->{'count'}
Truth table100%2/2
A
A= $move->{'count'}
| |||||||||||||||||||||||||||||||||
| 47 | 22 | ▶ | return $n > 1 ? "D$n" : 'D'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | 18 | my $text = $move->{tile}->stringify . '@' . $move->{arm}; | |||||||||||||||||||||||||||||||
| 50 | 18 | ▶ | $text .= '*' if $move->{spinner}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 18 | return $text; | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 60 | ▶ | sub parse_move { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_move: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 55 | 60 | my ($token) = @_; | |||||||||||||||||||||||||||||||
| 56 | 60 | ▶ | die 'Game::Dominoes::Notation: empty move' unless defined $token && length $token; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $token and length $token
MC/DC100%2/2
defined $token and length $token
defined $token
length $token
Truth table100%3/3
A and B
A= defined $tokenB= length $token
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 58 | ▶ | return { kind => 'pass' } if $token eq 'P'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 59 | 53 | ▶ | return { kind => 'hand_end' } if $token eq '|'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | 50 | ▶ | if ($token =~ /\AD(\d*)\z/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 7 | ▶ | my $n = length $1 ? $1 + 0 : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | 7 | ▶ | die "Game::Dominoes::Notation: a draw of $n is not a draw" if $n < 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | 6 | return { kind => 'draw', count => $n }; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | 43 | ▶ | if ($token =~ /\A([0-6]-[0-6])\@([LRUD])(\*?)\z/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | return { | ||||||||||||||||||||||||||||||||
| 67 | 39 | ▶ | kind => 'play', | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | tile => parse_tile($1), | ||||||||||||||||||||||||||||||||
| 69 | arm => $2, | ||||||||||||||||||||||||||||||||
| 70 | spinner => $3 ? 1 : 0, | ||||||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | 4 | die "Game::Dominoes::Notation: not a move: '$token'"; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 7 | ▶ | sub to_text { | ||||||||||||||||||||||||||||||
|
Subroutine
to_text: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 77 | 7 | my ($moves) = @_; | |||||||||||||||||||||||||||||||
| 78 | 7 | ▶ | die 'Game::Dominoes::Notation: to_text wants an arrayref' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | unless ref $moves eq 'ARRAY'; | ||||||||||||||||||||||||||||||||
| 80 | 7 | return join ' ', map { move_text($_) } @$moves; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 8 | ▶ | sub from_text { | ||||||||||||||||||||||||||||||
|
Subroutine
from_text: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 84 | 8 | my ($text) = @_; | |||||||||||||||||||||||||||||||
| 85 | 8 | ▶ | return [] unless defined $text; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 7 | $text =~ s/\A\s+//; | |||||||||||||||||||||||||||||||
| 87 | 7 | $text =~ s/\s+\z//; | |||||||||||||||||||||||||||||||
| 88 | 7 | ▶ | return [] unless length $text; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | 5 | return [ map { parse_move($_) } split /\s+/, $text ]; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | sub to_layout { | ||||||||||||||||||||||||||||||
|
Subroutine
to_layout: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 93 | 1 | my ($text) = @_; | |||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | my $moves = ref $text eq 'ARRAY' ? $text : from_text($text); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 1 | my $layout = Game::Dominoes::Layout->new; | |||||||||||||||||||||||||||||||
| 96 | 1 | for my $move (@$moves) { | |||||||||||||||||||||||||||||||
| 97 | 6 | ▶ | next unless ($move->{kind} || 'play') eq 'play'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$move->{'kind'} || 'play'
MC/DC0%0/1
$move->{'kind'} || 'play'
$move->{'kind'}
Truth table50%1/2
A
A= $move->{'kind'}
| |||||||||||||||||||||||||||||||||
| 98 | 4 | $layout->place($move->{tile}, $move->{arm}); | |||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | 1 | return $layout; | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 13 | 1; | |||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||||||