| 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
| |||||||||||||||||||||||||||||||||
| 36 | 13 | ▶ | $self->_out(\*STDOUT) unless $self->_out; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 54 | 27 | ▶ | last unless $self->command($line); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 63 | 28 | my ($cmd, @w) = split /\s+/, $line; | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 28 | ▶ | if ($cmd eq 'usi') { | ||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | $self->_board($board || Game::Shogi::Engine->new); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$board || 'Game::Shogi::Engine'->new
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
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
@w and $w[0] eq "startpos"
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"
| |||||||||||||||||||||||||||||||||
| 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"
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"
| |||||||||||||||||||||||||||||||||
| 88 | 6 | shift @w; | |||||||||||||||||||||||||||||||
| 89 | 6 | my @sfen; | |||||||||||||||||||||||||||||||
| 90 | 6 | ▶ | push @sfen, shift @w while @w && $w[0] ne 'moves'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@w and $w[0] ne "moves"
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"
| |||||||||||||||||||||||||||||||||
| 91 | 6 | ▶ | $board = eval { Game::Shogi::Engine->new(sfen => join ' ', @sfen) } or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||||||
| 94 | 1 | return undef; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | 11 | ▶ | if (@w && $w[0] eq 'moves') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@w and $w[0] eq "moves"
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"
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$board->move_fault($usi) or not $board->do_move($usi)
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)
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $arg{"nodes"} and $arg{"nodes"} >= 1
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
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $arg{"depth"} and not grep({defined $arg{$_};} "byoyomi", "movetime", "btime", "wtime")
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")
| |||||||||||||||||||||||||||||||||
| 110 | 9 | my $ms = 0; | |||||||||||||||||||||||||||||||
| 111 | 9 | ▶ | $ms += $arg{byoyomi} // 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"byoyomi"} // 0
MC/DC100%1/1
$arg{"byoyomi"} // 0
$arg{"byoyomi"}
Truth table100%2/2
A
A= $arg{"byoyomi"}
| |||||||||||||||||||||||||||||||||
| 112 | 9 | ▶ | $ms += $arg{movetime} // 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"movetime"} // 0
MC/DC100%1/1
$arg{"movetime"} // 0
$arg{"movetime"}
Truth table100%2/2
A
A= $arg{"movetime"}
| |||||||||||||||||||||||||||||||||
| 113 | 9 | ▶ | $ms += ($arg{"${side}time"} // 0) / 40; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"${side}time"} // 0
MC/DC100%1/1
$arg{"${side}time"} // 0
$arg{"${side}time"}
Truth table100%2/2
A
A= $arg{"${side}time"}
| |||||||||||||||||||||||||||||||||
| 114 | 9 | ▶ | $ms += $arg{"${side}inc"} // 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"${side}inc"} // 0
MC/DC100%1/1
$arg{"${side}inc"} // 0
$arg{"${side}inc"}
Truth table100%2/2
A
A= $arg{"${side}inc"}
| |||||||||||||||||||||||||||||||||
| 115 | 9 | ▶ | return $self->nodes // DEFAULT_NODES unless $ms > 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->nodes // 300000
MC/DC100%1/1
$self->nodes // 300000
$self->nodes
Truth table100%2/2
A
A= $self->nodes
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 123 | 1 | my $plies = MATE_SCORE - abs($score); | |||||||||||||||||||||||||||||||
| 124 | 1 | ▶ | return 'mate ' . ($score < 0 ? -$plies : $plies); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 131 | 9 | my $k = shift @w; | |||||||||||||||||||||||||||||||
| 132 | 9 | ▶ | next if $k eq 'infinite' || $k eq 'ponder'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$k eq "infinite" or $k eq "ponder"
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"
| |||||||||||||||||||||||||||||||||
| 133 | 9 | ▶ | return 'checkmate notimplemented' if $k eq 'mate'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | 8 | ▶ | $arg{$k} = shift @w if @w && $w[0] =~ /\A-?[0-9]+\z/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@w and $w[0] =~ /\A-?[0-9]+\z/u
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
| |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | 9 | my $board = $self->_board; | |||||||||||||||||||||||||||||||
| 137 | 9 | ▶ | return 'bestmove resign' if $board->outcome; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | 8 | ▶ | return 'bestmove win' if $board->can_declare; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | 7 | ▶ | my ($move, $info) = $board->search(nodes => $self->budget(%arg), depth => $arg{depth} // 0, seed => $self->seed); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"depth"} // 0
MC/DC100%1/1
$arg{"depth"} // 0
$arg{"depth"}
Truth table100%2/2
A
A= $arg{"depth"}
| |||||||||||||||||||||||||||||||||
| 140 | 7 | ▶ | return 'bestmove resign' unless defined $move; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||