| 1 | package Game::Merrills::Rules; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Game::Merrills::Points; | ||
| 7 | use Game::Merrills::Board; | ||
| 8 | use Game::Merrills::Move; | ||
| 9 | |||
| 10 | our $VERSION = '0.03'; | ||
| 11 | |||
| 12 | use constant { | ||
| 13 | HAND_WHITE => 24, | ||
| 14 | HAND_BLACK => 25, | ||
| 15 | }; | ||
| 16 | |||
| 17 | use constant { | ||
| 18 | RM_FROM => Game::Merrills::Move::RM_FROM, | ||
| 19 | RM_TO => Game::Merrills::Move::RM_TO, | ||
| 20 | RM_REMOVE => Game::Merrills::Move::RM_REMOVE, | ||
| 21 | RM_CLOSES => Game::Merrills::Move::RM_CLOSES, | ||
| 22 | RM_FLEW => Game::Merrills::Move::RM_FLEW, | ||
| 23 | }; | ||
| 24 | |||
| 25 | sub _value { | ||
| 26 | my ($side) = @_; | ||
| 27 | die 'side must be white or black, got ' . (defined $side ? "'$side'" : 'undef') | ||
| 28 | unless defined $side && exists $Game::Merrills::Board::VALUE{$side}; | ||
| 29 | return $Game::Merrills::Board::VALUE{$side}; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _check { | ||
| 33 | my ($position) = @_; | ||
| 34 | die 'a position is an arrayref of 26 values' | ||
| 35 | unless ref $position eq 'ARRAY' && @{$position} == 26; | ||
| 36 | return $position; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _in_mill { | ||
| 40 | my ($position, $point) = @_; | ||
| 41 | my $value = $position->[$point] or return 0; | ||
| 42 | for my $mill (@{ $Game::Merrills::Points::MILLS_OF[$point] }) { | ||
| 43 | return 1 if $position->[ $mill->[0] ] == $value | ||
| 44 | && $position->[ $mill->[1] ] == $value | ||
| 45 | && $position->[ $mill->[2] ] == $value; | ||
| 46 | } | ||
| 47 | return 0; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _closes { | ||
| 51 | my ($position, $value, $from, $to) = @_; | ||
| 52 | my $closed = 0; | ||
| 53 | for my $mill (@{ $Game::Merrills::Points::MILLS_OF[$to] }) { | ||
| 54 | my $whole = 1; | ||
| 55 | for my $point (@{$mill}) { | ||
| 56 | next if $point == $to; | ||
| 57 | next if $position->[$point] == $value && !(defined $from && $point == $from); | ||
| 58 | $whole = 0; | ||
| 59 | last; | ||
| 60 | } | ||
| 61 | $closed += $whole; | ||
| 62 | } | ||
| 63 | return $closed; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _removable { | ||
| 67 | my ($position, $value) = @_; | ||
| 68 | my (@loose, @all); | ||
| 69 | for my $point (0 .. 23) { | ||
| 70 | next unless $position->[$point] == -$value; | ||
| 71 | push @all, $point; | ||
| 72 | push @loose, $point unless _in_mill($position, $point); | ||
| 73 | } | ||
| 74 | return @loose ? @loose : @all; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub position { | ||
| 78 | my ($board) = @_; | ||
| 79 | return [ @{ $board->cells }, $board->in_hand('white'), $board->in_hand('black') ]; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub board { | ||
| 83 | my ($position) = @_; | ||
| 84 | _check($position); | ||
| 85 | return Game::Merrills::Board->new( | ||
| 86 | cells => [ @{$position}[ 0 .. 23 ] ], | ||
| 87 | hand => { white => $position->[HAND_WHITE], black => $position->[HAND_BLACK] } | ||
| 88 | ); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub closes { | ||
| 92 | my ($position, $side, $from, $to) = @_; | ||
| 93 | _check($position); | ||
| 94 | Game::Merrills::Points::_check($to); | ||
| 95 | Game::Merrills::Points::_check($from) if defined $from; | ||
| 96 | return _closes($position, _value($side), $from, $to); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub removable { | ||
| 100 | my ($position, $side) = @_; | ||
| 101 | _check($position); | ||
| 102 | return _removable($position, _value($side)); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _phase { | ||
| 106 | my ($position, $value, $flying) = @_; | ||
| 107 | return 'placing' if $position->[ $value > 0 ? HAND_WHITE : HAND_BLACK ]; | ||
| 108 | return 'moving' unless $flying; | ||
| 109 | my $men = grep { $_ == $value } @{$position}[ 0 .. 23 ]; | ||
| 110 | return $men == 3 ? 'flying' : 'moving'; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub phase { | ||
| 114 | my ($position, $side, $flying) = @_; | ||
| 115 | _check($position); | ||
| 116 | return _phase($position, _value($side), defined $flying ? $flying : 1); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub generate { | ||
| 120 | my ($position, $side, $flying) = @_; | ||
| 121 | _check($position); | ||
| 122 | my $value = _value($side); | ||
| 123 | my $phase = _phase($position, $value, defined $flying ? $flying : 1); | ||
| 124 | |||
| 125 | my (@moves, @takes, $asked, @own); | ||
| 126 | @own = grep { $position->[$_] == $value } 0 .. 23 if $phase eq 'flying'; | ||
| 127 | for my $to (0 .. 23) { | ||
| 128 | next if $position->[$to]; | ||
| 129 | my @from = $phase eq 'placing' ? (undef) | ||
| 130 | : $phase eq 'flying' ? @own | ||
| 131 | : grep { $position->[$_] == $value } @{ $Game::Merrills::Points::ADJACENT[$to] }; | ||
| 132 | for my $from (@from) { | ||
| 133 | my $flew = $phase eq 'flying' | ||
| 134 | && !grep { $_ == $from } @{ $Game::Merrills::Points::ADJACENT[$to] }; | ||
| 135 | my $closed = _closes($position, $value, $from, $to); | ||
| 136 | unless ($closed) { | ||
| 137 | push @moves, [ $from, $to, undef, 0, $flew ? 1 : 0 ]; | ||
| 138 | next; | ||
| 139 | } | ||
| 140 | @takes = _removable($position, $value) unless $asked++; | ||
| 141 | die 'a mill closed and there is no man to take' unless @takes; | ||
| 142 | push @moves, map { [ $from, $to, $_, $closed, $flew ? 1 : 0 ] } @takes; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | return \@moves; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub has_move { | ||
| 149 | my ($position, $side, $flying) = @_; | ||
| 150 | _check($position); | ||
| 151 | my $value = _value($side); | ||
| 152 | my $phase = _phase($position, $value, defined $flying ? $flying : 1); | ||
| 153 | if ($phase eq 'moving') { | ||
| 154 | for my $from (0 .. 23) { | ||
| 155 | next unless $position->[$from] == $value; | ||
| 156 | for my $to (@{ $Game::Merrills::Points::ADJACENT[$from] }) { | ||
| 157 | return 1 unless $position->[$to]; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | return 0; | ||
| 161 | } | ||
| 162 | return scalar(grep { !$_ } @{$position}[ 0 .. 23 ]) ? 1 : 0; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub apply { | ||
| 166 | my ($position, $side, $raw) = @_; | ||
| 167 | my $value = _value($side); | ||
| 168 | if (defined $raw->[RM_FROM]) { | ||
| 169 | $position->[ $raw->[RM_FROM] ] = 0; | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | $position->[ $value > 0 ? HAND_WHITE : HAND_BLACK ]--; | ||
| 173 | } | ||
| 174 | $position->[ $raw->[RM_TO] ] = $value; | ||
| 175 | $position->[ $raw->[RM_REMOVE] ] = 0 if defined $raw->[RM_REMOVE]; | ||
| 176 | return $position; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub unapply { | ||
| 180 | my ($position, $side, $raw) = @_; | ||
| 181 | my $value = _value($side); | ||
| 182 | $position->[ $raw->[RM_REMOVE] ] = -$value if defined $raw->[RM_REMOVE]; | ||
| 183 | $position->[ $raw->[RM_TO] ] = 0; | ||
| 184 | if (defined $raw->[RM_FROM]) { | ||
| 185 | $position->[ $raw->[RM_FROM] ] = $value; | ||
| 186 | } | ||
| 187 | else { | ||
| 188 | $position->[ $value > 0 ? HAND_WHITE : HAND_BLACK ]++; | ||
| 189 | } | ||
| 190 | return $position; | ||
| 191 | } | ||
| 192 | |||
| 193 | 1; | ||
| 194 | |||
| 195 | __END__ |