%% psgo.sty
%% Copyright 2004, 2003,2001 V. Bos
%
% This program may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.2
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.2 or later is part of all distributions of LaTeX 
% version 1999/12/01 or later.
%
% This program consists of the files:
%
%   psgo.sty
%   psgomanual.tex
%   psgomanual.ps
%
% RCS-ID: $Id: psgo.sty,v 1.7 2004/02/24 10:20:11 vbos Exp $
%
\NeedsTeXFormat{LaTeX2e}% 
\ProvidesPackage{psgo}[2008/09/04, v0.17]
\message{`PSGO', <2008/09/04>, v0.17}
\RequirePackage{pstricks,pst-node,calc,ifthen}

\newcommand{\psgo}{\textsf{psgo}}
\newsavebox{\stmarkbox}
\newcounter{gomove}
\setcounter{gomove}{-1}
\newcounter{boardsize}
\newboolean{blackmove}
\setboolean{blackmove}{true}
\newcommand{\toggleblackmove}{%
  \ifthenelse{\boolean{blackmove}}{\setboolean{blackmove}{false}}{\setboolean{blackmove}{true}}%
}
\newlength{\goxunit}
\newlength{\goyunit}
\newcommand{\setgounit}[1]{\setlength{\goxunit}{#1}\setlength{\goyunit}{1.115\goxunit}}
\setgounit{0.6cm}
\newcommand{\hatchangle}{-42.5}
\newlength{\tmplx}
\newlength{\tmply}
\newlength{\tmpxa}%
\newlength{\tmpxb}%
\newlength{\tmpya}%
\newlength{\tmpyb}%
\newcounter{gotmpc}
\newcommand{\pointbox}[1]{\raisebox{-0.5\height+0.5\depth}[0pt][0pt]{\makebox[0pt]{#1}}}
\newcommand{\golabelformat}[1]{{\sffamily #1}}
\newcommand{\goxposition}[1]{%
	\ifx#1a 0\else%
	\ifx#1b 1\else%
	\ifx#1c 2\else%
	\ifx#1d 3\else%
	\ifx#1e 4\else%
	\ifx#1f 5\else%
	\ifx#1g 6\else%
	\ifx#1h 7\else%
	\ifx#1j 8\else%
	\ifx#1k 9\else%
	\ifx#1l 10\else%
	\ifx#1m 11\else%
	\ifx#1n 12\else%
	\ifx#1o 13\else%
	\ifx#1p 14\else%
	\ifx#1q 15\else%
	\ifx#1r 16\else%
	\ifx#1s 17\else%
	\ifx#1t 18\else%
	\relax%
	\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi%
}%
\newcommand{\goyposition}[1]{%
	\ifnum#1=1 0\else%
	\ifnum#1=2 1\else%
	\ifnum#1=3 2\else%
	\ifnum#1=4 3\else%
	\ifnum#1=5 4\else%
	\ifnum#1=6 5\else%
	\ifnum#1=7 6\else%
	\ifnum#1=8 7\else%
	\ifnum#1=9 8\else%
	\ifnum#1=10 9\else%
	\ifnum#1=11 10\else%
	\ifnum#1=12 11\else%
	\ifnum#1=13 12\else%
	\ifnum#1=14 13\else%
	\ifnum#1=15 14\else%
	\ifnum#1=16 15\else%
	\ifnum#1=17 16\else%
	\ifnum#1=18 17\else%
	\ifnum#1=19 18\else%
	\relax%
	\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi%
}%

\newenvironment{gomoves}{%
	\newcommand{\mdd}[4]{%
		\setlength{\tmpxa}{\goxposition{##1}\goxunit}\addtolength{\tmpxa}{-0.5\goxunit}%
		\setlength{\tmpxb}{\goxposition{##3}\goxunit}\addtolength{\tmpxb}{0.5\goxunit}%
		\setlength{\tmpya}{\goyposition{##2}\goyunit}\addtolength{\tmpya}{-0.5\goyunit}%
		\setlength{\tmpyb}{\goyposition{##4}\goyunit}\addtolength{\tmpyb}{0.5\goyunit}%
		\psframe[dimen=middle,fillstyle=vlines,hatchsep=2pt,hatchwidth=0.2pt,hatchangle=\hatchangle,linestyle=none]%
		(\tmpxa,\tmpya)(\tmpxb,\tmpyb)%
		}%
	\newcommand{\placesymbol}[3]{%
		\ifthenelse{\value{boardsize} > \goxposition{##2}%
                            \and \value{boardsize} > \goyposition{##3}}{%
			\rput(\goxposition{##2},\goyposition{##3}){##1}%
		}{}%
		}%
	\newcommand{\markpos}[3]{\placesymbol{\psframebox[dimen=inner,fillcolor=white,fillstyle=solid,linestyle=none,boxsep=false,framesep=0.3\goxunit]{##1}}{##2}{##3}}%
	\newcommand{\blackstone}[3][\relax]{%
		\placesymbol{\pscircle[linewidth=0.4pt,fillstyle=solid,fillcolor=black](0,0){0.5\goxunit}%
			  {\psset{linecolor=white,fillcolor=white,hatchcolor=gray}\color{white}##1}}{##2}{##3}%
		}%
	\newcommand{\whitestone}[3][\relax]{%
		\placesymbol{\pscircle[linewidth=0.4pt,fillstyle=solid,fillcolor=white](0,0){0.5\goxunit}%
			  {\psset{linecolor=black,fillcolor=black,hatchcolor=gray}\color{black}##1}}{##2}{##3}%
		}%
	\renewcommand{\stone}[4][\nomark{\relax}]{%
%		\def\stmark{\raisebox{-0.5\height+0.5\depth}[0.5\totalheight][0.5\totalheight]{\makebox[0pt]{##1}}}%
		\def\stmark{\pointbox{##1}}
		\ifthenelse{\equal{##2}{black}}%
			   {\blackstone[\stmark]{##3}{##4}}%
			   {\whitestone[\stmark]{##3}{##4}}%
		}%
	\newcommand{\move}{\@ifstar{\movestar}{\movenostar}}%
	\newcommand{\movestar}[3][\nomark]{%
		\ifthenelse{\boolean{blackmove}}%
		           {\stone[##1]{black}{##2}{##3}}%
		           {\stone[##1]{white}{##2}{##3}}%
		\toggleblackmove%
		}%
	\newcommand{\movenostar}[3][\nomark]{%
		\refstepcounter{gomove}%		
		\ifthenelse{\boolean{blackmove}}%
		           {\stone[##1\rput(0,0){\golabelformat{\thegomove}}]{black}{##2}{##3}}%
		           {\stone[##1\rput(0,0){\golabelformat{\thegomove}}]{white}{##2}{##3}}%
		\toggleblackmove%
		}%
    \newcommand{\pass}{\@ifstar{\passstar}{\passnostar}}%
    \newcommand{\passstar}{\toggleblackmove}%
    \newcommand{\passnostar}{\refstepcounter{gomove}\toggleblackmove}%
	\newcommand{\goline}[4]{\psline[linewidth=1.4pt,linecolor=black,linestyle=solid]%
		(\goxposition{##1},\goyposition{##2})(\goxposition{##3},\goyposition{##4})%
		}%
	\newcommand{\goarrow}[4]{\psline[linewidth=1.4pt,linecolor=black,linestyle=solid,arrowscale=1.5]%
		{->}%
		(\goxposition{##1},\goyposition{##2})(\goxposition{##3},\goyposition{##4})%
		}%
	}{}%
\newcommand{\xpositionmarks}[2]{%
	\whiledo{\value{#1} < #2}{%
	  \rput[t](\value{#1},-\goyunit){%
	    \addtocounter{#1}{1}%
	    \ifthenelse{\value{#1} > 8}%
	      {\addtocounter{#1}{1}\golabelformat{\Alph{#1}}\addtocounter{#1}{-1}}%
	      {\golabelformat{\Alph{#1}}}%
	    \addtocounter{#1}{-1}}%
      \setlength{\tmply}{#2\goyunit}%
	  \rput[B](\value{#1},\tmply){%
	    \addtocounter{#1}{1}%
	    \ifthenelse{\value{#1} > 8}%
	      {\addtocounter{#1}{1}\golabelformat{\Alph{#1}}\addtocounter{#1}{-1}}%
	      {\golabelformat{\Alph{#1}}}%
	    \addtocounter{#1}{-1}}%
	  \stepcounter{#1}%
	  }%
	}%
\newcommand{\ypositionmarks}[2]{%
	\whiledo{\value{#1} < #2}{%
	  \addtocounter{#1}{1}%
	  \edef\ypos{\arabic{#1}}%
	  \addtocounter{#1}{-1}%
	  \rput[r](-\goxunit,\value{#1}){\golabelformat{\ypos}}%
      \setlength{\tmplx}{#2\goxunit}%
	  \rput[l](\tmplx,\value{#1}){\golabelformat{\ypos}}%
	  \stepcounter{#1}%
	  }%
	}%

\def\psgosetboardsizes(#1,#2)(#3,#4){%
  \def\psgollx{#1}%
  \def\psgolly{#2}%
  \ifthenelse{#3 > \value{boardsize}}{\def\psgourx{\value{boardsize}}}{\def\psgourx{#3}}%
  \ifthenelse{#4 > \value{boardsize}}{\def\psgoury{\value{boardsize}}}{\def\psgoury{#4}}%
}
\newboolean{withindices}%
\setboolean{withindices}{true}%
\newenvironment{psgopartialboard}[2][19]{%
	\setcounter{boardsize}{#1}%
    \psgosetboardsizes#2%
	\psset{xunit=\goxunit,yunit=\goyunit}%
    \ifthenelse{\boolean{withindices}}%
               {\def\xoffset{2\goxunit}\def\yoffset{2\goyunit}}%
               {\def\xoffset{.5\goxunit}\def\yoffset{.5\goyunit}}%
    \ifthenelse{\psgollx = 1 \or \psgollx < 1}%
               {\def\psgollxval{-\xoffset}}%
               {\setlength{\tmplx}{\psgollx\goxunit - 1\goxunit}\edef\psgollxval{\the\tmplx}}%
    \ifthenelse{\psgolly = 1 \or \psgolly < 1}%
               {\def\psgollyval{-\yoffset}}%
               {\setlength{\tmply}{\psgolly\goyunit - 1\goyunit}\edef\psgollyval{\the\tmply}}%
    \ifthenelse{\psgourx = \value{boardsize} \or \psgourx > \value{boardsize}}%
               {\setlength{\tmplx}{\value{boardsize}\goxunit - \goxunit + \xoffset}%
                \edef\psgourxval{\the\tmplx}}%
               {\def\psgourxval{\psgourx\goxunit}}%
    \ifthenelse{\psgoury = \value{boardsize} \or \psgoury > \value{boardsize}}%
               {\setlength{\tmply}{\value{boardsize}\goyunit - \goyunit + \yoffset}%
                \edef\psgouryval{\the\tmply}}%
               {\def\psgouryval{\psgoury\goyunit}}%
	\begin{pspicture}(\psgollxval,\psgollyval)(\psgourxval,\psgouryval)%
    \ifthenelse{\psgollx = 1 \or \psgollx < 1}%
               {\def\psgollxval{-\xoffset}}%
               {\setlength{\tmplx}{\psgollx\goxunit - 1.5\goxunit}\edef\psgollxval{\the\tmplx}}%
    \ifthenelse{\psgolly = 1 \or \psgolly < 1}%
               {\def\psgollyval{-\yoffset}}%
               {\setlength{\tmply}{\psgolly\goyunit - 1.5\goyunit}\edef\psgollyval{\the\tmply}}%
    \ifthenelse{\psgourx = \value{boardsize} \or \psgourx > \value{boardsize}}%
               {\setlength{\tmplx}{\value{boardsize}\goxunit - \goxunit + \xoffset}%
                \edef\psgourxval{\the\tmplx}}%
               {\setlength{\tmplx}{\psgourx\goxunit - .5\goxunit}%
                \edef\psgourxval{\the\tmplx}}%
    \ifthenelse{\psgoury = \value{boardsize} \or \psgoury > \value{boardsize}}%
               {\setlength{\tmply}{\value{boardsize}\goyunit - \goyunit + \yoffset}%
                \edef\psgouryval{\the\tmply}}%
               {\setlength{\tmply}{\psgoury\goyunit - .5\goyunit}%
                \edef\psgouryval{\the\tmply}}%
    %% uncomment the following line to get a visual bounding box 
    %% \psframe[linecolor=green,linewidth=0.4pt](\psgollxval,\psgollyval)(\psgourxval,\psgouryval)
    \psclip{\psframe[linecolor=white,linewidth=0](\psgollxval,\psgollyval)(\psgourxval,\psgouryval)}%
	\setlength{\tmply}{\value{boardsize}\goyunit + 0.5\goyunit}%
	\setlength{\tmplx}{\value{boardsize}\goxunit + 0.5\goxunit}%
	\addtolength{\tmply}{-1.5\goyunit}%
	\addtolength{\tmplx}{-1.5\goxunit}%
	\multips(0,0)(1,0){\value{boardsize}}{\psline[linewidth=0.2pt](0,0)(0,\tmply)}%
	\multips(0,0)(0,1){\value{boardsize}}{\psline[linewidth=0.2pt](0,0)(\tmplx,0)}%
	\psframe[linewidth=1pt](0,0)(\tmplx,\tmply)%
    \ifthenelse{\boolean{withindices}}{%
	  \setcounter{gotmpc}{0}%
	  \xpositionmarks{gotmpc}{\value{boardsize}}%
	  \setcounter{gotmpc}{0}%
	  \ypositionmarks{gotmpc}{\value{boardsize}}%
    }{\relax}%
	\ifthenelse{\equal{\value{boardsize}}{19}}{%
	  \multips(3,3)(0,6){3}{\pscircle[fillstyle=solid,fillcolor=black](0,0){.15\goxunit}}%
	  \multips(9,3)(0,6){3}{\pscircle[fillstyle=solid,fillcolor=black](0,0){.15\goxunit}}%
	  \multips(15,3)(0,6){3}{\pscircle[fillstyle=solid,fillcolor=black](0,0){.15\goxunit}}%
	}{\relax}%
	\begin{gomoves}%
	}%
	{\end{gomoves}%
    \endpsclip%
	\end{pspicture}%
	}%
\newenvironment{psgoboard}[1][19]{%
  \begin{psgopartialboard}[#1]{(0,0)(#1,#1)}%
}{%
  \end{psgopartialboard}%
}

\newenvironment{psgopartialboard*}[2][19]{%
  \setboolean{withindices}{false}%
  \begin{psgopartialboard}[#1]{#2}%
}{\end{psgopartialboard}}

\newenvironment{psgoboard*}[1][19]{%
  \begin{psgopartialboard*}[#1]{(0,0)(#1,#1)}%
}{%
  \end{psgopartialboard*}%
}

\newcommand{\nomark}{\relax}%
\newcommand{\factor}{0.28284271247461901}% = 2/5 * sin(1/4 * pi)
\newcommand{\markma}{%
	\psline[linewidth=1.4pt](-\factor,-\factor)(\factor,\factor)%
	\psline[linewidth=1.4pt](-\factor,\factor)(\factor,-\factor)%
	}
\newcommand{\marktr}{%
	\rput(0,-0.3\goxunit){\pstriangle(-0.66\goxunit,0.66\goxunit)}%
	}%
\newcommand{\markcr}{%
	\pscircle(0,0){0.33\goxunit}%
	}
\newcommand{\marksq}{%
	\psframe(-0.33\goxunit,-0.33\goxunit)(0.33\goxunit,0.33\goxunit)%
	}
\newcommand{\marklb}[1]{%
	\rput(0,0){\golabelformat{#1}}%
	}%
\newcommand{\marksl}{%
	\psframe[fillstyle=solid,fillcolor=gray,linecolor=gray](-0.22\goxunit,-0.22\goxunit)(0.22\goxunit,0.22\goxunit)%
	}%
\newcommand{\markdd}{%
	\psframe[dimen=middle,fillstyle=vlines,hatchsep=2pt,hatchwidth=0.2pt,hatchangle=\hatchangle,linestyle=none]%
	(-0.5\goxunit,-0.5\goyunit)(0.5\goxunit,0.5\goyunit)%
	}%
\newcommand{\stone}[2][\relax]{%
  \setlength{\tmply}{-0.5\goxunit + 0.5ex}%
  \raisebox{\tmply}{\psset{unit=\goxunit}\begin{pspicture}(-0.5,-0)(0.5,1)%
    \pscircle[fillstyle=solid,fillcolor=#2](0,0.5){0.5}%
    \ifthenelse{\equal{#2}{black}}%
      {\rput(0,0.5){{\psset{linecolor=white}\color{white}\golabelformat{#1}}}}%
      {\rput(0,0.5){{\psset{linecolor=black}\color{black}\golabelformat{#1}}}}%
  \end{pspicture}}}
