#! /usr/bin/perl -w my @fields; sub main { while () { # get rid of all trailing white space, CR, LF, etc. s/\s+$//; # break out the fields from snowie's .txt file @fields = split /;/; last if ($#fields == 39); } die "Can't find the expected data" if $#fields != 39; my ($neg, $pos); my $is_match = ($fields[0] != 0); my $is_jacoby = $fields[1] if (! $is_match); my $on_roll = $fields[4]; my @players = @fields[5..6]; my $is_crawford = $fields[7] if ($is_match); my @scores = @fields[8..9]; my $cube_val = $fields[10]; my $cube_owner = $fields[11]; my @dice = @fields[38..39]; my @board = @fields[13..37]; # The points are presented backwards if player 1 is on roll @board = reverse @board if ($on_roll == 1); # start the .sgf file print "(;FF[4]GM[6]CA[UTF-8]AP[Snowie .txt to .sgf]"; printf "MI[length:%d][game:0][ws:%d][bs:%d]", $fields[0], @scores; printf "PW[%s]PB[%s]", @players; # set the rules up, handle Crawford games if ($is_match) { printf "RU[Crawford%s]", $is_crawford ? ":CrawfordGame" : ""; } else { print "RU[Jacoby]" if ($is_jacoby); } print "\n;AE[a:y]"; # convert point numbers to letters a..x # $pos is the .sgf layout for the points with positive values (player # on roll), $neg is the non-on-roll player for (my $i = 0; $i <= $#board; ++$i) { my $c = sprintf "[%c]", ord ('a') + $i; if ($board[$i] > 0) { $pos .= $c x $board[$i]; } else { $board[$i] = - $board[$i]; $neg .= $c x $board[$i]; } } # add in any chequers on the bar. $pos .= "[y]" x ($on_roll == 0 ? $fields[12] : $fields[37]); $neg .= "[y]" x ($on_roll == 1 ? $fields[12] : $fields[37]); # finally output the chequer positions if ($on_roll == 0) { print "AW${pos}AB$neg\n" } else { print "AW${neg}AB$pos\n" } # set the cube and owner if ($cube_owner == 0) { $cube_owner = "c"; } elsif ($cube_owner == 1) { $cube_owner = ($on_roll == 0) ? "w" : "b"; } else { $cube_owner = ($on_roll == 0) ? "b" : "w"; } if (! $is_crawford) { printf ";CV[%d]\n;CP[%s]\n", $cube_val, $cube_owner; } # and the player on roll and dice printf ";PL[%s]DI[%d%d])\n", ($on_roll == 0) ? "W" : "B", @dice; } main;