| 1 | package Game::Oware::Notation; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 20 | ▶ | use Game::Oware::Board; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 20 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 20 | my @LETTER = ('A' .. 'F', 'a' .. 'f'); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 20 | my %INDEX = map { $LETTER[$_] => $_ } 0 .. $#LETTER; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | sub letters { return @LETTER } | ||||||||||||||||||||||||||
|
Subroutine
letters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8056 | ▶ | sub letter_of { | ||||||||||||||||||||||||||
|
Subroutine
letter_of: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 8056 | my ($class, $house) = @_; | |||||||||||||||||||||||||||
| 18 | 8056 | Game::Oware::Board->assert_house($house); | |||||||||||||||||||||||||||
| 19 | 8055 | return $LETTER[$house]; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 269 | ▶ | sub index_of { | ||||||||||||||||||||||||||
|
Subroutine
index_of: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 269 | my ($class, $letter) = @_; | |||||||||||||||||||||||||||
| 24 | die 'Game::Oware::Notation: a house is one of ABCDEFabcdef' | ||||||||||||||||||||||||||||
| 25 | 269 | ▶ | unless defined $letter && exists $INDEX{$letter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $letter and exists $INDEX{$letter}
MC/DC100%2/2
defined $letter and exists $INDEX{$letter}
defined $letter
exists $INDEX{$letter}
Truth table100%3/3
A and B
A= defined $letterB= exists $INDEX{$letter}
| |||||||||||||||||||||||||||||
| 26 | 258 | return $INDEX{$letter}; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | sub seat_of { | ||||||||||||||||||||||||||
|
Subroutine
seat_of: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 4 | my ($class, $letter) = @_; | |||||||||||||||||||||||||||
| 31 | 4 | return Game::Oware::Board->owner_of($class->index_of($letter)); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 11 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 11 | my ($class, $houses) = @_; | |||||||||||||||||||||||||||
| 36 | 11 | return join '', map { $class->letter_of($_) } @$houses; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 9 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 9 | my ($class, $text) = @_; | |||||||||||||||||||||||||||
| 41 | 9 | ▶ | die 'Game::Oware::Notation: a transcript is a string' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | unless defined $text; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 9 | $text =~ s/\s+//g; | |||||||||||||||||||||||||||
| 45 | 9 | ▶ | return [] unless length $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 8 | my @houses; | |||||||||||||||||||||||||||
| 48 | 8 | my $expect = 'p1'; | |||||||||||||||||||||||||||
| 49 | 8 | for my $letter (split //, $text) { | |||||||||||||||||||||||||||
| 50 | 35 | my $house = $class->index_of($letter); | |||||||||||||||||||||||||||
| 51 | 35 | my $seat = Game::Oware::Board->owner_of($house); | |||||||||||||||||||||||||||
| 52 | 35 | ▶ | die "Game::Oware::Notation: $letter is ${seat}'s move, but it is " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | . "$expect to play, and the seats alternate" | ||||||||||||||||||||||||||||
| 54 | unless $seat eq $expect; | ||||||||||||||||||||||||||||
| 55 | 32 | push @houses, $house; | |||||||||||||||||||||||||||
| 56 | 32 | $expect = Game::Oware::Board->other($expect); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 5 | return \@houses; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | sub board_to_text { | ||||||||||||||||||||||||||
|
Subroutine
board_to_text: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 3 | my ($class, $board) = @_; | |||||||||||||||||||||||||||
| 64 | 3 | ▶ | die 'Game::Oware::Notation: a board is fourteen cells' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $board eq "ARRAY" and @$board == "Game::Oware::Board"->CELLS
MC/DC50%1/2
ref $board eq "ARRAY" and @$board == "Game::Oware::Board"->CELLS
ref $board eq "ARRAY"
@$board == "Game::Oware::Board"->CELLS
Truth table66%2/3
A and B
A= ref $board eq "ARRAY"B= @$board == "Game::Oware::Board"->CELLS
| |||||||||||||||||||||||||||||
| 65 | unless ref $board eq 'ARRAY' && @$board == Game::Oware::Board->CELLS; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 2 | my @top = map { $board->[$_] } reverse 6 .. 11; | |||||||||||||||||||||||||||
| 68 | 2 | my @bottom = map { $board->[$_] } 0 .. 5; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | return join "\n", | ||||||||||||||||||||||||||||
| 71 | 12 | ' ' . join(' ', map { sprintf '%2s', $_ } reverse 'a' .. 'f'), | |||||||||||||||||||||||||||
| 72 | 12 | ' ' . join(' ', map { sprintf '%2d', $_ } @top) | |||||||||||||||||||||||||||
| 73 | . ' [' . $board->[Game::Oware::Board->P2_STORE] . ']', | ||||||||||||||||||||||||||||
| 74 | 12 | ' ' . join(' ', map { sprintf '%2d', $_ } @bottom) | |||||||||||||||||||||||||||
| 75 | . ' [' . $board->[Game::Oware::Board->P1_STORE] . ']', | ||||||||||||||||||||||||||||
| 76 | 2 | ' ' . join(' ', map { sprintf '%2s', $_ } 'A' .. 'F'); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 6 | ▶ | sub text_to_board { | ||||||||||||||||||||||||||
|
Subroutine
text_to_board: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 6 | my ($class, $text) = @_; | |||||||||||||||||||||||||||
| 81 | 6 | ▶ | die 'Game::Oware::Notation: a board is a string' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | unless defined $text; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 6 | my @rows; | |||||||||||||||||||||||||||
| 85 | 6 | for my $line (split /\n/, $text) { | |||||||||||||||||||||||||||
| 86 | 14 | ▶ | next unless $line =~ /\d/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 10 | ▶ | my ($counts, $store) = $line =~ /\A([^\[]*)(?:\[\s*(\d+)\s*\])?\s*\z/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | or die "Game::Oware::Notation: cannot read '$line'"; | ||||||||||||||||||||||||||||
| 89 | 10 | my @counts = $counts =~ /(\d+)/g; | |||||||||||||||||||||||||||
| 90 | 10 | ▶ | die "Game::Oware::Notation: a row is six houses, not " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | . scalar(@counts) . " in '$line'" | ||||||||||||||||||||||||||||
| 92 | unless @counts == 6; | ||||||||||||||||||||||||||||
| 93 | 9 | ▶ | push @rows, [ \@counts, $store || 0 ]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$store || 0
MC/DC100%1/1
$store || 0
$store
Truth table100%2/2
A
A= $store
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 5 | ▶ | die 'Game::Oware::Notation: a board is two rows of houses' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | unless @rows == 2; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | my ($top, $bottom) = @rows; | |||||||||||||||||||||||||||
| 100 | 4 | my @board; | |||||||||||||||||||||||||||
| 101 | 4 | @board[0 .. 5] = @{ $bottom->[0] }; | |||||||||||||||||||||||||||
| 102 | 4 | @board[6 .. 11] = reverse @{ $top->[0] }; | |||||||||||||||||||||||||||
| 103 | 4 | $board[ Game::Oware::Board->P1_STORE ] = $bottom->[1]; | |||||||||||||||||||||||||||
| 104 | 4 | $board[ Game::Oware::Board->P2_STORE ] = $top->[1]; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 4 | return \@board; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 20 | 1; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||