| 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 | # Make Dist::Zilla happy. | ||||||||||
| 13 | # ABSTRACT: Opening book for Chess-Plisco engine. | ||||||||||
| 14 | |||||||||||
| 15 | package Chess::Plisco::Engine::Book; | ||||||||||
| 16 | 1 | $Chess::Plisco::Engine::Book::VERSION = 'v1.0.3'; | |||||||||
| 17 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | use Chess::Opening::Book::Polyglot; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 22 | 1 | my ($class, $engine) = @_; | |||||||||
| 23 | |||||||||||
| 24 | 1 | bless {}, $class; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | sub setFile { | ||||||||
|
Subroutine
setFile: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 28 | 0 | my ($self, $filename, $callback) = @_; | |||||||||
| 29 | |||||||||||
| 30 | 0 | my $book = eval { Chess::Opening::Book::Polyglot->new($filename) }; | |||||||||
| 31 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | $callback->("Error opening book file: $@"); | |||||||||
| 33 | } else { | ||||||||||
| 34 | 0 | $callback->("Using book file '$filename'"); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 0 | $self->{__book} = $book; | |||||||||
| 38 | |||||||||||
| 39 | 0 | return $self; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | 0 | ▶ | sub pickMove { | ||||||||
|
Subroutine
pickMove: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||
| 44 | 0 | my ($self, $pos) = @_; | |||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | return if !$self->{__book}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | 0 | my $fen = $pos->toFEN; | |||||||||
| 49 | 0 | my $entry = $self->{__book}->lookupFEN($pos); | |||||||||
| 50 | 0 | ▶ | return if !$entry; # Out of book. | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | # Moves here are moves in algebraic (UCI notation). | ||||||||||
| 53 | 0 | my $moves = $entry->moves; | |||||||||
| 54 | 0 | ▶ | my @moves = keys %$moves or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | my $total = 0; | |||||||||
| 56 | 0 | my %weights; | |||||||||
| 57 | |||||||||||
| 58 | 0 | foreach my $move (@moves) { | |||||||||
| 59 | 0 | my $node = $moves->{$move}; | |||||||||
| 60 | 0 | my $weight = $node->weight; | |||||||||
| 61 | 0 | $total += $weight; | |||||||||
| 62 | 0 | $weights{$move} = $weight; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | my $r = int rand $total; | |||||||||
| 66 | 0 | my $cumulative; | |||||||||
| 67 | 0 | foreach my $move (@moves) { | |||||||||
| 68 | 0 | $cumulative += $weights{$move}; | |||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | return $move if $r < $cumulative; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | # Out of book. | ||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 1 | 1; | |||||||||