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