[GoWiki] Generate diagram from SGF
10) $movecnt = 10; if ($startmove < 1) $startmove = 1; if ($startmove > 400) $startmove = 400; $nextstart = $startmove ? $startmove + $movecnt : 1; $nextcount = $movecnt ? $movecnt : 10; ?>

Start move: · Number of moves (0-10): ·


Notes: Questions, feedback? -> GenerateDiagramFromSGF 0) $movecnt--; else $showposition = 1; $moves = preg_split('/;\s*/', $sgf); $movenum = 0; for ($mnum=0; $mnum < count($moves); $mnum++) { // AB, AW, AE if (preg_match_all( '/[^A-Z]?A(dd)?([BWE])(lack|hite|mpty)?((\[[a-s][a-s]\])+)/', $moves[$mnum], $matches)) add_stones($matches[2], $matches[4]); // B, W if (!preg_match('/(^|[^A-Z])([BW])(lack|hite)?\[(([a-t])([a-t]))?\]/', $moves[$mnum], $matches)) continue; $movenum++; if ($movenum - $startmove < 9) $mvchar = chr(ord('1') + $movenum - $startmove); else $mvchar = '0'; $mvnum = ($mvchar == '0') ? '10' : $mvchar; if ($matches[5] != 't' && $matches[6] != 't' && $matches[4] != '') // !(pass move) { $x = ord($matches[5]) - ord('a'); $y = ord($matches[6]) - ord('a'); $col = ($matches[2] == 'W') ? -1 : 1; if ($movenum < $startmove || $showposition == 1) play_move($x, $y, $col, $docapture); else { if ($movenum == $startmove) $startcolor = $matches[2]; if (!is_int($board[$y][$x]) && $board[$y][$x] >= 1 && $board[$y][$x] <= 9) { // there is already a move at that position $title .= " ($mvnum at " . $board[$y][$x] . ')'; } else $board[$y][$x] = $mvchar; } } elseif ($movenum >= $startmove && !$showposition) { $title .= " ($mvnum: pass)"; } if ($movenum >= $startmove + $movecnt) break; } if ($movenum < $startmove) { print "
Sorry. Your starting move number is beyond the end of the game.
This game has only $movenum moves. The end position looks like this: 
"; print_board($board, $startcolor, 'End position'); } else { if ($showposition) $title = "Position at move $startmove"; else $title = "Moves $startmove to $movenum$title"; print_board($board, $startcolor, $title); } } function add_stones($property, $propvalues) { global $board; for ($i = 0; $i < count($property); $i++) { switch ($property[$i]) { case 'B': $color = 1; break; case 'W': $color = -1; break; case 'E': $color = 0; break; } $values = preg_split('/\[/', $propvalues[$i]); for ($j = 0; $j < count($values); $j++) { $x = ord($values[$j][0]) - ord('a'); $y = ord($values[$j][1]) - ord('a'); $board[$y][$x] = $color; } } } function play_move($x, $y, $col) { global $board, $captureboard; $board[$y][$x] = $col; // do captures do_capture($x-1, $y, $col); do_capture($x+1, $y, $col); do_capture($x, $y-1, $col); do_capture($x, $y+1, $col); // suicide? do_capture($x, $y, -$col); } // returns number of captured stones function do_capture($x, $y, $col) { global $board, $captureboard, $captures; if ($x < 0 || $y < 0 || $x >=19 || $y >=19) return 0; if ($board[$y][$x] != -$col) return 0; $captureboard = array(); // clear capture board $captures = 0; if (capture($x, $y, $col)) return 0; // there are captures - remove stones $done = 0; for ($y=0; $y < 19; $y++) { for ($x=0; $x < 19; $x++) { if ($captureboard[$y][$x]) { $board[$y][$x] = 0; // remove stone $done++; if ($done == $captures) return $captures; } } } } // recursive function to mark group (searches for liberties) // returns 1 if a liberty was found, 0 otherwise function capture($x, $y, $col) { global $board, $captureboard, $captures; // out of bounds? if ($x < 0 || $y < 0 || $x >=19 || $y >=19) return 0; // found opposite color if ($board[$y][$x] == $col) return 0; // found a liberty if ($board[$y][$x] == 0) return 1; // already visited? if ($captureboard[$y][$x]) return 0; $captureboard[$y][$x] = 1; $captures++; if (capture($x-1, $y, $col)) return 1; if (capture($x+1, $y, $col)) return 1; if (capture($x, $y-1, $col)) return 1; if (capture($x, $y+1, $col)) return 1; return 0; } function print_board($board, $startcolor, $title) { print "
\$\$$startcolor $title\n";
    print "\$\$  ---------------------------------------\n";
    
    for ($y=0; $y < 19; $y++) {
        print "\$\$ | ";
    	for ($x=0; $x < 19; $x++) {
	    if ($board[$y][$x] === 1)
	    	print "X ";
	    elseif ($board[$y][$x] === -1)
	    	print "O ";
	    elseif ($board[$y][$x] === 0) {
		if (($x == 3 || $x==9 || $x==15) &&
		    ($y == 3 || $y==9 || $y==15))
		    print ", ";
		else
		    print ". ";
	    }
	    else
		print $board[$y][$x] . ' ';
	}
	print "|\n";
    }
    print "\$\$  ---------------------------------------\n";
    print "
\n\n"; } ?>