% GoTeX 0.1
% Youyi Chen
% Wed Jan 13 01:33:59 EST 1993
% ******************************
%
% This is a set of TeX and LateX macros changing
% an ASCII GO board into a prety looking LaTeX Go board.
% Put this file in a file called gotex.tex
% and add a line as follow
%         \input{gotex}
% in your latex file before \begin{document}
% Then following lines in LaTeX will do the trick
%
% **************************************************************
% 
% \goboardul(4mm)        %% set goboard unit length
% \goboardstyle(2,1,0,2) %% north and west edge close with mark
%                        %% east edge close with no mark
%                        %% south edge open
% \begin{goboard}{19}{8} %% a 19 by 8 board
%    A B C D E F G H J K L M N O P Q R S T
% 19 . . . . . . . . . . . . . . . . . . .
% 18 . . # # O . . . . . . . . . . . . . .
% 17 . . # O . O . . . # . . . . . . . . .
% 16 . # # O . . . . . + . . . A . # . . .        
% 15 . # O # O . . . . . . . . . . . . . .
% 14 . O O . . . . . . . . . . . . . . . .
% 13 . . O . . . . . . . . . . . . . . . .
% 12 . . . . . . . . . . . . . . . . . . .
% \end{goboard}
% % Note that nothing, except marks, allowed in the right end of the board!
% ********************************************************************
% This set of macros can also be used to translate
% other ASCII pictures to TeX. For example,
% you can do a plot in LaTeX as follow.
%
% \goboardul(4mm)
% \setnulltoken(.)  %% set . as a blank.
% \blackunit(0.3)
% \goboardstyle(0,0, 2 ,2)
% \begin{goboard}{20}{20}
% $\beta$  ....................
% 19       ....................
% 18       ....................
% 17       ....................
% 16       ....................
% $\alpha$ ....................
% 14       ....................
% 13       ....................
% 12       ....................
% 11       ....................
% $\gamma$ ........##..........
% 9        .......#..#........#
% 8        .....##...#......#..
% 7        .....#.....#..###...
% 6        ....#......#.#......
% $\delta$ ...#........#.......
% 4        ...#................
% 3        ..#.................
% 2        ##..................
% 1        ....................
%          a b c d e f g h i j k l m n o p q r s t
% \end{goboard}
% ********************************************************************
%
%  PUBLIC MACROS
%
%  \begin{goboard}{#1}{#2}
%  \end{goboard}
%  #1 and #2 are the size of the board
%
% \showtext(#1,#2,#3)
%    show text #3 at coordinate (#1, #2) 
% \showblack(#1, #2)
%    show black stone at (#1, #2)
% \showwhite(#1, #2)
% \showstar(#1, #2)
% \shownostone(#1,#2)
%
% ********************************************************************
% Following commands are to be used before \begin{goboard}
% 
% \goboardstyle(#1,#2,#3,#4)
%     north #1
%     east  #2
%     south #3 
%     west  #4
%     indix takes 3 values as follow
%     0 : open edge
%     1 : close edge without marker
%     2 : close edge with markers
%
% \goboardul(#1)
%    set go board's unit length
%
% \blackunit(#1)
%    set diammeter of black stone, default is 1 unit. 
%    Smaller unit is useful for doing plot, and translating other ASCII 
%    picture.
%
% \whiteunit(#1)
%    set diammeter of white stone, default is 1 unit
%
% \starunit(#1)
%    set diammeter of white stone, default is 0.3 unit
%
% \setnulltoken(#1)
%     set null token, default is {=}. Using this to plot without grid.
%
%  Following three commands from LaTeX
%
%  \thicklines %% from latex, default
%  \thinlines %% from latex
%  \linethickness{len} %% also from latex.
%
%
% \GoTeX 
%       GoTeX logo
% \Go
% \WeiQi
% *************************************************
%
% \!nulltoken    is  =
% \!nostonetoken is  .
% \!whitetoken   is  O
% \!blacktokena  is  @
% \!blacktokenb  or  #
% \!startoken    is  +
% \!vlinetoken   is  |
% \!hlinetoken   is  - 
% ********************************************************************

\catcode`!=11 %

\newif\if!gotexdebug
\!gotexdebugtrue
\def\!gowrite{\immediate\write16}
\!gowrite{Mysterious GoTeX}
\!gowrite{from the author of " Mysteries of WeiQi --- The King's Golden Rules"}

%  GoTeX logo, and funny WeiQi
\def\Go{G\kern-.11em\lower.5ex\hbox{O}\kern-.075em}
\def\GoTeX{\Go\kern-.11em\TeX}
\def\WeiQi{We\lower.3ex\hbox{i}\kern-.12em\lower-.5ex\hbox{Q}i}

%% To watch TeX in action
%% \tracingmacros=2
%% \tracingcommands=2

%% ********************************************************************
%%  PRIVATE MACROS
%% ...
%% ********************************************************************

\def\\{\let\!septoken= } \\  %  set \septoken to a space token
 
%% Commands to cycle through token list

\def\!doboard{ %
   \!checkposition
   \if!outofboard \let\!doit=\!dooutboard 
      \else \let\!doit=\!doinboard 
   \fi
   \!doit
}

\def\!dooutboard#1{ %
\edef\!texttoken{\!texttoken#1}
\futurelet\!nextchar\!getnext
}
\def\!getnext{
\ifx \!nextchar \!septoken
   \let \!next=\!textparser
\else 
   \let \!next=\!dooutboard
\fi
\!next
}

\def\!textparser{
   \ifx\!texttoken\!nulltoken %
      \else \showtext(\!currentx,\!currenty,\!texttoken)
   \fi
   \def\!texttoken{}
   \!advancecoord
   \!finished{\!cleanup}{\!doboard}
}

\long\def\!doinboard#1{ %
   \!goparser{#1}
   \!advancecoord
   \!finished{\!cleanup}{\!doboard}
}

\def\!cleanup{\message{ >done< }\ignorespaces}

\def\!finished#1#2{
\ifnum\!currenty<0    %% done!
   \let\!answer=#1
\else \let\!answer=#2
\fi
\!answer
}

\def\goboardstyle(#1,#2,#3,#4){
\!northindex=#1  %%% order on Bridge Table
\!eastindex=#2
\!southindex=#3
\!westindex=#4
}
\def\goboardul(#1){\setlength{\unitlength}{#1}}

%%
\global\newcount\!boardcount
\newcount\!northindex
\newcount\!eastindex
\newcount\!southindex
\newcount\!westindex
\newcount\!currentx  \newcount\!currenty
\newcount\!hboardsize \newcount\!vboardsize

\def\blackunit(#1){\def\!blackunit{#1}}
\def\whiteunit(#1){\def\!whiteunit{#1}}
\def\starunit(#1){\def\!starunit{#1}}

\newif\if!outofboard

\def\!initpara{
   \!outofboardfalse
   \global\!boardcount=0
   \goboardstyle(1,1,1,1)

% dimmention
   \thicklines %% from latex
   \blackunit(1)
   \whiteunit(1)
   \starunit(0.3)
   \goboardul(5mm)

   \def\!texttoken{} 
}
\!initpara

\def\!advancecoord{
\ifnum\!currentx<\!hboardsize
   \advance\!currentx by 1 
\else 
    \ifnum\!currentx=\!hboardsize
       \ifnum\!currenty>\!vboardsize \!nextline
       \else \ifnum\!currenty=0 \!nextline
              \else \ifnum\!eastindex<2 \!nextline
                    \else \advance\!currentx by 1 
                    \fi
              \fi
       \fi
    \else \!nextline
    \fi
\fi
}

\def\!nextline{
   \message{ > \the\!currenty }
   \advance\!currenty-1 
   \ifnum\!westindex<2 \!currentx=1
      \else \!currentx=0
   \fi

%%% adjustment
\ifnum\!currenty=0
   \ifnum\!southindex<2 \!currenty=-1
      \else \!currenty=0 \!currentx=1 %%% first at (1,0)
   \fi
\fi
}

\def\!checkposition
{
\!outofboardtrue
\ifnum\!currenty>\!vboardsize 
\else \ifnum\!currenty<1
      \else   
        \ifnum\!currentx>\!hboardsize
        \else \ifnum\!currentx<1 
              \else \!outofboardfalse
              \fi
        \fi
      \fi
\fi
}

\def\!resetpara{
\global\advance\!boardcount by 1

\!currentx=1 \!currenty=\!vboardsize
%% adjust
\ifnum\!northindex>1
  \advance \!currenty by 1
\else
    \ifnum\!westindex>1
       \advance \!currentx by -1
    \fi
\fi

}

\newenvironment{goboard}[2]{
      \!hboardsize=#1\!vboardsize=#2
      \!resetpara
   \!gowrite{Go board \the\!boardcount :  Begin}
   \newcount \!realhsize \!realhsize=\!vboardsize
   \newcount \!realvsize \!realvsize=\!vboardsize
   \advance\!realhsize 2 \advance\!realvsize 2
   \begin{picture}(\!realhsize, \!realvsize)(0,0)
      \catcode`\#=11    %% hash mark for black stone
      \!doboard
}{\end{picture}
      \catcode`\#=6 %% back to hash mark
      \!gowrite{Go board \the\!boardcount : End}
}

%%% GoTeX parser
\def\setnulltoken(#1){\let\!nulltoken=#1}
\let\!nulltoken==%
\let\!nostonetoken=.%
\let\!whitetoken=O%
\let\!blacktokena=@%
\catcode`\#=11 %% hash mark
\let\!blacktokenb=#%
\catcode`\#=6  %% back to hash mark
\let\!startoken=+%
\let\!vlinetoken=|%
\let\!hlinetoken=-%

\def\!goparser#1{
\ifx#1\!nulltoken % \relax
   \else 
      \ifx#1\!nostonetoken \shownostone(\!currentx,\!currenty)
         \else \!pwhite#1 
      \fi
\fi
}

\def\!pwhite#1{
\ifx#1\!whitetoken \showwhite(\!currentx,\!currenty)
\else \!pblack#1 \fi
}

\def\!pblack#1{
\ifx#1\!blacktokena \showblack(\!currentx,\!currenty)
   \else \ifx#1\!blacktokenb \showblack(\!currentx,\!currenty)
          \else \!pstar#1
          \fi
\fi
}

\def\!pstar#1{
\ifx#1\!startoken 
  \shownostone(\!currentx,\!currenty)
   \showstar(\!currentx,\!currenty)
\else \!pvline#1
\fi
}

\def\!pvline#1{
\ifx#1\!vlinetoken
  \showvline\!currentx\!currenty
\else \!phline#1
\fi
}

\def\!phline#1{
\ifx#1\!hlinetoken
  \showhline\!currentx\!currenty
\else \marknostone#1
\fi
}

\def\marknostone#1{
\showtext(\!currentx, \!currenty,#1)
\shownostone(\!currentx,\!currenty)
}

%% ************************************************
%% Board Images:
%% ************************************************

%% White Stone
\def\showwhite(#1,#2){
\put(#1,#2){\circle{\!whiteunit}}
}

%% Black Stone
\def\showblack(#1,#2){
\put(#1,#2){\circle*{\!blackunit}}
}

%% Mark
\def\showtext(#1,#2,#3){
\put(#1, #2){\kern-0.6ex\lower.6ex\hbox{#3}}
%% \put(#1, #2){#3}
}

%% Star point
\def\showstar(#1,#2){
\put(#1,#2){\circle*{\!starunit}}
}

%% no stone in the middle and edges

\def\shownostone(#1,#2){
%% to north
\ifnum\!northindex>0   %%
 \ifnum\!currenty<\!vboardsize
     \put(#1, #2){\line(0,1){0.5}}
  \fi
\else \put(#1, #2){\line(0,1){0.5}}
\fi

%% to South
\ifnum\!southindex>0
  \ifnum\!currenty>1
     \put(#1, #2){\line(0,-1){0.5}}
  \fi
\else \put(#1, #2){\line(0,-1){0.5}}
\fi

%% to east
\ifnum\!eastindex>0
  \ifnum\!currentx<\!hboardsize
     \put(#1, #2){\line(1,0){0.5}}
  \fi
\else \put(#1, #2){\line(1,0){0.5}}
\fi

%% to west
\ifnum\!westindex>0
  \ifnum\!currentx>1
     \put(#1, #2){\line(-1,0){0.5}}
  \fi
\else \put(#1, #2){\line(-1,0){0.5}}
\fi
}

%% horizontal line 1 unit to east
\def\showhline#1#2{
   \put(#1, #2){\line(1,0){1}}
}
%% vertical line to south
\def\showvline#1#2{
   \put(#1, #2){\line(0,-1){1}}
}

%%% Horizontal line from (#1, #2) with length #3
\def\Hline#1#2#3{
   \put(#1, #2){\line(1,0){#3}}
}
\def\HLINE#1#2{
   \multiput(1,#1)(0,1){#2}{\line(1,0){18}}
}

%%% Vertical Line from (#1, #2) with length #3
\def\Vline#1#2#3{
   \put(#1, #2){\line(0,1){#3}}
}
\def\VLINE#1#2{
   \multiput(#1,1)(1,0){#2}{\line(0,1){18}}
}

\catcode`!=12 % 
%% ***************************************************************
%% This is the end of GoTeX
%% ***************************************************************

