blib/lib/Game/Shogi/USI.pm

filter: Statementstmt 99.0% 106 / 107 Branchbran 90.6% 58 / 64 Conditioncond 76.1% 32 / 42 MC/DCmcdc 57.6% 15 / 26 Subroutinesub 94.1% 16 / 17 Podpod 85.7% 6 / 7 totaltotal 88.5% 233 / 263 CC 50 SCAR 22.2
CC
50
Cov
92%
position
13.1
budget
13.0
go
12.0
CRAP 9.2
« blib/lib/Game/Shogi/Terminal.pm ↑ Summary blib/lib/Game/Shogi.pm »
1 package Game::Shogi::USI;
2  
3 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Game::Shogi;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Game::Shogi::Engine ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '0.01';
13  
14 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 NODES_PER_MS => 1_000,
16 DEFAULT_NODES => 300_000,
17 LEAST_NODES => 400,
18 MOST_NODES => 200_000_000,
19 MATE_SCORE => 30_000,
20 MATE_BOUND => 29_000,
21 2 };
22  
23 2 has _in => (is => 'rw', init_arg => 'in', private => 1);
24  
25 2 has _out => (is => 'rw', init_arg => 'out', private => 1);
26  
27 2 has nodes => (is => 'rw');
28  
29 2 has seed => (is => 'rw', default => 0);
30  
31 2 has _board => (is => 'rw', private => 1);
32  
33 13 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
34 13 my ($self) = @_;
35 13 ▶ $self->_in(\*STDIN) unless $self->_in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in 2 11 13
36 13 ▶ $self->_out(\*STDOUT) unless $self->_out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_out 2 11 13
37 13 $self->_out->autoflush(1);
38 13 $self->_board(Game::Shogi::Engine->new);
39 13 return;
40 }
41  
42 0 ▶sub board { $_[0]->_board->clone }
Subroutine board: not called CC=1 SCAR=6.9
Pod documented
43  
44 14 ▶sub _say {
Subroutine _say: called CC=1 SCAR=0.0
45 14 my ($self, @lines) = @_;
46 14 print { $self->_out } map { "$_\n" } @lines;
47 14 return;
48 }
49  
50 10 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod documented
51 10 my ($self) = @_;
52 10 my $fh = $self->_in;
53 10 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 27 7 34
54 27 ▶ last unless $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->command($line) 3 24 27
55 }
56 10 return 0;
57 }
58  
59 28 ▶sub command {
Subroutine command: called CC=10 SCAR=23.0
Pod documented
60 28 my ($self, $line) = @_;
61 28 $line =~ s/\A\s+|\s+\z//g;
62 28 ▶ return 1 unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 28 28
63 28 my ($cmd, @w) = split /\s+/, $line;
64  
65 28 ▶ if ($cmd eq 'usi') {
Branch: 6 branches
BranchTrueFalseTotal
if ($cmd eq 'usi') { } 2 26 28
elsif ($cmd eq 'isready') { } 1 25 26
elsif ($cmd eq 'usinewgame') { } 1 24 25
elsif ($cmd eq 'position') { } 8 16 24
elsif ($cmd eq 'go') { } 10 6 16
elsif ($cmd eq 'quit') { } 3 3 6
66 2 $self->_say('id name Game::Shogi ' . $Game::Shogi::VERSION, 'id author LNATION', 'usiok');
67 }
68 1 elsif ($cmd eq 'isready') { $self->_say('readyok') }
69 1 elsif ($cmd eq 'usinewgame') { $self->_board(Game::Shogi::Engine->new) }
70 elsif ($cmd eq 'position') {
71 8 my $board = $self->position(@w);
72 8 ▶ $self->_say('info string position refused: ' . join ' ', @w) unless $board;
Branch: 1 branch
BranchTrueFalseTotal
unless $board 1 7 8
73 8 ▶ $self->_board($board || Game::Shogi::Engine->new);
Condition66%2/3
$board || 'Game::Shogi::Engine'->new
l!l&&r!l&&!r
710
MC/DC0%0/2
$board || 'Game::Shogi::Engine'->new
$board 'Game::Shogi::Engine'->new
Truth table66%2/3
A || B
A= $boardB= 'Game::Shogi::Engine'->new
ABresult
1X1
011
000
74 }
75 10 elsif ($cmd eq 'go') { $self->_say($self->go(@w)) }
76 3 elsif ($cmd eq 'quit') { return 0 }
77 25 return 1;
78 }
79  
80 13 ▶sub position {
Subroutine position: called CC=13 SCAR=25.8
Pod documented
81 13 my ($self, @w) = @_;
82 13 my $board;
83 13 ▶ if (@w && $w[0] eq 'startpos') {
Branch: 2 branches
BranchTrueFalseTotal
if (@w and $w[0] eq 'startpos') { } 6 7 13
elsif (@w and $w[0] eq 'sfen') { } 6 1 7
Condition66%2/3
@w and $w[0] eq "startpos"
!ll&&!rl&&r
076
MC/DC50%1/2
@w and $w[0] eq "startpos"
@w $w[0] eq "startpos"
Truth table66%2/3
A and B
A= @wB= $w[0] eq "startpos"
ABresult
0X0
100
111
84 6 shift @w;
85 6 $board = Game::Shogi::Engine->new;
86 }
87 ▶ elsif (@w && $w[0] eq 'sfen') {
Condition66%2/3
@w and $w[0] eq "sfen"
!ll&&!rl&&r
016
MC/DC50%1/2
@w and $w[0] eq "sfen"
@w $w[0] eq "sfen"
Truth table66%2/3
A and B
A= @wB= $w[0] eq "sfen"
ABresult
0X0
100
111
88 6 shift @w;
89 6 my @sfen;
90 6 ▶ push @sfen, shift @w while @w && $w[0] ne 'moves';
Branch: 1 branch
BranchTrueFalseTotal
@w and $w[0] ne "moves" and do { push @sfen, shift @w } 24 6 30
Condition100%3/3
@w and $w[0] ne "moves"
!ll&&!rl&&r
4224
MC/DC100%2/2
@w and $w[0] ne "moves"
@w $w[0] ne "moves"
Truth table100%3/3
A and B
A= @wB= $w[0] ne "moves"
ABresult
0X0
100
111
91 6 ▶ $board = eval { Game::Shogi::Engine->new(sfen => join ' ', @sfen) } or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$board = eval { do { "Game::Shogi::Engine"->new("sfen", join(" ", @sfen)) } } or return undef 5 1 6
92 }
93 else {
94 1 return undef;
95 }
96 11 ▶ if (@w && $w[0] eq 'moves') {
Branch: 1 branch
BranchTrueFalseTotal
if (@w and $w[0] eq "moves") 7 4 11
Condition66%2/3
@w and $w[0] eq "moves"
!ll&&!rl&&r
407
MC/DC50%1/2
@w and $w[0] eq "moves"
@w $w[0] eq "moves"
Truth table66%2/3
A and B
A= @wB= $w[0] eq "moves"
ABresult
0X0
100
111
97 7 shift @w;
98 7 for my $usi (@w) {
99 13 ▶ return undef if $board->move_fault($usi) || !$board->do_move($usi);
Branch: 1 branch
BranchTrueFalseTotal
if $board->move_fault($usi) or not $board->do_move($usi) 2 11 13
Condition66%2/3
$board->move_fault($usi) or not $board->do_move($usi)
l!l&&r!l&&!r
2011
MC/DC50%1/2
$board->move_fault($usi) or not $board->do_move($usi)
$board->move_fault($usi) not $board->do_move($usi)
Truth table66%2/3
A or not(B)
A= $board->move_fault($usi)B= not $board->do_move($usi)
ABresult
1X1
011
000
100 }
101 }
102 9 return $board;
103 }
104  
105 18 ▶sub budget {
Subroutine budget: called CC=13 SCAR=25.7
Pod documented
106 18 my ($self, %arg) = @_;
107 18 ▶ return $arg{nodes} if defined $arg{nodes} && $arg{nodes} >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg{"nodes"} and $arg{"nodes"} >= 1 7 11 18
Condition66%2/3
defined $arg{"nodes"} and $arg{"nodes"} >= 1
!ll&&!rl&&r
1107
MC/DC50%1/2
defined $arg{"nodes"} and $arg{"nodes"} >= 1
defined $arg{"nodes"} $arg{"nodes"} >= 1
Truth table66%2/3
A and B
A= defined $arg{"nodes"}B= $arg{"nodes"} >= 1
ABresult
0X0
100
111
108 11 my $side = $self->_board->side_to_move;
109 11 ▶ return MOST_NODES if defined $arg{depth} && !grep { defined $arg{$_} } qw(byoyomi movetime btime wtime);
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg{"depth"} and not grep({defined $arg{$_};} "byoyomi", "movetime", "btime", "wtime") 2 9 11
Condition100%3/3
defined $arg{"depth"} and not grep({defined $arg{$_};} "byoyomi", "movetime", "btime", "wtime")
!ll&&!rl&&r
812
MC/DC100%2/2
defined $arg{"depth"} and not grep({defined $arg{$_};} "byoyomi", "movetime", "btime", "wtime")
defined $arg{"depth"} not grep({defined $arg{$_};} "byoyomi", "movetime", "btime", "wtime")
Truth table100%3/3
A and not(B)
A= defined $arg{"depth"}B= not grep({defined $arg{$_};} "byoyomi", "movetime", "btime", "wtime")
ABresult
0X0
100
111
110 9 my $ms = 0;
111 9 ▶ $ms += $arg{byoyomi} // 0;
Condition100%2/2
$arg{"byoyomi"} // 0
l!l
45
MC/DC100%1/1
$arg{"byoyomi"} // 0
$arg{"byoyomi"}
Truth table100%2/2
A
A= $arg{"byoyomi"}
Aresult
11
00
112 9 ▶ $ms += $arg{movetime} // 0;
Condition100%2/2
$arg{"movetime"} // 0
l!l
18
MC/DC100%1/1
$arg{"movetime"} // 0
$arg{"movetime"}
Truth table100%2/2
A
A= $arg{"movetime"}
Aresult
11
00
113 9 ▶ $ms += ($arg{"${side}time"} // 0) / 40;
Condition100%2/2
$arg{"${side}time"} // 0
l!l
36
MC/DC100%1/1
$arg{"${side}time"} // 0
$arg{"${side}time"}
Truth table100%2/2
A
A= $arg{"${side}time"}
Aresult
11
00
114 9 ▶ $ms += $arg{"${side}inc"} // 0;
Condition100%2/2
$arg{"${side}inc"} // 0
l!l
18
MC/DC100%1/1
$arg{"${side}inc"} // 0
$arg{"${side}inc"}
Truth table100%2/2
A
A= $arg{"${side}inc"}
Aresult
11
00
115 9 ▶ return $self->nodes // DEFAULT_NODES unless $ms > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $ms > 0 3 6 9
Condition100%2/2
$self->nodes // 300000
l!l
21
MC/DC100%1/1
$self->nodes // 300000
$self->nodes
Truth table100%2/2
A
A= $self->nodes
Aresult
11
00
116 6 my $n = int($ms * NODES_PER_MS);
117 6 ▶ return $n < LEAST_NODES ? LEAST_NODES : $n > MOST_NODES ? MOST_NODES : $n;
Branch: 2 branches
BranchTrueFalseTotal
$n < 400 ? : 1 5 6
$n > 200000000 ? : 0 5 5
118 }
119  
120 7 ▶sub _score {
Subroutine _score: called CC=3 SCAR=11.0
121 7 my ($score) = @_;
122 7 ▶ return "cp $score" if abs($score) < MATE_BOUND;
Branch: 1 branch
BranchTrueFalseTotal
if abs $score < 29000 6 1 7
123 1 my $plies = MATE_SCORE - abs($score);
124 1 ▶ return 'mate ' . ($score < 0 ? -$plies : $plies);
Branch: 1 branch
BranchTrueFalseTotal
$score < 0 ? : 0 1 1
125 }
126  
127 10 ▶sub go {
Subroutine go: called CC=11 SCAR=24.8
Pod documented
128 10 my ($self, @w) = @_;
129 10 my %arg;
130 10 ▶ while (@w) {
Branch: 1 branch
BranchTrueFalseTotal
if (@w) 9 9 18
131 9 my $k = shift @w;
132 9 ▶ next if $k eq 'infinite' || $k eq 'ponder';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "infinite" or $k eq "ponder" 0 9 9
Condition33%1/3
$k eq "infinite" or $k eq "ponder"
l!l&&r!l&&!r
009
MC/DC0%0/2
$k eq "infinite" or $k eq "ponder"
$k eq "infinite" $k eq "ponder"
Truth table33%1/3
A or B
A= $k eq "infinite"B= $k eq "ponder"
ABresult
1X1
011
000
133 9 ▶ return 'checkmate notimplemented' if $k eq 'mate';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "mate" 1 8 9
134 8 ▶ $arg{$k} = shift @w if @w && $w[0] =~ /\A-?[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if @w and $w[0] =~ /\A-?[0-9]+\z/u 8 0 8
Condition33%1/3
@w and $w[0] =~ /\A-?[0-9]+\z/u
!ll&&!rl&&r
008
MC/DC0%0/2
@w and $w[0] =~ /\A-?[0-9]+\z/u
@w $w[0] =~ /\A-?[0-9]+\z/u
Truth table33%1/3
A and B
A= @wB= $w[0] =~ /\A-?[0-9]+\z/u
ABresult
0X0
100
111
135 }
136 9 my $board = $self->_board;
137 9 ▶ return 'bestmove resign' if $board->outcome;
Branch: 1 branch
BranchTrueFalseTotal
if $board->outcome 1 8 9
138 8 ▶ return 'bestmove win' if $board->can_declare;
Branch: 1 branch
BranchTrueFalseTotal
if $board->can_declare 1 7 8
139 7 ▶ my ($move, $info) = $board->search(nodes => $self->budget(%arg), depth => $arg{depth} // 0, seed => $self->seed);
Condition100%2/2
$arg{"depth"} // 0
l!l
16
MC/DC100%1/1
$arg{"depth"} // 0
$arg{"depth"}
Truth table100%2/2
A
A= $arg{"depth"}
Aresult
11
00
140 7 ▶ return 'bestmove resign' unless defined $move;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $move 0 7 7
141 return (
142 7 sprintf('info depth %d nodes %d score %s pv %s', $info->{depth}, $info->{nodes}, _score($info->{score}), $move),
143 "bestmove $move",
144 );
145 }
146  
147 2 1;
148  
149 __END__
« blib/lib/Game/Shogi/Terminal.pm ↑ Summary blib/lib/Game/Shogi.pm »