%% psgo.sty
%% Copyright 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.1 2001/10/13 11:27:36 vbos Exp vbos $
%
\NeedsTeXFormat{LaTeX2e}% 
\ProvidesPackage{psgo}[2001/10/03, v0.11]
\RequirePackage{pstricks,pstcol,pst-node,calc,ifthen}

\newcommand{\psgo}{\textsf{psgo}}

\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{\golabelformat}[1]{{\sffamily #1}}
\newenvironment{gomoves}{%
	\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%
	}%
	\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[fillcolor=white,fillstyle=solid,linestyle=none,boxsep=false,framesep=8pt]{##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}]{%
		\ifthenelse{\equal{##2}{black}}%
			   {\blackstone[##1]{##3}{##4}}%
			   {\whitestone[##1]{##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{\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[B](\value{#1},-1){%
	    \addtocounter{#1}{1}%
	    \ifthenelse{\value{#1} > 8}%
	      {\addtocounter{#1}{1}\golabelformat{\Alph{#1}}\addtocounter{#1}{-1}}%
	      {\golabelformat{\Alph{#1}}}%
	    \addtocounter{#1}{-1}}%
	  \rput(\value{#1},#2){%
	    \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]{%
%	\newcommand{\ypos}{\relax}%
	\whiledo{\value{#1} < #2}{%
	  \addtocounter{#1}{1}%
	  \edef\ypos{\arabic{#1}}%
	  \addtocounter{#1}{-1}%
	  \rput(-1,\value{#1}){\golabelformat{\ypos}}%
	  \rput(#2,\value{#1}){\golabelformat{\ypos}}%
	  \stepcounter{#1}%
	  }%
	}%
\newenvironment{psgoboard}[1][19]{%
	\setcounter{boardsize}{#1}%
	\psset{xunit=\goxunit,yunit=\goyunit}%	
	\setlength{\tmplx}{#1\goxunit + 0\goxunit}%
	\setlength{\tmply}{#1\goyunit + 0.25\goyunit}%
	\begin{pspicture}(-1.25,-1)(\tmplx,\tmply)%
	\setlength{\tmply}{#1\goyunit + 0.5\goyunit}%
	\setlength{\tmplx}{#1\goxunit + 0.5\goxunit}%
	\addtolength{\tmply}{-1.5\goyunit}%
	\addtolength{\tmplx}{-1.5\goxunit}%
	\multips(0,0)(1,0){#1}{\psline(0,0)(0,\tmply)}%
	\multips(0,0)(0,1){#1}{\psline(0,0)(\tmplx,0)}%
	\setcounter{gotmpc}{0}%
	\xpositionmarks{gotmpc}{#1}%
	\setcounter{gotmpc}{0}%
	\ypositionmarks{gotmpc}{#1}%
	\ifthenelse{\equal{#1}{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}%
	\end{pspicture}%
	}%
\newenvironment{psgoboard*}[1][19]{%
	\setcounter{boardsize}{#1}%
	\psset{xunit=\goxunit,yunit=\goyunit}%
	\setlength{\tmplx}{#1\goxunit + 0\goxunit}%
	\setlength{\tmply}{#1\goyunit + 0.25\goyunit}%
	\begin{pspicture}(-1.25,-1)(\tmplx,\tmply)%
	\setlength{\tmply}{#1\goyunit + 0.5\goyunit}%
	\setlength{\tmplx}{#1\goxunit + 0.5\goxunit}%
	\addtolength{\tmply}{-1.5\goyunit}%
	\addtolength{\tmplx}{-1.5\goxunit}%
	\multips(0,0)(1,0){#1}{\psline(0,0)(0,\tmply)}%
	\multips(0,0)(0,1){#1}{\psline(0,0)(\tmplx,0)}%
	\ifthenelse{\equal{#1}{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}%
	\end{pspicture}%
	}%


\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}}}
