| 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::Tablebase; | ||||||||||||||||||||||||||||
| 13 | 10 | $Chess::Plisco::Tablebase::VERSION = 'v1.0.3'; | |||||||||||||||||||||||||||
| 14 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 10 | ▶ | use Chess::Plisco::Engine::Position; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub getPV { | ||||||||||||||||||||||||||
|
Subroutine
getPV: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 0 | my ($self, $_pos) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Upgrade the position and make a copy of it. | ||||||||||||||||||||||||||||
| 23 | 0 | my $pos = Chess::Plisco::Engine::Position->newFromFEN($_pos->toFEN); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | my $wdl = $self->probeWdl($pos); | |||||||||||||||||||||||||||
| 26 | 0 | my $minimise_dtz; | |||||||||||||||||||||||||||
| 27 | 0 | ▶ | if ($wdl > 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 28 | 0 | $minimise_dtz = 1; | |||||||||||||||||||||||||||
| 29 | } elsif ($wdl < 0) { | ||||||||||||||||||||||||||||
| 30 | 0 | $minimise_dtz = 0; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | my @pv; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | MOVE: while (!$pos->gameOver) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | $wdl = -$wdl; | |||||||||||||||||||||||||||
| 36 | 0 | my ($best_dtz, $best_move); | |||||||||||||||||||||||||||
| 37 | 0 | my @moves = $pos->legalMoves; | |||||||||||||||||||||||||||
| 38 | 0 | my @backup = @$pos; | |||||||||||||||||||||||||||
| 39 | 0 | foreach my $move (@moves) { | |||||||||||||||||||||||||||
| 40 | 0 | $pos->move($move); | |||||||||||||||||||||||||||
| 41 | 0 | my $next_wdl = $self->safeProbeWdl($pos); | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | last MOVE if !defined $next_wdl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ($next_wdl != $wdl) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | @$pos = @backup; | |||||||||||||||||||||||||||
| 45 | 0 | next; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my $next_dtz = abs $self->safeProbeDtz($pos); | |||||||||||||||||||||||||||
| 49 | 0 | @$pos = @backup; | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | next if !defined $next_dtz; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if (!defined $best_dtz || ($minimise_dtz != ($next_dtz > $best_dtz))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $best_dtz or $minimise_dtz != $next_dtz > $best_dtz
MC/DC0%0/2
not defined $best_dtz or $minimise_dtz != $next_dtz > $best_dtz
not defined $best_dtz
$minimise_dtz != $next_dtz > $best_dtz
Truth table0%0/3
not(A) or B
A= not defined $best_dtzB= $minimise_dtz != $next_dtz > $best_dtz
| |||||||||||||||||||||||||||||
| 53 | 0 | $best_dtz = $next_dtz; | |||||||||||||||||||||||||||
| 54 | 0 | $best_move = $move; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | push @pv, $best_move; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # If the game is a draw, we decide from move to move what seems to be | ||||||||||||||||||||||||||||
| 61 | # the better strategy, otherwise, the perspective changes with every | ||||||||||||||||||||||||||||
| 62 | # move. | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($wdl == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | $minimise_dtz = $self->__getMinimiseDtz($pos); | |||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | 0 | $minimise_dtz = !$minimise_dtz; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | die "should not happen" if !$best_move; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | $pos->move($best_move); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | return @pv; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub __getMinimiseDtz { | ||||||||||||||||||||||||||
|
Subroutine
__getMinimiseDtz: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 0 | my ($self, $pos) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # This assumes that the position is a draw. The side that seems to be | ||||||||||||||||||||||||||||
| 80 | # ahead will try to minimise the DTZ. | ||||||||||||||||||||||||||||
| 81 | 0 | my $score = $pos->evaluate; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | return $score < 0; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 10 | 1; | |||||||||||||||||||||||||||