| 1 | package Game::Go::Error; | ||||||||||
| 2 | |||||||||||
| 3 | 25 | ▶ | use 5.010; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 25 | ▶ | use strict; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 25 | ▶ | use warnings; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 25 | ▶ | use Object::Proto::Sugar -types; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 25 | ▶ | use Game::Go::Rules; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 25 | our $VERSION = '0.02'; | |||||||||
| 12 | |||||||||||
| 13 | 25 | our @FLAGS; | |||||||||
| 14 | 25 | ▶ | BEGIN { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 25 | @FLAGS = qw( | |||||||||
| 16 | not_your_turn | ||||||||||
| 17 | game_over | ||||||||||
| 18 | off_board | ||||||||||
| 19 | point_taken | ||||||||||
| 20 | is_ko | ||||||||||
| 21 | is_suicide | ||||||||||
| 22 | is_repeat | ||||||||||
| 23 | not_marking | ||||||||||
| 24 | still_marking | ||||||||||
| 25 | alive_chain | ||||||||||
| 26 | not_a_chain | ||||||||||
| 27 | bad_colour | ||||||||||
| 28 | ); | ||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 25 | our %MESSAGE; | |||||||||
| 32 | 25 | ▶ | BEGIN { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 25 | %MESSAGE = ( | |||||||||
| 34 | not_your_turn => 'it is not your turn', | ||||||||||
| 35 | game_over => 'the game has ended', | ||||||||||
| 36 | off_board => 'that point is not on the board', | ||||||||||
| 37 | point_taken => 'there is already a stone there', | ||||||||||
| 38 | is_ko => 'the ko rule forbids retaking that point immediately', | ||||||||||
| 39 | is_suicide => 'that move would leave your own stones with no liberty', | ||||||||||
| 40 | is_repeat => 'that move would repeat a position the game has already had', | ||||||||||
| 41 | not_marking => 'the game is not being scored, so there is nothing to mark', | ||||||||||
| 42 | still_marking => 'the game is being scored, so no stone can be played', | ||||||||||
| 43 | alive_chain => 'that group is alive whatever happens, so it cannot be agreed dead', | ||||||||||
| 44 | not_a_chain => 'there is no group on that point to mark', | ||||||||||
| 45 | bad_colour => 'that is not a colour', | ||||||||||
| 46 | ); | ||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 25 | our %FROM_CODE; | |||||||||
| 50 | 25 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 25 | %FROM_CODE = ( | |||||||||
| 52 | 1 => 'off_board', | ||||||||||
| 53 | 2 => 'point_taken', | ||||||||||
| 54 | 3 => 'is_ko', | ||||||||||
| 55 | 4 => 'is_suicide', | ||||||||||
| 56 | 5 => 'is_repeat', | ||||||||||
| 57 | 6 => 'bad_colour', | ||||||||||
| 58 | ); | ||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 25 | has [@FLAGS] => (is => 'ro'); | |||||||||
| 62 | |||||||||||
| 63 | 25 | has error => (is => 'ro', default => 1); | |||||||||
| 64 | |||||||||||
| 65 | 25 | has message => (is => 'ro', isa => Str, default => ''); | |||||||||
| 66 | |||||||||||
| 67 | 25 | has legal => (is => 'ro', isa => ArrayRef, default => []); | |||||||||
| 68 | |||||||||||
| 69 | 38 | ▶ | sub throw { | ||||||||
|
Subroutine
throw: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 70 | 38 | my ($class, $flag, %extra) = @_; | |||||||||
| 71 | 38 | ▶ | die "Game::Go::Error: no such flag '$flag'" unless exists $MESSAGE{$flag}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | return $class->new( | ||||||||||
| 73 | $flag => 1, | ||||||||||
| 74 | error => 1, | ||||||||||
| 75 | 38 | message => $MESSAGE{$flag}, | |||||||||
| 76 | %extra, | ||||||||||
| 77 | ); | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 19 | ▶ | sub from_code { | ||||||||
|
Subroutine
from_code: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 81 | 19 | my ($class, $code, %extra) = @_; | |||||||||
| 82 | 19 | my $flag = $FROM_CODE{$code}; | |||||||||
| 83 | 19 | ▶ | die "Game::Go::Error: the engine returned code $code, which has no flag" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | unless defined $flag; | ||||||||||
| 85 | 19 | return $class->throw($flag, %extra); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | sub flags { return [ grep { $_[0]->$_ } @FLAGS ] } | ||||||||
|
Subroutine
flags: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 89 | |||||||||||
| 90 | 18 | ▶ | sub code { | ||||||||
|
Subroutine
code: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 91 | 18 | my ($self) = @_; | |||||||||
| 92 | 18 | for my $flag (@FLAGS) { | |||||||||
| 93 | 78 | ▶ | return $flag if $self->$flag; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | } | ||||||||||
| 95 | 0 | return undef; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | sub stringify { $_[0]->message } | ||||||||
|
Subroutine
stringify: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 99 | |||||||||||
| 100 | 25 | 1; | |||||||||
| 101 | |||||||||||
| 102 | __END__ | ||||||||||