| 1 | package Game::Senet::Rules; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 66 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 66 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 66 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 66 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 66 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 66 | ▶ | use Object::Proto::Sugar; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 66 | ▶ | use Game::Senet::Engine qw(SIDE_LIGHT SIDE_DARK LIGHT DARK WON DRAWN BY_HOME BY_PLY_CAP); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 66 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 66 | my $E = 'Game::Senet::Engine'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 66 | my (@SIDE_NAME, %SIDE_OF); | |||||||||||||||||||||||||||
| 18 | 66 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 66 | @SIDE_NAME = ('light', 'dark'); | |||||||||||||||||||||||||||
| 20 | 66 | %SIDE_OF = (light => SIDE_LIGHT, dark => SIDE_DARK); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 66 | has _given => (is => 'ro', init_arg => 'rules', private => 1); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 66 | has _position => (is => 'ro', init_arg => 'position', private => 1); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 66 | has _first => (is => 'ro', init_arg => 'first', private => 1, default => 'light'); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 66 | has _engine => (is => 'rw', private => 1); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 66 | has _spelled => (is => 'rw', private => 1); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 66 | has _stack => (is => 'rw', private => 1); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 66 | has _bound => (is => 'rw', private => 1); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 7198 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 7198 | my ($self) = @_; | |||||||||||||||||||||||||||
| 39 | 7198 | my $spelled = $E->rules($self->_given); | |||||||||||||||||||||||||||
| 40 | 7198 | $self->_spelled($spelled); | |||||||||||||||||||||||||||
| 41 | 7198 | $self->_stack([]); | |||||||||||||||||||||||||||
| 42 | 7198 | $self->_bound(0); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 7198 | my $position = $self->_position; | |||||||||||||||||||||||||||
| 45 | 7198 | my $engine; | |||||||||||||||||||||||||||
| 46 | 7198 | ▶ | if (defined $position) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 157 | $engine = $E->new(position => $position); | |||||||||||||||||||||||||||
| 48 | Carp::croak('Game::Senet::Rules: the position has ' . $engine->pieces | ||||||||||||||||||||||||||||
| 49 | . ' pieces a side and the rule set ' . $spelled->{pieces}) | ||||||||||||||||||||||||||||
| 50 | 157 | ▶ | unless $engine->pieces == $spelled->{pieces}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | else { | ||||||||||||||||||||||||||||
| 53 | 7041 | my $first = $self->_first; | |||||||||||||||||||||||||||
| 54 | Carp::croak("Game::Senet::Rules: first is 'light' or 'dark'") | ||||||||||||||||||||||||||||
| 55 | 7041 | ▶ | unless defined $first && exists $SIDE_OF{$first}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $first and exists $SIDE_OF{$first}
MC/DC0%0/2
defined $first and exists $SIDE_OF{$first}
defined $first
exists $SIDE_OF{$first}
Truth table33%1/3
A and B
A= defined $firstB= exists $SIDE_OF{$first}
| |||||||||||||||||||||||||||||
| 56 | 7041 | $engine = $E->new(pieces => $spelled->{pieces}); | |||||||||||||||||||||||||||
| 57 | 7041 | ▶ | $engine->put($_, $_ % 2 ? DARK : LIGHT) for 1 .. 2 * $spelled->{pieces}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 7041 | $engine->set_side($SIDE_OF{$first}); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 7198 | $self->_engine($engine); | |||||||||||||||||||||||||||
| 61 | 7198 | return; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub rules { return { %{ $_[0]->_spelled } } } | ||||||||||||||||||||||||||
|
Subroutine
rules: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 3365170 | ▶ | sub side { $SIDE_NAME[ $_[0]->_engine->side ] } | ||||||||||||||||||||||||||
|
Subroutine
side: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 232605 | ▶ | sub only { $_[0]->_bound } | ||||||||||||||||||||||||||
|
Subroutine
only: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 861380 | ▶ | sub ply { $_[0]->_engine->ply } | ||||||||||||||||||||||||||
|
Subroutine
ply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 90299 | ▶ | sub position { $_[0]->_engine->to_string } | ||||||||||||||||||||||||||
|
Subroutine
position: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 90159 | ▶ | sub key { $_[0]->_engine->key_hex } | ||||||||||||||||||||||||||
|
Subroutine
key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 582075 | ▶ | sub board { $_[0]->_engine->clone } | ||||||||||||||||||||||||||
|
Subroutine
board: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 15565 | ▶ | sub home { | ||||||||||||||||||||||||||
|
Subroutine
home: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 15565 | my ($self, $side) = @_; | |||||||||||||||||||||||||||
| 80 | 15565 | ▶ | Carp::croak("Game::Senet::Rules: a side is 'light' or 'dark'") unless exists $SIDE_OF{ $side // '' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$side // ""
MC/DC0%0/1
$side // ""
$side
Truth table50%1/2
A
A= $side
| |||||||||||||||||||||||||||||
| 81 | 15565 | return $self->_engine->home($SIDE_OF{$side}); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2265 | ▶ | sub protected { $_[0]->_engine->protected($_[1], $_[0]->_given) } | ||||||||||||||||||||||||||
|
Subroutine
protected: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 4202 | ▶ | sub in_blockade { $_[0]->_engine->in_blockade($_[1], $_[0]->_given) } | ||||||||||||||||||||||||||
|
Subroutine
in_blockade: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 7317262 | ▶ | sub status { | ||||||||||||||||||||||||||
|
Subroutine
status: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 7317262 | my ($self) = @_; | |||||||||||||||||||||||||||
| 90 | 7317262 | my $status = $self->_engine->status; | |||||||||||||||||||||||||||
| 91 | 7317262 | ▶ | return $status == WON ? 'won' : $status == DRAWN ? 'drawn' : 'ongoing'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1411 | ▶ | sub how { | ||||||||||||||||||||||||||
|
Subroutine
how: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 1411 | my ($self) = @_; | |||||||||||||||||||||||||||
| 96 | 1411 | my $how = $self->_engine->how; | |||||||||||||||||||||||||||
| 97 | 1411 | ▶ | return $how == BY_HOME ? 'home' : $how == BY_PLY_CAP ? 'ply_cap' : undef; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1412 | ▶ | sub winner { | ||||||||||||||||||||||||||
|
Subroutine
winner: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 1412 | my ($self) = @_; | |||||||||||||||||||||||||||
| 102 | 1412 | my $winner = $self->_engine->winner; | |||||||||||||||||||||||||||
| 103 | 1412 | ▶ | return $winner < 0 ? undef : $SIDE_NAME[$winner]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 7317259 | ▶ | sub is_over { $_[0]->status ne 'ongoing' } | ||||||||||||||||||||||||||
|
Subroutine
is_over: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 854737 | ▶ | sub moves { | ||||||||||||||||||||||||||
|
Subroutine
moves: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 854737 | my ($self, $roll) = @_; | |||||||||||||||||||||||||||
| 110 | 854737 | ▶ | return wantarray ? () : 0 if $self->is_over; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 111 | 854736 | return $self->_engine->moves($roll, $self->_given, $self->_bound); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 748776 | ▶ | sub apply { | ||||||||||||||||||||||||||
|
Subroutine
apply: called CC=8 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 748776 | my ($self, $move) = @_; | |||||||||||||||||||||||||||
| 116 | 748776 | ▶ | Carp::croak('Game::Senet::Rules: apply takes a move') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($move) and $move->isa("Game::Senet::Move")
MC/DC0%0/2
builtin::blessed($move) and $move->isa("Game::Senet::Move")
builtin::blessed($move)
$move->isa("Game::Senet::Move")
Truth table33%1/3
A and B
A= builtin::blessed($move)B= $move->isa("Game::Senet::Move")
| |||||||||||||||||||||||||||||
| 117 | unless Scalar::Util::blessed($move) && $move->isa('Game::Senet::Move'); | ||||||||||||||||||||||||||||
| 118 | 748776 | ▶ | return undef if $self->is_over; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 748775 | ▶ | return undef if $move->side ne $self->side; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 748775 | ▶ | return undef if $self->_bound && $move->from != $self->_bound; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->_bound and $move->from != $self->_bound
MC/DC100%2/2
$self->_bound and $move->from != $self->_bound
$self->_bound
$move->from != $self->_bound
Truth table100%3/3
A and B
A= $self->_boundB= $move->from != $self->_bound
| |||||||||||||||||||||||||||||
| 121 | 748774 | my ($undo, $bound) = $self->_engine->apply($move, $self->_given); | |||||||||||||||||||||||||||
| 122 | 748774 | ▶ | return undef unless defined $undo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 748774 | push @{ $self->_stack }, [ $undo, $self->_bound ]; | |||||||||||||||||||||||||||
| 124 | 748774 | $self->_bound($bound); | |||||||||||||||||||||||||||
| 125 | 748774 | return $move; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 21487 | ▶ | sub forfeit { | ||||||||||||||||||||||||||
|
Subroutine
forfeit: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 21487 | my ($self, $roll) = @_; | |||||||||||||||||||||||||||
| 130 | 21487 | ▶ | return 0 if $self->is_over; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 21486 | my $undo = $self->_engine->forfeit($roll); | |||||||||||||||||||||||||||
| 132 | 21486 | push @{ $self->_stack }, [ $undo, $self->_bound ]; | |||||||||||||||||||||||||||
| 133 | 21486 | $self->_bound(0); | |||||||||||||||||||||||||||
| 134 | 21486 | return 1; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 627 | ▶ | sub undo { | ||||||||||||||||||||||||||
|
Subroutine
undo: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 627 | my ($self) = @_; | |||||||||||||||||||||||||||
| 139 | 627 | my $top = pop @{ $self->_stack }; | |||||||||||||||||||||||||||
| 140 | 627 | ▶ | return 0 unless defined $top; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 627 | $self->_engine->unapply($top->[0]); | |||||||||||||||||||||||||||
| 142 | 627 | $self->_bound($top->[1]); | |||||||||||||||||||||||||||
| 143 | 627 | return 1; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 749294 | ▶ | sub depth { scalar @{ $_[0]->_stack } } | ||||||||||||||||||||||||||
|
Subroutine
depth: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 66 | 1; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||