| 1 | package Game::Sternhalma; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 30 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 30 | ▶ | use Object::Proto::Sugar; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 30 | ▶ | use Game::Sternhalma::Engine qw(:all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 30 | ▶ | use Game::Sternhalma::Error (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 30 | ▶ | use Game::Sternhalma::Layout (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 30 | ▶ | use Game::Sternhalma::Notation (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 30 | ▶ | use Game::Sternhalma::Result (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 30 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 30 | our %REASON = ( | |||||||||||||||||||||||||||||||||||||||||
| 18 | bad_string => 'that is not a move', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | no_such_hole => 'that names a hole the star does not have', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | no_piece => 'there is no piece there', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | not_your_piece => 'that piece is not yours to move', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | must_pass => 'you have no move, and must pass', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | occupied => 'a piece is standing there', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | not_reachable => 'that piece cannot get there this turn', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | pass_not_allowed => 'you have a move, so you may not pass', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | no_such_seat => 'the game has no such seat', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | seat_finished => 'that seat is home already', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | seat_resigned => 'that seat has resigned already', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | game_over => 'the game is over', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 30 | my %HOW = (BY_HOME, 'home', BY_CAP, 'cap', BY_DEADLOCK, 'deadlock', BY_RESIGN, 'resign'); | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 30 | has _want_seats => (is => 'ro', init_arg => 'seats', private => 1, default => 2); | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 30 | has _want_pieces => (is => 'ro', init_arg => 'pieces', private => 1, default => 10); | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 30 | has _want_first => (is => 'ro', init_arg => 'first', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 30 | has _want_position => (is => 'ro', init_arg => 'position', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 30 | has _start => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 30 | has solo => (is => 'ro', default => 0); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 30 | has _want_cap => (is => 'ro', init_arg => 'cap', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 30 | has _board => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 30 | has _undo => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 30 | has _moves => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 30 | has _keys => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 30 | has _movers => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 182 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=12 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 182 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 182 | my $position = $self->_want_position; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 182 | my $board; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 182 | ▶ | if (defined $position) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 15 | ▶ | Game::Sternhalma::Error->throw(bad_first => 'a game from a position starts with its seat to move') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | if defined $self->_want_first; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 14 | my ($read, $code) = Game::Sternhalma::Engine->from_string($position); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 14 | ▶ | Game::Sternhalma::Error->throw(bad_position => Game::Sternhalma::Engine->reason($code)) unless $read; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 13 | my $why = Game::Sternhalma::Layout::why_not($read->seats, $read->pieces, $self->solo); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 13 | ▶ | Game::Sternhalma::Error->throw(bad_layout => $why) if defined $why; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 13 | $board = $read; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 13 | $self->_start($position); | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 167 | my ($seats, $pieces) = ($self->_want_seats, $self->_want_pieces); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 167 | my $why = Game::Sternhalma::Layout::why_not($seats, $pieces, $self->solo); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 167 | ▶ | Game::Sternhalma::Error->throw(bad_layout => $why) if defined $why; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 162 | ▶ | my $first = $self->_want_first // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_want_first // 0
MC/DC100%1/1
$self->_want_first // 0
$self->_want_first
Truth table100%2/2
A
A= $self->_want_first
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 162 | ▶ | Game::Sternhalma::Error->throw(bad_first => "the first seat must be 0 to " . ($seats - 1)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$first =~ /\A[0-9]+\z/u and $first < $seats
MC/DC100%2/2
$first =~ /\A[0-9]+\z/u and $first < $seats
$first =~ /\A[0-9]+\z/u
$first < $seats
Truth table100%3/3
A and B
A= $first =~ /\A[0-9]+\z/uB= $first < $seats
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | unless $first =~ /\A[0-9]+\z/ && $first < $seats; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 160 | $board = Game::Sternhalma::Engine->new(seats => $seats, pieces => $pieces); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 160 | $board->set_to_move($first); | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 173 | my $cap = $self->_want_cap; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 173 | ▶ | if (defined $cap) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 27 | ▶ | Game::Sternhalma::Error->throw(bad_cap => 'the cap is a number of plies above 0') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cap =~ /\A[0-9]+\z/u and $cap > 0
MC/DC100%2/2
$cap =~ /\A[0-9]+\z/u and $cap > 0
$cap =~ /\A[0-9]+\z/u
$cap > 0
Truth table100%3/3
A and B
A= $cap =~ /\A[0-9]+\z/uB= $cap > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | unless $cap =~ /\A[0-9]+\z/ && $cap > 0; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 25 | $board->set_cap($cap); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 171 | $self->_board($board); | |||||||||||||||||||||||||||||||||||||||||
| 89 | 171 | $self->_undo([]); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 171 | $self->_moves([]); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 171 | $self->_keys([ map { [ $board->own_key($_) ] } 0 .. $board->seats - 1 ]); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 171 | $self->_movers([]); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 171 | return; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # ---- reading the game ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 24329 | ▶ | sub seats { $_[0]->_board->seats } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
seats: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 251 | ▶ | sub pieces { $_[0]->_board->pieces } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pieces: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 234 | ▶ | sub first { defined $_[0]->_start ? undef : $_[0]->_want_first // 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
first: called CC=3 SCAR=11.0
Pod
documented
Branch: 1 branch
Condition100%2/2
$_[0]->_want_first // 0
MC/DC100%1/1
$_[0]->_want_first // 0
$_[0]->_want_first
Truth table100%2/2
A
A= $_[0]->_want_first
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub start { $_[0]->_start } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 8788 | ▶ | sub to_move { $_[0]->_board->to_move } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_move: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 6565 | ▶ | sub over { $_[0]->_board->over } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
over: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 526 | ▶ | sub ply { $_[0]->_board->ply } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 238 | ▶ | sub cap { $_[0]->_board->cap } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 624 | ▶ | sub places { $_[0]->_board->places } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
places: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 20 | ▶ | sub position { $_[0]->_board->to_string } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
position: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 24 | ▶ | sub key { $_[0]->_board->key } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 40880 | ▶ | sub board { $_[0]->_board->clone } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
board: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 1610 | ▶ | sub distance_left { $_[0]->_board->distance_left($_[1]) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
distance_left: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 61712 | ▶ | sub at { $_[0]->_board->at(Game::Sternhalma::Notation::hole_of($_[1]) // $_[1]) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
at: called CC=2 SCAR=7.2
Pod
documented
Condition66%2/3
Game::Sternhalma::Notation::hole_of($_[1]) // $_[1]
MC/DC0%0/2
Game::Sternhalma::Notation::hole_of($_[1]) // $_[1]
Game::Sternhalma::Notation::hole_of($_[1])
$_[1]
Truth table66%2/3
A // B
A= Game::Sternhalma::Notation::hole_of($_[1])B= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 40 | ▶ | sub how { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
how: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 40 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 40 | ▶ | return $self->over ? $HOW{ $self->_board->how } : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 1234 | ▶ | sub finished { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
finished: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 1234 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 1234 | my $bd = $self->_board; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 1234 | my @home = grep { $bd->finished($_) } 0 .. $self->seats - 1; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 1234 | return sort { $bd->place_of($a) <=> $bd->place_of($b) } @home; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 5 | ▶ | sub result { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
result: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 5 | ▶ | return undef unless $self->over; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 5 | return Game::Sternhalma::Result->new( | |||||||||||||||||||||||||||||||||||||||||
| 129 | how => $self->how, places => [ $self->places ], ply => $self->ply, position => $self->position, | ||||||||||||||||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 2033 | ▶ | sub legal { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
legal: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 2033 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 2033 | my $bd = $self->_board; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 2033 | ▶ | return () if $bd->over; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 2033 | ▶ | return ({ kind => 'pass', text => 'pass' }) if $bd->can_pass; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | return map { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 2032 | my ($from, $to) = @$_; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 79851 | { kind => 'move', from => $from, to => $to, path => [ $bd->path($from, $to) ], text => $bd->move_text($from, $to) } | |||||||||||||||||||||||||||||||||||||||||
| 141 | } $bd->legal; | ||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 1001 | ▶ | sub moves { @{ $_[0]->_moves } } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
moves: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 4214 | ▶ | sub history { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
history: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 4214 | my ($self, $seat) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 4214 | ▶ | $seat = $self->to_move unless defined $seat; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 4214 | ▶ | my $k = $self->_keys->[$seat] or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 4214 | return @$k; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 18 | ▶ | sub reason { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reason: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 18 | my (undef, $flag) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 18 | ▶ | return defined $flag ? $REASON{$flag} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | # ---- playing ------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # PLAY RETURNS A FLAG AND NEVER THROWS. Undef when the move was made; otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 161 | # one name, one reason. A caller maps the names, and nothing here is a second | ||||||||||||||||||||||||||||||||||||||||||
| 162 | # opinion on the engine's. | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 5673 | ▶ | sub play { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
play: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 5673 | my ($self, $move) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 5673 | my $flag = eval { $self->_play($move) }; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 5673 | ▶ | return $@ ? 'bad_string' : $flag; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 5673 | ▶ | sub _play { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_play: called CC=16 SCAR=27.7
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 5673 | my ($self, $move) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 5673 | my $bd = $self->_board; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 5673 | ▶ | return 'game_over' if $bd->over; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 5672 | my ($kind, $from, $to, $path, $seat) = _parse($move); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 5672 | ▶ | return $kind if $kind ne 'move' && $kind ne 'pass' && $kind ne 'resign'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$kind ne "move" and $kind ne "pass" and $kind ne "resign"
$kind ne "move" and $kind ne "pass"
MC/DC100%3/3
$kind ne "move" and $kind ne "pass" and $kind ne "resign"
$kind ne "move"
$kind ne "pass"
$kind ne "resign"
Truth table100%4/4
A and B and C
A= $kind ne "move"B= $kind ne "pass"C= $kind ne "resign"
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 5654 | ▶ | return $self->_resign($seat) if $kind eq 'resign'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 5652 | ▶ | if ($kind eq 'pass') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 2 | ▶ | return 'pass_not_allowed' unless $bd->can_pass; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | my $mover = $bd->to_move; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | my ($why, $undo) = $bd->pass; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | return $self->_done($undo, 'pass', $mover); | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 5650 | my $piece = $bd->at($from); | |||||||||||||||||||||||||||||||||||||||||
| 184 | 5650 | ▶ | return 'no_piece' unless defined $piece; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 5648 | ▶ | return 'not_your_piece' unless $piece == $bd->to_move; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 5647 | ▶ | return 'must_pass' if $bd->can_pass; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 5646 | ▶ | return 'occupied' if defined $bd->at($to); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 5645 | ▶ | if ($path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 867 | for my $i (1 .. $#$path) { | |||||||||||||||||||||||||||||||||||||||||
| 190 | 2044 | ▶ | return 'not_reachable' unless grep { $_ == $path->[$i] } $bd->hops($path->[ $i - 1 ], $from); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 5644 | my $text = $bd->move_text($from, $to); | |||||||||||||||||||||||||||||||||||||||||
| 194 | 5644 | my $mover = $bd->to_move; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 5644 | my ($why, $undo) = $bd->play($from, $to); | |||||||||||||||||||||||||||||||||||||||||
| 196 | 5644 | ▶ | return 'not_reachable' unless $why == PLAY_OK; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 5641 | return $self->_done($undo, $text, $mover); | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 14 | ▶ | sub resign { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resign: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 14 | my ($self, $seat) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 14 | ▶ | my $flag = eval { $self->_board->over ? 'game_over' : $self->_resign($seat) }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 14 | ▶ | return $@ ? 'no_such_seat' : $flag; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 15 | ▶ | sub _resign { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_resign: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 15 | my ($self, $seat) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 15 | my $bd = $self->_board; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 15 | ▶ | return 'no_such_seat' unless defined $seat && $seat =~ /\A[0-9]+\z/ && $seat < $self->seats; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $seat and $seat =~ /\A[0-9]+\z/u and $seat < $self->seats
defined $seat and $seat =~ /\A[0-9]+\z/u
MC/DC100%3/3
defined $seat and $seat =~ /\A[0-9]+\z/u and $seat < $self->seats
defined $seat
$seat =~ /\A[0-9]+\z/u
$seat < $self->seats
Truth table100%4/4
A and B and C
A= defined $seatB= $seat =~ /\A[0-9]+\z/uC= $seat < $self->seats
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 8 | ▶ | return 'seat_finished' if $bd->finished($seat); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 7 | my ($why, $undo) = $bd->resign($seat); | |||||||||||||||||||||||||||||||||||||||||
| 212 | 7 | ▶ | return 'seat_resigned' if $why == WHY_SEAT_DONE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 6 | ▶ | return 'no_such_seat' unless $why == PLAY_OK; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 6 | return $self->_done($undo, "resign $seat"); | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 5648 | ▶ | sub _done { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_done: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 5648 | my ($self, $undo, $text, $mover) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 5648 | push @{ $self->_undo }, $undo; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 5648 | push @{ $self->_moves }, $text; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 5648 | push @{ $self->_movers }, $mover; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 5648 | ▶ | push @{ $self->_keys->[$mover] }, $self->_board->own_key($mover) if defined $mover; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 5648 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | # A move as text, a hash of holes or names, or a pass or resignation. Answers | ||||||||||||||||||||||||||||||||||||||||||
| 227 | # the kind and its parts, or a flag naming what is wrong with it. | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 5672 | ▶ | sub _parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=18 SCAR=29.0
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 5672 | my ($move) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 5672 | ▶ | return ('bad_string') unless defined $move; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 5671 | ▶ | if (ref $move) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 4091 | ▶ | return ('bad_string') unless ref $move eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 4088 | ▶ | my $kind = $move->{kind} // 'move'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$move->{"kind"} // "move"
MC/DC100%1/1
$move->{"kind"} // "move"
$move->{"kind"}
Truth table100%2/2
A
A= $move->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 4088 | ▶ | return ('pass') if $kind eq 'pass'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 4088 | ▶ | return ('resign', undef, undef, undef, $move->{seat}) if $kind eq 'resign'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 4087 | ▶ | return ('bad_string') unless $kind eq 'move'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 4086 | my ($from, $to) = map { _hole($_) } $move->{from}, $move->{to}; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 4086 | ▶ | return ('no_such_hole') unless defined $from && defined $to; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $from and defined $to
MC/DC50%1/2
defined $from and defined $to
defined $from
defined $to
Truth table66%2/3
A and B
A= defined $fromB= defined $to
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 4083 | ▶ | return ('bad_string') if $from == $to; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 4083 | return ('move', $from, $to); | |||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 1580 | ▶ | return ('bad_string') if length $move > 200; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 1579 | my $m = Game::Sternhalma::Notation::parse_move($move); | |||||||||||||||||||||||||||||||||||||||||
| 244 | 1579 | ▶ | unless ($m) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 9 | ▶ | return ('bad_string') unless $move =~ /\A[a-z][0-9]+(?:[-:][a-z][0-9]+)+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 1 | my @names = split /[-:]/, $move; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 1 | ▶ | return (grep { !defined Game::Sternhalma::Notation::hole_of($_) } @names) ? ('no_such_hole') : ('bad_string'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 1570 | ▶ | return ('pass') if $m->{kind} eq 'pass'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 1568 | ▶ | return ('resign', undef, undef, undef, $m->{seat}) if $m->{kind} eq 'resign'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 1567 | my @h = @{ $m->{holes} }; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 1567 | ▶ | return ('move', $h[0], $h[-1], $m->{kind} eq 'chain' ? \@h : undef); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 8172 | ▶ | sub _hole { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hole: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 8172 | my ($h) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 257 | 8172 | ▶ | return undef unless defined $h && !ref $h; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $h and not ref $h
MC/DC100%2/2
defined $h and not ref $h
defined $h
not ref $h
Truth table100%3/3
A and not(B)
A= defined $hB= not ref $h
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 8168 | ▶ | return $h + 0 if $h =~ /\A[0-9]+\z/ && $h < HOLES; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$h =~ /\A[0-9]+\z/u and $h < 121
MC/DC100%2/2
$h =~ /\A[0-9]+\z/u and $h < 121
$h =~ /\A[0-9]+\z/u
$h < 121
Truth table100%3/3
A and B
A= $h =~ /\A[0-9]+\z/uB= $h < 121
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 3 | return Game::Sternhalma::Notation::hole_of($h); | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 752 | ▶ | sub undo { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
undo: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 752 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 752 | ▶ | my $undo = pop @{ $self->_undo } or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 748 | pop @{ $self->_moves }; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 748 | my $mover = pop @{ $self->_movers }; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 748 | ▶ | pop @{ $self->_keys->[$mover] } if defined $mover; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 748 | return $self->_board->unplay($undo); | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | # ---- the log --------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 233 | ▶ | sub log { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 233 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 233 | ▶ | return Game::Sternhalma::Notation::format_game({ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->first // 0
MC/DC100%1/1
$self->first // 0
$self->first
Truth table100%2/2
A
A= $self->first
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | seats => $self->seats, pieces => $self->pieces, cap => $self->cap, first => $self->first // 0, | ||||||||||||||||||||||||||||||||||||||||||
| 277 | (defined $self->_start ? (start => $self->_start) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 278 | moves => [ $self->moves ], | ||||||||||||||||||||||||||||||||||||||||||
| 279 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 20 | ▶ | sub replay { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replay: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 20 | my ($class, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 20 | my ($g, $why) = Game::Sternhalma::Notation::parse_game($text); | |||||||||||||||||||||||||||||||||||||||||
| 285 | 20 | ▶ | Game::Sternhalma::Error->throw(bad_log => $why) unless $g; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | my $game = defined $g->{start} | ||||||||||||||||||||||||||||||||||||||||||
| 287 | ? $class->new(position => $g->{start}, cap => $g->{cap}) | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 18 | ▶ | : $class->new(seats => $g->{seats}, pieces => $g->{pieces}, cap => $g->{cap}, first => $g->{first}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 18 | my $n = 0; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 18 | for my $move (@{ $g->{moves} }) { | |||||||||||||||||||||||||||||||||||||||||
| 291 | 767 | $n++; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 767 | my $flag = $game->play($move); | |||||||||||||||||||||||||||||||||||||||||
| 293 | 767 | ▶ | Game::Sternhalma::Error->throw(bad_log => "move $n, $move: " . $REASON{$flag}) if defined $flag; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 17 | return $game; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 30 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | __END__ | ||||||||||||||||||||||||||||||||||||||||||