| 1 | package Game::Brandubh::Result; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use Object::Proto::Sugar; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | our $VERSION = '0.03'; | ||
| 11 | |||
| 12 | our (%WIN, %DRAW); | ||
| 13 | BEGIN { | ||
| 14 | %WIN = map { $_ => 1 } qw(corner edge capture no_pieces resign); | ||
| 15 | %DRAW = map { $_ => 1 } qw(repetition no_move ply_cap agreed); | ||
| 16 | } | ||
| 17 | |||
| 18 | has how => (is => 'ro'); | ||
| 19 | |||
| 20 | has winner => (is => 'ro'); | ||
| 21 | |||
| 22 | has seat => (is => 'ro'); | ||
| 23 | |||
| 24 | has ply => (is => 'ro', default => 0); | ||
| 25 | |||
| 26 | has position => (is => 'ro'); | ||
| 27 | |||
| 28 | sub BUILD { | ||
| 29 | my ($self) = @_; | ||
| 30 | my $how = $self->how; | ||
| 31 | Carp::croak('Game::Brandubh::Result: a result says how the game ended') | ||
| 32 | unless defined $how && ($WIN{$how} || $DRAW{$how}); | ||
| 33 | |||
| 34 | my $winner = $self->winner; | ||
| 35 | if ($WIN{$how}) { | ||
| 36 | Carp::croak("Game::Brandubh::Result: a game ended by $how has a winner, attackers or defenders") | ||
| 37 | unless defined $winner && ($winner eq 'attackers' || $winner eq 'defenders'); | ||
| 38 | Carp::croak("Game::Brandubh::Result: a game ended by $how names the winner's seat") | ||
| 39 | unless defined $self->seat; | ||
| 40 | } | ||
| 41 | else { | ||
| 42 | Carp::croak("Game::Brandubh::Result: a game drawn by $how has no winner") | ||
| 43 | if defined $winner || defined $self->seat; | ||
| 44 | } | ||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub is_draw { $DRAW{ $_[0]->how } ? 1 : 0 } | ||
| 49 | |||
| 50 | sub loser { | ||
| 51 | my ($self) = @_; | ||
| 52 | my $winner = $self->winner; | ||
| 53 | return undef unless defined $winner; | ||
| 54 | return $winner eq 'attackers' ? 'defenders' : 'attackers'; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub by_players { | ||
| 58 | my $how = $_[0]->how; | ||
| 59 | return $how eq 'resign' || $how eq 'agreed' ? 1 : 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub hows { sort(keys %WIN, keys %DRAW) } | ||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | __END__ | ||