| 1 | package Game::Merrills::Points; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.03'; | ||
| 7 | |||
| 8 | use constant { | ||
| 9 | POINTS => 24, | ||
| 10 | SIZE => 7, | ||
| 11 | }; | ||
| 12 | |||
| 13 | our (@NAME, %POINT, @ROW, @COL, @ADJACENT, @MILLS, @MILLS_OF); | ||
| 14 | |||
| 15 | BEGIN { | ||
| 16 | @NAME = qw/ | ||
| 17 | a7 d7 g7 | ||
| 18 | b6 d6 f6 | ||
| 19 | c5 d5 e5 | ||
| 20 | a4 b4 c4 e4 f4 g4 | ||
| 21 | c3 d3 e3 | ||
| 22 | b2 d2 f2 | ||
| 23 | a1 d1 g1 | ||
| 24 | /; | ||
| 25 | |||
| 26 | @ADJACENT = ( | ||
| 27 | [1, 9], [0, 2, 4], [1, 14], | ||
| 28 | [4, 10], [1, 3, 5, 7], [4, 13], | ||
| 29 | [7, 11], [4, 6, 8], [7, 12], | ||
| 30 | [0, 10, 21], [3, 9, 11, 18], [6, 10, 15], | ||
| 31 | [8, 13, 17], [5, 12, 14, 20], [2, 13, 23], | ||
| 32 | [11, 16], [15, 17, 19], [12, 16], | ||
| 33 | [10, 19], [16, 18, 20, 22], [13, 19], | ||
| 34 | [9, 22], [19, 21, 23], [14, 22], | ||
| 35 | ); | ||
| 36 | |||
| 37 | @MILLS = ( | ||
| 38 | [0, 1, 2], [3, 4, 5], [6, 7, 8], | ||
| 39 | [9, 10, 11], [12, 13, 14], | ||
| 40 | [15, 16, 17], [18, 19, 20], [21, 22, 23], | ||
| 41 | [0, 9, 21], [3, 10, 18], [6, 11, 15], | ||
| 42 | [1, 4, 7], [16, 19, 22], | ||
| 43 | [8, 12, 17], [5, 13, 20], [2, 14, 23], | ||
| 44 | ); | ||
| 45 | |||
| 46 | for my $n (0 .. $#NAME) { | ||
| 47 | my ($file, $rank) = $NAME[$n] =~ m/^([a-g])([1-7])$/; | ||
| 48 | $POINT{$NAME[$n]} = $n; | ||
| 49 | $ROW[$n] = SIZE - $rank; | ||
| 50 | $COL[$n] = ord($file) - ord('a'); | ||
| 51 | $MILLS_OF[$n] = []; | ||
| 52 | } | ||
| 53 | |||
| 54 | for my $mill (@MILLS) { | ||
| 55 | push @{$MILLS_OF[$_]}, $mill for @{$mill}; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _check { | ||
| 60 | my ($n) = @_; | ||
| 61 | die 'point must be 0 .. 23, got ' . (defined $n ? "'$n'" : 'undef') | ||
| 62 | unless defined $n && $n =~ m/^[0-9]+$/ && $n < POINTS; | ||
| 63 | return $n; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub all_points { | ||
| 67 | return (0 .. POINTS - 1); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub point { | ||
| 71 | my ($name) = @_; | ||
| 72 | return undef unless defined $name; | ||
| 73 | return $POINT{lc $name}; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub name { | ||
| 77 | my ($n) = @_; | ||
| 78 | _check($n); | ||
| 79 | return $NAME[$n]; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub row_of { | ||
| 83 | my ($n) = @_; | ||
| 84 | _check($n); | ||
| 85 | return $ROW[$n]; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub col_of { | ||
| 89 | my ($n) = @_; | ||
| 90 | _check($n); | ||
| 91 | return $COL[$n]; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub adjacent { | ||
| 95 | my ($n) = @_; | ||
| 96 | _check($n); | ||
| 97 | return @{$ADJACENT[$n]}; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub is_adjacent { | ||
| 101 | my ($from, $to) = @_; | ||
| 102 | _check($from); | ||
| 103 | _check($to); | ||
| 104 | return scalar(grep { $_ == $to } @{$ADJACENT[$from]}) ? 1 : 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub mills { | ||
| 108 | return map { [@{$_}] } @MILLS; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub mills_of { | ||
| 112 | my ($n) = @_; | ||
| 113 | _check($n); | ||
| 114 | return map { [@{$_}] } @{$MILLS_OF[$n]}; | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 | __END__ |