File Coverage

blib/lib/Chess/Opening/Book/Entry.pm
Criterion Covered Total %
statement 30 37 81.0
branch 9 14 64.2
condition 6 12 50.0
subroutine 9 9 100.0
pod 5 5 100.0
total 59 77 76.6


line stmt bran cond sub pod time code
1             #! /bin/false
2              
3             # Copyright (C) 2019 Guido Flohr ,
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::Opening::Book::Entry;
13             $Chess::Opening::Book::Entry::VERSION = '0.6';
14 3     3   26 use common::sense;
  3         7  
  3         35  
15              
16 3     3   184 use Locale::TextDomain 'com.cantanea.Chess-Opening';
  3         7  
  3         19  
17 3     3   448 use Scalar::Util qw(reftype);
  3         6  
  3         139  
18              
19 3     3   1325 use Chess::Opening::Book::Move;
  3         6  
  3         1564  
20              
21             sub new {
22 115     115 1 113205 my ($class, $fen, %args) = @_;
23              
24 115         597 my $self = bless {
25             __fen => $fen,
26             __moves => {},
27             __count => 0,
28             __length => -1,
29             __significant => -1,
30             __history => [],
31             }, $class;
32              
33 115 100       313 $self->{__length} = $args{length} if exists $args{length};
34 115 100       249 $self->{__significant} = $args{significant} if exists $args{significant};
35 115 50 66     277 if (exists $args{history} && ref $args{history}
      33        
36             && 'ARRAY' eq $args{history}) {
37 0         0 $self->{__history} = $args{history};
38             }
39              
40 115         309 return $self;
41             }
42              
43             sub addMove {
44 289     289 1 1440 my ($self, %args) = @_;
45              
46 289 50       651 if (!exists $args{move}) {
47 0         0 require Carp;
48 0         0 Carp::croak(__x("the named argument '{arg}' is required",
49             arg => 'move'));
50             }
51 289 50       1066 if ($args{move} !~ /^[a-h][1-8][a-h][1-8][qrbn]?$/) {
52 0         0 require Carp;
53 0         0 Carp::croak(__x("invalid move '{move}'",
54             move => '$args{move}'));
55             }
56 289 50       610 $args{count} = $args{weight} if exists $args{weight};
57 289 50 66     1591 if (exists $args{count} && $args{count}
      33        
58             && $args{count} !~ /^[1-9][0-9]*$/) {
59 0         0 require Carp;
60 0         0 Carp::croak(__"count must be a positive integer");
61             }
62              
63 289         1064 my $move = Chess::Opening::Book::Move->new(%args);
64 289         718 $self->{__moves}->{$args{move}} = $move;
65 289         773 $self->{__counts} += $move->count;
66              
67 289         849 return $self;
68             }
69              
70 3     3 1 1723 sub moves { shift->{__moves} }
71 3     3 1 2069 sub counts { shift->{__counts} }
72 3     3 1 1746 sub weights { shift->{__counts} }
73              
74             1;