ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Games-Go-SimpleBoard/SimpleBoard.pm
(Generate patch)

Comparing Games-Go-SimpleBoard/SimpleBoard.pm (file contents):
Revision 1.13 by elmex, Thu Jul 24 08:50:53 2008 UTC vs.
Revision 1.18 by root, Tue Jul 29 10:09:53 2008 UTC

14 14
15=head2 EXPORTED CONSTANTS 15=head2 EXPORTED CONSTANTS
16 16
17Marker types for each board position (ORed together): 17Marker types for each board position (ORed together):
18 18
19 MARK_TRIANGLE # triangle mark
20 MARK_SQUARE # square mark
21 MARK_CIRCLE # circle mark
22 MARK_CROSS # cross mark
23 MARK_SMALL_B # small stone, used for scoring or marking
24 MARK_SMALL_W # small stone, used for scoring or marking
25 MARK_B # normal black stone 19 MARK_B # normal black stone
26 MARK_W # normal whit stone 20 MARK_W # normal whit stone
27 MARK_GRAYED # in conjunction with MARK_[BW], grays the stone 21 MARK_GRAYED # in conjunction with MARK_[BW], grays the stone
22
23 MARK_SMALL_B # small stone, used for scoring or marking
24 MARK_SMALL_W # small stone, used for scoring or marking
25 MARK_SMALL_GRAYED # in conjunction with MARK_SMALL_[BW], grays the stone
26
27 MARK_TRIANGLE # triangle mark
28 MARK_SQUARE # square mark
29 MARK_CIRCLE # circle mark
30 MARK_CROSS # cross mark
31
28 MARK_LABEL # a text label 32 MARK_LABEL # a text label
29 MARK_HOSHI # this is a hoshi point (not used much) 33 MARK_HOSHI # this is a hoshi point (not used much)
30 MARK_MOVE # this is a regular move 34 MARK_MOVE # this is a regular move
31 MARK_KO # this is a ko position 35 MARK_KO # this is a ko position
32 MARK_REDRAW # ignored, can be used for your own purposes 36 MARK_REDRAW # ignored, can be used for your own purposes
33 37
34 COLOUR_WHITE # guarenteed to be 0 38 COLOUR_WHITE # guaranteed to be 0
35 COLOUR_BLACK # guarenteed to be 1 39 COLOUR_BLACK # guaranteed to be 1
36 40
37 MOVE_HANDICAP # used as "x-coordinate" for handicap moves 41 MOVE_HANDICAP # used as "x-coordinate" for handicap moves
38 MOVE_PASS # can be used as "x-coordinate" for pass moves 42 MOVE_PASS # can be used as "x-coordinate" for pass moves
39 43
40=head2 METHODS 44=head2 METHODS
41 45
42=over 4 46=over 4
43 47
48 52
49use Carp (); 53use Carp ();
50 54
51use base Exporter::; 55use base Exporter::;
52 56
53our $VERSION = '1.0'; 57our $VERSION = '1.01';
54 58
55our @EXPORT = qw( 59our @EXPORT = qw(
56 MARK_TRIANGLE MARK_SQUARE MARK_CIRCLE MARK_SMALL_B MARK_SMALL_W MARK_B 60 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 MARK_CROSS 61 MARK_W MARK_GRAYED MARK_SMALL_GRAYED MARK_MOVE MARK_LABEL MARK_HOSHI MARK_KO MARK_CROSS
58 MARK_REDRAW 62 MARK_REDRAW
59 COLOUR_BLACK COLOUR_WHITE 63 COLOUR_BLACK COLOUR_WHITE
60 MOVE_HANDICAP MOVE_PASS 64 MOVE_HANDICAP MOVE_PASS
61); 65);
62 66
63# marker types for each board position (ORed together) 67# marker types for each board position (ORed together)
64 68
65sub MARK_TRIANGLE (){ 0x0001 } 69sub MARK_TRIANGLE (){ 0x0001 }
66sub MARK_SQUARE (){ 0x0002 } 70sub MARK_SQUARE (){ 0x0002 }
67sub MARK_CIRCLE (){ 0x0004 } 71sub MARK_CIRCLE (){ 0x0004 }
72sub MARK_CROSS (){ 0x0008 }
73
68sub MARK_SMALL_B (){ 0x0008 } # small stone, used for scoring or marking 74sub MARK_SMALL_B (){ 0x0010 } # small stone, used for scoring or marking
69sub MARK_SMALL_W (){ 0x0010 } # small stone, used for scoring or marking 75sub MARK_SMALL_W (){ 0x0020 } # small stone, used for scoring or marking
76sub MARK_SMALL_GRAYED (){ 0x0040 }
77
70sub MARK_B (){ 0x0020 } # normal black stone 78sub MARK_B (){ 0x0080 } # normal black stone
71sub MARK_W (){ 0x0040 } # normal whit stone 79sub MARK_W (){ 0x0100 } # normal whit stone
72sub MARK_GRAYED (){ 0x0080 } # in conjunction with MARK_[BW], grays the stone 80sub MARK_GRAYED (){ 0x0200 } # in conjunction with MARK_[BW], grays the stone
81
73sub MARK_LABEL (){ 0x0100 } 82sub MARK_LABEL (){ 0x0400 }
74sub MARK_HOSHI (){ 0x0200 } # this is a hoshi point (not used much) 83sub MARK_HOSHI (){ 0x0800 } # this is a hoshi point (not used much)
75sub MARK_MOVE (){ 0x0400 } # this is a regular move 84sub MARK_MOVE (){ 0x1000 } # this is a regular move
76sub MARK_KO (){ 0x0800 } # this is a ko position 85sub MARK_KO (){ 0x2000 } # this is a ko position
77sub MARK_CROSS (){ 0x1000 }
78sub MARK_REDRAW (){ 0x8000 } 86sub MARK_REDRAW (){ 0x8000 }
79 87
80sub COLOUR_WHITE (){ 0 } 88sub COLOUR_WHITE (){ 0 }
81sub COLOUR_BLACK (){ 1 } 89sub COLOUR_BLACK (){ 1 }
82 90
83sub MOVE_PASS (){ undef } 91sub MOVE_PASS (){ undef }
84sub MOVE_HANDICAP (){ -2 } 92sub MOVE_HANDICAP (){ -2 }
85 93
86=item my $board = new $size 94=item my $board = new $size
87 95
88Creates a new empty board of the given size. 96Creates a new empty board of the given size.
89 97
130 138
131 my $max = $self->{max}; 139 my $max = $self->{max};
132 140
133 while (@nodes) { 141 while (@nodes) {
134 my ($x, $y) = @{pop @nodes}; 142 my ($x, $y) = @{pop @nodes};
143
135 unless ($seen{$x,$y}++) { 144 unless ($seen{$x,$y}++) {
136 if ($board->[$x][$y] & $mark) { 145 if ($board->[$x][$y] & $mark) {
137 push @found, [$x, $y]; 146 push @found, [$x, $y];
138 147
139 push @nodes, [$x-1, $y] unless $seen{$x-1, $y} || $x <= 0; 148 push @nodes, [$x-1, $y] unless $seen{$x-1, $y} || $x <= 0;
251 260
252 if ($set & MARK_MOVE) { 261 if ($set & MARK_MOVE) {
253 $$_ &= ~$mark_symbols for @{ $self->{unmark} || [] }; 262 $$_ &= ~$mark_symbols for @{ $self->{unmark} || [] };
254 @{ $self->{unmark} } = $space; 263 @{ $self->{unmark} } = $space;
255 264
265 # remark the space, in case the move was on the same spot as the
266 # old mark
267 $$space |= $set;
268
256 unless (${ $_->[5] ||= \my $hint }) { 269 unless (${ $_->[5] ||= \my $hint }) {
257 my ($own, $opp) = 270 my ($own, $opp) =
258 $set & MARK_B 271 $set & MARK_B
259 ? (MARK_B, MARK_W) 272 ? (MARK_B, MARK_W)
260 : (MARK_W, MARK_B); 273 : (MARK_W, MARK_B);
261 274
262 my (@capture, $suicide); 275 my (@capture, @suicide);
263 276
264 push @capture, $self->capture ($opp, $x-1, $y) if $x > 0 && $board->[$x-1][$y] & $opp; 277 push @capture, $self->capture ($opp, $x-1, $y) if $x > 0 && $board->[$x-1][$y] & $opp;
265 push @capture, $self->capture ($opp, $x+1, $y) if $x < $self->{max} && $board->[$x+1][$y] & $opp; 278 push @capture, $self->capture ($opp, $x+1, $y) if $x < $self->{max} && $board->[$x+1][$y] & $opp;
266 push @capture, $self->capture ($opp, $x, $y-1) if $y > 0 && $board->[$x][$y-1] & $opp; 279 push @capture, $self->capture ($opp, $x, $y-1) if $y > 0 && $board->[$x][$y-1] & $opp;
267 push @capture, $self->capture ($opp, $x, $y+1) if $y < $self->{max} && $board->[$x][$y+1] & $opp; 280 push @capture, $self->capture ($opp, $x, $y+1) if $y < $self->{max} && $board->[$x][$y+1] & $opp;
272 # remove captured stones 285 # remove captured stones
273 $self->{captures}[$own == MARK_B ? COLOUR_BLACK : COLOUR_WHITE] += @capture; 286 $self->{captures}[$own == MARK_B ? COLOUR_BLACK : COLOUR_WHITE] += @capture;
274 $self->{board}[$_->[0]][$_->[1]] = 0 287 $self->{board}[$_->[0]][$_->[1]] = 0
275 for @capture; 288 for @capture;
276 289
277 $suicide += $self->capture ($own, $x, $y); 290 push @suicide, $self->capture ($own, $x, $y);
278 291
279 ${ $_->[5] } ||= !(@capture || $suicide); 292 ${ $_->[5] } ||= !(@capture || @suicide);
280 293
281 if ($suicide) { 294 if (@suicide) {
282 $self->{board}[$x][$y] = 0 295 $self->{board}[$_->[0]][$_->[1]] = 0
296 for @suicide;
297 # count suicides as other sides stones
298 $self->{captures}[$opp == MARK_B ? COLOUR_BLACK : COLOUR_WHITE] += @suicide;
283 299
284 } elsif (!$suicide && @capture == 1) { 300 } elsif (!@suicide && @capture == 1) {
285 # possible ko. now check liberties on placed stone 301 # possible ko. now check liberties on placed stone
286 302
287 my $libs; 303 my $libs;
288 304
289 $libs++ if $x > 0 && !($board->[$x-1][$y] & $opp); 305 $libs++ if $x > 0 && !($board->[$x-1][$y] & $opp);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines