lib/Games/Checkers/CreateMoveList.pm

filter: Statementstmt 36.7% 43 / 117 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 53.8% 14 / 26 Podpod 0.0% 0 / 3 totaltotal 29.3% 57 / 194 CC 22 SCAR 38.1
CC
22
Cov
27%
new
156.0
gather_move
30.0
gather_move
12.0
CRAP 45.3
« lib/Games/Checkers/Game.pm ↑ Summary lib/Games/Checkers/MoveLocationConstructor.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 # ----------------------------------------------------------------------------
20  
21 package Games::Checkers::CreateMoveList;
22  
23 1 ▶use base 'Games::Checkers::ExpandMoveList';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use Games::Checkers::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Games::Checkers::MoveConstants;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 0 ▶sub new ($$$) {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
29 0 my $class = shift;
30 0 my $board = shift;
31 0 my $color = shift;
32 0 my $self = $class->SUPER::new($board, $color);
33  
34 0 $self->{mbs} = [];
35  
36 0 $self->build;
37 0 return $self;
38 }
39  
40 0 ▶sub gather_move ($) {
Subroutine gather_move: not called CC=3 SCAR=24.8
Pod undocumented
41 0 my $self = shift;
42  
43 0 my $move = $self->create_move;
44 0 ▶ return Err unless $move; ### not needed
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 0 0
45 0 ▶ die "Internal Error" if $move == NO_MOVE;
Branch: 1 branch
BranchTrueFalseTotal
if $move == &NO_MOVE() 0 0 0
46  
47 0 push @{$self->{mbs}}, [ $move, $self->{work_board}->clone ];
48  
49 0 return Ok;
50 }
51  
52 0 ▶sub get_move_boards ($) {
Subroutine get_move_boards: not called CC=1 SCAR=6.9
Pod undocumented
53 0 my $self = shift;
54  
55 0 return $self->{mbs};
56 }
57  
58 # ----------------------------------------------------------------------------
59  
60 package Games::Checkers::CountMoveList;
61  
62 1 ▶use base 'Games::Checkers::ExpandMoveList';
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 1 ▶use Games::Checkers::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 0 ▶sub new ($$$) {
Subroutine new: not called CC=1 SCAR=6.9
67 0 my $class = shift;
68 0 my $board = shift;
69 0 my $color = shift;
70 0 my $self = $class->SUPER::new($board, $color);
71  
72 0 $self->{count} = 0;
73  
74 0 $self->build;
75 0 return $self;
76 }
77  
78 0 ▶sub gather_move ($) {
Subroutine gather_move: not called CC=1 SCAR=6.9
79 0 my $self = shift;
80 0 $self->{count}++;
81 0 return Ok;
82 }
83  
84 0 ▶sub get_count ($) {
Subroutine get_count: not called CC=2 SCAR=17.9
85 0 my $self = shift;
86 0 ▶ return $self->{status} == Ok ? $self->{count} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'status'} == &Ok() ? : 0 0 0
87 }
88  
89 # ----------------------------------------------------------------------------
90  
91 package Games::Checkers::CreateUniqueMove;
92  
93 1 ▶use base 'Games::Checkers::ExpandMoveList';
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 1 ▶use Games::Checkers::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 1 ▶use Games::Checkers::MoveConstants;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 0 ▶sub new ($$$) {
Subroutine new: not called CC=1 SCAR=6.9
99 0 my $class = shift;
100 0 my $board = shift;
101 0 my $color = shift;
102 0 my $self = $class->SUPER::new($board, $color);
103  
104 0 $self->{move} = NO_MOVE;
105  
106 0 $self->build;
107 0 return $self;
108 }
109  
110 0 ▶sub gather_move ($) {
Subroutine gather_move: not called CC=2 SCAR=17.9
111 0 my $self = shift;
112 0 ▶ return Err if $self->{move} != NO_MOVE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"move"} != &NO_MOVE() 0 0 0
113 0 $self->{move} = $self->create_move;
114 0 return Ok;
115 }
116  
117 0 ▶sub get_move ($) {
Subroutine get_move: not called CC=2 SCAR=17.9
118 0 my $self = shift;
119 0 ▶ return $self->{status} == Ok ? $self->{move} : NO_MOVE;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'status'} == &Ok() ? : 0 0 0
120 }
121  
122 # ----------------------------------------------------------------------------
123  
124 package Games::Checkers::CreateVergeMove;
125  
126 1 ▶use base 'Games::Checkers::ExpandMoveList';
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 1 ▶use Games::Checkers::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 1 ▶use Games::Checkers::MoveConstants;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 1 ▶use Games::Checkers::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 0 ▶sub new ($$$$$$) {
Subroutine new: not called CC=12 SCAR=50.5
133 0 my $class = shift;
134 0 my $board = shift;
135 0 my $color = shift;
136 0 my $self = $class->SUPER::new($board, $color);
137  
138 0 ▶ die "Not enough arguments in constructor" unless @_ >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 3 0 0 0
139 0 my $is_beat = $self->{is_beat} = shift;
140 0 my $src = $self->{src0} = shift;
141 0 my $dst = $self->{dst0} = shift;
142 0 my @extra_dsts = @_;
143  
144 0 ▶ die "Bad verge move source location ($src): not occupied\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $board->occup($src) 0 0 0
145 unless $board->occup($src);
146 0 ▶ die "Bad verge move source location ($src): incorrect color\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $board->color($src) == $color 0 0 0
147 unless $board->color($src) == $color;
148 0 ▶ die "Bad verge move source location ($src): can't beat\n"
Branch: 1 branch
BranchTrueFalseTotal
unless not $is_beat or $board->can_piece_beat($src) 0 0 0
Condition0%0/3
not $is_beat or $board->can_piece_beat($src)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $is_beat or $board->can_piece_beat($src)
not $is_beat $board->can_piece_beat($src)
Truth table0%0/3
not(A) or B
A= not $is_beatB= $board->can_piece_beat($src)
ABresult
1X1
011
000
149 unless !$is_beat || $board->can_piece_beat($src);
150 # die "Bad verge move source location ($src): can't step\n"
151 # unless $is_beat || $board->can_piece_step($src);
152  
153 0 ▶ if (!$is_beat) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_beat) 0 0 0
154 0 ▶ if ($board->can_piece_step($src, $dst)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($board->can_piece_step($src, $dst)) 0 0 0
155 0 ▶ die "Bad verge move ($src-$dst): extra destinations @extra_dsts given\n"
Branch: 1 branch
BranchTrueFalseTotal
if @extra_dsts 0 0 0
156 if @extra_dsts;
157 0 $self->{move} = new Games::Checkers::Move($is_beat, $src, [$dst]);
158 0 return $self;
159 }
160 # give it the last chance
161 0 ▶ $board->can_piece_beat($src) ? $is_beat = 1 :
Branch: 1 branch
BranchTrueFalseTotal
$board->can_piece_beat($src) ? : 0 0 0
162 die "Bad verge move ($src-$dst): can't step\n";
163 }
164  
165 # support British rules
166 0 ▶ if ($is_beat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_beat) 0 0 0
167 0 ▶ if ($board->can_piece_beat($src, $dst)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($board->can_piece_beat($src, $dst)) 0 0 0
168 0 $self->{move} = new Games::Checkers::Move($is_beat, $src, [$dst, @extra_dsts]);
169 0 return $self;
170 }
171 }
172 0 $self->{move} = NO_MOVE;
173  
174 0 $self->build;
175 0 return $self;
176 }
177  
178 0 ▶sub gather_move ($) {
Subroutine gather_move: not called CC=5 SCAR=34.0
179 0 my $self = shift;
180  
181 0 ▶ return Err if !$self->{must_beat};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"must_beat"} 0 0 0
182 0 ▶ return Ok if $self->{src} != $self->{src0} || $self->dst_1 != $self->{dst0};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"src"} != $self->{"src0"} or $self->dst_1 != $self->{"dst0"} 0 0 0
Condition0%0/3
$self->{"src"} != $self->{"src0"} or $self->dst_1 != $self->{"dst0"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"src"} != $self->{"src0"} or $self->dst_1 != $self->{"dst0"}
$self->{"src"} != $self->{"src0"} $self->dst_1 != $self->{"dst0"}
Truth table0%0/3
A or B
A= $self->{"src"} != $self->{"src0"}B= $self->dst_1 != $self->{"dst0"}
ABresult
1X1
011
000
183  
184 0 ▶ return Err if $self->{move} != NO_MOVE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"move"} != &NO_MOVE() 0 0 0
185 0 $self->{move} = $self->create_move;
186 0 return Ok;
187 }
188  
189 0 ▶sub get_move ($) {
Subroutine get_move: not called CC=2 SCAR=17.9
190 0 my $self = shift;
191 0 ▶ return $self->{status} == Ok ? $self->{move} : NO_MOVE;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'status'} == &Ok() ? : 0 0 0
192 }
193  
194 1 1;
« lib/Games/Checkers/Game.pm ↑ Summary lib/Games/Checkers/MoveLocationConstructor.pm »