Reversi/Notation.pm

filter: Statementstmt 100% 79 / 79 Branchbran 96.4% 27 / 28 Conditioncond 75.0% 9 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 11 / 11 Podpod 100% 8 / 8 totaltotal 95.1% 138 / 145 CC 24 SCAR 17.2
CC
24
Cov
97%
text_to_board
9.0
parse
7.0
walk
6.0
CRAP 5.6
« Reversi/Result.pm ↑ Summary Reversi/Bot.pm »
1 package Game::Reversi::Notation;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Game::Reversi::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 our $VERSION = '0.03';
9  
10 11 my $PASS = '--';
11  
12 11 ▶sub square_to_text {
Subroutine square_to_text: called CC=1 SCAR=0.0
Pod documented
13 11 my ($class, $square) = @_;
14 11 return Game::Reversi::Board->name_of($square);
15 }
16  
17 228 ▶sub text_to_square {
Subroutine text_to_square: called CC=3 SCAR=11.0
Pod documented
18 228 my ($class, $text) = @_;
19 228 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 227 228
20 227 my ($file, $rank) = lc($text) =~ /\A\s*([a-h])([1-8])\s*\z/;
21 227 ▶ return undef unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 6 221 227
22 221 return Game::Reversi::Board->square_of($file, $rank);
23 }
24  
25 15 ▶sub parse {
Subroutine parse: called CC=7 SCAR=19.5
Pod documented
26 15 my ($class, $text) = @_;
27 15 ▶ return [] unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 1 14 15
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
0114
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
28  
29 14 my @squares;
30 14 my $rest = lc $text;
31 14 ▶ while (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 52 13 65
32 52 ▶ if ($rest =~ s/\A[\s,;.\-]+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A[\s,;.\-]+//) 12 40 52
33 12 next;
34 }
35 40 ▶ if ($rest =~ s/\A(pass|pa)\b//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A(pass|pa)\b//) 1 39 40
36 1 next;
37 }
38 39 ▶ if ($rest =~ s/\A([a-h])([1-8])//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A([a-h])([1-8])//) 38 1 39
39 38 push @squares, Game::Reversi::Board->square_of($1, $2);
40 38 next;
41 }
42 1 die "Game::Reversi::Notation: cannot read '$rest' as a transcript";
43 }
44 13 return \@squares;
45 }
46  
47 8 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod documented
48 8 my ($class, $squares) = @_;
49 8 return join '', map { Game::Reversi::Board->name_of($_) } @$squares;
50 }
51  
52 1 ▶sub render_with_passes {
Subroutine render_with_passes: called CC=1 SCAR=0.0
Pod documented
53 1 my ($class, $steps) = @_;
54 return join '', map {
55 1 ($PASS x $_->{passes}) . Game::Reversi::Board->name_of($_->{square})
56 1 } @$steps;
57 }
58  
59 5 ▶sub walk {
Subroutine walk: called CC=6 SCAR=17.9
Pod documented
60 5 my ($class, $board, $colour, $squares) = @_;
61 5 my $B = 'Game::Reversi::Board';
62 5 my @steps;
63 5 my $now = [ @$board ];
64  
65 5 for my $square (@$squares) {
66 9 my $passes = 0;
67 9 ▶ while (!$B->has_move($now, $colour)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($B->has_move($now, $colour)) 2 8 10
68 2 my $them = $B->other($colour);
69 2 ▶ die 'Game::Reversi::Notation: the transcript continues past the end '
Branch: 1 branch
BranchTrueFalseTotal
unless $B->has_move($now, $them) 1 1 2
70 . 'of the game, where neither side can move'
71 unless $B->has_move($now, $them);
72 1 $colour = $them;
73 1 $passes++;
74 }
75  
76 8 my @flips = $B->flips_for($now, $square, $colour);
77 8 ▶ die "Game::Reversi::Notation: $colour cannot play "
Branch: 1 branch
BranchTrueFalseTotal
unless @flips 2 6 8
Condition50%1/2
$B->name_of($square) // "?"
l!l
20
MC/DC0%0/1
$B->name_of($square) // "?"
$B->name_of($square)
Truth table50%1/2
A
A= $B->name_of($square)
Aresult
11
00
78 . ($B->name_of($square) // '?') . ' in this position'
79 unless @flips;
80  
81 6 $now = $B->apply($now, $square, $colour);
82 6 push @steps, {
83 colour => $colour,
84 square => $square,
85 flips => [ @flips ],
86 passes => $passes,
87 board => $now,
88 };
89 6 $colour = $B->other($colour);
90 }
91  
92 2 return \@steps;
93 }
94  
95 2 ▶sub board_to_text {
Subroutine board_to_text: called CC=3 SCAR=11.0
Pod documented
96 2 my ($class, $board) = @_;
97 2 my @lines;
98 2 for my $row (0 .. 7) {
99 16 ▶ push @lines, join '', map { $board->[ $row * 8 + $_ ] // '.' } 0 .. 7;
Condition100%2/2
$$board[$row * 8 + $_] // "."
l!l
8120
MC/DC100%1/1
$$board[$row * 8 + $_] // "."
$$board[$row * 8 + $_]
Truth table100%2/2
A
A= $$board[$row * 8 + $_]
Aresult
11
00
100 }
101 2 return join "\n", @lines;
102 }
103  
104 4 ▶sub text_to_board {
Subroutine text_to_board: called CC=9 SCAR=22.0
Pod documented
105 4 my ($class, $text) = @_;
106 4 ▶ my @lines = grep { length } map { my $l = $_; $l =~ s/\s+//g; $l }
Condition50%1/2
$text // ""
l!l
40
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
107 split /\n/, ($text // '');
108 4 ▶ die 'Game::Reversi::Notation: a board is eight lines of eight squares'
Branch: 1 branch
BranchTrueFalseTotal
unless @lines == 8 1 3 4
109 unless @lines == 8;
110  
111 3 my $board = Game::Reversi::Board->empty;
112 3 for my $row (0 .. 7) {
113 17 my @cells = split //, $lines[$row];
114 17 ▶ die 'Game::Reversi::Notation: a board is eight lines of eight squares'
Branch: 1 branch
BranchTrueFalseTotal
unless @cells == 8 0 17 17
115 unless @cells == 8;
116 17 for my $col (0 .. 7) {
117 129 my $c = $cells[$col];
118 129 ▶ next if $c eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "." 120 9 129
119 9 ▶ die "Game::Reversi::Notation: '$c' is not a disc"
Branch: 1 branch
BranchTrueFalseTotal
unless $c eq "b" or $c eq "w" 1 8 9
Condition100%3/3
$c eq "b" or $c eq "w"
l!l&&r!l&&!r
441
MC/DC100%2/2
$c eq "b" or $c eq "w"
$c eq "b" $c eq "w"
Truth table100%3/3
A or B
A= $c eq "b"B= $c eq "w"
ABresult
1X1
011
000
120 unless $c eq 'b' || $c eq 'w';
121 8 $board->[ $row * 8 + $col ] = $c;
122 }
123 }
124 2 return $board;
125 }
126  
127 11 1;
128  
129 __END__
« Reversi/Result.pm ↑ Summary Reversi/Bot.pm »