… | |
… | |
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_..., |
… | |
… | |
134 | =item $hint = $board->update ([update-structures...]) |
149 | =item $hint = $board->update ([update-structures...]) |
135 | |
150 | |
136 | Each update-structure itself is also an array-ref: |
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, set handicap |
|
|
155 | [MOVE_PASS] # pass |
139 | [] # pass |
156 | [] # also pass |
140 | |
157 | |
141 | It 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>. |
|
|
163 | |
|
|
164 | If C<$set> contains C<MARK_MOVE>, then a circle symbol will be placed on |
|
|
165 | this coordinate only if this is the last move done (which is useful for a |
|
|
166 | move marker). |
|
|
167 | |
|
|
168 | For handicap "moves", currently only board sizes 9, 13 and 19 are |
|
|
169 | supported and only handicap values from 2 to 9. The placement follows the |
|
|
170 | IGS rules, if you want other placements, you have to set it up yourself. |
146 | |
171 | |
147 | This function modifies the hint member of the specified path to speed up |
172 | This function modifies the hint member of the specified path to speed up |
148 | repeated board generation and updates with the same update structures. |
173 | repeated board generation and updates with the same update structures. |
149 | |
174 | |
150 | If the hint member is a reference the scalar pointed to by the reference |
175 | If the hint member is a reference the scalar pointed to by the reference |
… | |
… | |
157 | it from fresh update structures. |
182 | it from fresh update structures. |
158 | |
183 | |
159 | Example, make two silly moves: |
184 | Example, make two silly moves: |
160 | |
185 | |
161 | $board->update ([[0, 18, -1, MARK_B|MARK_MOVE], |
186 | $board->update ([[0, 18, -1, MARK_B|MARK_MOVE], |
162 | [0, 17, -1, MARK_W|MARK_MOVE]); |
187 | [0, 17, -1, MARK_W|MARK_MOVE]]); |
163 | |
188 | |
164 | =cut |
189 | =cut |
|
|
190 | |
|
|
191 | our %HANDICAP_COORD = ( |
|
|
192 | 9 => [2, 4, 6], |
|
|
193 | 13 => [3, 6, 9], |
|
|
194 | 19 => [3, 9, 15], |
|
|
195 | ); |
|
|
196 | our %HANDICAP_XY = ( |
|
|
197 | 2 => [qw(0,2 2,0 )], |
|
|
198 | 3 => [qw(0,2 2,0 2,2 )], |
|
|
199 | 4 => [qw(0,2 2,0 2,2 0,0 )], |
|
|
200 | 5 => [qw(0,2 2,0 2,2 0,0 1,1)], |
|
|
201 | 6 => [qw(0,2 2,0 2,2 0,0 0,1 2,1 )], |
|
|
202 | 7 => [qw(0,2 2,0 2,2 0,0 0,1 2,1 1,1)], |
|
|
203 | 8 => [qw(0,2 2,0 2,2 0,0 0,1 2,1 1,0 1,2 )], |
|
|
204 | 9 => [qw(0,2 2,0 2,2 0,0 0,1 2,1 1,0 1,2 1,1)], |
|
|
205 | ); |
165 | |
206 | |
166 | sub update { |
207 | sub update { |
167 | my ($self, $path) = @_; |
208 | my ($self, $path) = @_; |
168 | |
209 | |
169 | my $board = $self->{board}; |
210 | my $board = $self->{board}; |
… | |
… | |
174 | my $nodemask = |
215 | my $nodemask = |
175 | $_ == $path->[-1] |
216 | $_ == $path->[-1] |
176 | ? ~0 |
217 | ? ~0 |
177 | : ~(MARK_SQUARE | MARK_TRIANGLE | MARK_CIRCLE | MARK_LABEL | MARK_KO); |
218 | : ~(MARK_SQUARE | MARK_TRIANGLE | MARK_CIRCLE | MARK_LABEL | MARK_KO); |
178 | |
219 | |
179 | if (defined $x) { |
220 | if (!defined $x) { |
|
|
221 | # pass |
|
|
222 | $self->{last} = $set & MARK_B ? COLOUR_BLACK : COLOUR_WHITE; |
|
|
223 | |
|
|
224 | } elsif ($x == MOVE_HANDICAP) { |
|
|
225 | # $y = #handicap stones |
|
|
226 | my $c = $HANDICAP_COORD{$self->{size}} |
|
|
227 | or Carp::croak "$self->{size}: illegal board size for handicap"; |
|
|
228 | my $h = $HANDICAP_XY{$y} |
|
|
229 | or Carp::croak "$y: illegal number of handicap stones"; |
|
|
230 | |
|
|
231 | for (@$h) { |
|
|
232 | my ($x, $y) = map $c->[$_], split /,/; |
|
|
233 | $board->[$x][$y] = MARK_B | MARK_MOVE; |
|
|
234 | } |
|
|
235 | |
|
|
236 | } else { |
180 | $board->[$x][$y] = |
237 | $board->[$x][$y] = |
181 | $board->[$x][$y] |
238 | $board->[$x][$y] |
182 | & ~$clr |
239 | & ~$clr |
183 | | $set |
240 | | $set |
184 | & $nodemask; |
241 | & $nodemask; |
… | |
… | |
229 | $board->[$x][$y] |= MARK_KO & $nodemask; |
286 | $board->[$x][$y] |= MARK_KO & $nodemask; |
230 | } |
287 | } |
231 | } |
288 | } |
232 | } |
289 | } |
233 | } |
290 | } |
234 | } else { |
|
|
235 | $self->{last} = $set & MARK_B ? COLOUR_BLACK : COLOUR_WHITE; |
|
|
236 | } |
291 | } |
237 | } |
292 | } |
238 | } |
293 | } |
239 | |
294 | |
240 | =item $board->is_valid_move ($colour, $x, $y[, $may_suicide]) |
295 | =item $board->is_valid_move ($colour, $x, $y[, $may_suicide]) |
… | |
… | |
278 | |
333 | |
279 | Marc Lehmann <schmorp@schmorp.de> |
334 | Marc Lehmann <schmorp@schmorp.de> |
280 | |
335 | |
281 | =head2 SEE ALSO |
336 | =head2 SEE ALSO |
282 | |
337 | |
283 | L<KGS::Protocol>, L<KGS::Game::Tree>, L<Gtk2::GoBoard>. |
338 | L<Gtk2::GoBoard>. |
284 | |
339 | |
285 | =cut |
340 | =cut |
286 | |
341 | |