blib/lib/Game/Oware/Notation.pm

filter: Statementstmt 100% 70 / 70 Branchbran 81.8% 18 / 22 Conditioncond 87.5% 14 / 16 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 11 / 11 Podpod 100% 8 / 8 totaltotal 92.6% 126 / 136 CC 20 SCAR 17.4
CC
20
Cov
94%
parse
9.1
text_to_board
8.0
board_to_text
3.0
CRAP 5.7
« blib/lib/Game/Oware/Bot.pm ↑ Summary blib/lib/Game/Oware/Rules.pm »
1 package Game::Oware::Notation;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use Game::Oware::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 23 our $VERSION = '0.04';
9  
10 23 my @LETTER = ('A' .. 'F', 'a' .. 'f');
11  
12 23 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 24795 ▶sub letter_of {
Subroutine letter_of: called CC=1 SCAR=0.0
Pod documented
17 24795 my ($class, $house) = @_;
18 24795 Game::Oware::Board->assert_house($house);
19 24794 return $LETTER[$house];
20 }
21  
22 2117 ▶sub index_of {
Subroutine index_of: called CC=3 SCAR=11.0
Pod documented
23 2117 my ($class, $letter) = @_;
24 die 'Game::Oware::Notation: a house is one of ABCDEFabcdef'
25 2117 ▶ unless defined $letter && exists $INDEX{$letter};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $letter and exists $INDEX{$letter} 6 2111 2117
Condition100%3/3
defined $letter and exists $INDEX{$letter}
!ll&&!rl&&r
152111
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}
ABresult
0X0
100
111
26 2111 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 488 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod documented
35 488 my ($class, $houses) = @_;
36 488 return join '', map { $class->letter_of($_) } @$houses;
37 }
38  
39 42 ▶sub parse {
Subroutine parse: called CC=9 SCAR=22.1
Pod documented
40 42 my ($class, $text, $first) = @_;
41 42 ▶ die 'Game::Oware::Notation: a transcript is a string'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 42 42
42 unless defined $text;
43 42 ▶ die 'Game::Oware::Notation: the first seat is p1 or p2'
Branch: 1 branch
BranchTrueFalseTotal
if defined $first and $first ne "p1" and $first ne "p2" 0 42 42
Condition83%5/6
defined $first and $first ne "p1" and $first ne "p2"
!ll&&!rl&&r
4020
defined $first and $first ne "p1"
!ll&&!rl&&r
8322
MC/DC0%0/3
defined $first and $first ne "p1" and $first ne "p2"
defined $first $first ne "p1" $first ne "p2"
Truth table75%3/4
A and B and C
A= defined $firstB= $first ne "p1"C= $first ne "p2"
ABCresult
0XX0
10X0
1100
1111
44 if defined $first && $first ne 'p1' && $first ne 'p2';
45  
46 42 $text =~ s/\s+//g;
47 42 ▶ return [] unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 8 34 42
48  
49 34 my @houses;
50 34 ▶ my $expect = $first // 'p1';
Condition100%2/2
$first // "p1"
l!l
277
MC/DC100%1/1
$first // "p1"
$first
Truth table100%2/2
A
A= $first
Aresult
11
00
51 34 for my $letter (split //, $text) {
52 1886 my $house = $class->index_of($letter);
53 1886 my $seat = Game::Oware::Board->owner_of($house);
54 1886 ▶ die "Game::Oware::Notation: $letter is ${seat}'s move, but it is "
Branch: 1 branch
BranchTrueFalseTotal
unless $seat eq $expect 4 1882 1886
55 . "$expect to play, and the seats alternate"
56 unless $seat eq $expect;
57 1882 push @houses, $house;
58 1882 $expect = Game::Oware::Board->other($expect);
59 }
60  
61 30 return \@houses;
62 }
63  
64 3 ▶sub board_to_text {
Subroutine board_to_text: called CC=3 SCAR=11.0
Pod documented
65 3 my ($class, $board) = @_;
66 3 ▶ die 'Game::Oware::Notation: a board is fourteen cells'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $board eq "ARRAY" and @$board == "Game::Oware::Board"->CELLS 1 2 3
Condition66%2/3
ref $board eq "ARRAY" and @$board == "Game::Oware::Board"->CELLS
!ll&&!rl&&r
012
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
ABresult
0X0
100
111
67 unless ref $board eq 'ARRAY' && @$board == Game::Oware::Board->CELLS;
68  
69 2 my @top = map { $board->[$_] } reverse 6 .. 11;
70 2 my @bottom = map { $board->[$_] } 0 .. 5;
71  
72 return join "\n",
73 12 ' ' . join(' ', map { sprintf '%2s', $_ } reverse 'a' .. 'f'),
74 12 ' ' . join(' ', map { sprintf '%2d', $_ } @top)
75 . ' [' . $board->[Game::Oware::Board->P2_STORE] . ']',
76 12 ' ' . join(' ', map { sprintf '%2d', $_ } @bottom)
77 . ' [' . $board->[Game::Oware::Board->P1_STORE] . ']',
78 2 ' ' . join(' ', map { sprintf '%2s', $_ } 'A' .. 'F');
79 }
80  
81 6 ▶sub text_to_board {
Subroutine text_to_board: called CC=8 SCAR=20.8
Pod documented
82 6 my ($class, $text) = @_;
83 6 ▶ die 'Game::Oware::Notation: a board is a string'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 6 6
84 unless defined $text;
85  
86 6 my @rows;
87 6 for my $line (split /\n/, $text) {
88 14 ▶ next unless $line =~ /\d/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\d/ 4 10 14
89 10 ▶ my ($counts, $store) = $line =~ /\A([^\[]*)(?:\[\s*(\d+)\s*\])?\s*\z/
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /\A([^\[]*)(?:\[\s*(\d+)\s*\])?\s*\z/ or die "Game::Oware::Notation: cannot read '${line}'" 10 0 10
90 or die "Game::Oware::Notation: cannot read '$line'";
91 10 my @counts = $counts =~ /(\d+)/g;
92 10 ▶ die "Game::Oware::Notation: a row is six houses, not "
Branch: 1 branch
BranchTrueFalseTotal
unless @counts == 6 1 9 10
93 . scalar(@counts) . " in '$line'"
94 unless @counts == 6;
95 9 ▶ push @rows, [ \@counts, $store || 0 ];
Condition100%2/2
$store || 0
l!l
45
MC/DC100%1/1
$store || 0
$store
Truth table100%2/2
A
A= $store
Aresult
11
00
96 }
97  
98 5 ▶ die 'Game::Oware::Notation: a board is two rows of houses'
Branch: 1 branch
BranchTrueFalseTotal
unless @rows == 2 1 4 5
99 unless @rows == 2;
100  
101 4 my ($top, $bottom) = @rows;
102 4 my @board;
103 4 @board[0 .. 5] = @{ $bottom->[0] };
104 4 @board[6 .. 11] = reverse @{ $top->[0] };
105 4 $board[ Game::Oware::Board->P1_STORE ] = $bottom->[1];
106 4 $board[ Game::Oware::Board->P2_STORE ] = $top->[1];
107  
108 4 return \@board;
109 }
110  
111 23 1;
112  
113 __END__
« blib/lib/Game/Oware/Bot.pm ↑ Summary blib/lib/Game/Oware/Rules.pm »