Plisco/Engine.pm

filter: Statementstmt 45.3% 245 / 540 Branchbran 28.8% 56 / 194 Conditioncond 34.1% 28 / 82 MC/DCmcdc 2.4% 1 / 41 Subroutinesub 53.9% 34 / 63 Podpod 0.0% 0 / 2 totaltotal 39.4% 364 / 922 CC 134 SCAR 42.0
CC
134
Cov
40%
__onUciCmdPgn
210.0
__onUciCmdSetoption
210.0
__onUciCmdGo
105.1
CRAP 66.8
« Plisco/Tablebase.pm ↑ Summary Plisco/Engine/Win32Wrapper.pm »
1 #! /bin/false
2  
3 # Copyright (C) 2021-2026 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software. It comes without any warranty, to
7 # the extent permitted by applicable law. You can redistribute it
8 # and/or modify it under the terms of the Do What the Fuck You Want
9 # to Public License, Version 2, as published by Sam Hocevar. See
10 # http://www.wtfpl.net/ for more details.
11  
12 package Chess::Plisco::Engine;
13 1 $Chess::Plisco::Engine::VERSION = 'v1.0.3';
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use POSIX qw(:sys_wait_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Locale::TextDomain qw(Chess-Plisco);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Chess::Plisco qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use Chess::Plisco::Engine::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Chess::Plisco::Engine::Position;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Chess::Plisco::Engine::TimeControl;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Chess::Plisco::Engine::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Chess::Plisco::Engine::InputWatcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Chess::Plisco::Engine::TranspositionTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Chess::Plisco::Engine::Book;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Chess::Plisco::Engine::LimitsType;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Chess::Plisco::Tablebase::Syzygy;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # These figures are taken from
34 1 ▶use constant MIN_HASH_SIZE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant DEFAULT_HASH_SIZE => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant MAX_HASH_SIZE => 33554432;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use constant UCI_OPTIONS => [
Subroutine BEGIN: called CC=1 SCAR=0.0
39 {
40 name => 'Hash',
41 type => 'spin',
42 default => DEFAULT_HASH_SIZE,
43 min => MIN_HASH_SIZE,
44 max => MAX_HASH_SIZE,
45 callback => '__resizeTranspositionTable',
46 },
47 {
48 name => 'Clear Hash',
49 type => 'button',
50 callback => '__clearTranspositionTable',
51 },
52 {
53 name => 'Batch',
54 type => 'check',
55 default => 'false',
56 callback => '__changeBatchMode',
57 },
58 {
59 name => 'OwnBook',
60 type => 'check',
61 default => 'false',
62 },
63 {
64 name => 'BookFile',
65 type => 'string',
66 callback => '__setBookFile',
67 },
68 {
69 name => 'BookDepth',
70 type => 'spin',
71 min => 1,
72 max => 1024,
73 default => 20,
74 },
75 {
76 name => 'Ponder',
77 type => 'check',
78 default => 'false',
79 },
80 {
81 name => 'Move Overhead',
82 type => 'spin',
83 min => 0,
84 max => 5000,
85 default => 10,
86 callback => '__setMoveOverhead',
87 },
88 {
89 name => 'SyzygyPath',
90 type => 'string',
91 callback => '__setSyzygyPath',
92 },
93 {
94 name => 'Syzygy7TimeCushion',
95 type => 'spin',
96 min => 500,
97 max => 30000,
98 default => 5000,
99 },
100 {
101 name => 'Syzygy3TimeCushion',
102 type => 'spin',
103 min => 100,
104 max => 3000,
105 default => 500,
106 },
107 {
108 name => 'SyzygyProbeDepth',
109 type => 'spin',
110 min => 1,
111 max => 100,
112 default => 1,
113 },
114 {
115 name => 'Syzygy50MoveRule',
116 type => 'check',
117 default => 'true',
118 },
119 {
120 name => 'SyzygyProbeLimit',
121 type => 'spin',
122 min => 0,
123 max => 7,
124 default => 7,
125 },
126 1 ];
127  
128 1 my $uci_options = UCI_OPTIONS;
129 1 my %uci_options = map { $_->{name} => $_ } @$uci_options;
130  
131 1 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
132 1 my ($class, %options) = @_;
133  
134 1 my $position = Chess::Plisco::Engine::Position->new;
135 my $self = {
136 __position => $position,
137 __signatures => [$position->signature],
138 __options => {},
139 __book => Chess::Plisco::Engine::Book->new,
140 __setup => 0,
141 __fen => undef,
142 __turn => CP_WHITE,
143 __started => undef,
144 __moves => [],
145 __continued => 0,
146 __move_overhead => 10,
147 __move_overheads => [],
148 __move_overhead_fixed => 0,
149 __last_params => {},
150 __original_time_adjust => -1,
151 __tb => Chess::Plisco::Tablebase::Syzygy->new,
152 __last_score => 0,
153 __fen_out => $options{fen_out},
154 __pgn_out => $options{pgn_out},
155 1 };
156  
157 1 my $options = UCI_OPTIONS;
158 1 foreach my $option (@$options) {
159 14 $self->{__options}->{$option->{name}} = $option->{default};
160 }
161  
162 1 my $tt_size = $self->{__options}->{Hash};
163 1 $self->{__tt} = Chess::Plisco::Engine::TranspositionTable->new($tt_size);
164  
165 1 bless $self, $class;
166  
167 1 $self->__calibrateNPS;
168  
169 1 return $self;
170 }
171  
172 1 ▶sub __calibrateNPS {
Subroutine __calibrateNPS: called CC=2 SCAR=6.9
173 1 my ($self) = @_;
174  
175 1 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 1 my $position = Chess::Plisco::Engine::Position->new;
178  
179 1 my $watcher = Chess::Plisco::Engine::InputWatcher->new(\*STDIN);
180 1 $watcher->setBatchMode(1);
181  
182 my %options = (
183 position => $position,
184 tt => $self->{__tt},
185 watcher => $watcher,
186 10 ▶ info => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
187 1 signatures => [],
188 );
189 1 my $tree = Chess::Plisco::Engine::Tree->new(%options);
190 1 $tree->{max_depth} = 3;
191 1 $tree->{start_time} = [gettimeofday];
192  
193 1 $tree->think;
194  
195 1 my $elapsed = 1000 * tv_interval($tree->{start_time});
196 1 ▶ $elapsed ||= 1;
Condition50%1/2
$elapsed ||= 1
l!l
10
MC/DC0%0/1
$elapsed ||= 1
$elapsed
Truth table50%1/2
A
A= $elapsed
Aresult
11
00
197 1 $self->{__nps} = $tree->{nodes} / $elapsed;
198  
199 1 $tree->{tt}->clear;
200 }
201  
202 0 ▶sub uci {
Subroutine uci: not called CC=5 SCAR=34.0
Pod undocumented
203 0 my ($self, $in, $out, $init) = @_;
204  
205 0 ▶ if ($^O =~ /win32/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /win32/i) 0 0 0
206 0 $in = $self->__msDosSocket($in);
207 }
208  
209 0 $self->{__out} = $out;
210 0 $self->{__out}->autoflush(1);
211 0 $self->{__watcher} = Chess::Plisco::Engine::InputWatcher->new($in, @$init);
212 0 ▶ $self->{__watcher}->onInput(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 $self->__onUciInput(@_);
214 0 });
215 0 ▶ $self->{__watcher}->onEof(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
216 0 $self->__onEof(@_);
217 0 });
218  
219 0 ▶ my $version = $Chess::Plisco::Engine::VERSION || 'development version';
Condition0%0/2
$Chess::Plisco::Engine::VERSION || 'development version'
l!l
00
MC/DC0%0/1
$Chess::Plisco::Engine::VERSION || 'development version'
$Chess::Plisco::Engine::VERSION
Truth table0%0/2
A
A= $Chess::Plisco::Engine::VERSION
Aresult
11
00
220 0 $self->__output(<<"EOF");
221 🦊 Welcome to Plisco $version!
222 💡 Try 'help' for a list of commands!
223 EOF
224  
225 0 foreach my $command (@$init) {
226 0 $out->print("$command\n");
227 0 $self->__onUciInput($command);
228 }
229  
230 0 while (1) {
231 0 $self->{__watcher}->check;
232 0 ▶ if (delete $self->{__abort}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $self->{"__abort"}) 0 0 0
233 0 $self->DESTROY; # Make sure to clean-up for MS-DOS.
234 0 last;
235 }
236 }
237  
238 0 return $self;
239 }
240  
241 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
242 0 my ($self) = @_;
243  
244 0 ▶ if ($self->{__child_pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__child_pid"}) 0 0 0
245 # Seems to be useless for MS-DOS but ...
246 0 kill 'QUIT', $self->{__child_pid};
247 }
248 0 ▶ if ($self->{__socket}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__socket"}) 0 0 0
249 0 unlink $self->{__socket};
250 }
251  
252 # No point to call waitpid() because the "child process" is actually
253 # a thread in MS-DOS.
254 }
255  
256 0 ▶sub __msDosSocket {
Subroutine __msDosSocket: not called CC=8 SCAR=42.8
257 0 my ($self, $real_in) = @_;
258  
259 0 require IO::Socket::UNIX;
260 0 require File::Temp;
261  
262 0 my $path = $self->{__socket} = File::Temp::tmpnam();
263  
264 0 ▶ my $sock = IO::Socket::UNIX->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::UNIX"->new("Type", IO::Socket::UNIX::SOCK_STREAM(), "Local", $path, "Listen", 1) or die &__x("Cannot create socket write-end '{path}': {error}!\n", "path", $path, "error", $!) 0 0 0
265 Type => IO::Socket::UNIX::SOCK_STREAM(),
266 Local => $path,
267 Listen => 1,
268 ) or die __x("Cannot create socket write-end '{path}': {error}!\n",
269 path => $path, error => $!);
270  
271 0 my $pid = fork;
272 0 ▶ if (!defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
273 0 die __x("Cannot fork: {error}!\n", error => $!);
274 }
275  
276 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
277 # Parent.
278 0 $sock->close;
279 0 $self->{__child_pid} = $pid;
280 0 ▶ my $new_in = IO::Socket::UNIX->new(
Branch: 1 branch
BranchTrueFalseTotal
my $new_in = "IO::Socket::UNIX"->new("Type", IO::Socket::UNIX::SOCK_STREAM(), "Peer", $path) or die &__x("Cannot create socket read-end '{path}': {error}!\n", "path", $path, "error", $!) 0 0 0
281 Type => IO::Socket::UNIX::SOCK_STREAM(),
282 Peer => $path,
283 ) or die __x("Cannot create socket read-end '{path}': {error}!\n",
284 path => $path, error => $!);
285 0 return $new_in;
286 } else {
287 0 ▶ my $fh = $sock->accept
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $sock->accept or die &__x("Error accepting connection on '{path}': {error}!\n", "path", $path, "error", $!) 0 0 0
288 or die __x("Error accepting connection on '{path}': {error}!\n",
289 path => $path, error => $!);
290 0 while (1) {
291 0 my $line = $real_in->getline;
292 0 ▶ exit if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
293 0 $fh->print($line);
294 0 $line = $self->__trim($line);
295 # There seems to be no other way to get rid of the input reading
296 # thread under MS-DOS.
297 0 ▶ exit if 'quit' eq $line;
Branch: 1 branch
BranchTrueFalseTotal
if "quit" eq $line 0 0 0
298 }
299 }
300 }
301  
302 0 ▶sub __onEof {
Subroutine __onEof: not called CC=1 SCAR=6.9
303 0 my ($self, $line) = @_;
304  
305 0 $self->DESTROY;
306  
307 0 exit;
308 }
309  
310 4 ▶sub __onUciInput {
Subroutine __onUciInput: called CC=7 SCAR=22.2
311 4 my ($self, $line) = @_;
312  
313 4 $line = $self->__trim($line);
314 4 ▶ return $self if !length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 4 4
315  
316 4 my ($cmd, $args) = split /[ \t]+/, $line, 2;
317  
318 4 my $method = '__onUciCmd' . ucfirst lc $cmd;
319 4 $args = $self->__trim($args);
320 4 ▶ if ($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 4 0 4
321 4 my $success = $self->$method($args);
322 # Was this go command cancelled because of an already running search?
323 # In this case, we have remembered the arguments, and can start a new
324 # search now.
325 #
326 # This will happen instantly. If a "go" command is sent during a
327 # running search, this will only be noticed during the time control
328 # check of the search tree. The "go" handler does not start a new
329 # search but simply cancels the current search. Because this happens
330 # during the time control check, the current search will terminate
331 # instantly, and we will end up here. Now, the current search will
332 # basically be replaced by a new one without recursion.
333 4 ▶ if ($success && 'go' eq lc $cmd && $self->{__go_queue} && !$self->{__tree}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success and "go" eq lc $cmd and $self->{"__go_queue"} and not $self->{"__tree"}) 0 4 4
Condition55%5/9
$success and "go" eq lc $cmd and $self->{"__go_queue"} and not $self->{"__tree"}
!ll&&!rl&&r
400
$success and "go" eq lc $cmd and $self->{"__go_queue"}
!ll&&!rl&&r
220
$success and "go" eq lc $cmd
!ll&&!rl&&r
022
MC/DC0%0/4
$success and "go" eq lc $cmd and $self->{"__go_queue"} and not $self->{"__tree"}
$success "go" eq lc $cmd $self->{"__go_queue"} not $self->{"__tree"}
Truth table40%2/5
A and B and C and not(D)
A= $successB= "go" eq lc $cmdC= $self->{"__go_queue"}D= not $self->{"__tree"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
334 0 $args = delete $self->{__go_queue};
335 0 $self->__onUciCmdGo($args);
336 }
337 } else {
338 0 $self->{__out}->print("info unknown command '$cmd'\n");
339 }
340  
341 4 return $self;
342 }
343  
344 0 ▶sub __onUciCmdFen {
Subroutine __onUciCmdFen: not called CC=2 SCAR=17.9
345 0 my ($self, $fh) = @_;
346  
347 0 ▶ $fh //= $self->{__out};
Condition0%0/3
$fh //= $self->{"__out"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$fh //= $self->{"__out"}
$fh $self->{"__out"}
Truth table0%0/3
A //= B
A= $fhB= $self->{"__out"}
ABresult
1X1
011
000
348  
349 0 $fh->print($self->{__position}->toFEN . "\n");
350  
351 0 return $self;
352 }
353  
354 0 ▶sub __onUciCmdPgn {
Subroutine __onUciCmdPgn: not called CC=14 SCAR=53.5
355 0 my ($self, $fh) = @_;
356  
357 0 ▶ $fh //= $self->{__out};
Condition0%0/3
$fh //= $self->{"__out"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$fh //= $self->{"__out"}
$fh $self->{"__out"}
Truth table0%0/3
A //= B
A= $fhB= $self->{"__out"}
ABresult
1X1
011
000
358  
359 0 my @now = localtime;
360 0 my $date = sprintf '%04d.%02d.%02d', $now[5] + 1900, $now[4] + 1, $now[3];
361  
362 0 my $pgn = <<"EOF";
363 [Event "Computer Chess Game"]
364 [Site "Computer Chess"]
365 [Date "$date"]
366 [Round "?"]
367 EOF
368  
369 0 ▶ my $version = $Chess::Plisco::Engine::VERSION || 'development version';
Condition0%0/2
$Chess::Plisco::Engine::VERSION || 'development version'
l!l
00
MC/DC0%0/1
$Chess::Plisco::Engine::VERSION || 'development version'
$Chess::Plisco::Engine::VERSION
Truth table0%0/2
A
A= $Chess::Plisco::Engine::VERSION
Aresult
11
00
370 0 ▶ if ($self->{__turn} == CP_WHITE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'__turn'} == 0) { } 0 0 0
371 0 $pgn .= << "EOF";
372 [White "Chess::Plisco $version"]
373 [Black "Unknown opponent"]
374 EOF
375 } else {
376 0 $pgn .= << "EOF";
377 [White "Unknown opponent"]
378 [Black "Chess::Plisco $version"]
379 EOF
380 }
381  
382 0 my $state = $self->{__position}->gameOver;
383 0 my $result;
384 0 ▶ if ($state & CP_GAME_OVER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state & 1) { } 0 0 0
385 0 ▶ if ($state & CP_GAME_WHITE_WINS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state & 2) { } 0 0 0
386 0 $result = '1-0';
387 } else {
388 0 $result = '0-1';
389 }
390 } else {
391 0 $result = '*';
392 }
393  
394 0 $pgn .= qq{[Result "$result"]\n};
395  
396 0 ▶ if ($self->{__setup}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__setup"}) 0 0 0
397 0 $pgn .= <<"EOF";
398 [SetUp "1"]
399 [FEN "$self->{__fen}"]
400 EOF
401 }
402  
403 0 $pgn .= "\n";
404  
405 0 ▶ my $fen = $self->{__setup} ? $self->{__fen} : Chess::Plisco->new->toFEN;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'__setup'} ? : 0 0 0
406 0 my $pos = Chess::Plisco->new($fen);
407  
408 0 my @move_tokens;
409 my $moveno;
410 0 ▶ if ($pos->toMove == CP_WHITE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos->toMove == 0) { } 0 0 0
411 0 $moveno = $pos->[CP_POS_HALFMOVES] >> 1;
412 } else {
413 0 $moveno = 1 + ($pos->[CP_POS_HALFMOVES] >> 1);
414 0 push @move_tokens, "$moveno...";
415 }
416  
417 0 foreach my $cn (@{$self->{__moves}}) {
418 0 ▶ if ($pos->toMove == CP_WHITE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos->toMove == 0) 0 0 0
419 0 ++$moveno;
420 0 push @move_tokens, "$moveno.";
421 }
422  
423 0 my $move = $pos->parseMove($cn);
424 0 push @move_tokens, $pos->SAN($move);
425 0 $pos->doMove($move);
426 }
427 0 push @move_tokens, $result;
428  
429 0 my $moves = join ' ', @move_tokens;
430 0 my $last_space = 0;
431 0 my $line_pos = 0;
432 0 my @chars = split //, $moves;
433 0 foreach my $char (@chars) {
434 0 ++$line_pos;
435  
436 0 ▶ if ($char eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq " ") 0 0 0
437 0 ▶ if ($line_pos > 78) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_pos > 78) 0 0 0
438 0 $chars[$last_space] = "\n";
439 0 $line_pos = 0;
440 }
441 }
442 }
443 0 chomp $moves;
444  
445 0 $pgn .= "$moves\n";
446  
447 0 $fh->print($pgn);
448 }
449  
450 0 ▶sub __onUciCmdBoard {
Subroutine __onUciCmdBoard: not called CC=1 SCAR=6.9
451 0 my ($self) = @_;
452  
453 0 my $board = $self->{__position}->board;
454 0 $self->{__out}->print($board);
455  
456 0 return;
457 }
458  
459 0 ▶sub __onUciCmdEvaluate {
Subroutine __onUciCmdEvaluate: not called CC=1 SCAR=6.9
460 0 my ($self) = @_;
461  
462 0 my $score = $self->{__position}->evaluate;
463  
464 0 my ($cp_pos_game_phase, $cp_pos_opening_score, $cp_pos_endgame_score) = (
465 Chess::Plisco::Engine::Position::CP_POS_GAME_PHASE,
466 Chess::Plisco::Engine::Position::CP_POS_OPENING_SCORE,
467 Chess::Plisco::Engine::Position::CP_POS_ENDGAME_SCORE,
468 );
469  
470 0 my $phase = $self->{__position}->[$cp_pos_game_phase];
471 0 my $op_score = $self->{__position}->[$cp_pos_opening_score];
472 0 my $eg_score = $self->{__position}->[$cp_pos_endgame_score];
473  
474 0 $self->{__out}->print("$score cp (phase: $phase, opening: $op_score, endgame: $eg_score)\n");
475  
476 0 return $self;
477 }
478  
479 0 ▶sub __onUciCmdSee {
Subroutine __onUciCmdSee: not called CC=3 SCAR=24.8
480 0 my ($self, $args) = @_;
481  
482 0 my $san = $self->__trim($args);
483 0 ▶ if (!length $san) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $san) 0 0 0
484 0 $self->{__out}->print("usage: see MOVE\n");
485 0 return $self;
486 }
487 0 my $position = $self->{__position};
488 0 my $move = $position->parseMove($san);
489 0 ▶ if (!$move) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($move) 0 0 0
490 0 $self->{__out}->print("error: invalid or illegal move '$san'\n");
491 0 return $self;
492 }
493  
494 0 my $score = $position->SEE($move);
495 0 $self->{__out}->print("$score cp\n");
496  
497 0 return $self;
498 }
499  
500 4 ▶sub __cancelSearch {
Subroutine __cancelSearch: called CC=2 SCAR=10.1
501 4 my ($self) = @_;
502  
503 4 ▶ return if !$self->{__tree};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__tree"} 4 0 4
504  
505 0 $self->{__watcher}->requestStop;
506 0 $self->{__tree}->{cancelled} = 1;
507  
508 0 return $self;
509 }
510  
511 2 ▶sub __onUciCmdGo {
Subroutine __onUciCmdGo: called CC=40 SCAR=46.5
512 2 my ($self, $args) = @_;
513  
514 2 $self->{__start_time} = [gettimeofday];
515  
516 2 ▶ if ($self->__cancelSearch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->__cancelSearch) 0 2 2
517 # Remember the arguments to this go command and re-try as soon as the
518 # currently still running search terminates.
519 0 $self->{__go_queue} = $args;
520 0 return;
521 }
522  
523 2 my @args = split /[ \t]+/, $args;
524  
525 2 my %params;
526  
527 2 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 8 2 10
528 8 my $arg = lc shift @args;
529 8 ▶ if ('searchmoves' eq $arg) {
Branch: 3 branches
BranchTrueFalseTotal
if ('searchmoves' eq $arg) { } 0 8 8
elsif ('ponder' eq $arg or 'infinite' eq $arg) { } 0 8 8
elsif ('wtime' eq $arg or 'btime' eq $arg or 'winc' eq $arg or 'binc' eq $arg or 'movestogo' eq $arg or 'depth' eq $arg or 'nodes' eq $arg or 'mate' eq $arg or 'movetime' eq $arg or 'perft' eq $arg) { } 8 0 8
530 0 my @searchmoves;
531 0 ▶ while (@args && $args[0] =~ /^[a-h][1-8][a-h][1-8][qrbn]?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and $args[0] =~ /^[a-h][1-8][a-h][1-8][qrbn]?$/) 0 0 0
Condition0%0/3
@args and $args[0] =~ /^[a-h][1-8][a-h][1-8][qrbn]?$/
!ll&&!rl&&r
000
MC/DC0%0/2
@args and $args[0] =~ /^[a-h][1-8][a-h][1-8][qrbn]?$/
@args $args[0] =~ /^[a-h][1-8][a-h][1-8][qrbn]?$/
Truth table0%0/3
A and B
A= @argsB= $args[0] =~ /^[a-h][1-8][a-h][1-8][qrbn]?$/
ABresult
0X0
100
111
532 0 push @searchmoves, shift @args;
533 }
534 ▶ } elsif ('ponder' eq $arg || 'infinite' eq $arg) {
Condition33%1/3
"ponder" eq $arg or "infinite" eq $arg
l!l&&r!l&&!r
008
MC/DC0%0/2
"ponder" eq $arg or "infinite" eq $arg
"ponder" eq $arg "infinite" eq $arg
Truth table33%1/3
A or B
A= "ponder" eq $argB= "infinite" eq $arg
ABresult
1X1
011
000
535 0 $params{$arg} = 1;
536 ▶ } elsif ('wtime' eq $arg || 'btime' eq $arg
Condition51%14/27
"wtime" eq $arg or "btime" eq $arg or "winc" eq $arg or "binc" eq $arg or "movestogo" eq $arg or "depth" eq $arg or "nodes" eq $arg or "mate" eq $arg or "movetime" eq $arg or "perft" eq $arg
l!l&&r!l&&!r
800
"wtime" eq $arg or "btime" eq $arg or "winc" eq $arg or "binc" eq $arg or "movestogo" eq $arg or "depth" eq $arg or "nodes" eq $arg or "mate" eq $arg or "movetime" eq $arg
l!l&&r!l&&!r
800
"wtime" eq $arg or "btime" eq $arg or "winc" eq $arg or "binc" eq $arg or "movestogo" eq $arg or "depth" eq $arg or "nodes" eq $arg or "mate" eq $arg
l!l&&r!l&&!r
800
"wtime" eq $arg or "btime" eq $arg or "winc" eq $arg or "binc" eq $arg or "movestogo" eq $arg or "depth" eq $arg or "nodes" eq $arg
l!l&&r!l&&!r
800
"wtime" eq $arg or "btime" eq $arg or "winc" eq $arg or "binc" eq $arg or "movestogo" eq $arg or "depth" eq $arg
l!l&&r!l&&!r
800
"wtime" eq $arg or "btime" eq $arg or "winc" eq $arg or "binc" eq $arg or "movestogo" eq $arg
l!l&&r!l&&!r
800
"wtime" eq $arg or "btime" eq $arg or "winc" eq $arg or "binc" eq $arg
l!l&&r!l&&!r
620
"wtime" eq $arg or "btime" eq $arg or "winc" eq $arg
l!l&&r!l&&!r
422
"wtime" eq $arg or "btime" eq $arg
l!l&&r!l&&!r
224
MC/DC0%0/10
"wtime" eq $arg or "btime" eq $arg or "winc" eq $arg or "binc" eq $arg or "movestogo" eq $arg or "depth" eq $arg or "nodes" eq $arg or "mate" eq $arg or "movetime" eq $arg or "perft" eq $arg
"wtime" eq $arg "btime" eq $arg "winc" eq $arg "binc" eq $arg "movestogo" eq $arg "depth" eq $arg "nodes" eq $arg "mate" eq $arg "movetime" eq $arg "perft" eq $arg
Truth table36%4/11
A or B or C or D or E or F or G or H or I or J
A= "wtime" eq $argB= "btime" eq $argC= "winc" eq $argD= "binc" eq $argE= "movestogo" eq $argF= "depth" eq $argG= "nodes" eq $argH= "mate" eq $argI= "movetime" eq $argJ= "perft" eq $arg
ABCDEFGHIJresult
1XXXXXXXXX1
01XXXXXXXX1
001XXXXXXX1
0001XXXXXX1
00001XXXXX1
000001XXXX1
0000001XXX1
00000001XX1
000000001X1
00000000011
00000000000
537 || 'winc' eq $arg || 'binc' eq $arg
538 || 'movestogo' eq $arg || 'depth' eq $arg
539 || 'nodes' eq $arg || 'mate' eq $arg
540 || 'movetime' eq $arg
541 || 'perft' eq $arg) {
542 8 my $val = shift @args;
543 8 ▶ $val ||= 0;
Condition50%1/2
$val ||= 0
l!l
80
MC/DC0%0/1
$val ||= 0
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
544 8 $val = +$val;
545 8 ▶ if ($val < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < 0) 0 8 8
546 0 $self->__info("error: argument '$arg' expects an integer > 0");
547 0 return;
548 }
549 8 $params{$arg} = $val;
550 }
551 }
552  
553 17 ▶ my $info = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
554 17 my ($msg) = @_;
555 17 $self->__output("info $msg");
556 2 };
557  
558 2 ▶ if ($params{perft}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"perft"}) 0 2 2
559 0 $self->{__position}->perftWithOutput($params{perft}, $self->{__out});
560 0 return $self;
561 }
562  
563 2 ▶ if ($self->{__options}->{'Batch'} eq 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'__options'}{'Batch'} eq 'true') { } 0 2 2
564 0 $self->{__watcher}->setBatchMode(1);
565 } else {
566 2 $self->{__watcher}->setBatchMode(0);
567 }
568  
569 my %options = (
570 position => $self->{__position}->copy,
571 tt => $self->{__tt},
572 watcher => $self->{__watcher},
573 info => $info,
574 signatures => $self->{__signatures},
575 tb => $self->{__tb},
576 tb_cardinality => $self->{__tb}->largestWdl,
577 tb_probe_depth => $self->{__options}->{SyzygyProbeDepth},
578 tb_probe_limit => $self->{__options}->{SyzygyProbeLimit},
579 tb_50_move_rule => $self->{__options}->{Syzygy50MoveRule},
580 tb_7 => $self->{__options}->{Syzygy7TimeCushion},
581 tb_3 => $self->{__options}->{Syzygy3TimeCushion},
582 2 );
583 2 ▶ if ($self->{__options}->{OwnBook} eq 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__options"}{"OwnBook"} eq "true") 0 2 2
584 0 $options{book} = $self->{__book};
585 0 $options{book_depth} = $self->{__options}->{BookDepth};
586 }
587  
588 2 my $tree = Chess::Plisco::Engine::Tree->new(%options);
589 2 ▶ $tree->{debug} = 1 if $self->{__debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__debug"} 0 2 2
590 2 ▶ $tree->{ponder} = 1 if $params{ponder};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"ponder"} 0 2 2
591 2 $tree->{start_time} = $self->{__start_time};
592 2 ▶ $tree->{score} = $self->{__continued} ? $self->{__last_score} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'__continued'} ? : 1 1 2
593 2 ▶ if ($params{depth}) {
Branch: 5 branches
BranchTrueFalseTotal
if ($params{'depth'}) { } 0 2 2
elsif ($params{'mate'}) { } 0 2 2
elsif ($params{'movetime'}) { } 0 2 2
elsif ($params{'infinite'}) { } 0 2 2
elsif ($params{'node'}) { } 0 2 2
594 0 $tree->{max_depth} = $params{depth};
595 } elsif ($params{mate}) {
596 0 $tree->{max_depth} = 2 * $params{mate} - 1;
597 } elsif ($params{movetime}) {
598 0 $tree->{maximum} = $tree->{optimum} = $params{movetime};
599 0 $tree->{fixed_time} = 1;
600 } elsif ($params{infinite}) {
601 0 $tree->{max_depth} = MAX_PLY;
602 } elsif ($params{node}) {
603 0 $tree->{max_nodes} = $params{node};
604 } else {
605 2 $tree->{use_time_management} = 1;
606 }
607  
608 2 ▶ if ($self->{__continued}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__continued"}) 1 1 2
609 1 $self->{__continued} = $self->__measureMoveOverhead(%params)
610 }
611 2 $self->{__last_params} = {%params};
612  
613 2 my $tc = $self->{__tc} = Chess::Plisco::Engine::TimeControl->new($tree);
614  
615 2 my $limits = Chess::Plisco::Engine::LimitsType->new;
616 # $limits->{moves} = FIXME!
617 2 $limits->{time}->[0] = $params{wtime};
618 2 $limits->{time}->[1] = $params{btime};
619 2 $limits->{inc}->[0] = $params{winc};
620 2 $limits->{inc}->[1] = $params{binc};
621 2 $limits->{start_time} = $self->{__start_time};
622 2 $limits->{movestogo} = $params{movestogo};
623 2 $limits->{depth} = $params{depth};
624 #$limits-{mate} = FIXME!
625 2 $limits->{infinite} = $params{infinite};
626 2 $limits->{ponder} = $params{ponder};
627 2 $limits->{nodes} = $params{nodes};
628  
629 2 ▶ $params{move_overhead} = $self->{__move_overhead_fixed} ? $self->{__options}->{'Move Overhead'} : $self->{__move_overhead};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'__move_overhead_fixed'} ? : 0 2 2
630  
631 $tc->init(
632 $limits, $tree->{position}->turn,
633 $tree->{position}->[CP_POS_HALFMOVES] + 1,
634 \%params, \$self->{__original_time_adjust},
635 2 );
636  
637 # This is the logic from checkTime() of the tree.
638 {
639 1 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
640 2 my $max_nodes = $self->{__nps} >> 2;
641 2 my $dyn_nodes = ($tree->{maximum} * $self->{__nps}) >> 13;
642 2 ▶ $tree->{nodes_to_tc} = ($max_nodes < $dyn_nodes) ? $max_nodes : $dyn_nodes;
Branch: 1 branch
BranchTrueFalseTotal
$max_nodes < $dyn_nodes ? : 0 2 2
643 2 ▶ $tree->{nodes_to_tc} = 50 if $tree->{nodes_to_tc} < 50;
Branch: 1 branch
BranchTrueFalseTotal
if $tree->{"nodes_to_tc"} < 50 1 1 2
644 2 ▶ $tree->{nodes_to_tc} = 5000 if $tree->{nodes_to_tc} > 5000;
Branch: 1 branch
BranchTrueFalseTotal
if $tree->{"nodes_to_tc"} > 5000 0 2 2
645 }
646  
647 2 $self->{__tree} = $tree;
648  
649 2 my ($bestmove, $ponder);
650 2 eval {
651 2 ($bestmove, $ponder) = $tree->think;
652 };
653 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
654 0 $self->__output("unexpected exception: $@");
655 }
656 {
657 1 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
658 2 $self->{__last_search_time} = int(1000 * tv_interval($self->{__start_time}));
659 }
660 2 ▶ my $elapsed = $self->{__last_search_time} || 1;
Condition50%1/2
$self->{'__last_search_time'} || 1
l!l
20
MC/DC0%0/1
$self->{'__last_search_time'} || 1
$self->{'__last_search_time'}
Truth table50%1/2
A
A= $self->{'__last_search_time'}
Aresult
11
00
661 2 $self->{__nps} = (1000 * $tree->{nodes}) / $elapsed;
662  
663 2 delete $self->{__tree};
664 2 delete $self->{__tc};
665  
666 2 ▶ if ($bestmove) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bestmove) { } 2 0 2
667 2 $self->{__last_score} = $tree->{score};
668 2 my $cn = $self->{__position}->moveCoordinateNotation($bestmove);
669 2 ▶ if (defined $ponder) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ponder) { } 2 0 2
670 2 my $pcn = $self->{__position}->copy->moveCoordinateNotation($ponder);
671 2 $self->__output("bestmove $cn ponder $pcn");
672 } else {
673 0 $self->__output("bestmove $cn");
674 }
675 2 push @{$self->{__moves}}, $cn;
676 } else {
677 0 $self->__output("bestmove (none)");
678 }
679  
680 2 $self->{__watcher}->setBatchMode(0);
681  
682  
683 2 return $self;
684 }
685  
686 1 ▶sub __measureMoveOverhead {
Subroutine __measureMoveOverhead: called CC=10 SCAR=24.7
687 1 my ($self, %params) = @_;
688  
689 1 ▶ return if !defined $self->{__last_search_time};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"__last_search_time"} 0 1 1
690  
691 1 ▶ my $my_time = $self->{__position}->turn == CP_WHITE ? 'wtime' : 'btime';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'__position'}->turn == 0 ? : 1 0 1
692 1 ▶ my $my_inc = $self->{__position}->turn == CP_WHITE ? 'winc' : 'binc';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'__position'}->turn == 0 ? : 1 0 1
693  
694 1 my $last_params = $self->{__last_params};
695 1 ▶ return if !($params{$my_time} && $last_params->{$my_time});
Branch: 1 branch
BranchTrueFalseTotal
unless $params{$my_time} and $last_params->{$my_time} 0 1 1
Condition33%1/3
$params{$my_time} and $last_params->{$my_time}
!ll&&!rl&&r
001
MC/DC0%0/2
$params{$my_time} and $last_params->{$my_time}
$params{$my_time} $last_params->{$my_time}
Truth table33%1/3
A and B
A= $params{$my_time}B= $last_params->{$my_time}
ABresult
0X0
100
111
696 1 ▶ return if $params{$my_inc} != $last_params->{$my_inc};
Branch: 1 branch
BranchTrueFalseTotal
if $params{$my_inc} != $last_params->{$my_inc} 0 1 1
697  
698 1 my $expected_time = $last_params->{$my_time} + $last_params->{$my_inc} - $self->{__last_search_time};
699 1 my $overhead = $expected_time - $params{$my_time};
700 1 ▶ if ($overhead >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overhead >= 0) 1 0 1
701 1 push @{$self->{__move_overheads}}, $overhead;
702 1 ▶ push @{$self->{__move_overheads}}, $overhead if @{$self->{__move_overheads}} < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"__move_overheads"};} < 2 1 0 1
703 0 ▶ shift @{$self->{__move_overheads}} if @{$self->{__move_overheads}} > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"__move_overheads"};} > 3 0 1 1
704 1 my @values = sort { $a <=> $b } @{$self->{__move_overheads}};
705 1 $self->{__options}->{'Move Overhead'} = $values[1];
706 }
707  
708 1 return $self;
709 }
710  
711 0 ▶sub __onUciCmdPonderhit {
Subroutine __onUciCmdPonderhit: not called CC=2 SCAR=17.9
712 0 my ($self) = @_;
713  
714 0 ▶ return if !$self->{__tc};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__tc"} 0 0 0
715  
716 0 delete $self->{__tree}->{ponder};
717  
718 0 return $self;
719 }
720  
721 0 ▶sub __onUciCmdUcinewgame {
Subroutine __onUciCmdUcinewgame: not called CC=1 SCAR=6.9
722 0 my ($self) = @_;
723  
724 0 $self->__cancelSearch;
725  
726 0 $self->{__tt}->clear;
727 0 $self->{__last_score} = 0;
728  
729 0 return $self;
730 }
731  
732 0 ▶sub __onUciCmdSetoption {
Subroutine __onUciCmdSetoption: not called CC=14 SCAR=53.5
733 0 my ($self, $args) = @_;
734  
735 0 ▶ if ($args !~ /^name[ \t]+(.*?)(?:value[ \t]+(.*))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args =~ /^name[ \t]+(.*?)(?:value[ \t]+(.*))?$/) 0 0 0
736 0 $self->__output("info Error: usage setoption name NAME[ value VALUE]");
737 0 return $self;
738 }
739  
740 0 my ($name, $value) = map { $self->__trim($_) } ($1, $2);
741 0 ▶ $name = 'Move Overhead' if $name eq 'MoveOverhead';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "MoveOverhead" 0 0 0
742 0 ▶ if (!exists $uci_options{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $uci_options{$name}) 0 0 0
743 0 $self->__output("info Error: unsupported option '$name'");
744 0 return $self;
745 }
746  
747 0 my $option = $uci_options{$name};
748  
749 0 ▶ if (exists $option->{min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"min"}) 0 0 0
750 0 my $min = $option->{min};
751 0 ▶ if (($value || 0) < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (($value || 0) < $min) 0 0 0
Condition0%0/2
$value || 0
l!l
00
MC/DC0%0/1
$value || 0
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
752 0 $self->__output("info Error: minimum value for option"
753 . " '$name' is $min");
754 0 return $self;
755 }
756 }
757  
758 0 ▶ if (exists $option->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"max"}) 0 0 0
759 0 my $max = $option->{max};
760 0 ▶ if (($value || 0) > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (($value || 0) > $max) 0 0 0
Condition0%0/2
$value || 0
l!l
00
MC/DC0%0/1
$value || 0
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
761 0 $self->__output("info Error: maximum value for option"
762 ." '$name' is $max");
763 0 return $self;
764 }
765 }
766  
767 0 ▶ if ('check' eq $option->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ("check" eq $option->{"type"}) 0 0 0
768 0 ▶ if ($value ne 'true' && $value ne 'false') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne "true" and $value ne "false") 0 0 0
Condition0%0/3
$value ne "true" and $value ne "false"
!ll&&!rl&&r
000
MC/DC0%0/2
$value ne "true" and $value ne "false"
$value ne "true" $value ne "false"
Truth table0%0/3
A and B
A= $value ne "true"B= $value ne "false"
ABresult
0X0
100
111
769 0 $self->__output("info Error: only 'true' and 'false' are allowed"
770 . " for option '$name'");
771 0 return $self;
772 }
773 }
774  
775 0 $self->{__options}->{$name} = $value;
776  
777 0 ▶ if (exists $option->{callback}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"callback"}) 0 0 0
778 0 my $method = $option->{callback};
779 0 $self->$method($value);
780 }
781  
782 0 return $self;
783 }
784  
785 0 ▶sub __resizeTranspositionTable {
Subroutine __resizeTranspositionTable: not called CC=1 SCAR=6.9
786 0 my ($self, $size) = @_;
787  
788 0 $self->{__tt}->resize($size);
789 }
790  
791 0 ▶sub __clearTranspositionTable {
Subroutine __clearTranspositionTable: not called CC=1 SCAR=6.9
792 0 my ($self, $size) = @_;
793  
794 0 $self->{__tt}->clear;
795 }
796  
797 0 ▶sub __changeBatchMode {
Subroutine __changeBatchMode: not called CC=2 SCAR=17.9
798 0 my ($self, $value) = @_;
799  
800 0 ▶ if ('true' eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ("true" eq $value) 0 0 0
801 0 $self->__output("info all commands are ignored during search in"
802 . " batch mode!")
803 }
804 }
805  
806 0 ▶sub __setBookFile {
Subroutine __setBookFile: not called CC=2 SCAR=17.9
807 0 my ($self, $value) = @_;
808  
809 0 ▶ if ($self->{__options}->{OwnBook} ne 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__options"}{"OwnBook"} ne "true") 0 0 0
810 0 $self->__info("Warning: book file will not be used, when OwnBook is set to 'false'!");
811 }
812  
813 0 ▶ my $callback = sub { $self->__info(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
814 0 $self->{__book}->setFile($value, $callback);
815  
816 0 return $self;
817 }
818  
819 0 ▶sub __setMoveOverhead {
Subroutine __setMoveOverhead: not called CC=1 SCAR=6.9
820 0 my ($self, $value) = @_;
821  
822 0 $self->{__move_overhead_fixed} = 1;
823  
824 0 $self->__info('');
825  
826 0 return $self;
827 }
828  
829 0 ▶sub __setSyzygyPath {
Subroutine __setSyzygyPath: not called CC=3 SCAR=24.8
830 0 my ($self, $value) = @_;
831  
832 0 ▶ my $path_sep = $^O eq 'MSWin32' ? ';' : ':';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
833  
834 0 my @directories = map { $self->__trim($_) } split $path_sep, $value;
835  
836 0 $self->{__tb} = Chess::Plisco::Tablebase::Syzygy->new(undef);
837 0 foreach my $directory (@directories) {
838 0 $self->{__tb}->addDirectory($directory, recursive => 1);
839 }
840  
841 0 my $largest = $self->{__tb}->largestWdl;
842 0 my $num_wdl = $self->{__tb}->numWdlFiles;
843 0 my $num_dtz = $self->{__tb}->numDtzFiles;
844 0 $self->__info("Found $num_wdl WDL and $num_dtz DTZ tablebase files (up to $largest-man)");
845  
846 0 return $self;
847 }
848  
849 0 ▶sub __onUciCmdStop {
Subroutine __onUciCmdStop: not called CC=2 SCAR=17.9
850 0 my ($self) = @_;
851  
852 0 ▶ if ($self->{__tree}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__tree"}) 0 0 0
853 0 $self->__cancelSearch;
854  
855 # Apparently, all GUIs send a "stop" to cancel a (failed) ponder. And
856 # They also expect a "bestmove" reply. But the tree suppresses that
857 # "bestmove" reply, when the "cancelled" flag is set. We therefore
858 # unset the flag here.
859 0 delete $self->{__tree}->{cancelled};
860 }
861  
862 0 return $self;
863 }
864  
865 2 ▶sub __movelistContinuous {
Subroutine __movelistContinuous: called CC=4 SCAR=13.9
866 2 my ($self, $last_moves) = @_;
867  
868 2 ▶ return if @{$self->{__moves}} - @$last_moves != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"__moves"};} - @$last_moves != 1 1 1 2
869  
870 1 ▶ for (my $i = 0; $i < @$last_moves; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$last_moves) 1 1 2
871 1 ▶ return if $self->{__moves}->[$i] ne $last_moves->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__moves"}[$i] ne $last_moves->[$i] 0 1 1
872 }
873  
874 1 return $self;
875 }
876  
877 2 ▶sub __onUciCmdPosition {
Subroutine __onUciCmdPosition: called CC=19 SCAR=44.5
878 2 my ($self, $args) = @_;
879  
880 2 $self->__cancelSearch;
881  
882 2 ▶ unless (defined $args && length $args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args and length $args) 0 2 2
Condition33%1/3
defined $args and length $args
!ll&&!rl&&r
002
MC/DC0%0/2
defined $args and length $args
defined $args length $args
Truth table33%1/3
A and B
A= defined $argsB= length $args
ABresult
0X0
100
111
883 0 $self->__info("error: usage: position FEN POSITION | startpos [MOVES...]");
884 0 return;
885 }
886  
887 2 my ($type, @moves) = split /[ \t]+/, $args;
888 2 my $position;
889 2 $self->{__started} = time;
890  
891 2 my $last_setup = $self->{__setup};
892 2 my $last_fen = $self->{__fen};
893 2 my @last_moves = @{$self->{__moves}};
894  
895 2 ▶ if ('fen' eq lc $type) {
Branch: 2 branches
BranchTrueFalseTotal
if ('fen' eq lc $type) { } 0 2 2
elsif ('startpos' eq lc $type) { } 2 0 2
896 0 my $fen = shift @moves;
897 0 ▶ unless (defined $fen && length $fen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fen and length $fen) 0 0 0
Condition0%0/3
defined $fen and length $fen
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fen and length $fen
defined $fen length $fen
Truth table0%0/3
A and B
A= defined $fenB= length $fen
ABresult
0X0
100
111
898 0 $self->__info("error: position missing after 'fen'");
899 0 return;
900 }
901  
902 0 $self->{__moves} = [@moves];
903  
904 0 ▶ while (@moves) {
Branch: 1 branch
BranchTrueFalseTotal
if (@moves) 0 0 0
905 0 ▶ if ('moves' eq $moves[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ("moves" eq $moves[0]) 0 0 0
906 0 last;
907 }
908 0 $fen .= ' ' . shift @moves;
909 }
910 0 eval {
911 0 $position = Chess::Plisco::Engine::Position->new($fen);
912 };
913 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
914 0 $self->__info("error: invalid FEN string: $@");
915 0 return;
916 }
917 0 $self->{__setup} = 1;
918 0 $self->{__fen} = $fen;
919 0 $self->{__turn} = $position->turn;
920 } elsif ('startpos' eq lc $type) {
921 2 $self->{__setup} = 0;
922 2 $self->{__turn} = CP_WHITE;
923 2 $position = Chess::Plisco::Engine::Position->new;
924 } else {
925 0 $self->__info("error: usage: position FEN POSITION | startpos moves [MOVES...]");
926 0 return;
927 }
928  
929 2 $self->{__moves} = [];
930 2 my @signatures = ($position->signature);
931 2 ▶ if ('moves' eq shift @moves) {
Branch: 1 branch
BranchTrueFalseTotal
if ("moves" eq shift @moves) 1 1 2
932 1 $self->{__moves} = [@moves];
933 1 ▶ for (my $i = 0; $i < @moves; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @moves) 2 1 3
934 2 my $move = $moves[$i];
935 2 eval { $position->applyMove($move) };
936 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
937 0 $@ =~ s/(.+) at .*/$1/s;
938 0 $moves[$i] = ">>>$move<<<";
939 0 my $moves = join ' ', @moves;
940 0 $self->__info("Error with given moves: $moves: $@");
941 0 return;
942 }
943 2 push @signatures, $position->signature;
944 }
945 }
946  
947 # Check whether this continues an ongoing game.
948 $self->{__continued} = $self->{__fen} eq $last_fen
949 2 ▶ && $self->{__setup} == $last_setup
Condition50%3/6
$self->{'__fen'} eq $last_fen && $self->{'__setup'} == $last_setup && $self->__movelistContinuous(\@last_moves)
!ll&&!rl&&r
011
$self->{"__fen"} eq $last_fen and $self->{"__setup"} == $last_setup
!ll&&!rl&&r
002
MC/DC33%1/3
$self->{'__fen'} eq $last_fen && $self->{'__setup'} == $last_setup && $self->__movelistContinuous(\@last_moves)
$self->{"__fen"} eq $last_fen $self->{"__setup"} == $last_setup $self->__movelistContinuous(\@last_moves)
Truth table50%2/4
A and B && C
A= $self->{"__fen"} eq $last_fenB= $self->{"__setup"} == $last_setupC= $self->__movelistContinuous(\@last_moves)
ABCresult
0XX0
10X0
1100
1111
950 && $self->__movelistContinuous(\@last_moves);
951  
952 2 $self->{__position} = $position;
953 2 $self->{__signatures} = \@signatures;
954  
955 2 ▶ if (length $self->{__fen_out}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"__fen_out"}) 0 2 2
956 0 ▶ if (open my $fh, '>', $self->{__fen_out}) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>', $self->{'__fen_out'}) { } 0 0 0
957 0 $self->__onUciCmdFen($fh);
958 } else {
959 $self->__info(__x("error: cannot open '{filename}' for writing: {err}!",
960 filename => $self->{__fen_out},
961 0 error => $!));
962 }
963 }
964  
965 2 ▶ if (length $self->{__pgn_out}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"__pgn_out"}) 0 2 2
966 0 ▶ if (open my $fh, '>', $self->{__pgn_out}) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>', $self->{'__pgn_out'}) { } 0 0 0
967 0 $self->__onUciCmdPgn($fh);
968 } else {
969 $self->__info(__x("error: cannot open '{filename}' for writing: {err}!",
970 filename => $self->{__pgn_out},
971 0 error => $!));
972 }
973 }
974  
975 2 return $self;
976 }
977  
978 0 ▶sub __onUciCmdHelp {
Subroutine __onUciCmdHelp: not called CC=1 SCAR=6.9
979 0 my ($self) = @_;
980  
981 0 $self->__output(<<"EOF");
982 The Plisco Chess Engine
983  
984 The engine understands the following commands:
985  
986 uci - switch to UCI mode (no-op)
987 debug (on|off) - switch debugging on or off
988 go [depth, wtime, btime, ... see protocol!]
989 go perft DEPTH - do performance test (blocks engine, hit CTRL-C ...)
990 setoption name NAME[ value VALUE] - set option NAME to VALUE
991 isready - ping the engine
992 stop - move immediately
993 fen - print the current position as FEN
994 board - print a compact representation of the board
995 evaluate - print the static score of the current position
996 see MOVE - do a static exchange evaluation for MOVE
997 help - show available commands
998 quit - quit the engine immediately
999  
1000 See https://gist.github.com/DOBRO/2592c6dad754ba67e6dcaec8c90165bf for
1001 more information!
1002  
1003 In batch mode, the engine is unresponsive during searches.
1004 EOF
1005  
1006 0 return;
1007 }
1008  
1009 0 ▶sub __onUciCmdQuit {
Subroutine __onUciCmdQuit: not called CC=1 SCAR=6.9
1010 0 my ($self) = @_;
1011  
1012 0 exit;
1013 }
1014  
1015 0 ▶sub __onUciCmdUci {
Subroutine __onUciCmdUci: not called CC=5 SCAR=34.0
1016 0 my ($self) = @_;
1017  
1018 0 ▶ my $version = $Chess::Plisco::Engine::VERSION || 'development version';
Condition0%0/2
$Chess::Plisco::Engine::VERSION || 'development version'
l!l
00
MC/DC0%0/1
$Chess::Plisco::Engine::VERSION || 'development version'
$Chess::Plisco::Engine::VERSION
Truth table0%0/2
A
A= $Chess::Plisco::Engine::VERSION
Aresult
11
00
1019 0 $self->__output("id name Plisco $version");
1020 0 $self->__output("id author Guido Flohr <guido.flohr\@cantanea.com>");
1021  
1022 0 my $options = UCI_OPTIONS;
1023 0 foreach my $option (@{$options}) {
1024 0 my $output = "option name $option->{name} type $option->{type}";
1025 0 $output .= " default $option->{default}";
1026 0 ▶ $output .= " min $option->{min}" if exists $option->{min};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option->{"min"} 0 0 0
1027 0 ▶ $output .= " max $option->{max}" if exists $option->{max};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option->{"max"} 0 0 0
1028 0 $self->__output($output);
1029 }
1030  
1031 0 $self->__output("uciok");
1032  
1033 0 return;
1034 }
1035  
1036 0 ▶sub __onUciCmdIsready {
Subroutine __onUciCmdIsready: not called CC=1 SCAR=6.9
1037 0 my ($self) = @_;
1038  
1039 0 $self->__output("readyok");
1040  
1041 0 return;
1042 }
1043  
1044 0 ▶sub __onUciCmdDebug {
Subroutine __onUciCmdDebug: not called CC=4 SCAR=30.0
1045 0 my ($self, $onoff) = @_;
1046  
1047 0 ▶ $onoff = '' if !defined $onoff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $onoff 0 0 0
1048  
1049 0 ▶ if ('on' eq lc $onoff) {
Branch: 2 branches
BranchTrueFalseTotal
if ('on' eq lc $onoff) { } 0 0 0
elsif ('off' eq lc $onoff) { } 0 0 0
1050 0 $self->{__debug} = 1;
1051 } elsif ('off' eq lc $onoff) {
1052 0 undef $self->{__debug};
1053 } else {
1054 0 $self->__info("usage debug on|off");
1055 }
1056  
1057 0 return;
1058 }
1059  
1060 19 ▶sub __output {
Subroutine __output: called CC=1 SCAR=0.0
1061 19 my ($self, $msg) = @_;
1062  
1063 19 chomp $msg;
1064  
1065 19 $self->{__out}->print("$msg\n");
1066  
1067 19 return $self;
1068 }
1069  
1070 0 ▶sub __info {
Subroutine __info: not called CC=1 SCAR=6.9
1071 0 my ($self, $msg) = @_;
1072  
1073 0 return $self->__output("info $msg");
1074 }
1075  
1076 0 ▶sub __debug {
Subroutine __debug: not called CC=1 SCAR=6.9
1077 0 my ($self, $msg) = @_;
1078  
1079 0 return $self->__info("debug $msg");
1080 }
1081  
1082 8 ▶sub __trim {
Subroutine __trim: called CC=1 SCAR=0.0
1083 8 my ($self, $what) = @_;
1084  
1085 8 $what =~ s/^[ \t]+//;
1086 8 $what =~ s/[ \t\r\n]+$//;
1087  
1088 8 return $what;
1089 }
1090  
1091 1 1;
« Plisco/Tablebase.pm ↑ Summary Plisco/Engine/Win32Wrapper.pm »