lib/Games/Checkers/Game.pm

filter: Statementstmt 16.7% 25 / 149 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 29.6% 8 / 27 Podpod 0.0% 0 / 18 totaltotal 9.5% 33 / 346 CC 60 SCAR 41.1
CC
60
Cov
9%
new
210.0
show_result_code
110.0
color_name
56.0
CRAP 60.8
« lib/Games/Checkers/BoardTree.pm ↑ Summary lib/Games/Checkers/CreateMoveList.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::Game;
20  
21 1 ▶use Games::Checkers::Rules;
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::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Games::Checkers::BoardTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Games::Checkers::CreateMoveList;
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=14 SCAR=53.5
Pod undocumented
29 0 my $class = shift;
30 0 my %params = @_;
31  
32 0 my $variant = $params{variant};
33 0 Games::Checkers::Rules::set_variant($variant);
34  
35 0 ▶ $ENV{DUMB_CHARS} = 1 if $params{dumb_chars};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"dumb_chars"} 0 0 0
36  
37 0 ▶ my $title = $params{title} || "Unknown White - Unknown Black";
Condition0%0/2
$params{'title'} || 'Unknown White - Unknown Black'
l!l
00
MC/DC0%0/1
$params{'title'} || 'Unknown White - Unknown Black'
$params{'title'}
Truth table0%0/2
A
A= $params{'title'}
Aresult
11
00
38 0 my $board = Games::Checkers::Board->new($params{board}, $params{size});
39 my $color = defined $params{color} ? $params{color} :
40 (defined $ENV{_WHITE_STARTS} ? $ENV{_WHITE_STARTS} :
41 0 ▶ $::RULES{WHITE_STARTS}) ^ ($params{black} || 0) ? White : Black;
Branch: 3 branches
BranchTrueFalseTotal
defined $params{'color'} ? : 0 0 0
(defined $ENV{'_WHITE_STARTS'} ? $ENV{'_WHITE_STARTS'} : $main::RULES{'WHITE_STARTS'}) ^ ($params{'black'} || 0) ? : 0 0 0
defined $ENV{'_WHITE_STARTS'} ? : 0 0 0
Condition0%0/2
$params{'black'} || 0
l!l
00
MC/DC0%0/1
$params{'black'} || 0
$params{'black'}
Truth table0%0/2
A
A= $params{'black'}
Aresult
11
00
42  
43 # probe and use if available
44 0 ▶ my $frontend = !($params{use_term} || $ENV{USE_TERM}) && eval q{
Condition0%0/6
!($params{'use_term'} || $ENV{'USE_TERM'}) && eval "\n\t\tuse Games::Checkers::SDL;\n\t\tGames::Checkers::SDL->new(\$title, \$board, fullscreen => \$params{fullscreen});\n\t"
!ll&&!rl&&r
000
$params{'use_term'} || $ENV{'USE_TERM'}
l!l&&r!l&&!r
000
MC/DC0%0/3
!($params{'use_term'} || $ENV{'USE_TERM'}) && eval "\n\t\tuse Games::Checkers::SDL;\n\t\tGames::Checkers::SDL->new(\$title, \$board, fullscreen => \$params{fullscreen});\n\t"
$params{'use_term'} $ENV{'USE_TERM'} eval "\n\t\tuse Games::Checkers::SDL;\n\t\tGames::Checkers::SDL->new(\$title, \$board, fullscreen => \$params{fullscreen});\n\t"
Truth table0%0/4
not(A || B) && C
A= $params{'use_term'}B= $ENV{'USE_TERM'}C= eval "\n\t\tuse Games::Checkers::SDL;\n\t\tGames::Checkers::SDL->new(\$title, \$board, fullscreen => \$params{fullscreen});\n\t"
ABCresult
1XX0
01X0
0000
0011
45 use Games::Checkers::SDL;
46 Games::Checkers::SDL->new($title, $board, fullscreen => $params{fullscreen});
47 };
48  
49 my $self = {
50 variant => $variant,
51 title => $title,
52 board => $board,
53 color => $color,
54 frontend => $frontend,
55 dumb_term => $params{dumb_term},
56 level => $params{level} || 3,
57 random => $params{random} || 0,
58 0 ▶ max_move_num => $params{max_move_num} || 1000,
Condition0%0/6
$params{'level'} || 3
l!l
00
$params{'random'} || 0
l!l
00
$params{'max_move_num'} || 1000
l!l
00
MC/DC0%0/1
$params{'level'} || 3
$params{'level'}
MC/DC0%0/1
$params{'random'} || 0
$params{'random'}
MC/DC0%0/1
$params{'max_move_num'} || 1000
$params{'max_move_num'}
Truth table0%0/2
A
A= $params{'level'}
Aresult
11
00
Truth table0%0/2
A
A= $params{'random'}
Aresult
11
00
Truth table0%0/2
A
A= $params{'max_move_num'}
Aresult
11
00
59 plies => [],
60 initial => {
61 board => $board->clone,
62 color => $color,
63 },
64 };
65  
66 0 bless $self, $class;
67  
68 0 ▶ $SIG{__DIE__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 $self->show_result(shift);
70 0 };
71  
72 0 ▶ $self->edit_board if $params{edit_board};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"edit_board"} 0 0 0
73  
74 0 ▶ $self->show_menu if $params{show_menu};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"show_menu"} 0 0 0
75  
76 0 $self->init;
77 }
78  
79 0 ▶sub show_menu ($) {
Subroutine show_menu: not called CC=3 SCAR=24.8
Pod undocumented
80 0 my $self = shift;
81  
82 0 ▶ if ($self->{frontend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"frontend"}) 0 0 0
83 0 $self->{board} = $self->{frontend}->show_menu($self->{board});
84 }
85  
86 0 ▶ $self->quit unless $self->{board};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"board"} 0 0 0
87 }
88  
89 0 ▶sub init ($) {
Subroutine init: not called CC=3 SCAR=24.8
Pod undocumented
90 0 my $self = shift;
91  
92 0 $self->{plies} = [];
93  
94 0 ▶ if ($self->{frontend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'frontend'}) { } 0 0 0
95 0 $self->{frontend}->init;
96 } else {
97 0 $| = 1;
98 0 ▶ print "\e[2J" unless $self->{dumb_term};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dumb_term"} 0 0 0
99 }
100  
101 0 return $self;
102 }
103  
104 0 ▶sub restart ($) {
Subroutine restart: not called CC=2 SCAR=17.9
Pod undocumented
105 0 my $self = shift;
106  
107 0 $self->{board} = $self->{initial}{board}->clone;
108 0 $self->{color} = $self->{initial}{color};
109  
110 0 ▶ if ($self->{frontend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"frontend"}) 0 0 0
111 0 $self->{frontend}->restart($self->{board});
112 }
113  
114 0 $self->init;
115 0 $self->show_board;
116 0 $self->sleep(1);
117 }
118  
119 0 ▶sub quit ($) {
Subroutine quit: not called CC=3 SCAR=24.8
Pod undocumented
120 0 my $self = shift;
121  
122 0 ▶ if ($self->{frontend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"frontend"}) 0 0 0
123 0 ▶ $self->{frontend}->quit or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"frontend"}->quit or return 0 0 0
124 }
125  
126 0 exit(0);
127 }
128  
129 0 ▶sub call_frontend ($$@) {
Subroutine call_frontend: not called CC=4 SCAR=30.0
Pod undocumented
130 0 my $self = shift;
131 0 ▶ my $method = shift || die;
Condition0%0/2
shift() || die()
l!l
00
MC/DC0%0/1
shift() || die()
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
132  
133 0 my $rv = $self->{frontend}->$method(@_);
134  
135 0 ▶ if ($rv == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == -1) 0 0 0
136 0 $self->restart;
137 }
138 0 ▶ if ($rv == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == -2) 0 0 0
139 0 $self->quit;
140 }
141  
142 0 return $rv;
143 }
144  
145 0 ▶sub sleep ($$) {
Subroutine sleep: not called CC=3 SCAR=24.8
Pod undocumented
146 0 my $self = shift;
147 0 ▶ my $secs = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
148  
149 0 ▶ if ($self->{frontend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'frontend'}) { } 0 0 0
150 0 $self->call_frontend('sleep', $secs);
151 } else {
152 0 sleep($secs);
153 }
154 }
155  
156 0 ▶sub hold ($;$) {
Subroutine hold: not called CC=3 SCAR=24.8
Pod undocumented
157 0 my $self = shift;
158 0 my $break = shift;
159  
160 0 ▶ if ($self->{frontend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'frontend'}) { } 0 0 0
161 0 $self->call_frontend('hold', $break);
162 } else {
163 0 ▶ $self->sleep($break) if $break;
Branch: 1 branch
BranchTrueFalseTotal
if $break 0 0 0
164 }
165 }
166  
167 0 ▶sub show_board ($) {
Subroutine show_board: not called CC=4 SCAR=30.0
Pod undocumented
168 0 my $self = shift;
169  
170 0 ▶ if ($self->{frontend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'frontend'}) { } 0 0 0
171 0 $self->call_frontend('show_board');
172 } else {
173 0 my $title = $self->{title};
174 0 my $indent = int((37 - length($title)) / 2);
175 0 ▶ print "\e[1;1H\e[?25l" unless $self->{dumb_term};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dumb_term"} 0 0 0
176 0 print " " x $indent, $title, "\n";
177 0 print $self->{board}->dump;
178 0 ▶ print "\e[?25h" unless $self->{dumb_term};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dumb_term"} 0 0 0
179 }
180 }
181  
182 0 ▶sub can_move ($) {
Subroutine can_move: not called CC=1 SCAR=6.9
Pod undocumented
183 0 my $self = shift;
184  
185 0 $self->{board}->can_color_move($self->{color});
186 }
187  
188 0 ▶sub is_max_move_num_reached ($) {
Subroutine is_max_move_num_reached: not called CC=1 SCAR=6.9
Pod undocumented
189 0 my $self = shift;
190  
191 0 return @{$self->{plies}} >= $self->{max_move_num} * 2;
192 }
193  
194 0 ▶sub choose_move ($) {
Subroutine choose_move: not called CC=4 SCAR=30.0
Pod undocumented
195 0 my $self = shift;
196  
197 my ($board, $color, $level, $random) = map {
198 0 $self->{$_}
199 } qw(board color level random);
200  
201 0 my $board_tree = Games::Checkers::BoardTree->new($board, $color, $level);
202  
203 0 ▶ my $move = $random eq 1 || $random eq ($color == White ? 'w' : 'b')
Branch: 2 branches
BranchTrueFalseTotal
$random eq 1 || $random eq ($color == &White() ? 'w' : 'b') ? : 0 0 0
$color == &White() ? : 0 0 0
Condition0%0/3
$random eq 1 or $random eq ($color == &White() ? "w" : "b")
l!l&&r!l&&!r
000
MC/DC0%0/2
$random eq 1 or $random eq ($color == &White() ? "w" : "b")
$random eq 1 $random eq ($color == &White() ? "w" : "b")
Truth table0%0/3
A or B
A= $random eq 1B= $random eq ($color == &White() ? "w" : "b")
ABresult
1X1
011
000
204 ? $board_tree->choose_random_move
205 : $board_tree->choose_best_move;
206  
207 0 return $move;
208 }
209  
210 0 ▶sub create_move ($$$@) {
Subroutine create_move: not called CC=3 SCAR=24.8
Pod undocumented
211 0 my $self = shift;
212 0 my $is_beat = shift;
213 0 my $src = shift;
214 0 my @dsts = @_;
215  
216 my $creating_move = Games::Checkers::CreateVergeMove->new(
217 0 $self->{board}, $self->{color}, $is_beat, $src, @dsts
218 );
219 0 ▶ die "Internal problem" unless $creating_move->status == Ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $creating_move->status == &Ok() 0 0 0
220 0 my $move = $creating_move->get_move;
221  
222 0 ▶ return $move == NO_MOVE ? undef : $move;
Branch: 1 branch
BranchTrueFalseTotal
$move == &NO_MOVE() ? : 0 0 0
223 }
224  
225 0 ▶sub show_move ($$) {
Subroutine show_move: not called CC=5 SCAR=34.0
Pod undocumented
226 0 my $self = shift;
227 0 my $move = shift;
228  
229 my ($board, $color, $plies) = map {
230 0 $self->{$_}
231 } qw(board color plies);
232  
233 0 my $move_str = $move->dump($board);
234 0 my $is_second = ($color eq White) != $::RULES{WHITE_STARTS};
235  
236 0 ▶ if ($self->{frontend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'frontend'}) { } 0 0 0
237 0 ▶ $self->call_frontend('show_move', $move, $move_str, $is_second, $plies)
Branch: 1 branch
BranchTrueFalseTotal
$self->call_frontend("show_move", $move, $move_str, $is_second, $plies) and return 0 0 0
238 && return; # return on "restart" or unconfirmed "quit"
239 } else {
240 0 ▶ printf " %02d. %s", 1 + @$plies / 2, $is_second ? "... " : "";
Branch: 1 branch
BranchTrueFalseTotal
$is_second ? : 0 0 0
241 0 print "$move_str \n";
242 }
243  
244 0 $board->apply_move($move);
245  
246 0 ▶ $self->{color} = $color == White ? Black : White;
Branch: 1 branch
BranchTrueFalseTotal
$color == &White() ? : 0 0 0
247 0 push @$plies, $move;
248 }
249  
250 0 ▶sub color_name ($;$$) {
Subroutine color_name: not called CC=7 SCAR=40.3
Pod undocumented
251 0 my $self = shift;
252 0 ▶ my $opposite = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
253 0 ▶ my $starting = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
254  
255 # does it ever make sense to use $self->{initial}{color} instead?
256 0 ▶ my $starting_color = $::RULES{WHITE_STARTS} ? White : Black;
Branch: 1 branch
BranchTrueFalseTotal
$main::RULES{'WHITE_STARTS'} ? : 0 0 0
257  
258 return (($starting ? $starting_color : $self->{color})
259 0 ▶ == White xor $opposite) ? 'White' : 'Black';
Branch: 2 branches
BranchTrueFalseTotal
($starting ? $starting_color : $self->{'color'}) == &White() ^^ $opposite ? : 0 0 0
$starting ? : 0 0 0
Condition0%0/4
($starting ? $starting_color : $self->{"color"}) == &White() xor $opposite
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
($starting ? $starting_color : $self->{"color"}) == &White() xor $opposite
($starting ? $starting_color : $self->{"color"}) == &White() $opposite
Truth table0%0/4
A xor B
A= ($starting ? $starting_color : $self->{"color"}) == &White()B= $opposite
ABresult
110
101
011
000
260 }
261  
262 0 ▶sub show_result ($;$$) {
Subroutine show_result: not called CC=5 SCAR=34.0
Pod undocumented
263 0 my $self = shift;
264 my $message = shift || ($self->is_max_move_num_reached
265 ? "Automatic draw after $self->{max_move_num} moves"
266 0 ▶ : $self->color_name(!$::RULES{GIVE_AWAY}) . " won"
Branch: 1 branch
BranchTrueFalseTotal
$self->is_max_move_num_reached ? : 0 0 0
Condition0%0/3
shift() || ($self->is_max_move_num_reached ? "Automatic draw after $self->{'max_move_num'} moves" : $self->color_name(!$main::RULES{'GIVE_AWAY'}) . ' won')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || ($self->is_max_move_num_reached ? "Automatic draw after $self->{'max_move_num'} moves" : $self->color_name(!$main::RULES{'GIVE_AWAY'}) . ' won')
shift() ($self->is_max_move_num_reached ? "Automatic draw after $self->{'max_move_num'} moves" : $self->color_name(!$main::RULES{'GIVE_AWAY'}) . ' won')
Truth table0%0/3
A || B
A= shift()B= ($self->is_max_move_num_reached ? "Automatic draw after $self->{'max_move_num'} moves" : $self->color_name(!$main::RULES{'GIVE_AWAY'}) . ' won')
ABresult
1X1
011
000
267 );
268 0 my $break = shift;
269  
270 0 ▶ if ($self->{frontend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'frontend'}) { } 0 0 0
271 0 ▶ $self->call_frontend('show_result', $message)
Branch: 1 branch
BranchTrueFalseTotal
$self->call_frontend("show_result", $message) and return 0 0 0
272 && return; # return on "restart" or unconfirmed "quit"
273 } else {
274 0 print "\n$message.\e[0K\n";
275 }
276  
277 0 $self->hold($break);
278 }
279  
280 0 ▶sub show_result_code ($;$$) {
Subroutine show_result_code: not called CC=10 SCAR=47.0
Pod undocumented
281 0 my $self = shift;
282 0 ▶ my $code = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
283 0 my $break = shift;
284  
285 0 ▶ my $message =
Branch: 4 branches
BranchTrueFalseTotal
$code eq '1-0' || $code eq '2-0' ? : 0 0 0
$code eq '0-1' || $code eq '0-2' ? : 0 0 0
$code eq '1/2-1/2' || $code eq '1-1' ? : 0 0 0
$code eq '*' ? : 0 0 0
Condition0%0/9
$code eq "1-0" or $code eq "2-0"
l!l&&r!l&&!r
000
$code eq "0-1" or $code eq "0-2"
l!l&&r!l&&!r
000
$code eq "1/2-1/2" or $code eq "1-1"
l!l&&r!l&&!r
000
MC/DC0%0/2
$code eq "1-0" or $code eq "2-0"
$code eq "1-0" $code eq "2-0"
MC/DC0%0/2
$code eq "0-1" or $code eq "0-2"
$code eq "0-1" $code eq "0-2"
MC/DC0%0/2
$code eq "1/2-1/2" or $code eq "1-1"
$code eq "1/2-1/2" $code eq "1-1"
Truth table0%0/3
A or B
A= $code eq "1-0"B= $code eq "2-0"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $code eq "0-1"B= $code eq "0-2"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $code eq "1/2-1/2"B= $code eq "1-1"
ABresult
1X1
011
000
286 $code eq '1-0' || $code eq '2-0' ? '%C %l' :
287 $code eq '0-1' || $code eq '0-2' ? '%c %l' :
288 $code eq '1/2-1/2' || $code eq '1-1' ? 'Draw is agreed' :
289 $code eq '*' ? 'Unfinished game' : "Unknown result ($code)";
290  
291 0 $message =~ s/%([cC])/$self->color_name($1 ne 'c', 1)/e;
292 0 ▶ $message =~ s/%l/$self->can_move ? 'resigned' : 'lost'/e;
Branch: 1 branch
BranchTrueFalseTotal
$self->can_move ? : 0 0 0
293  
294 0 $self->show_result($message, $break);
295 }
296  
297 0 ▶sub edit_board ($) {
Subroutine edit_board: not called CC=2 SCAR=17.9
Pod undocumented
298 0 my $self = shift;
299  
300 0 my $board = $self->{board};
301  
302 0 ▶ if ($self->{frontend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"frontend"}) 0 0 0
303 0 $self->{frontend}->edit_board($board);
304 }
305  
306 0 $self->{initial}{board}->copy($board);
307 }
308  
309 1 1;
« lib/Games/Checkers/BoardTree.pm ↑ Summary lib/Games/Checkers/CreateMoveList.pm »