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