… | |
… | |
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 |
… | |
… | |
31 | MARK_REDRAW # ignored, can be used for your own purposes |
32 | MARK_REDRAW # ignored, can be used for your own purposes |
32 | |
33 | |
33 | COLOUR_BLACK # used for $board->{last} |
34 | COLOUR_BLACK # used for $board->{last} |
34 | COLOUR_WHITE # to mark the colour of the last move |
35 | COLOUR_WHITE # to mark the colour of the last move |
35 | |
36 | |
|
|
37 | MOVE_HANDICAP # used as "x-coordinate" for handicap moves |
|
|
38 | MOVE_PASS # can be used as "x-coordinate" for handicap moves |
|
|
39 | |
36 | =head2 METHODS |
40 | =head2 METHODS |
37 | |
41 | |
38 | =over 4 |
42 | =over 4 |
39 | |
43 | |
40 | =cut |
44 | =cut |
41 | |
45 | |
|
|
46 | no warnings; |
|
|
47 | use strict; |
|
|
48 | |
|
|
49 | use Carp (); |
|
|
50 | |
42 | use base Exporter; |
51 | use base Exporter::; |
43 | |
52 | |
44 | our $VERSION = '1.0'; |
53 | our $VERSION = '1.0'; |
45 | |
54 | |
46 | @EXPORT = qw( |
55 | our @EXPORT = qw( |
47 | 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 |
48 | 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 |
49 | MARK_REDRAW |
58 | MARK_REDRAW |
50 | COLOUR_BLACK COLOUR_WHITE |
59 | COLOUR_BLACK COLOUR_WHITE |
|
|
60 | MOVE_HANDICAP MOVE_PASS |
51 | ); |
61 | ); |
52 | |
62 | |
53 | # marker types for each board position (ORed together) |
63 | # marker types for each board position (ORed together) |
54 | |
64 | |
55 | sub MARK_TRIANGLE (){ 0x0001 } |
65 | sub MARK_TRIANGLE (){ 0x0001 } |
… | |
… | |
62 | 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 |
63 | sub MARK_LABEL (){ 0x0100 } |
73 | sub MARK_LABEL (){ 0x0100 } |
64 | 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) |
65 | sub MARK_MOVE (){ 0x0400 } # this is a regular move |
75 | sub MARK_MOVE (){ 0x0400 } # this is a regular move |
66 | sub MARK_KO (){ 0x0800 } # this is a ko position |
76 | sub MARK_KO (){ 0x0800 } # this is a ko position |
|
|
77 | sub MARK_CIRCLE (){ 0x1000 } |
67 | sub MARK_REDRAW (){ 0x8000 } |
78 | sub MARK_REDRAW (){ 0x8000 } |
68 | |
79 | |
69 | sub COLOUR_BLACK (){ 0 } |
80 | sub COLOUR_BLACK (){ 0 } |
70 | sub COLOUR_WHITE (){ 1 } |
81 | sub COLOUR_WHITE (){ 1 } |
71 | |
82 | |
|
|
83 | sub MOVE_PASS (){ undef } |
|
|
84 | sub MOVE_HANDICAP (){ -2 } |
|
|
85 | |
72 | =item my $board = new $size |
86 | =item my $board = new $size |
73 | |
87 | |
74 | Creates a new empty board of the given size. |
88 | Creates a new empty board of the given size. |
|
|
89 | |
|
|
90 | C<< $board->{size} >> stores the board size. |
75 | |
91 | |
76 | C<< $board->{max} >> stores the maximum board coordinate (size-1). |
92 | C<< $board->{max} >> stores the maximum board coordinate (size-1). |
77 | |
93 | |
78 | C<< $board->{captures}[COLOUR] >> stores the number of captured stones for |
94 | C<< $board->{captures}[COLOUR] >> stores the number of captured stones for |
79 | the given colour. |
95 | the given colour. |
… | |
… | |
87 | sub new { |
103 | sub new { |
88 | my $class = shift; |
104 | my $class = shift; |
89 | my $size = shift; |
105 | my $size = shift; |
90 | bless { |
106 | bless { |
91 | max => $size - 1, |
107 | max => $size - 1, |
|
|
108 | size => $size, |
92 | board => [map [(0) x $size], 1 .. $size], |
109 | board => [map [(0) x $size], 1 .. $size], |
93 | captures => [0, 0], # captures |
110 | captures => [0, 0], # captures |
94 | #timer => [], |
111 | #timer => [], |
95 | #score => [], |
112 | #score => [], |
96 | #last => COLOUR_..., |
113 | #last => COLOUR_..., |
… | |
… | |
134 | =item $hint = $board->update ([update-structures...]) |
151 | =item $hint = $board->update ([update-structures...]) |
135 | |
152 | |
136 | Each update-structure itself is also an array-ref: |
153 | Each update-structure itself is also an array-ref: |
137 | |
154 | |
138 | [$x, $y, $clr, $set, $label, $hint] # update or move |
155 | [$x, $y, $clr, $set, $label, $hint] # update or move |
|
|
156 | [MOVE_HANDICAP, $handicap] # black move, setup handicap |
|
|
157 | [MOVE_PASS] # pass |
139 | [] # pass |
158 | [] # also pass (deprecated!) |
140 | |
159 | |
141 | It changes the board or executes a move, by first clearing the bits |
160 | It changes the board or executes a move, by first clearing the bits |
142 | specified in C<$clr>, then setting bits specified in C<$set>. |
161 | specified in C<$clr>, then setting bits specified in C<$set>. |
143 | |
162 | |
144 | If C<$set> includes C<MARK_LABEL>, the label text must be given in |
163 | If C<$set> includes C<MARK_LABEL>, the label text must be given in |
145 | C<$label>. |
164 | C<$label>. |
146 | |
165 | |
|
|
166 | If C<$set> contains C<MARK_MOVE>, then a circle symbol will be placed |
|
|
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 | C<MARK_KO>. The circle and square markings are removed with the next |
|
|
170 | update that uses C<MARK_MOVE>, so this flag is suited well for marking, |
|
|
171 | well, moves. Note that you can make invalid "moves" (such as suicide) and |
|
|
172 | C<update> will try to cope with it. You can use C<is_valid_move> to avoid |
|
|
173 | making illegal moves. |
|
|
174 | |
|
|
175 | 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 |
|
|
177 | IGS rules, if you want other placements, you have to set it up yourself. |
|
|
178 | |
147 | This function modifies the hint member of the specified path to speed up |
179 | This function modifies the C<$hint> member of the specified structure |
148 | repeated board generation and updates with the same update structures. |
180 | to speed up repeated board generation and updates with the same update |
|
|
181 | structures. |
149 | |
182 | |
150 | If the hint member is a reference the scalar pointed to by the reference |
183 | If the hint member is a reference the scalar pointed to by the reference |
151 | is updated instead. |
184 | is updated instead. |
152 | |
185 | |
153 | If all this hint member thing is unclear, just ignore it and specify it |
186 | If all this hint member thing is confusing, just ignore it and specify |
154 | as C<undef> or leave it out of the array entirely. Do make sure that you |
187 | it as C<undef> or leave it out of the array entirely. Do make sure that |
155 | keep your update structures around, however, as regenerating a full board |
188 | you keep your update structures around as long as previous updates don't |
156 | position from hinted update structures is I<much> faster then recreating |
189 | change, however, as regenerating a full board position from hinted |
157 | it from fresh update structures. |
190 | update structures is I<much> faster then recreating it from fresh update |
|
|
191 | structures. |
158 | |
192 | |
159 | Example, make two silly moves: |
193 | Example, make two silly moves: |
160 | |
194 | |
161 | $board->update ([[0, 18, -1, MARK_B|MARK_MOVE], |
195 | $board->update ([[0, 18, -1, MARK_B | MARK_MOVE], |
162 | [0, 17, -1, MARK_W|MARK_MOVE]); |
196 | [0, 17, -1, MARK_W | MARK_MOVE]]); |
163 | |
197 | |
164 | =cut |
198 | =cut |
|
|
199 | |
|
|
200 | our %HANDICAP_COORD = ( |
|
|
201 | 9 => [2, 4, 6], |
|
|
202 | 13 => [3, 6, 9], |
|
|
203 | 19 => [3, 9, 15], |
|
|
204 | ); |
|
|
205 | our %HANDICAP_XY = ( |
|
|
206 | 2 => [qw(0,2 2,0 )], |
|
|
207 | 3 => [qw(0,2 2,0 0,0 )], |
|
|
208 | 4 => [qw(0,2 2,0 0,0 2,2 )], |
|
|
209 | 5 => [qw(0,2 2,0 0,0 2,2 1,1)], |
|
|
210 | 6 => [qw(0,2 2,0 0,0 2,2 0,1 2,1 )], |
|
|
211 | 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 )], |
|
|
213 | 9 => [qw(0,2 2,0 0,0 2,2 0,1 2,1 1,0 1,2 1,1)], |
|
|
214 | ); |
165 | |
215 | |
166 | sub update { |
216 | sub update { |
167 | my ($self, $path) = @_; |
217 | my ($self, $path) = @_; |
168 | |
218 | |
169 | my $board = $self->{board}; |
219 | my $board = $self->{board}; |
… | |
… | |
174 | my $nodemask = |
224 | my $nodemask = |
175 | $_ == $path->[-1] |
225 | $_ == $path->[-1] |
176 | ? ~0 |
226 | ? ~0 |
177 | : ~(MARK_SQUARE | MARK_TRIANGLE | MARK_CIRCLE | MARK_LABEL | MARK_KO); |
227 | : ~(MARK_SQUARE | MARK_TRIANGLE | MARK_CIRCLE | MARK_LABEL | MARK_KO); |
178 | |
228 | |
179 | if (defined $x) { |
229 | if (!defined $x) { |
|
|
230 | # pass |
|
|
231 | $self->{last} = $set & MARK_B ? COLOUR_BLACK : COLOUR_WHITE; |
|
|
232 | |
|
|
233 | } elsif ($x == MOVE_HANDICAP) { |
|
|
234 | # $y = #handicap stones |
|
|
235 | my $c = $HANDICAP_COORD{$self->{size}} |
|
|
236 | or Carp::croak "$self->{size}: illegal board size for handicap"; |
|
|
237 | my $h = $HANDICAP_XY{$y} |
|
|
238 | or Carp::croak "$y: illegal number of handicap stones"; |
|
|
239 | |
|
|
240 | for (@$h) { |
|
|
241 | my ($x, $y) = map $c->[$_], split /,/; |
|
|
242 | $board->[$x][$y] = MARK_B | MARK_MOVE; |
|
|
243 | } |
|
|
244 | |
|
|
245 | } else { |
180 | $board->[$x][$y] = |
246 | $board->[$x][$y] = |
181 | $board->[$x][$y] |
247 | $board->[$x][$y] |
182 | & ~$clr |
248 | & ~$clr |
183 | | $set |
249 | | $set |
184 | & $nodemask; |
250 | & $nodemask; |
… | |
… | |
229 | $board->[$x][$y] |= MARK_KO & $nodemask; |
295 | $board->[$x][$y] |= MARK_KO & $nodemask; |
230 | } |
296 | } |
231 | } |
297 | } |
232 | } |
298 | } |
233 | } |
299 | } |
234 | } else { |
|
|
235 | $self->{last} = $set & MARK_B ? COLOUR_BLACK : COLOUR_WHITE; |
|
|
236 | } |
300 | } |
237 | } |
301 | } |
238 | } |
302 | } |
239 | |
303 | |
240 | =item $board->is_valid_move ($colour, $x, $y[, $may_suicide]) |
304 | =item $board->is_valid_move ($colour, $x, $y[, $may_suicide]) |
241 | |
305 | |
242 | Returns true if the move of the given colour on the given coordinates is |
306 | Returns true if the move of the given colour on the given coordinates is |
243 | valid or not. |
307 | valid or not. Kos are taken into account as long as they are marked with |
|
|
308 | C<MARK_KO>. Suicides are invalid unless C<$may_suicide> is true (e.g. for |
|
|
309 | new zealand rules) |
244 | |
310 | |
245 | =cut |
311 | =cut |
246 | |
312 | |
247 | sub is_valid_move { |
313 | sub is_valid_move { |
248 | my ($self, $colour, $x, $y, $may_suicide) = @_; |
314 | my ($self, $colour, $x, $y, $may_suicide) = @_; |
… | |
… | |
278 | |
344 | |
279 | Marc Lehmann <schmorp@schmorp.de> |
345 | Marc Lehmann <schmorp@schmorp.de> |
280 | |
346 | |
281 | =head2 SEE ALSO |
347 | =head2 SEE ALSO |
282 | |
348 | |
283 | L<KGS::Protocol>, L<KGS::Game::Tree>, L<Gtk2::GoBoard>. |
349 | L<Gtk2::GoBoard>. |
284 | |
350 | |
285 | =cut |
351 | =cut |
286 | |
352 | |