# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This program is distributed with GNU Go, a Go program. #
# Write gnugo@gnu.org or see http://www.gnu.org/software/gnugo/ #
# for more information. #
# Copyright 1999, 2000, 2001 by the Free Software Foundation. #
# This program is free software; you can redistribute it and/or #
# modify it under the terms of the GNU General Public License #
# as published by the Free Software Foundation - version 3, #
# or (at your option) any later version. #
# This program is distributed in the hope that it will be #
# useful, but WITHOUT ANY WARRANTY; without even the implied #
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR #
# PURPOSE. See the GNU General Public License in file COPYING #
# You should have received a copy of the GNU General Public #
# License along with this program; if not, write to the Free #
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, #
# Boston, MA 02111, USA. #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
our @EXPORT = qw(ttNewGame ttShowBoard ttPlaceStone ttGetBoard ttScore);
my @sbd = (); # working board
my @letter = qw
( A B C D E F G H J K L M N O P Q R S T U V W X Y Z
);
my %tocol = ( 'b' => 2, 'B' => 2, 'w' => 1, 'W' => 1 );
foreach my $ix (0 .. $#letter) {
$toix{ $letter[$ix] } = $ix;
my %hashed_boards = (); # for convenient rep testing
my @all_boards = (); # for move takebacks
my %tovisual = ( 2 => 'W', 1 => 'B', 0 => '+' );
my $s = ($bs+2) * ($bs+1);
foreach my $i (0 .. $s-1) {
foreach my $x (0 .. $bs - 1) {
foreach my $y (0 .. $bs - 1) {
$bd[ ($y+1) * ($bs+1) + $x ] = 0; # empty
push( @all_boards, join(',', @bd) );
my @prev_board = @bd; # to take back if needed
$hashed_boards{join(',',@prev_board)} = 1; # hash previous board
my $sq = ($y+1) * ($bs+1) + $x;
if ($bd[ ($y+1) * ($bs+1) + $x ] != 0) {
print "Illegal move, square occupied\n";
# did we capture anything?
# ========================
my $cc = $tocol{$c}; # current color
if ($bd[$sq+$d] == (3 ^ $cc)) {
my $lc = lib_count
( 3 ^ $cc, $sq + $d );
print "Capture possible\n";
capture
( 3 ^ $cc, $sq+$d );
# if capture not possible, it might be suicide
# ============================================
$bd[$sq] = 0; # make it empty again
my $lc = lib_count
($tocol{$c}, $sq );
print "liberty count = $lc\n";
print "Illegal move, suicide!\n";
if ( defined( $hashed_boards{ join(',',@bd) } ) ) {
print "Illegal move, repeated positions\n";
push( @all_boards, join(',', @bd) );
if ($sbd[ $sq + $d ] == 0) {
if ($sbd[ $sq + $d ] == 3) { next; }
if ($sbd[ $sq + $d ] == $c) {
$count += lib_count
( $c, $sq + $d );
if ( $bd[ $sq + $d ] == $c ) {
foreach my $y (0 .. $bs + 1) {
foreach my $x (0 .. $bs) {
printf ( "%2d", $bd[ $y * ($bs+1) + $x ] );
foreach my $y (0 .. $bs-1) {
foreach my $x (0 .. $bs-1) {
push @tbd, $tovisual{ $bd[ ($y+1) * ($bs+1) + $x ] };
foreach my $sq (0 .. (($bs+2) * ($bs+1))-1 ) {
if ( $bd[$sq]==1 || $bd[$sq]==2 ) { $stc[$bd[$sq]] ++; }
my ($cnt, $who) = count_space
($sq);
if ($who == 1 || $who == 2) {
print "white stones=$stc[$white] territory=$ter[$white]\n";
print "black stones=$stc[$black] territory=$ter[$black]\n";
return( ($stc[$black] + $ter[$black])-($stc[$white] + $ter[$white]) );
if ( $sbd[$sq] == 9 || $sbd[$sq] == 3) {
} elsif ( $sbd[$sq] != 0 ) {
my ($c, $w) = count_space
( $sq + $d );