| 1 | package Game::RoyalUr::Rules; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 61 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 61 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 61 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 61 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 61 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 61 | ▶ | use Object::Proto::Sugar; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 61 | ▶ | use Game::RoyalUr::Engine qw(SIDE_LIGHT SIDE_DARK WON DRAWN BY_HOME BY_PLY_CAP); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 61 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 61 | my $E = 'Game::RoyalUr::Engine'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 61 | my (@SIDE_NAME, %SIDE_OF); | |||||||||||||||||||||||||||
| 18 | 61 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 61 | @SIDE_NAME = ('light', 'dark'); | |||||||||||||||||||||||||||
| 20 | 61 | %SIDE_OF = (light => SIDE_LIGHT, dark => SIDE_DARK); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 61 | has _given => (is => 'ro', init_arg => 'rules', private => 1); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 61 | has _position => (is => 'ro', init_arg => 'position', private => 1); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 61 | has _first => (is => 'ro', init_arg => 'first', private => 1); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 61 | has _start_ply => (is => 'ro', init_arg => 'ply', private => 1); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 61 | has _engine => (is => 'rw', private => 1); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 61 | has _spelled => (is => 'rw', private => 1); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 61 | has _stack => (is => 'rw', private => 1); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1452 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 1452 | my ($self) = @_; | |||||||||||||||||||||||||||
| 39 | 1452 | my $spelled = $E->rules($self->_given); | |||||||||||||||||||||||||||
| 40 | 1451 | $self->_spelled($spelled); | |||||||||||||||||||||||||||
| 41 | 1451 | $self->_stack([]); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1451 | my $position = $self->_position; | |||||||||||||||||||||||||||
| 44 | my $engine = defined $position | ||||||||||||||||||||||||||||
| 45 | ? $E->new(position => $position) | ||||||||||||||||||||||||||||
| 46 | 1451 | ▶ | : $E->new(pieces => $spelled->{pieces}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1450 | my $first = $self->_first; | |||||||||||||||||||||||||||
| 49 | 1450 | ▶ | if (defined $first) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | Carp::croak("Game::RoyalUr::Rules: first is 'light' or 'dark'") | ||||||||||||||||||||||||||||
| 51 | 1241 | ▶ | unless exists $SIDE_OF{$first}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 1240 | $engine->set_side($SIDE_OF{$first}); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1449 | my $ply = $self->_start_ply; | |||||||||||||||||||||||||||
| 56 | 1449 | ▶ | if (defined $ply) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 5 | ▶ | Carp::croak('Game::RoyalUr::Rules: ply is a whole number from 0 up') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | unless $ply =~ /\A\d+\z/; | ||||||||||||||||||||||||||||
| 59 | 4 | $engine->set_ply($ply); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1448 | $self->_engine($engine); | |||||||||||||||||||||||||||
| 63 | 1448 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | sub rules { return { %{ $_[0]->_spelled } } } | ||||||||||||||||||||||||||
|
Subroutine
rules: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 693604 | ▶ | sub side { $SIDE_NAME[ $_[0]->_engine->side ] } | ||||||||||||||||||||||||||
|
Subroutine
side: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 222685 | ▶ | sub ply { $_[0]->_engine->ply } | ||||||||||||||||||||||||||
|
Subroutine
ply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 15962 | ▶ | sub position { $_[0]->_engine->to_string } | ||||||||||||||||||||||||||
|
Subroutine
position: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 7554 | ▶ | sub key { $_[0]->_engine->key_hex } | ||||||||||||||||||||||||||
|
Subroutine
key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 56866 | ▶ | sub board { $_[0]->_engine->clone } | ||||||||||||||||||||||||||
|
Subroutine
board: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 7122 | ▶ | sub hand { | ||||||||||||||||||||||||||
|
Subroutine
hand: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 7122 | my ($self, $side) = @_; | |||||||||||||||||||||||||||
| 80 | 7122 | ▶ | Carp::croak("Game::RoyalUr::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 | 7122 | return $self->_engine->hand($SIDE_OF{$side}); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 12274 | ▶ | sub home { | ||||||||||||||||||||||||||
|
Subroutine
home: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 12274 | my ($self, $side) = @_; | |||||||||||||||||||||||||||
| 86 | 12274 | ▶ | Carp::croak("Game::RoyalUr::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
| |||||||||||||||||||||||||||||
| 87 | 12274 | return $self->_engine->home($SIDE_OF{$side}); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2164625 | ▶ | sub status { | ||||||||||||||||||||||||||
|
Subroutine
status: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 2164625 | my ($self) = @_; | |||||||||||||||||||||||||||
| 92 | 2164625 | my $status = $self->_engine->status($self->_spelled); | |||||||||||||||||||||||||||
| 93 | 2164625 | ▶ | return $status == WON ? 'won' : $status == DRAWN ? 'drawn' : 'ongoing'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2564 | ▶ | sub how { | ||||||||||||||||||||||||||
|
Subroutine
how: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 2564 | my ($self) = @_; | |||||||||||||||||||||||||||
| 98 | 2564 | my $how = $self->_engine->how($self->_spelled); | |||||||||||||||||||||||||||
| 99 | 2564 | ▶ | return $how == BY_HOME ? 'home' : $how == BY_PLY_CAP ? 'ply_cap' : undef; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2568 | ▶ | sub winner { | ||||||||||||||||||||||||||
|
Subroutine
winner: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 2568 | my ($self) = @_; | |||||||||||||||||||||||||||
| 104 | 2568 | my $winner = $self->_engine->winner($self->_spelled); | |||||||||||||||||||||||||||
| 105 | 2568 | ▶ | return $winner < 0 ? undef : $SIDE_NAME[$winner]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2164605 | ▶ | sub is_over { $_[0]->status ne 'ongoing' } | ||||||||||||||||||||||||||
|
Subroutine
is_over: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 621249 | ▶ | sub moves { | ||||||||||||||||||||||||||
|
Subroutine
moves: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 621249 | my ($self, $roll) = @_; | |||||||||||||||||||||||||||
| 112 | 621249 | ▶ | return wantarray ? () : 0 if $self->is_over; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 113 | 621243 | return $self->_engine->moves($roll, $self->_spelled); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 202972 | ▶ | sub apply { | ||||||||||||||||||||||||||
|
Subroutine
apply: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 202972 | my ($self, $move) = @_; | |||||||||||||||||||||||||||
| 118 | 202972 | ▶ | Carp::croak('Game::RoyalUr::Rules: apply takes a move') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($move) and $move->isa("Game::RoyalUr::Move")
MC/DC50%1/2
builtin::blessed($move) and $move->isa("Game::RoyalUr::Move")
builtin::blessed($move)
$move->isa("Game::RoyalUr::Move")
Truth table66%2/3
A and B
A= builtin::blessed($move)B= $move->isa("Game::RoyalUr::Move")
| |||||||||||||||||||||||||||||
| 119 | unless Scalar::Util::blessed($move) && $move->isa('Game::RoyalUr::Move'); | ||||||||||||||||||||||||||||
| 120 | 202971 | ▶ | return undef if $self->is_over; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 202970 | ▶ | return undef if $move->side ne $self->side; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 202969 | my $undo = $self->_engine->apply($move, $self->_spelled); | |||||||||||||||||||||||||||
| 123 | 202969 | ▶ | return undef unless defined $undo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 202969 | push @{ $self->_stack }, $undo; | |||||||||||||||||||||||||||
| 125 | 202969 | return $move; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 8872 | ▶ | sub forfeit { | ||||||||||||||||||||||||||
|
Subroutine
forfeit: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 8872 | my ($self) = @_; | |||||||||||||||||||||||||||
| 130 | 8872 | ▶ | return 0 if $self->is_over; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 8871 | push @{ $self->_stack }, $self->_engine->forfeit; | |||||||||||||||||||||||||||
| 132 | 8871 | return 1; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 727 | ▶ | sub undo { | ||||||||||||||||||||||||||
|
Subroutine
undo: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 727 | my ($self) = @_; | |||||||||||||||||||||||||||
| 137 | 727 | my $undo = pop @{ $self->_stack }; | |||||||||||||||||||||||||||
| 138 | 727 | ▶ | return 0 unless defined $undo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 726 | $self->_engine->unapply($undo); | |||||||||||||||||||||||||||
| 140 | 726 | return 1; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 204200 | ▶ | sub depth { scalar @{ $_[0]->_stack } } | ||||||||||||||||||||||||||
|
Subroutine
depth: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 61 | 1; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||