| 1 | package Game::Merrills::Error; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Object::Proto::Sugar -types; | ||
| 7 | |||
| 8 | our $VERSION = '0.03'; | ||
| 9 | |||
| 10 | our (@FLAGS, %MESSAGE); | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | @FLAGS = qw/ | ||
| 14 | game_over | ||
| 15 | not_a_move | ||
| 16 | men_in_hand | ||
| 17 | no_men_in_hand | ||
| 18 | not_your_man | ||
| 19 | occupied | ||
| 20 | not_adjacent | ||
| 21 | must_remove | ||
| 22 | nothing_to_remove | ||
| 23 | no_man_there | ||
| 24 | man_in_mill | ||
| 25 | not_legal | ||
| 26 | no_offer | ||
| 27 | nothing_to_undo | ||
| 28 | /; | ||
| 29 | |||
| 30 | %MESSAGE = ( | ||
| 31 | game_over => 'the game is over', | ||
| 32 | not_a_move => 'that is not a move', | ||
| 33 | men_in_hand => 'you still have men to place, so place one', | ||
| 34 | no_men_in_hand => 'every man is placed, so move one', | ||
| 35 | not_your_man => 'you have no man on that point', | ||
| 36 | occupied => 'that point is not empty', | ||
| 37 | not_adjacent => 'a man moves along a line to the next point', | ||
| 38 | must_remove => 'that closes a mill, so say which man you take', | ||
| 39 | nothing_to_remove => 'that closes no mill, so it takes no man', | ||
| 40 | no_man_there => 'there is no man of theirs on that point to take', | ||
| 41 | man_in_mill => 'a man in a mill is safe while another stands outside one', | ||
| 42 | not_legal => 'that is not a legal move', | ||
| 43 | no_offer => 'there is no draw to answer', | ||
| 44 | nothing_to_undo => 'there is nothing to undo', | ||
| 45 | ); | ||
| 46 | } | ||
| 47 | |||
| 48 | has [@FLAGS] => ( | ||
| 49 | is => 'ro' | ||
| 50 | ); | ||
| 51 | |||
| 52 | has error => ( | ||
| 53 | is => 'ro', | ||
| 54 | default => 1 | ||
| 55 | ); | ||
| 56 | |||
| 57 | has message => ( | ||
| 58 | is => 'ro', | ||
| 59 | isa => Str | ||
| 60 | ); | ||
| 61 | |||
| 62 | has legal => ( | ||
| 63 | is => 'ro', | ||
| 64 | isa => ArrayRef, | ||
| 65 | default => sub { [] } | ||
| 66 | ); | ||
| 67 | |||
| 68 | sub throw { | ||
| 69 | my ($class, $flag, %extra) = @_; | ||
| 70 | die "'" . (defined $flag ? $flag : 'undef') . "' is not an error flag" | ||
| 71 | unless defined $flag && $MESSAGE{$flag}; | ||
| 72 | return $class->new( | ||
| 73 | $flag => 1, | ||
| 74 | message => $MESSAGE{$flag}, | ||
| 75 | %extra | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub flags { | ||
| 80 | return @FLAGS; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub messages { | ||
| 84 | return {%MESSAGE}; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub code { | ||
| 88 | my ($self) = @_; | ||
| 89 | for my $flag (@FLAGS) { | ||
| 90 | return $flag if $self->$flag; | ||
| 91 | } | ||
| 92 | return undef; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub stringify { | ||
| 96 | return $_[0]->message; | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | __END__ |