… | |
… | |
29 | MARK_HOSHI # this is a hoshi point (not used much) |
29 | MARK_HOSHI # this is a hoshi point (not used much) |
30 | MARK_MOVE # this is a regular move |
30 | MARK_MOVE # this is a regular move |
31 | MARK_KO # this is a ko position |
31 | MARK_KO # this is a ko position |
32 | MARK_REDRAW # ignored, can be used for your own purposes |
32 | MARK_REDRAW # ignored, can be used for your own purposes |
33 | |
33 | |
34 | COLOUR_BLACK # used for $board->{last} |
34 | COLOUR_WHITE # guarenteed to be 0 |
35 | COLOUR_WHITE # to mark the colour of the last move |
35 | COLOUR_BLACK # guarenteed to be 1 |
36 | |
36 | |
37 | MOVE_HANDICAP # used as "x-coordinate" for handicap moves |
37 | MOVE_HANDICAP # used as "x-coordinate" for handicap moves |
38 | MOVE_PASS # can be used as "x-coordinate" for handicap moves |
38 | MOVE_PASS # can be used as "x-coordinate" for pass moves |
39 | |
39 | |
40 | =head2 METHODS |
40 | =head2 METHODS |
41 | |
41 | |
42 | =over 4 |
42 | =over 4 |
43 | |
43 | |
… | |
… | |
52 | |
52 | |
53 | our $VERSION = '1.0'; |
53 | our $VERSION = '1.0'; |
54 | |
54 | |
55 | our @EXPORT = qw( |
55 | our @EXPORT = qw( |
56 | MARK_TRIANGLE MARK_SQUARE MARK_CIRCLE MARK_SMALL_B MARK_SMALL_W MARK_B |
56 | MARK_TRIANGLE MARK_SQUARE MARK_CIRCLE MARK_SMALL_B MARK_SMALL_W MARK_B |
57 | MARK_W MARK_GRAYED MARK_MOVE MARK_LABEL MARK_HOSHI MARK_KO |
57 | MARK_W MARK_GRAYED MARK_MOVE MARK_LABEL MARK_HOSHI MARK_KO MARK_CROSS |
58 | MARK_REDRAW |
58 | MARK_REDRAW |
59 | COLOUR_BLACK COLOUR_WHITE |
59 | COLOUR_BLACK COLOUR_WHITE |
60 | MOVE_HANDICAP MOVE_PASS |
60 | MOVE_HANDICAP MOVE_PASS |
61 | ); |
61 | ); |
62 | |
62 | |
… | |
… | |
72 | sub MARK_GRAYED (){ 0x0080 } # in conjunction with MARK_[BW], grays the stone |
72 | sub MARK_GRAYED (){ 0x0080 } # in conjunction with MARK_[BW], grays the stone |
73 | sub MARK_LABEL (){ 0x0100 } |
73 | sub MARK_LABEL (){ 0x0100 } |
74 | sub MARK_HOSHI (){ 0x0200 } # this is a hoshi point (not used much) |
74 | sub MARK_HOSHI (){ 0x0200 } # this is a hoshi point (not used much) |
75 | sub MARK_MOVE (){ 0x0400 } # this is a regular move |
75 | sub MARK_MOVE (){ 0x0400 } # this is a regular move |
76 | sub MARK_KO (){ 0x0800 } # this is a ko position |
76 | sub MARK_KO (){ 0x0800 } # this is a ko position |
77 | sub MARK_CIRCLE (){ 0x1000 } |
77 | sub MARK_CROSS (){ 0x1000 } |
78 | sub MARK_REDRAW (){ 0x8000 } |
78 | sub MARK_REDRAW (){ 0x8000 } |
79 | |
79 | |
80 | sub COLOUR_BLACK (){ 0 } |
|
|
81 | sub COLOUR_WHITE (){ 1 } |
80 | sub COLOUR_WHITE (){ 0 } |
|
|
81 | sub COLOUR_BLACK (){ 1 } |
82 | |
82 | |
83 | sub MOVE_PASS (){ undef } |
83 | sub MOVE_PASS (){ undef } |
84 | sub MOVE_HANDICAP (){ -2 } |
84 | sub MOVE_HANDICAP (){ -2 } |
85 | |
85 | |
86 | =item my $board = new $size |
86 | =item my $board = new $size |
… | |
… | |
89 | |
89 | |
90 | C<< $board->{size} >> stores the board size. |
90 | C<< $board->{size} >> stores the board size. |
91 | |
91 | |
92 | C<< $board->{max} >> stores the maximum board coordinate (size-1). |
92 | C<< $board->{max} >> stores the maximum board coordinate (size-1). |
93 | |
93 | |
94 | C<< $board->{captures}[COLOUR] >> stores the number of captured stones for |
94 | C<< $board->{captures}[COLOUR_xxx] >> stores the number of captured stones for |
95 | the given colour. |
95 | the given colour. |
96 | |
|
|
97 | C<< $board->{last} >> stores the colour of the last move that was played. |
|
|
98 | |
96 | |
99 | C<< $board->{board} >> stores a two-dimensional array with board contents. |
97 | C<< $board->{board} >> stores a two-dimensional array with board contents. |
100 | |
98 | |
101 | =cut |
99 | =cut |
102 | |
100 | |
103 | sub new { |
101 | sub new { |
104 | my $class = shift; |
102 | my $class = shift; |
105 | my $size = shift; |
103 | my $size = shift; |
|
|
104 | |
|
|
105 | unless ($size > 0) { |
|
|
106 | Carp::croak ("no board size given!"); |
|
|
107 | } |
|
|
108 | |
106 | bless { |
109 | bless { |
107 | max => $size - 1, |
110 | max => $size - 1, |
108 | size => $size, |
111 | size => $size, |
109 | board => [map [(0) x $size], 1 .. $size], |
112 | board => [map [(0) x $size], 1 .. $size], |
110 | captures => [0, 0], # captures |
113 | captures => [0, 0], # captures |
111 | #timer => [], |
114 | #timer => [], |
112 | #score => [], |
115 | #score => [], |
113 | #last => COLOUR_..., |
|
|
114 | @_ |
|
|
115 | }, |
116 | @_, |
116 | $class; |
117 | }, $class |
117 | } |
118 | } |
118 | |
119 | |
119 | # inefficient and primitive, I hear you say? |
120 | # inefficient and primitive, I hear you say? |
120 | # well... you are right :) |
121 | # well... you are right :) |
121 | # use an extremely dumb floodfill algorithm to get rid of captured stones |
122 | # use an extremely dumb floodfill algorithm to get rid of captured stones |
… | |
… | |
129 | |
130 | |
130 | my $max = $self->{max}; |
131 | my $max = $self->{max}; |
131 | |
132 | |
132 | while (@nodes) { |
133 | while (@nodes) { |
133 | my ($x, $y) = @{pop @nodes}; |
134 | my ($x, $y) = @{pop @nodes}; |
|
|
135 | |
134 | unless ($seen{$x,$y}++) { |
136 | unless ($seen{$x,$y}++) { |
135 | if ($board->[$x][$y] & $mark) { |
137 | if ($board->[$x][$y] & $mark) { |
136 | push @found, [$x, $y]; |
138 | push @found, [$x, $y]; |
137 | |
139 | |
138 | push @nodes, [$x-1, $y] unless $seen{$x-1, $y} || $x <= 0; |
140 | push @nodes, [$x-1, $y] unless $seen{$x-1, $y} || $x <= 0; |
… | |
… | |
161 | specified in C<$clr>, then setting bits specified in C<$set>. |
163 | specified in C<$clr>, then setting bits specified in C<$set>. |
162 | |
164 | |
163 | If C<$set> includes C<MARK_LABEL>, the label text must be given in |
165 | If C<$set> includes C<MARK_LABEL>, the label text must be given in |
164 | C<$label>. |
166 | C<$label>. |
165 | |
167 | |
166 | If C<$set> contains C<MARK_MOVE>, then a circle symbol will be placed |
168 | If C<$set> contains C<MARK_MOVE> then surrounded stones will be removed |
167 | at this coordinate. Also, surrounded stones will be removed from the |
|
|
168 | board and (simple) Kos are detected and marked with square symbols and |
169 | from the board and (simple) Kos are detected and marked with square |
169 | C<MARK_KO>. The circle and square markings are removed with the next |
170 | symbols and C<MARK_KO>, after removing other marking symbols. The |
|
|
171 | markings are also removed with the next next update structure that uses |
170 | update that uses C<MARK_MOVE>, so this flag is suited well for marking, |
172 | C<MARK_MOVE>, so this flag is suited well for marking, well, moves. Note |
171 | well, moves. Note that you can make invalid "moves" (such as suicide) and |
173 | that you can make invalid "moves" (such as suicide) and C<update> will |
172 | C<update> will try to cope with it. You can use C<is_valid_move> to avoid |
174 | try to cope with it. You can use C<is_valid_move> to avoid making illegal |
173 | making illegal moves. |
175 | moves. |
174 | |
176 | |
175 | For handicap "moves", currently only board sizes 9, 13 and 19 are |
177 | For handicap "moves", currently only board sizes 9, 13 and 19 are |
176 | supported and only handicap values from 2 to 9. The placement follows the |
178 | supported and only handicap values from 2 to 9. The placement follows the |
177 | IGS rules, if you want other placements, you have to set it up yourself. |
179 | IGS rules, if you want other placements, you have to set it up yourself. |
178 | |
180 | |
… | |
… | |
211 | 7 => [qw(0,2 2,0 0,0 2,2 0,1 2,1 1,1)], |
213 | 7 => [qw(0,2 2,0 0,0 2,2 0,1 2,1 1,1)], |
212 | 8 => [qw(0,2 2,0 0,0 2,2 0,1 2,1 1,0 1,2 )], |
214 | 8 => [qw(0,2 2,0 0,0 2,2 0,1 2,1 1,0 1,2 )], |
213 | 9 => [qw(0,2 2,0 0,0 2,2 0,1 2,1 1,0 1,2 1,1)], |
215 | 9 => [qw(0,2 2,0 0,0 2,2 0,1 2,1 1,0 1,2 1,1)], |
214 | ); |
216 | ); |
215 | |
217 | |
|
|
218 | our $mark_symbols = MARK_CIRCLE | MARK_SQUARE | MARK_TRIANGLE | MARK_CROSS | MARK_KO; |
|
|
219 | |
216 | sub update { |
220 | sub update { |
217 | my ($self, $path) = @_; |
221 | my ($self, $path) = @_; |
218 | |
222 | |
219 | my $board = $self->{board}; |
223 | my $board = $self->{board}; |
220 | |
224 | |
221 | for (@$path) { |
225 | for (@$path) { |
222 | my ($x, $y, $clr, $set, $label) = @$_; |
226 | my ($x, $y, $clr, $set, $label) = @$_; |
223 | |
227 | |
224 | my $nodemask = |
|
|
225 | $_ == $path->[-1] |
|
|
226 | ? ~0 |
|
|
227 | : ~(MARK_SQUARE | MARK_TRIANGLE | MARK_CIRCLE | MARK_LABEL | MARK_KO); |
|
|
228 | |
|
|
229 | if (!defined $x) { |
228 | if (!defined $x) { |
|
|
229 | $$_ &= ~$mark_symbols for @{ delete $self->{unmark} || [] }; |
230 | # pass |
230 | # pass |
231 | $self->{last} = $set & MARK_B ? COLOUR_BLACK : COLOUR_WHITE; |
|
|
232 | |
231 | |
233 | } elsif ($x == MOVE_HANDICAP) { |
232 | } elsif ($x == MOVE_HANDICAP) { |
|
|
233 | $$_ &= ~$mark_symbols for @{ delete $self->{unmark} || [] }; |
|
|
234 | |
234 | # $y = #handicap stones |
235 | # $y = #handicap stones |
235 | my $c = $HANDICAP_COORD{$self->{size}} |
236 | my $c = $HANDICAP_COORD{$self->{size}} |
236 | or Carp::croak "$self->{size}: illegal board size for handicap"; |
237 | or Carp::croak "$self->{size}: illegal board size for handicap"; |
237 | my $h = $HANDICAP_XY{$y} |
238 | my $h = $HANDICAP_XY{$y} |
238 | or Carp::croak "$y: illegal number of handicap stones"; |
239 | or Carp::croak "$y: illegal number of handicap stones"; |
… | |
… | |
241 | my ($x, $y) = map $c->[$_], split /,/; |
242 | my ($x, $y) = map $c->[$_], split /,/; |
242 | $board->[$x][$y] = MARK_B | MARK_MOVE; |
243 | $board->[$x][$y] = MARK_B | MARK_MOVE; |
243 | } |
244 | } |
244 | |
245 | |
245 | } else { |
246 | } else { |
246 | $board->[$x][$y] = |
247 | my $space = \$board->[$x][$y]; |
247 | $board->[$x][$y] |
248 | |
248 | & ~$clr |
249 | $$space = $$space & ~$clr | $set; |
249 | | $set |
|
|
250 | & $nodemask; |
|
|
251 | |
250 | |
252 | $self->{label}[$x][$y] = $label if $set & MARK_LABEL; |
251 | $self->{label}[$x][$y] = $label if $set & MARK_LABEL; |
253 | |
252 | |
254 | if ($set & MARK_MOVE) { |
253 | if ($set & MARK_MOVE) { |
255 | $self->{last} = $set & MARK_B ? COLOUR_BLACK : COLOUR_WHITE; |
254 | $$_ &= ~$mark_symbols for @{ $self->{unmark} || [] }; |
|
|
255 | @{ $self->{unmark} } = $space; |
|
|
256 | |
|
|
257 | # remark the space, in case the move was on the same spot as the |
|
|
258 | # old mark |
|
|
259 | $$space |= $set; |
256 | |
260 | |
257 | unless (${ $_->[5] ||= \my $hint }) { |
261 | unless (${ $_->[5] ||= \my $hint }) { |
258 | my ($own, $opp) = |
262 | my ($own, $opp) = |
259 | $set & MARK_B |
263 | $set & MARK_B |
260 | ? (MARK_B, MARK_W) |
264 | ? (MARK_B, MARK_W) |
261 | : (MARK_W, MARK_B); |
265 | : (MARK_W, MARK_B); |
262 | |
266 | |
263 | my (@capture, $suicide); |
267 | my (@capture, @suicide); |
264 | |
268 | |
265 | push @capture, $self->capture ($opp, $x-1, $y) if $x > 0 && $board->[$x-1][$y] & $opp; |
269 | push @capture, $self->capture ($opp, $x-1, $y) if $x > 0 && $board->[$x-1][$y] & $opp; |
266 | push @capture, $self->capture ($opp, $x+1, $y) if $x < $self->{max} && $board->[$x+1][$y] & $opp; |
270 | push @capture, $self->capture ($opp, $x+1, $y) if $x < $self->{max} && $board->[$x+1][$y] & $opp; |
267 | push @capture, $self->capture ($opp, $x, $y-1) if $y > 0 && $board->[$x][$y-1] & $opp; |
271 | push @capture, $self->capture ($opp, $x, $y-1) if $y > 0 && $board->[$x][$y-1] & $opp; |
268 | push @capture, $self->capture ($opp, $x, $y+1) if $y < $self->{max} && $board->[$x][$y+1] & $opp; |
272 | push @capture, $self->capture ($opp, $x, $y+1) if $y < $self->{max} && $board->[$x][$y+1] & $opp; |
269 | |
273 | |
270 | # keep only unique coordinates |
274 | # keep only unique coordinates |
271 | @capture = do { my %seen; grep !$seen{"$_->[0],$_->[1]"}++, @capture }; |
275 | @capture = do { my %seen; grep !$seen{"$_->[0],$_->[1]"}++, @capture }; |
272 | |
276 | |
273 | # remove captured stones |
277 | # remove captured stones |
274 | $self->{captures}[$self->{last}] += @capture; |
278 | $self->{captures}[$own == MARK_B ? COLOUR_BLACK : COLOUR_WHITE] += @capture; |
275 | $self->{board}[$_->[0]][$_->[1]] &= ~(MARK_B | MARK_W | MARK_MOVE) |
279 | $self->{board}[$_->[0]][$_->[1]] = 0 |
276 | for @capture; |
280 | for @capture; |
277 | |
281 | |
278 | $suicide += $self->capture ($own, $x, $y); |
282 | push @suicide, $self->capture ($own, $x, $y); |
279 | |
283 | |
280 | ${ $_->[5] } ||= !(@capture || $suicide); |
284 | ${ $_->[5] } ||= !(@capture || @suicide); |
281 | |
285 | |
|
|
286 | if (@suicide) { |
|
|
287 | $self->{board}[$_->[0]][$_->[1]] = 0 |
|
|
288 | for @suicide; |
|
|
289 | # count suicides as other sides stones |
|
|
290 | $self->{captures}[$opp == MARK_B ? COLOUR_BLACK : COLOUR_WHITE] += @suicide; |
|
|
291 | |
282 | if (!$suicide && @capture == 1) { |
292 | } elsif (!@suicide && @capture == 1) { |
283 | # possible ko. now check liberties on placed stone |
293 | # possible ko. now check liberties on placed stone |
284 | |
294 | |
285 | my $libs; |
295 | my $libs; |
286 | |
296 | |
287 | $libs++ if $x > 0 && !($board->[$x-1][$y] & $opp); |
297 | $libs++ if $x > 0 && !($board->[$x-1][$y] & $opp); |
288 | $libs++ if $x < $self->{max} && !($board->[$x+1][$y] & $opp); |
298 | $libs++ if $x < $self->{max} && !($board->[$x+1][$y] & $opp); |
289 | $libs++ if $y > 0 && !($board->[$x][$y-1] & $opp); |
299 | $libs++ if $y > 0 && !($board->[$x][$y-1] & $opp); |
290 | $libs++ if $y < $self->{max} && !($board->[$x][$y+1] & $opp); |
300 | $libs++ if $y < $self->{max} && !($board->[$x][$y+1] & $opp); |
291 | |
301 | |
292 | if ($libs == 1) { |
302 | if ($libs == 1) { |
293 | $board->[$x][$y] = $board->[$x][$y] & ~MARK_CIRCLE | (MARK_KO & $nodemask); |
303 | $$space = $$space & ~$mark_symbols | MARK_KO; |
|
|
304 | |
294 | ($x, $y) = @{$capture[0]}; |
305 | ($x, $y) = @{$capture[0]}; |
295 | $board->[$x][$y] |= MARK_KO & $nodemask; |
306 | $board->[$x][$y] |= MARK_KO; |
|
|
307 | |
|
|
308 | push @{ $self->{unmark} }, \$board->[$x][$y]; |
296 | } |
309 | } |
297 | } |
310 | } |
298 | } |
311 | } |
299 | } |
312 | } |
300 | } |
313 | } |
… | |
… | |
313 | sub is_valid_move { |
326 | sub is_valid_move { |
314 | my ($self, $colour, $x, $y, $may_suicide) = @_; |
327 | my ($self, $colour, $x, $y, $may_suicide) = @_; |
315 | |
328 | |
316 | my $board = $self->{board}; |
329 | my $board = $self->{board}; |
317 | |
330 | |
318 | return if $board->[$x][$y] & (MARK_B | MARK_W | MARK_KO); |
331 | return if $board->[$x][$y] & (MARK_B | MARK_W | MARK_KO) |
|
|
332 | && !($board->[$x][$y] & MARK_GRAYED); |
319 | |
333 | |
320 | if ($may_suicide) { |
334 | if ($may_suicide) { |
321 | return 1; |
335 | return 1; |
322 | } else { |
336 | } else { |
323 | my ($own, $opp) = $colour == COLOUR_BLACK |
337 | my ($own, $opp) = $colour == COLOUR_BLACK |