blib/lib/Game/Oware/Notation.pm

filter: Statementstmt 100% 69 / 69 Branchbran 85.0% 17 / 20 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 8 / 8 totaltotal 95.8% 116 / 121 CC 16 SCAR 14.8
CC
16
Cov
96%
text_to_board
8.0
parse
5.0
board_to_text
3.0
CRAP 4.4
« blib/lib/Game/Oware/Bot.pm ↑ Summary blib/lib/Game/Oware/Rules.pm »
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
BranchTrueFalseTotal
unless defined $letter and exists $INDEX{$letter} 11 258 269
Condition100%3/3
defined $letter and exists $INDEX{$letter}
!ll&&!rl&&r
110258
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 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
BranchTrueFalseTotal
unless defined $text 0 9 9
42 unless defined $text;
43  
44 9 $text =~ s/\s+//g;
45 9 ▶ return [] unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 1 8 9
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
BranchTrueFalseTotal
unless $seat eq $expect 3 32 35
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
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
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
BranchTrueFalseTotal
unless defined $text 0 6 6
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
BranchTrueFalseTotal
unless $line =~ /\d/ 4 10 14
87 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
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
BranchTrueFalseTotal
unless @counts == 6 1 9 10
91 . scalar(@counts) . " in '$line'"
92 unless @counts == 6;
93 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
94 }
95  
96 5 ▶ die 'Game::Oware::Notation: a board is two rows of houses'
Branch: 1 branch
BranchTrueFalseTotal
unless @rows == 2 1 4 5
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__
« blib/lib/Game/Oware/Bot.pm ↑ Summary blib/lib/Game/Oware/Rules.pm »