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