lib/Games/Checkers/MoveLocationConstructor.pm

filter: Statementstmt 17.7% 16 / 90 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 31.2% 5 / 16 Podpod 0.0% 0 / 11 totaltotal 9.0% 21 / 232 CC 36 SCAR 36.8
CC
36
Cov
9%
add_dst
90.0
source
72.0
create_move
42.0
CRAP 39.7
« lib/Games/Checkers/CreateMoveList.pm ↑ Summary lib/Games/Checkers/Rules.pm »
1 # Games::Checkers, Copyright (C) 1996-2012 Mikhael Goikhman, migo@cpan.org
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 package Games::Checkers::MoveLocationConstructor;
20  
21 1 ▶use Games::Checkers::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Games::Checkers::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Games::Checkers::MoveConstants;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶sub new ($$$) {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my $class = shift;
27 0 my $board = shift;
28 0 my $color = shift;
29  
30 0 my $self = {
31 color => $color,
32 destin => [],
33 src => NL,
34 piece => 0,
35 must_beat => $board->can_color_beat($color),
36 orig_board => $board,
37 };
38 0 bless $self, $class;
39  
40 0 return $self;
41 }
42
43 0 ▶sub init_work_board ($) {
Subroutine init_work_board: not called CC=2 SCAR=17.9
Pod undocumented
44 0 my $self = shift;
45  
46 return $self->{work_board}
47 ? $self->{work_board}->copy($self->{orig_board})
48 0 ▶ : ($self->{work_board} = $self->{orig_board}->clone);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'work_board'} ? : 0 0 0
49 }
50  
51 0 ▶sub source ($$) {
Subroutine source: not called CC=8 SCAR=42.8
Pod undocumented
52 0 my $self = shift;
53 0 my $loc = shift;
54  
55 0 my $board = $self->{orig_board};
56 return Err
57 if $loc == NL
58 || !$board->occup($loc)
59 || $board->color($loc) != $self->{color}
60 || $self->{must_beat} && !$board->can_piece_beat($loc)
61 0 ▶ || !$self->{must_beat} && !$board->can_piece_step($loc);
Branch: 1 branch
BranchTrueFalseTotal
if $loc == &NL() or not $board->occup($loc) or $board->color($loc) != $self->{"color"} or $self->{"must_beat"} and not $board->can_piece_beat($loc) or not $self->{"must_beat"} || $board->can_piece_step($loc) 0 0 0
Condition0%0/18
$loc == &NL() or not $board->occup($loc) or $board->color($loc) != $self->{"color"} or $self->{"must_beat"} and not $board->can_piece_beat($loc) or not $self->{"must_beat"} || $board->can_piece_step($loc)
l!l&&r!l&&!r
000
$loc == &NL() or not $board->occup($loc) or $board->color($loc) != $self->{"color"} or $self->{"must_beat"} and not $board->can_piece_beat($loc)
l!l&&r!l&&!r
000
$loc == &NL() or not $board->occup($loc) or $board->color($loc) != $self->{"color"}
l!l&&r!l&&!r
000
$loc == &NL() or not $board->occup($loc)
l!l&&r!l&&!r
000
$self->{"must_beat"} and not $board->can_piece_beat($loc)
!ll&&!rl&&r
000
$self->{'must_beat'} || $board->can_piece_step($loc)
l!l&&r!l&&!r
000
MC/DC0%0/7
$loc == &NL() or not $board->occup($loc) or $board->color($loc) != $self->{"color"} or $self->{"must_beat"} and not $board->can_piece_beat($loc) or not $self->{"must_beat"} || $board->can_piece_step($loc)
$loc == &NL() not $board->occup($loc) $board->color($loc) != $self->{"color"} $self->{"must_beat"} not $board->can_piece_beat($loc) $self->{'must_beat'} $board->can_piece_step($loc)
Truth table0%0/10
A or not(B) or C or D and not(E) or not(F || G)
A= $loc == &NL()B= not $board->occup($loc)C= $board->color($loc) != $self->{"color"}D= $self->{"must_beat"}E= not $board->can_piece_beat($loc)F= $self->{'must_beat'}G= $board->can_piece_step($loc)
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
00011XX1
0000X001
00010001
0000X1X0
0000X010
000101X0
00010010
62  
63 0 $self->{piece} = $board->piece($self->{src} = $loc);
64 0 $self->{destin} = [];
65 0 $self->init_work_board;
66  
67 0 return Ok;
68 }
69  
70 0 ▶sub add_dst ($$) {
Subroutine add_dst: not called CC=9 SCAR=45.0
Pod undocumented
71 0 my $self = shift;
72 0 my $dst = shift;
73  
74 0 ▶ return Err if $self->{src} == NL || @{$self->{destin}} == 100;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"src"} == &NL() or @{$self->{"destin"};} == 100 0 0 0
Condition0%0/3
$self->{"src"} == &NL() or @{$self->{"destin"};} == 100
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"src"} == &NL() or @{$self->{"destin"};} == 100
$self->{"src"} == &NL() @{$self->{"destin"};} == 100
Truth table0%0/3
A or B
A= $self->{"src"} == &NL()B= @{$self->{"destin"};} == 100
ABresult
1X1
011
000
75  
76 0 ▶ my $board = $self->{work_board} or die "Internal";
Branch: 1 branch
BranchTrueFalseTotal
my $board = $self->{"work_board"} or die "Internal" 0 0 0
77 0 ▶ if ($self->{must_beat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'must_beat'}) { } 0 0 0
78 0 ▶ die "Internal" unless $board->occup($self->dst_1);
Branch: 1 branch
BranchTrueFalseTotal
unless $board->occup($self->dst_1) 0 0 0
79 0 ▶ return Err unless $board->can_piece_beat($self->dst_1, $dst);
Branch: 1 branch
BranchTrueFalseTotal
unless $board->can_piece_beat($self->dst_1, $dst) 0 0 0
80 } else {
81 0 ▶ return Err if @{$self->{destin}} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"destin"};} > 0 0 0 0
82 0 ▶ return Err unless $board->can_piece_step($self->{src}, $dst);
Branch: 1 branch
BranchTrueFalseTotal
unless $board->can_piece_step($self->{"src"}, $dst) 0 0 0
83 }
84 0 push @{$self->{destin}}, $dst;
85 0 $self->apply_last_dst;
86  
87 0 return Ok;
88 }
89  
90 0 ▶sub del_dst ($) {
Subroutine del_dst: not called CC=3 SCAR=24.8
Pod undocumented
91 0 my $self = shift;
92 0 ▶ return NL if $self->{src} == NL || @{$self->{destin}} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"src"} == &NL() or @{$self->{"destin"};} == 0 0 0 0
Condition0%0/3
$self->{"src"} == &NL() or @{$self->{"destin"};} == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"src"} == &NL() or @{$self->{"destin"};} == 0
$self->{"src"} == &NL() @{$self->{"destin"};} == 0
Truth table0%0/3
A or B
A= $self->{"src"} == &NL()B= @{$self->{"destin"};} == 0
ABresult
1X1
011
000
93 0 my $dst = pop @{$self->{destin}};
94 0 $self->reapply_all;
95 0 return $dst;
96 }
97  
98 0 ▶sub can_create_move ($) {
Subroutine can_create_move: not called CC=5 SCAR=34.0
Pod undocumented
99 0 my $self = shift;
100 0 ▶ return $self->{must_beat} && @{$self->{destin}} > 0
Condition0%0/3
not $self->{"must_beat"} and @{$self->{"destin"};} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"must_beat"} and @{$self->{"destin"};} == 1
not $self->{"must_beat"} @{$self->{"destin"};} == 1
Truth table0%0/3
not(A) and B
A= not $self->{"must_beat"}B= @{$self->{"destin"};} == 1
ABresult
0X0
100
111
101 && $self->{work_board}->can_piece_beat($self->dst_1) == No
102 0 ▶ || !$self->{must_beat} && @{$self->{destin}} == 1;
Condition0%0/9
$self->{'must_beat'} && @{$self->{'destin'};} > 0 && $self->{'work_board'}->can_piece_beat($self->dst_1) == &No() || !$self->{'must_beat'} && @{$self->{'destin'};} == 1
l!l&&r!l&&!r
000
$self->{"must_beat"} and @{$self->{"destin"};} > 0 and $self->{"work_board"}->can_piece_beat($self->dst_1) == &No()
!ll&&!rl&&r
000
$self->{"must_beat"} and @{$self->{"destin"};} > 0
!ll&&!rl&&r
000
MC/DC0%0/4
$self->{'must_beat'} && @{$self->{'destin'};} > 0 && $self->{'work_board'}->can_piece_beat($self->dst_1) == &No() || !$self->{'must_beat'} && @{$self->{'destin'};} == 1
$self->{"must_beat"} @{$self->{"destin"};} > 0 $self->{"work_board"}->can_piece_beat($self->dst_1) == &No() !$self->{'must_beat'} && @{$self->{'destin'};} == 1
Truth table0%0/7
A and B and C || D
A= $self->{"must_beat"}B= @{$self->{"destin"};} > 0C= $self->{"work_board"}->can_piece_beat($self->dst_1) == &No()D= !$self->{'must_beat'} && @{$self->{'destin'};} == 1
ABCDresult
111X1
0XX11
10X11
11011
0XX00
10X00
11000
103 }
104  
105 0 ▶sub create_move ($) {
Subroutine create_move: not called CC=6 SCAR=37.4
Pod undocumented
106 0 my $self = shift;
107 return NO_MOVE if $self->{src} == NL
108 0 ▶ || $self->{must_beat} && @{$self->{destin}} < 1
Condition0%0/3
not $self->{"must_beat"} and @{$self->{"destin"};} != 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"must_beat"} and @{$self->{"destin"};} != 1
not $self->{"must_beat"} @{$self->{"destin"};} != 1
Truth table0%0/3
not(A) and B
A= not $self->{"must_beat"}B= @{$self->{"destin"};} != 1
ABresult
0X0
100
111
109 0 ▶ || !$self->{must_beat} && @{$self->{destin}} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"src"} == &NL() or $self->{"must_beat"} and @{$self->{"destin"};} < 1 or not $self->{"must_beat"} and @{$self->{"destin"};} != 1 0 0 0
Condition0%0/9
$self->{"src"} == &NL() or $self->{"must_beat"} and @{$self->{"destin"};} < 1 or not $self->{"must_beat"} and @{$self->{"destin"};} != 1
l!l&&r!l&&!r
000
$self->{"src"} == &NL() or $self->{"must_beat"} and @{$self->{"destin"};} < 1
l!l&&r!l&&!r
000
$self->{"must_beat"} and @{$self->{"destin"};} < 1
!ll&&!rl&&r
000
MC/DC0%0/4
$self->{"src"} == &NL() or $self->{"must_beat"} and @{$self->{"destin"};} < 1 or not $self->{"must_beat"} and @{$self->{"destin"};} != 1
$self->{"src"} == &NL() $self->{"must_beat"} @{$self->{"destin"};} < 1 not $self->{"must_beat"} and @{$self->{"destin"};} != 1
Truth table0%0/6
A or B and C or D
A= $self->{"src"} == &NL()B= $self->{"must_beat"}C= @{$self->{"destin"};} < 1D= not $self->{"must_beat"} and @{$self->{"destin"};} != 1
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
110 return new Games::Checkers::Move(
111 0 $self->{must_beat}, $self->{src}, $self->{destin});
112 }
113  
114 0 ▶sub apply_last_dst ($) {
Subroutine apply_last_dst: not called CC=4 SCAR=30.0
Pod undocumented
115 0 my $self = shift;
116  
117 0 my $board = $self->{work_board};
118 0 my $src = $self->dst_2;
119 0 my $dst = $self->dst_1;
120 0 $board->clr($src);
121 0 $board->set($dst, $self->{color}, $self->{piece});
122 0 ▶ $board->clr($board->enclosed_figure($src, $dst)) if $self->{must_beat};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"must_beat"} 0 0 0
123 0 ▶ if ($self->{piece} == Pawn && $board->is_crowning->[$self->{color}][$dst]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"piece"} == &Pawn() and $board->is_crowning->[$self->{"color"}][$dst]) 0 0 0
Condition0%0/3
$self->{"piece"} == &Pawn() and $board->is_crowning->[$self->{"color"}][$dst]
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"piece"} == &Pawn() and $board->is_crowning->[$self->{"color"}][$dst]
$self->{"piece"} == &Pawn() $board->is_crowning->[$self->{"color"}][$dst]
Truth table0%0/3
A and B
A= $self->{"piece"} == &Pawn()B= $board->is_crowning->[$self->{"color"}][$dst]
ABresult
0X0
100
111
124 0 $board->cnv($dst);
125 0 $self->{piece} ^= 1;
126 }
127 }
128  
129 0 ▶sub reapply_all ($) {
Subroutine reapply_all: not called CC=4 SCAR=30.0
Pod undocumented
130 0 my $self = shift;
131  
132 0 my $board = $self->init_work_board;
133 0 ▶ return if $self->{src} == NL || @{$self->{destin}} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"src"} == &NL() or @{$self->{"destin"};} == 0 0 0 0
Condition0%0/3
$self->{"src"} == &NL() or @{$self->{"destin"};} == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"src"} == &NL() or @{$self->{"destin"};} == 0
$self->{"src"} == &NL() @{$self->{"destin"};} == 0
Truth table0%0/3
A or B
A= $self->{"src"} == &NL()B= @{$self->{"destin"};} == 0
ABresult
1X1
011
000
134  
135 0 $self->{piece} = $board->piece($self->{src});
136 0 my $destin = $self->{destin};
137 0 $self->{destin} = [];
138 0 ▶ while (@$destin) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$destin) 0 0 0
139 0 push @{$self->{destin}}, shift @$destin;
140 0 $self->apply_last_dst;
141 }
142 }
143  
144 0 ▶sub dst_1 ($) {
Subroutine dst_1: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my $self = shift;
146 0 ▶ return @{$self->{destin}} == 0 ? $self->{src} : $self->{destin}->[-1];
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'destin'};} == 0 ? : 0 0 0
147 }
148  
149 0 ▶sub dst_2 ($) {
Subroutine dst_2: not called CC=2 SCAR=17.9
Pod undocumented
150 0 my $self = shift;
151 0 ▶ return @{$self->{destin}} == 1 ? $self->{src} : $self->{destin}->[-2];
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'destin'};} == 1 ? : 0 0 0
152 }
153  
154 1 1;
« lib/Games/Checkers/CreateMoveList.pm ↑ Summary lib/Games/Checkers/Rules.pm »