blib/lib/Game/Go/Engine.pm

filter: Statementstmt 98.8% 88 / 89 Branchbran 85.0% 34 / 40 Conditioncond 63.3% 19 / 30 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 97.7% 44 / 45 Podpod 100% 39 / 39 totaltotal 88.3% 228 / 258 CC 35 SCAR 10.9
CC
35
Cov
89%
new
7.0
search
6.6
score
6.6
CRAP 3.0
« blib/lib/Game/Go/Scoring.pm ↑ Summary blib/lib/Game/Go/Rules.pm »
1 package Game::Go::Engine;
2  
3 26 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 our $VERSION = '0.02';
8  
9 26 require XSLoader;
10 26 XSLoader::load('Game::Go', $VERSION);
11  
12 26 ▶use Game::Go::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 494 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
15 494 my ($class, %o) = @_;
16 494 ▶ my $size = defined $o{size} ? $o{size} : 19;
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'size'} ? : 494 0 494
17 494 ▶ die "Game::Go::Engine: size must be an integer from 2 to 19, not '$size'"
Branch: 1 branch
BranchTrueFalseTotal
unless $size =~ /\A[0-9]+\z/ and $size >= 2 and $size <= 19 2 492 494
Condition83%5/6
$size =~ /\A[0-9]+\z/ and $size >= 2 and $size <= 19
!ll&&!rl&&r
11492
$size =~ /\A[0-9]+\z/ and $size >= 2
!ll&&!rl&&r
01493
MC/DC66%2/3
$size =~ /\A[0-9]+\z/ and $size >= 2 and $size <= 19
$size =~ /\A[0-9]+\z/ $size >= 2 $size <= 19
Truth table75%3/4
A and B and C
A= $size =~ /\A[0-9]+\z/B= $size >= 2C= $size <= 19
ABCresult
0XX0
10X0
1100
1111
18 unless $size =~ /\A[0-9]+\z/ && $size >= 2 && $size <= 19;
19  
20 492 my $self = bless { ptr => _new_board($size), size => $size }, $class;
21  
22 492 ▶ my $want = exists $o{history} ? $o{history} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $o{'history'} ? : 7 485 492
23 492 ▶ if ($want) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want) 485 7 492
24 485 $self->{hist} = _new_hist($size);
25 485 _hist_push($self->{hist}, $self->{ptr});
26 }
27 492 return $self;
28 }
29  
30 22 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
31 22 my ($self) = @_;
32 22 my $new = bless { ptr => _copy_board($self->{ptr}), size => $self->{size} }, ref $self;
33 22 ▶ $new->{hist} = _copy_hist($self->{hist}) if $self->{hist};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hist"} 2 20 22
34 22 return $new;
35 }
36  
37 4402 ▶sub _hist { $_[0]{hist} || 0 }
Subroutine _hist: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"hist"} or 0 4399 3 4402
38  
39 776 ▶sub size { _size_of($_[0]{ptr}) }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
40 5 ▶sub stride { _stride_of($_[0]{ptr}) }
Subroutine stride: called CC=1 SCAR=0.0
Pod documented
41 39519 ▶sub point_of { _point_of($_[0]{ptr}, $_[1], $_[2]) }
Subroutine point_of: called CC=1 SCAR=0.0
Pod documented
42 3996 ▶sub col_of { _col_of($_[0]{ptr}, $_[1]) }
Subroutine col_of: called CC=1 SCAR=0.0
Pod documented
43 3996 ▶sub row_of { _row_of($_[0]{ptr}, $_[1]) }
Subroutine row_of: called CC=1 SCAR=0.0
Pod documented
44  
45 21325 ▶sub at { _at($_[0]{ptr}, $_[1]) }
Subroutine at: called CC=1 SCAR=0.0
Pod documented
46 240 ▶sub libs { _libs($_[0]{ptr}, $_[1]) }
Subroutine libs: called CC=1 SCAR=0.0
Pod documented
47 578 ▶sub chain_size { _chain_size($_[0]{ptr}, $_[1]) }
Subroutine chain_size: called CC=1 SCAR=0.0
Pod documented
48 502 ▶sub chain_at { [ _chain_at($_[0]{ptr}, $_[1]) ] }
Subroutine chain_at: called CC=1 SCAR=0.0
Pod documented
49 72 ▶sub stones { _stones($_[0]{ptr}, $_[1]) }
Subroutine stones: called CC=1 SCAR=0.0
Pod documented
50 13 ▶sub pack_position { _pack($_[0]{ptr}) }
Subroutine pack_position: called CC=1 SCAR=0.0
Pod documented
51 29 ▶sub hash_hex { _hash_hex($_[0]{ptr}) }
Subroutine hash_hex: called CC=1 SCAR=0.0
Pod documented
52  
53 464 ▶sub put { _put($_[0]{ptr}, $_[1], $_[2]); return $_[0] }
Subroutine put: called CC=1 SCAR=0.0
Pod documented
54 81 ▶sub lift { _lift($_[0]{ptr}, $_[1]) }
Subroutine lift: called CC=1 SCAR=0.0
Pod documented
55  
56 1797 ▶sub zobrist_hex { _zobrist_hex($_[1], $_[2]) }
Subroutine zobrist_hex: called CC=1 SCAR=0.0
Pod documented
57  
58  
59 89 ▶sub legal { _legal($_[0]{ptr}, $_[0]->_hist, $_[1], $_[2]) }
Subroutine legal: called CC=1 SCAR=0.0
Pod documented
60 6 ▶sub is_legal { _legal($_[0]{ptr}, $_[0]->_hist, $_[1], $_[2]) == Game::Go::Rules::OK }
Subroutine is_legal: called CC=1 SCAR=0.0
Pod documented
61 890 ▶sub legal_moves { [ _legal_moves($_[0]{ptr}, $_[0]->_hist, $_[1]) ] }
Subroutine legal_moves: called CC=1 SCAR=0.0
Pod documented
62  
63 3417 ▶sub play {
Subroutine play: called CC=3 SCAR=11.0
Pod documented
64 3417 my ($self, $pt, $colour) = @_;
65 3417 my ($code, $ko, @caps) = _play($self->{ptr}, $self->_hist, $pt, $colour);
66 return {
67 3417 ▶ code => $code,
Branch: 2 branches
BranchTrueFalseTotal
$code == 0 ? : 3394 23 3417
$code == 0 ? : 3394 23 3417
68 ok => ($code == Game::Go::Rules::OK ? 1 : 0),
69 ko_point => $ko,
70 caps => \@caps,
71 ($code == Game::Go::Rules::OK ? () : (message => Game::Go::Rules::refusal($code))),
72 };
73 }
74  
75 313 ▶sub pass { _pass($_[0]{ptr}, $_[1]); return $_[0] }
Subroutine pass: called CC=1 SCAR=0.0
Pod documented
76 13 ▶sub ko_point { _ko_point($_[0]{ptr}) }
Subroutine ko_point: called CC=1 SCAR=0.0
Pod documented
77 2 ▶sub ko_colour { _ko_colour($_[0]{ptr}) }
Subroutine ko_colour: called CC=1 SCAR=0.0
Pod documented
78  
79 28 ▶sub superko {
Subroutine superko: called CC=3 SCAR=11.1
Pod documented
80 28 my ($self, $on) = @_;
81 28 ▶ _set_superko($self->{ptr}, $on ? 1 : 0) if defined $on;
Branch: 2 branches
BranchTrueFalseTotal
if defined $on 28 0 28
$on ? : 1 27 28
82 28 return _superko_on($self->{ptr});
83 }
84  
85 3 ▶sub has_history { $_[0]{hist} ? 1 : 0 }
Subroutine has_history: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'hist'} ? : 1 2 3
86  
87 35 ▶sub push_position { $_[0]{hist} ? _hist_push($_[0]{hist}, $_[0]{ptr}) : 0 }
Subroutine push_position: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'hist'} ? : 35 0 35
88  
89 7 ▶sub history_length { $_[0]{hist} ? _hist_len($_[0]{hist}) : 0 }
Subroutine history_length: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'hist'} ? : 6 1 7
90 3 ▶sub seen_position { $_[0]{hist} ? _hist_has($_[0]{hist}, $_[0]{ptr}) : 0 }
Subroutine seen_position: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'hist'} ? : 2 1 3
91  
92 1 ▶sub hash_after_hex { _hash_after_hex($_[0]{ptr}, $_[1], $_[2]) }
Subroutine hash_after_hex: called CC=1 SCAR=0.0
Pod documented
93  
94 70 ▶sub alive { [ _alive($_[0]{ptr}, $_[1]) ] }
Subroutine alive: called CC=1 SCAR=0.0
Pod documented
95  
96 31 ▶sub is_alive {
Subroutine is_alive: called CC=3 SCAR=11.0
Pod documented
97 31 my ($self, $pt) = @_;
98 31 my $colour = $self->at($pt);
99 31 ▶ return 0 unless Game::Go::Rules::is_colour($colour);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Go::Rules::is_colour($colour) 2 29 31
100 29 my %alive = map { $_ => 1 } @{ $self->alive($colour) };
101 29 ▶ return $alive{$pt} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$alive{$pt} ? : 4 25 29
102 }
103  
104 64 ▶sub score {
Subroutine score: called CC=6 SCAR=18.8
Pod documented
105 64 my ($self, %o) = @_;
106 return { _score(
107 $self->{ptr},
108 $o{dead} || [],
109 $o{seki} || [],
110 $o{prisoners_b} || 0,
111 $o{prisoners_w} || 0,
112 64 ▶ defined $o{komi_tenths} ? $o{komi_tenths} : 0,
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'komi_tenths'} ? : 64 0 64
Condition75%6/8
$o{'dead'} || []
l!l
640
$o{'seki'} || []
l!l
640
$o{'prisoners_b'} || 0
l!l
1351
$o{'prisoners_w'} || 0
l!l
658
MC/DC0%0/1
$o{'dead'} || []
$o{'dead'}
MC/DC0%0/1
$o{'seki'} || []
$o{'seki'}
MC/DC100%1/1
$o{'prisoners_b'} || 0
$o{'prisoners_b'}
MC/DC100%1/1
$o{'prisoners_w'} || 0
$o{'prisoners_w'}
Truth table50%1/2
A
A= $o{'dead'}
Aresult
11
00
Truth table50%1/2
A
A= $o{'seki'}
Aresult
11
00
Truth table100%2/2
A
A= $o{'prisoners_b'}
Aresult
11
00
Truth table100%2/2
A
A= $o{'prisoners_w'}
Aresult
11
00
113 ) };
114 }
115  
116 2 ▶sub area_score { my ($self) = @_; return { _score_area($self->{ptr}) } }
Subroutine area_score: called CC=1 SCAR=0.0
Pod documented
117  
118 1 ▶sub territory_map {
Subroutine territory_map: called CC=3 SCAR=12.0
Pod documented
119 1 my ($self, %o) = @_;
120 1 ▶ return { _territory($self->{ptr}, $o{dead} || [], $o{seki} || []) };
Condition50%2/4
$o{'dead'} || []
l!l
10
$o{'seki'} || []
l!l
10
MC/DC0%0/1
$o{'dead'} || []
$o{'dead'}
MC/DC0%0/1
$o{'seki'} || []
$o{'seki'}
Truth table50%1/2
A
A= $o{'dead'}
Aresult
11
00
Truth table50%1/2
A
A= $o{'seki'}
Aresult
11
00
121 }
122  
123  
124 0 ▶sub prng_next { _prng_next($_[-1]) }
Subroutine prng_next: not called CC=1 SCAR=6.9
Pod documented
125  
126 120 ▶sub playout {
Subroutine playout: called CC=2 SCAR=7.1
Pod documented
127 120 my ($self, %o) = @_;
128 120 ▶ my ($diff, $moves) = _playout($self->{ptr}, $o{colour}, $o{seed} || 1);
Condition50%1/2
$o{'seed'} || 1
l!l
1200
MC/DC0%0/1
$o{'seed'} || 1
$o{'seed'}
Truth table50%1/2
A
A= $o{'seed'}
Aresult
11
00
129 120 return { diff => $diff, moves => $moves, cap => 3 * $self->size * $self->size };
130 }
131  
132 799 ▶sub search {
Subroutine search: called CC=5 SCAR=18.9
Pod documented
133 799 my ($self, %o) = @_;
134 return { _search(
135 $self->{ptr},
136 $o{colour},
137 $o{allowed} || [],
138 $o{seed} || 1,
139 $o{playouts} || 100,
140 799 ▶ defined $o{explore} ? $o{explore} : 700,
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'explore'} ? : 799 0 799
Condition50%3/6
$o{'allowed'} || []
l!l
7990
$o{'seed'} || 1
l!l
7990
$o{'playouts'} || 100
l!l
7990
MC/DC0%0/1
$o{'allowed'} || []
$o{'allowed'}
MC/DC0%0/1
$o{'seed'} || 1
$o{'seed'}
MC/DC0%0/1
$o{'playouts'} || 100
$o{'playouts'}
Truth table50%1/2
A
A= $o{'allowed'}
Aresult
11
00
Truth table50%1/2
A
A= $o{'seed'}
Aresult
11
00
Truth table50%1/2
A
A= $o{'playouts'}
Aresult
11
00
141 ) };
142 }
143  
144 22 ▶sub dead_guess {
Subroutine dead_guess: called CC=3 SCAR=12.0
Pod documented
145 22 my ($self, %o) = @_;
146 22 ▶ return [ _dead_guess($self->{ptr}, $o{seed} || 1, $o{playouts} || 50) ];
Condition50%2/4
$o{'seed'} || 1
l!l
220
$o{'playouts'} || 50
l!l
220
MC/DC0%0/1
$o{'seed'} || 1
$o{'seed'}
MC/DC0%0/1
$o{'playouts'} || 50
$o{'playouts'}
Truth table50%1/2
A
A= $o{'seed'}
Aresult
11
00
Truth table50%1/2
A
A= $o{'playouts'}
Aresult
11
00
147 }
148  
149 514 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
150 514 my ($self) = @_;
151 514 ▶ _drop_board(delete $self->{ptr}) if $self->{ptr};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ptr"} 514 0 514
152 514 ▶ _drop_hist(delete $self->{hist}) if $self->{hist};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hist"} 487 27 514
153 514 return;
154 }
155  
156 26 1;
157  
158 __END__
« blib/lib/Game/Go/Scoring.pm ↑ Summary blib/lib/Game/Go/Rules.pm »