%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% File          : eigo.sty by Marc Levivier (with AI assistance)
%% Version       : 1 
%% Date          : 2025-09-06
%% Purpose       : Go diagrams with colors, symbols and transformations
%% License       : LaTeX Project Public License v1.3c or later
%% Inspiration   : igo.sty (by Etienne Dupuis)
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% 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.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work consists of the files: eigo.sty, eigo.tex, eigo.pdf, README.md
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{eigo}[2025/09/06 v1 Go diagrams]

% Ensure proper encoding for special characters
\RequirePackage{textcomp}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Package options
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Ensure LuaLaTeX compatibility
\RequirePackage{ifluatex}
\ifluatex
\RequirePackage{luatex85} % For better LuaTeX compatibility
\else
\RequirePackage[T1]{fontenc}
\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% UTF-8 character support for LuaLaTeX
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\RequirePackage{newunicodechar}

% Define common characters that may appear in Go documentation
\newunicodechar{×}{\ensuremath{\times}}  % Multiplication sign
\newunicodechar{°}{\textdegree}          % Degree symbol
\newunicodechar{–}{\textendash}          % En dash
\newunicodechar{—}{\textemdash}          % Em dash
\newunicodechar{“}{\textquotedblleft}    % Left double quotation mark
\newunicodechar{”}{\textquotedblright}   % Right double quotation mark
\newunicodechar{‘}{\textquoteleft}       % Left single quotation mark
\newunicodechar{’}{\textquoteright}      % Right single quotation mark

% Additional characters that might be useful
\newunicodechar{±}{\ensuremath{\pm}}     % Plus-minus sign
\newunicodechar{≤}{\ensuremath{\leq}}    % Less than or equal to
\newunicodechar{≥}{\ensuremath{\geq}}    % Greater than or equal to
\newunicodechar{≠}{\ensuremath{\neq}}    % Not equal to

\newif\ifeigo@color
\newif\ifeigo@frame
\newif\ifeigo@coords
\DeclareOption{color}{\eigo@colortrue}
\DeclareOption{bw}{\eigo@colorfalse}
\DeclareOption{frame}{\eigo@frametrue}
\DeclareOption{noframe}{\eigo@framefalse}
\DeclareOption{coords}{\eigo@coordstrue}
\DeclareOption{nocoords}{\eigo@coordsfalse}
\DeclareOption{debug}{\def\eigo@debug{true}}

% Default options
\ExecuteOptions{color,noframe,nocoords}
\ProcessOptions\relax

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Required packages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\RequirePackage{tikz}
\RequirePackage{xcolor}
\RequirePackage{calc}
\RequirePackage{ifthen}
\RequirePackage{pgffor}

% TikZ libraries needed
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{shapes.misc}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Global variables and counters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Board size (2-50, default 19)
\newcounter{eigo@size}
\setcounter{eigo@size}{19}

% Current board number (for multiple boards)
\newcounter{eigo@boardnum}
\setcounter{eigo@boardnum}{1}

% Unit size for one intersection
\newlength{\eigo@unit}
\setlength{\eigo@unit}{8mm}

% Temporary variables
\newcounter{eigo@tempcol}
\newcounter{eigo@temprow}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Configuration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Coordinate text size (relative to unit size)
\def\eigo@coord@scale{0.8}

% Coordinate text color
\definecolor{eigo@coord@color}{RGB}{0,0,0}

% Distance from board edge (in units)
\def\eigo@coord@distance{0.7}

% Background color variable
\definecolor{eigo@board@bg}{RGB}{255,255,255}

% User commands to customize coordinates
\newcommand{\eigocoordscale}[1]{\def\eigo@coord@scale{#1}}
\newcommand{\eigocoorddistance}[1]{\def\eigo@coord@distance{#1}}

% Extension length for partial boards
\def\eigo@extend{0.3}

% Extension style
\def\eigo@extension@style{eigo line}

% Border line width
\def\eigo@border@width{2pt}

% User commands to customize all stone colors - CSV parsing version
\newcommand{\eigoblackcolor}[1]{\eigo@parse@csv{eigo@black}{#1}}
\newcommand{\eigowhitecolor}[1]{\eigo@parse@csv{eigo@white}{#1}}
\newcommand{\eigoredcolor}[1]{\eigo@parse@csv{eigo@red}{#1}}
\newcommand{\eigobluecolor}[1]{\eigo@parse@csv{eigo@blue}{#1}}
\newcommand{\eigogreencolor}[1]{\eigo@parse@csv{eigo@green}{#1}}
% New customizable colors
\newcommand{\eigocustomonecolor}[1]{\eigo@parse@csv{eigo@customone}{#1}}
\newcommand{\eigocustomtwocolor}[1]{\eigo@parse@csv{eigo@customtwo}{#1}}

% CSV parser helper macro
\def\eigo@parse@csv#1#2{\eigo@parse@csv@aux{#1}#2,\relax}
\def\eigo@parse@csv@aux#1#2,#3,#4,#5\relax{\definecolor{#1}{RGB}{#2,#3,#4}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Debug utilities
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\eigo@debug@message#1{%
	\ifdefined\eigo@debug%
	\typeout{EIGO DEBUG: #1}%
	\fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Color definitions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\definecolor{eigo@black}{RGB}{0,0,0}
\definecolor{eigo@white}{RGB}{255,255,255}
\definecolor{eigo@red}{RGB}{200,0,0}
\definecolor{eigo@blue}{RGB}{0,0,200}
\definecolor{eigo@green}{RGB}{0,150,0}
% Default values for custom colors
\definecolor{eigo@customone}{RGB}{128,0,128}  % Purple par défaut
\definecolor{eigo@customtwo}{RGB}{255,165,0}  % Orange par défaut

\definecolor{eigo@background}{RGB}{245,222,179}
\definecolor{eigo@lines}{RGB}{0,0,0}
\definecolor{eigo@border}{RGB}{139,69,19}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Coordinate system functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Convert column number to letter (1=a, 2=b, ..., skipping 'i')
\def\eigo@num@to@col#1{%
	\ifcase#1\relax%
	?% 0
	\or a\or b\or c\or d\or e\or f\or g\or h% 1-8
	\or j\or k\or l\or m\or n\or o\or p\or q\or r\or s\or t% 9-19
	\or u\or v\or w\or x\or y\or z% 20-25
	\else ?% >25
	\fi%
}

\def\eigo@col@to@num#1{%
	\def\eigo@test@char{#1}%
	\def\eigo@char@a{a}\ifx\eigo@test@char\eigo@char@a\def\eigo@col@result{1}\else%
	\def\eigo@char@b{b}\ifx\eigo@test@char\eigo@char@b\def\eigo@col@result{2}\else%
	\def\eigo@char@c{c}\ifx\eigo@test@char\eigo@char@c\def\eigo@col@result{3}\else%
	\def\eigo@char@d{d}\ifx\eigo@test@char\eigo@char@d\def\eigo@col@result{4}\else%
	\def\eigo@char@e{e}\ifx\eigo@test@char\eigo@char@e\def\eigo@col@result{5}\else%
	\def\eigo@char@f{f}\ifx\eigo@test@char\eigo@char@f\def\eigo@col@result{6}\else%
	\def\eigo@char@g{g}\ifx\eigo@test@char\eigo@char@g\def\eigo@col@result{7}\else%
	\def\eigo@char@h{h}\ifx\eigo@test@char\eigo@char@h\def\eigo@col@result{8}\else%
	\def\eigo@char@j{j}\ifx\eigo@test@char\eigo@char@j\def\eigo@col@result{9}\else%
	\def\eigo@char@k{k}\ifx\eigo@test@char\eigo@char@k\def\eigo@col@result{10}\else%
	\def\eigo@char@l{l}\ifx\eigo@test@char\eigo@char@l\def\eigo@col@result{11}\else%
	\def\eigo@char@m{m}\ifx\eigo@test@char\eigo@char@m\def\eigo@col@result{12}\else%
	\def\eigo@char@n{n}\ifx\eigo@test@char\eigo@char@n\def\eigo@col@result{13}\else%
	\def\eigo@char@o{o}\ifx\eigo@test@char\eigo@char@o\def\eigo@col@result{14}\else%
	\def\eigo@char@p{p}\ifx\eigo@test@char\eigo@char@p\def\eigo@col@result{15}\else%
	\def\eigo@char@q{q}\ifx\eigo@test@char\eigo@char@q\def\eigo@col@result{16}\else%
	\def\eigo@char@r{r}\ifx\eigo@test@char\eigo@char@r\def\eigo@col@result{17}\else%
	\def\eigo@char@s{s}\ifx\eigo@test@char\eigo@char@s\def\eigo@col@result{18}\else%
	\def\eigo@char@t{t}\ifx\eigo@test@char\eigo@char@t\def\eigo@col@result{19}\else%
	\def\eigo@char@u{u}\ifx\eigo@test@char\eigo@char@u\def\eigo@col@result{20}\else%
	\def\eigo@char@v{v}\ifx\eigo@test@char\eigo@char@v\def\eigo@col@result{21}\else%
	\def\eigo@char@w{w}\ifx\eigo@test@char\eigo@char@w\def\eigo@col@result{22}\else%
	\def\eigo@char@x{x}\ifx\eigo@test@char\eigo@char@x\def\eigo@col@result{23}\else%
	\def\eigo@char@y{y}\ifx\eigo@test@char\eigo@char@y\def\eigo@col@result{24}\else%
	\def\eigo@char@z{z}\ifx\eigo@test@char\eigo@char@z\def\eigo@col@result{25}\else%
	\def\eigo@col@result{0}%
	\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi%
}

\def\eigo@parse@position#1{%
	\expandafter\eigo@parse@position@aux#1\relax%
}

\def\eigo@parse@position@aux#1#2\relax{%
	\eigo@col@to@num{#1}%
	\edef\eigo@parsed@col{\eigo@col@result}%
	\edef\eigo@parsed@row{#2}%
	\eigo@debug@message{Parsed position: col=\eigo@parsed@col, row=\eigo@parsed@row}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Storage system
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\eigo@set@stone#1#2#3{%
	\expandafter\gdef\csname eigo@stone@\the\c@eigo@boardnum @#1@#2\endcsname{#3}%
}

\def\eigo@set@num#1#2#3{%
	\ifnum#3>0\relax%
	\expandafter\gdef\csname eigo@num@\the\c@eigo@boardnum @#1@#2\endcsname{#3}%
	\else%
	\expandafter\global\expandafter\let\csname eigo@num@\the\c@eigo@boardnum @#1@#2\endcsname\undefined%
	\fi%
}

\def\eigo@set@symbol#1#2#3{%
	\expandafter\gdef\csname eigo@symbol@\the\c@eigo@boardnum @#1@#2\endcsname{#3}%
}

\def\eigo@get@stone#1#2{%
	\ifcsname eigo@stone@\the\c@eigo@boardnum @#1@#2\endcsname%
	\csname eigo@stone@\the\c@eigo@boardnum @#1@#2\endcsname%
	\else%
	\empty%
	\fi%
}

\def\eigo@get@num#1#2{%
	\ifcsname eigo@num@\the\c@eigo@boardnum @#1@#2\endcsname%
	\csname eigo@num@\the\c@eigo@boardnum @#1@#2\endcsname%
	\else%
	\empty%
	\fi%
}

\def\eigo@get@symbol#1#2{%
	\ifcsname eigo@symbol@\the\c@eigo@boardnum @#1@#2\endcsname%
	\csname eigo@symbol@\the\c@eigo@boardnum @#1@#2\endcsname%
	\else%
	\empty%
	\fi%
}

\def\eigo@clear@at#1#2{%
	\expandafter\global\expandafter\let\csname eigo@stone@\the\c@eigo@boardnum @#1@#2\endcsname\undefined%
	\expandafter\global\expandafter\let\csname eigo@num@\the\c@eigo@boardnum @#1@#2\endcsname\undefined%
	\expandafter\global\expandafter\let\csname eigo@symbol@\the\c@eigo@boardnum @#1@#2\endcsname\undefined%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Zone parsing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Marker for zone parsing
\def\eigo@zone@end{ZONE_END_MARKER}

\def\eigo@parse@zone#1{%
	\eigo@parse@zone@aux#1,\eigo@zone@end,\relax%
}

\def\eigo@parse@zone@aux#1,#2,#3\relax{%
	\eigo@parse@position{#1}%
	\edef\eigo@zone@min@col{\eigo@parsed@col}%
	\edef\eigo@zone@min@row{\eigo@parsed@row}%
	
	\def\eigo@temp@second{#2}%
	\def\eigo@temp@marker{\eigo@zone@end}%
	\ifx\eigo@temp@second\eigo@temp@marker%
	% Single position - show 5x5 area
	\setcounter{eigo@tempcol}{\eigo@zone@min@col}%
	\addtocounter{eigo@tempcol}{-2}%
	\ifnum\value{eigo@tempcol}<1\relax%
	\setcounter{eigo@tempcol}{1}%
	\fi%
	\edef\eigo@zone@min@col{\arabic{eigo@tempcol}}%
	
	\setcounter{eigo@tempcol}{\eigo@parsed@col}%
	\addtocounter{eigo@tempcol}{2}%
	\ifnum\value{eigo@tempcol}>\value{eigo@size}\relax%
	\setcounter{eigo@tempcol}{\value{eigo@size}}%
	\fi%
	\edef\eigo@zone@max@col{\arabic{eigo@tempcol}}%
	
	\setcounter{eigo@temprow}{\eigo@zone@min@row}%
	\addtocounter{eigo@temprow}{-2}%
	\ifnum\value{eigo@temprow}<1\relax%
	\setcounter{eigo@temprow}{1}%
	\fi%
	\edef\eigo@zone@min@row{\arabic{eigo@temprow}}%
	
	\setcounter{eigo@temprow}{\eigo@parsed@row}%
	\addtocounter{eigo@temprow}{2}%
	\ifnum\value{eigo@temprow}>\value{eigo@size}\relax%
	\setcounter{eigo@temprow}{\value{eigo@size}}%
	\fi%
	\edef\eigo@zone@max@row{\arabic{eigo@temprow}}%
	\else%
	% Two positions
	\eigo@parse@position{#2}%
	\edef\eigo@zone@max@col{\eigo@parsed@col}%
	\edef\eigo@zone@max@row{\eigo@parsed@row}%
	\fi%
	\eigo@debug@message{Zone: (\eigo@zone@min@col,\eigo@zone@min@row) to (\eigo@zone@max@col,\eigo@zone@max@row)}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Board management
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\gobansize}[1]{%
	\ifnum#1<2\relax\setcounter{eigo@size}{2}\else%
	\ifnum#1>25\relax\setcounter{eigo@size}{25}\else%
	\setcounter{eigo@size}{#1}\fi\fi%
	\cleargoban%
}

\newcommand{\usegoban}[1]{\setcounter{eigo@boardnum}{#1}}

\newcommand{\cleargoban}[1][]{%
	\def\eigo@zone@spec{#1}%
	\ifx\eigo@zone@spec\empty%
	% Clear entire board
	\eigo@foreach@position{1}{1}{\value{eigo@size}}{\value{eigo@size}}{%
		\eigo@clear@at%
	}%
	\else%
	% Clear specific zone
	\eigo@parse@zone{#1}%
	\eigo@foreach@position{\eigo@zone@min@col}{\eigo@zone@min@row}%
	{\eigo@zone@max@col}{\eigo@zone@max@row}{%
		\eigo@clear@at%
	}%
	\fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Helper macros to reduce duplication
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Generic foreach over board positions
\def\eigo@foreach@position#1#2#3#4#5{%
	% #1=min col, #2=min row, #3=max col, #4=max row, #5=action
	\edef\eigo@min@x{#1}%
	\edef\eigo@min@y{#2}%
	\edef\eigo@max@x{#3}%
	\edef\eigo@max@y{#4}%
	\foreach \col in {\eigo@min@x,...,\eigo@max@x} {%
		\foreach \row in {\eigo@min@y,...,\eigo@max@y} {%
			#5{\col}{\row}%
		}%
	}%
}

% Generic stone command creator
\def\eigo@define@stone@command#1{%
	\expandafter\newcommand\csname #1stones\endcsname[2][0]{%
		\foreach \pos in {##2} {%
			\eigo@parse@position{\pos}%
			\eigo@set@stone{\eigo@parsed@col}{\eigo@parsed@row}{#1}%
			\eigo@set@num{\eigo@parsed@col}{\eigo@parsed@row}{##1}%
		}%
	}%
}

% Create all stone commands using the helper
\eigo@define@stone@command{red}
\eigo@define@stone@command{blue}
\eigo@define@stone@command{green}
\eigo@define@stone@command{customone}
\eigo@define@stone@command{customtwo}

% Define blackstones and whitestones with explicit commands
\newcommand{\blackstones}[2][0]{%
	\foreach \pos in {#2} {%
		\eigo@parse@position{\pos}%
		\eigo@set@stone{\eigo@parsed@col}{\eigo@parsed@row}{black}%
		\eigo@set@num{\eigo@parsed@col}{\eigo@parsed@row}{#1}%
	}%
}

\newcommand{\whitestones}[2][0]{%
	\foreach \pos in {#2} {%
		\eigo@parse@position{\pos}%
		\eigo@set@stone{\eigo@parsed@col}{\eigo@parsed@row}{white}%
		\eigo@set@num{\eigo@parsed@col}{\eigo@parsed@row}{#1}%
	}%
}

% Clear command
\newcommand{\eigoclear}[1]{%
	\foreach \pos in {#1} {%
		\eigo@parse@position{\pos}%
		\eigo@clear@at{\eigo@parsed@col}{\eigo@parsed@row}%
	}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Automatic numbering system with separate counters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Counters for automatic numbering
\newcounter{eigo@autonumalt}  % For alternating colors
\newcounter{eigo@autonummono} % For single color with increment of 2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% \autoblackfirst - Black stones with automatic numbering and color alternation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\autoblackfirst}[2][1]{%
	\ifnum#1<1\relax%
	% If n=0 or negative, use regular black command
	\blackstones[0]{#2}%
	\else%
	% Start automatic numbering with alternation
	\setcounter{eigo@autonumalt}{#1}%
	\eigo@recursive@autoblack[#1]#2,,\relax%
	\fi%
}

\def\eigo@recursive@autoblack[#1]#2,#3\relax{%
	% Check if #2 is empty (end of list)
	\def\eigo@temp{#2}%
	\ifx\eigo@temp\empty%
	% End of recursion - do nothing
	\else%
	% Check for dash (skip entry)
	\def\eigo@dash{-}%
	\ifx\eigo@temp\eigo@dash%
	% Skip this entry, continue with same color and incremented number
	\setcounter{eigo@autonumalt}{#1}%
	\addtocounter{eigo@autonumalt}{1}%
	\edef\eigo@next@num{\arabic{eigo@autonumalt}}%
	\expandafter\eigo@recursive@autoblack\expandafter[\eigo@next@num]#3\relax%
	\else%
	% Parse and place the stone
	\eigo@parse@position{#2}%
	\eigo@set@stone{\eigo@parsed@col}{\eigo@parsed@row}{black}%
	\eigo@set@num{\eigo@parsed@col}{\eigo@parsed@row}{#1}%
	
	% Prepare for next stone
	\setcounter{eigo@autonumalt}{#1}%
	\addtocounter{eigo@autonumalt}{1}%
	\edef\eigo@next@num{\arabic{eigo@autonumalt}}%
	
	% Check if there are more positions
	\def\eigo@rest{#3}%
	\ifx\eigo@rest\empty%
	% No more positions, end
	\else%
	% Continue with white
	\expandafter\eigo@recursive@autowhite\expandafter[\eigo@next@num]#3\relax%
	\fi%
	\fi%
	\fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% \autowhitefirst - White stones with automatic numbering and color alternation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\autowhitefirst}[2][2]{%
	\ifnum#1<1\relax%
	% If n=0 or negative, use regular white command
	\whitestones[0]{#2}%
	\else%
	% Start automatic numbering with alternation
	\setcounter{eigo@autonumalt}{#1}%
	\eigo@recursive@autowhite[#1]#2,,\relax%
	\fi%
}

\def\eigo@recursive@autowhite[#1]#2,#3\relax{%
	% Check if #2 is empty (end of list)
	\def\eigo@temp{#2}%
	\ifx\eigo@temp\empty%
	% End of recursion - do nothing
	\else%
	% Check for dash (skip entry)
	\def\eigo@dash{-}%
	\ifx\eigo@temp\eigo@dash%
	% Skip this entry, continue with same color and incremented number
	\setcounter{eigo@autonumalt}{#1}%
	\addtocounter{eigo@autonumalt}{1}%
	\edef\eigo@next@num{\arabic{eigo@autonumalt}}%
	\expandafter\eigo@recursive@autowhite\expandafter[\eigo@next@num]#3\relax%
	\else%
	% Parse and place the stone
	\eigo@parse@position{#2}%
	\eigo@set@stone{\eigo@parsed@col}{\eigo@parsed@row}{white}%
	\eigo@set@num{\eigo@parsed@col}{\eigo@parsed@row}{#1}%
	
	% Prepare for next stone
	\setcounter{eigo@autonumalt}{#1}%
	\addtocounter{eigo@autonumalt}{1}%
	\edef\eigo@next@num{\arabic{eigo@autonumalt}}%
	
	% Check if there are more positions
	\def\eigo@rest{#3}%
	\ifx\eigo@rest\empty%
	% No more positions, end
	\else%
	% Continue with black
	\expandafter\eigo@recursive@autoblack\expandafter[\eigo@next@num]#3\relax%
	\fi%
	\fi%
	\fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% \blackn - Black stones with automatic numbering (no alternation, increment of 2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\blackn}[2][0]{%
	\ifnum#1<1\relax%
	% If n=0 or negative, use regular black command
	\blackstones[0]{#2}%
	\else%
	% Start automatic numbering with increment of 2
	\setcounter{eigo@autonummono}{#1}%
	\eigo@recursive@blackn[#1]#2,,\relax%
	\fi%
}

\def\eigo@recursive@blackn[#1]#2,#3\relax{%
	% Check if #2 is empty (end of list)
	\def\eigo@temp{#2}%
	\ifx\eigo@temp\empty%
	% End of recursion - do nothing
	\else%
	% Check for dash (skip entry)
	\def\eigo@dash{-}%
	\ifx\eigo@temp\eigo@dash%
	% Skip this entry, continue with same color and incremented number
	\setcounter{eigo@autonummono}{#1}%
	\addtocounter{eigo@autonummono}{2}%
	\edef\eigo@next@num{\arabic{eigo@autonummono}}%
	\expandafter\eigo@recursive@blackn\expandafter[\eigo@next@num]#3\relax%
	\else%
	% Parse and place the stone
	\eigo@parse@position{#2}%
	\eigo@set@stone{\eigo@parsed@col}{\eigo@parsed@row}{black}%
	\eigo@set@num{\eigo@parsed@col}{\eigo@parsed@row}{#1}%
	
	% Prepare for next stone - increment by 2
	\setcounter{eigo@autonummono}{#1}%
	\addtocounter{eigo@autonummono}{2}%
	\edef\eigo@next@num{\arabic{eigo@autonummono}}%
	
	% Check if there are more positions
	\def\eigo@rest{#3}%
	\ifx\eigo@rest\empty%
	% No more positions, end
	\else%
	% Continue with black (no color alternation)
	\expandafter\eigo@recursive@blackn\expandafter[\eigo@next@num]#3\relax%
	\fi%
	\fi%
	\fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% \whiten - White stones with automatic numbering (no alternation, increment of 2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\whiten}[2][0]{%
	\ifnum#1<1\relax%
	% If n=0 or negative, use regular white command
	\whitestones[0]{#2}%
	\else%
	% Start automatic numbering with increment of 2
	\setcounter{eigo@autonummono}{#1}%
	\eigo@recursive@whiten[#1]#2,,\relax%
	\fi%
}

\def\eigo@recursive@whiten[#1]#2,#3\relax{%
	% Check if #2 is empty (end of list)
	\def\eigo@temp{#2}%
	\ifx\eigo@temp\empty%
	% End of recursion - do nothing
	\else%
	% Check for dash (skip entry)
	\def\eigo@dash{-}%
	\ifx\eigo@temp\eigo@dash%
	% Skip this entry, continue with same color and incremented number
	\setcounter{eigo@autonummono}{#1}%
	\addtocounter{eigo@autonummono}{2}%
	\edef\eigo@next@num{\arabic{eigo@autonummono}}%
	\expandafter\eigo@recursive@whiten\expandafter[\eigo@next@num]#3\relax%
	\else%
	% Parse and place the stone
	\eigo@parse@position{#2}%
	\eigo@set@stone{\eigo@parsed@col}{\eigo@parsed@row}{white}%
	\eigo@set@num{\eigo@parsed@col}{\eigo@parsed@row}{#1}%
	
	% Prepare for next stone - increment by 2
	\setcounter{eigo@autonummono}{#1}%
	\addtocounter{eigo@autonummono}{2}%
	\edef\eigo@next@num{\arabic{eigo@autonummono}}%
	
	% Check if there are more positions
	\def\eigo@rest{#3}%
	\ifx\eigo@rest\empty%
	% No more positions, end
	\else%
	% Continue with white (no color alternation)
	\expandafter\eigo@recursive@whiten\expandafter[\eigo@next@num]#3\relax%
	\fi%
	\fi%
	\fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% END OF AUTOMATIC NUMBERING SYSTEM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Symbol commands
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Special symbol markers (internal)
\def\eigo@triangle{TRIANGLE}
\def\eigo@square{SQUARE}
\def\eigo@circle{CIRCLE}
\def\eigo@cross{CROSS}

% User-accessible symbol commands
\newcommand{\eigotriangle}{\eigo@triangle}
\newcommand{\eigosquare}{\eigo@square}
\newcommand{\eigocircle}{\eigo@circle}
\newcommand{\eigocross}{\eigo@cross}

% Main gobansymbol command
\newcommand{\gobansymbol}[2]{%
	\foreach \pos in {#1} {%
		\eigo@parse@position{\pos}%
		% Clear any stone at this position when placing symbol
		\expandafter\global\expandafter\let\csname eigo@stone@\the\c@eigo@boardnum @\eigo@parsed@col @\eigo@parsed@row\endcsname\undefined%
		\expandafter\global\expandafter\let\csname eigo@num@\the\c@eigo@boardnum @\eigo@parsed@col @\eigo@parsed@row\endcsname\undefined%
		% Set the symbol
		\eigo@set@symbol{\eigo@parsed@col}{\eigo@parsed@row}{#2}%
	}%
}

% Clear all symbols command
\newcommand{\cleargobansymbols}{%
	\eigo@foreach@position{1}{1}{\value{eigo@size}}{\value{eigo@size}}{%
		\eigo@clear@symbol@at%
	}%
}

\def\eigo@clear@symbol@at#1#2{%
	\expandafter\global\expandafter\let\csname eigo@symbol@\the\c@eigo@boardnum @#1@#2\endcsname\undefined%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Geometric transformations - FACTORISED VERSION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Common helper for all transformations
\def\eigo@copy@to@board@zero{%
	\edef\eigo@saved@board{\the\c@eigo@boardnum}%
	% Copy current board to board 0
	\foreach \col in {1,...,\value{eigo@size}} {%
		\foreach \row in {1,...,\value{eigo@size}} {%
			\expandafter\global\expandafter\let\csname eigo@stone@0@\col @\row\endcsname\undefined%
			\expandafter\global\expandafter\let\csname eigo@num@0@\col @\row\endcsname\undefined%
			\expandafter\global\expandafter\let\csname eigo@symbol@0@\col @\row\endcsname\undefined%
			% Copy if exists
			\ifcsname eigo@stone@\eigo@saved@board @\col @\row\endcsname%
			\expandafter\global\expandafter\let\csname eigo@stone@0@\col @\row\expandafter\endcsname%
			\csname eigo@stone@\eigo@saved@board @\col @\row\endcsname%
			\fi%
			\ifcsname eigo@num@\eigo@saved@board @\col @\row\endcsname%
			\expandafter\global\expandafter\let\csname eigo@num@0@\col @\row\expandafter\endcsname%
			\csname eigo@num@\eigo@saved@board @\col @\row\endcsname%
			\fi%
			\ifcsname eigo@symbol@\eigo@saved@board @\col @\row\endcsname%
			\expandafter\global\expandafter\let\csname eigo@symbol@0@\col @\row\expandafter\endcsname%
			\csname eigo@symbol@\eigo@saved@board @\col @\row\endcsname%
			\fi%
		}%
	}%
}

% Common helper for applying transformation
\def\eigo@apply@transformation#1#2{%
	% #1 = transformation function for coordinates
	% #2 = transformation name for debug
	\eigo@copy@to@board@zero%
	\cleargoban%
	% Apply transformation to all positions
	\foreach \col in {1,...,\value{eigo@size}} {%
		\foreach \row in {1,...,\value{eigo@size}} {%
			% Calculate new position using the provided transformation
			#1{\col}{\row}%
			% Copy from board 0 to current board with new position
			\ifcsname eigo@stone@0@\col @\row\endcsname%
			\expandafter\global\expandafter\let\csname eigo@stone@\eigo@saved@board @\eigo@newcol @\eigo@newrow\expandafter\endcsname%
			\csname eigo@stone@0@\col @\row\endcsname%
			\fi%
			\ifcsname eigo@num@0@\col @\row\endcsname%
			\expandafter\global\expandafter\let\csname eigo@num@\eigo@saved@board @\eigo@newcol @\eigo@newrow\expandafter\endcsname%
			\csname eigo@num@0@\col @\row\endcsname%
			\fi%
			\ifcsname eigo@symbol@0@\col @\row\endcsname%
			\expandafter\global\expandafter\let\csname eigo@symbol@\eigo@saved@board @\eigo@newcol @\eigo@newrow\expandafter\endcsname%
			\csname eigo@symbol@0@\col @\row\endcsname%
			\fi%
		}%
	}%
	% Clean up board 0
	\setcounter{eigo@boardnum}{0}%
	\cleargoban%
	\setcounter{eigo@boardnum}{\eigo@saved@board}%
	\eigo@debug@message{Applied transformation: #2}%
}

% Coordinate transformation functions
\def\eigo@transform@ninety#1#2{%
	\edef\eigo@newcol{#2}%
	\setcounter{eigo@temprow}{\value{eigo@size}}%
	\addtocounter{eigo@temprow}{1}%
	\addtocounter{eigo@temprow}{-#1}%
	\edef\eigo@newrow{\arabic{eigo@temprow}}%
}

\def\eigo@transform@oneeighty#1#2{%
	\setcounter{eigo@tempcol}{\value{eigo@size}}%
	\addtocounter{eigo@tempcol}{1}%
	\addtocounter{eigo@tempcol}{-#1}%
	\edef\eigo@newcol{\arabic{eigo@tempcol}}%
	\setcounter{eigo@temprow}{\value{eigo@size}}%
	\addtocounter{eigo@temprow}{1}%
	\addtocounter{eigo@temprow}{-#2}%
	\edef\eigo@newrow{\arabic{eigo@temprow}}%
}

\def\eigo@transform@twoseventy#1#2{%
	\setcounter{eigo@tempcol}{\value{eigo@size}}%
	\addtocounter{eigo@tempcol}{1}%
	\addtocounter{eigo@tempcol}{-#2}%
	\edef\eigo@newcol{\arabic{eigo@tempcol}}%
	\edef\eigo@newrow{#1}%
}

\def\eigo@transform@horizontal#1#2{%
	\setcounter{eigo@tempcol}{\value{eigo@size}}%
	\addtocounter{eigo@tempcol}{1}%
	\addtocounter{eigo@tempcol}{-#1}%
	\edef\eigo@newcol{\arabic{eigo@tempcol}}%
	\edef\eigo@newrow{#2}%
}

\def\eigo@transform@vertical#1#2{%
	\edef\eigo@newcol{#1}%
	\setcounter{eigo@temprow}{\value{eigo@size}}%
	\addtocounter{eigo@temprow}{1}%
	\addtocounter{eigo@temprow}{-#2}%
	\edef\eigo@newrow{\arabic{eigo@temprow}}%
}

\def\eigo@transform@diagonal#1#2{%
	\edef\eigo@newcol{#2}%
	\edef\eigo@newrow{#1}%
}

% Main transformation commands using the factorised approach
\def\eigo@rotate@ninety{%
	\eigo@apply@transformation{\eigo@transform@ninety}{rotate 90°}%
}

\def\eigo@rotate@oneeighty{%
	\eigo@apply@transformation{\eigo@transform@oneeighty}{rotate 180°}%
}

\def\eigo@rotate@twoseventy{%
	\eigo@apply@transformation{\eigo@transform@twoseventy}{rotate 270°}%
}

\def\eigo@mirror@horizontal{%
	\eigo@apply@transformation{\eigo@transform@horizontal}{mirror horizontal}%
}

\def\eigo@mirror@vertical{%
	\eigo@apply@transformation{\eigo@transform@vertical}{mirror vertical}%
}

\def\eigo@mirror@diagonal{%
	\eigo@apply@transformation{\eigo@transform@diagonal}{mirror diagonal}%
}

% Swap colors (unchanged as it doesn't use the transformation system)
\def\eigo@swap@colors{%
	\foreach \col in {1,...,\value{eigo@size}} {%
		\foreach \row in {1,...,\value{eigo@size}} {%
			\edef\eigo@current@stone{\eigo@get@stone{\col}{\row}}%
			\def\eigo@test@black{black}%
			\def\eigo@test@white{white}%
			\ifx\eigo@current@stone\eigo@test@black%
			\eigo@set@stone{\col}{\row}{white}%
			\else\ifx\eigo@current@stone\eigo@test@white%
			\eigo@set@stone{\col}{\row}{black}%
			\fi\fi%
		}%
	}%
}

% User interface commands (unchanged)
\newcommand{\eigorotate}[1]{%
	\ifnum#1=90\relax%
	\eigo@rotate@ninety%
	\else\ifnum#1=180\relax%
	\eigo@rotate@oneeighty%
	\else\ifnum#1=270\relax%
	\eigo@rotate@twoseventy%
	\else%
	\PackageError{eigo}{Invalid rotation angle #1}{Use 90, 180, or 270}%
	\fi\fi\fi%
}

\newcommand{\eigomirrorh}{\eigo@mirror@horizontal}
\newcommand{\eigomirrorv}{\eigo@mirror@vertical}
\newcommand{\eigomirrordiag}{\eigo@mirror@diagonal}
\newcommand{\eigoswapcolors}{\eigo@swap@colors}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Fine control of display size
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% User command for fine size control
\newcommand{\eigounitsize}[1]{%
	\setlength{\eigo@unit}{#1}%
	\eigo@debug@message{Unit size set to #1}%
}

% Enhanced size commands with debug info
\newcommand{\smallgoban}{%
	\setlength{\eigo@unit}{6mm}%
	\eigo@debug@message{Small goban selected (6mm)}%
}

\newcommand{\normalgoban}{%
	\setlength{\eigo@unit}{8mm}%
	\eigo@debug@message{Normal goban selected (8mm)}%
}

\newcommand{\largegoban}{%
	\setlength{\eigo@unit}{12mm}%
	\eigo@debug@message{Large goban selected (12mm)}%
}

% Helper command to get current unit size (for debugging/info)
\newcommand{\theeigountisize}{%
	\the\eigo@unit%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Size validation and helpers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Internal: Validate unit size is reasonable
\def\eigo@validate@unitsize{%
	\ifdim\eigo@unit<2mm\relax%
	\PackageWarning{eigo}{Unit size \the\eigo@unit\space is very small, diagrams may be unreadable}%
	\fi%
	\ifdim\eigo@unit>20mm\relax%
	\PackageWarning{eigo}{Unit size \the\eigo@unit\space is very large, diagrams may not fit on page}%
	\fi%
}

% Enhanced \eigounitsize with validation
\renewcommand{\eigounitsize}[1]{%
	\setlength{\eigo@unit}{#1}%
	\eigo@validate@unitsize%
	\eigo@debug@message{Unit size set to #1}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Convenient size presets (in addition to small/normal/large)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Extra size presets for common needs
\newcommand{\tinygoban}{\eigounitsize{4mm}}        % For large overviews
\newcommand{\hugegoban}{\eigounitsize{16mm}}       % For detailed analysis
\newcommand{\massivegoban}{\eigounitsize{20mm}}    % Maximum practical size

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Size information commands
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Command to display current settings (useful for documents)
\newcommand{\eigosizeinfo}{%
	Current goban unit size: \theeigountisize%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TikZ Styles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\tikzset{
	eigo goban/.style={
		x=\eigo@unit, 
		y=\eigo@unit,
		every node/.style={inner sep=0pt, outer sep=0pt}
	},
	eigo line/.style={
		color=eigo@lines, 
		line width=0.8pt
	},
	eigo border/.style={
		color=eigo@lines, 
		line width=\eigo@border@width   
	},
	eigo coord/.style={
		color=eigo@coord@color,
		font=\footnotesize,
		inner sep=2pt
	},
	eigo black stone/.style={
		circle, 
		fill=eigo@black, 
		draw=black, 
		minimum size=0.85\eigo@unit, 
		inner sep=0pt, outer sep=0pt,
		text=white, font=\fontsize{0.4\eigo@unit}{0.8\eigo@unit}\selectfont
	},
	eigo white stone/.style={
		circle, 
		fill=eigo@white, 
		draw=black, 
		line width=0.8pt,
		minimum size=0.85\eigo@unit, 
		inner sep=0pt, outer sep=0pt,
		text=black, font=\fontsize{0.4\eigo@unit}{0.8\eigo@unit}\selectfont
	},
	eigo red stone/.style={
		circle, 
		fill=eigo@red, 
		draw=black, 
		line width=0.8pt,
		minimum size=0.85\eigo@unit, 
		inner sep=0pt, outer sep=0pt,
		text=white, font=\fontsize{0.4\eigo@unit}{0.8\eigo@unit}\selectfont
	},
	eigo blue stone/.style={
		circle, 
		fill=eigo@blue, 
		draw=black, 
		line width=0.8pt,
		minimum size=0.85\eigo@unit, 
		inner sep=0pt, outer sep=0pt,
		text=white, font=\fontsize{0.4\eigo@unit}{0.8\eigo@unit}\selectfont
	},
	eigo green stone/.style={
		circle, 
		fill=eigo@green, 
		draw=black, 
		line width=0.8pt,
		minimum size=0.85\eigo@unit, 
		inner sep=0pt, outer sep=0pt,
		text=white, font=\fontsize{0.4\eigo@unit}{0.8\eigo@unit}\selectfont
	},
	eigo customone stone/.style={
		circle, 
		fill=eigo@customone, 
		draw=black, 
		line width=0.8pt,
		minimum size=0.85\eigo@unit, 
		inner sep=0pt, outer sep=0pt,
		text=white, font=\fontsize{0.4\eigo@unit}{0.8\eigo@unit}\selectfont
	},
	eigo customtwo stone/.style={
		circle, 
		fill=eigo@customtwo, 
		draw=black, 
		line width=0.8pt,
		minimum size=0.85\eigo@unit, 
		inner sep=0pt, outer sep=0pt,
		text=white, font=\fontsize{0.4\eigo@unit}{0.8\eigo@unit}\selectfont
	},
	eigo hoshi/.style={
		circle, 
		fill=black, 
		minimum size=0.15\eigo@unit
	},
	eigo background/.style={
		fill=eigo@background,
		draw=eigo@border,
		line width=1pt
	},
	eigo symbol/.style={
		font=\normalsize\bfseries,
		inner sep=0pt, 
		outer sep=0pt,
		text=black,
		fill=eigo@board@bg,
		draw=none,
		rectangle,
		minimum width=0.6\eigo@unit,
		minimum height=0.6\eigo@unit
	},
	eigo triangle/.style={
		regular polygon,
		regular polygon sides=3,
		draw=black,
		fill=none,
		line width=1pt,
		minimum size=0.5\eigo@unit,
		inner sep=0pt,
		outer sep=0pt
	},
	eigo square symbol/.style={
		draw=black,
		fill=none,
		line width=1pt,
		minimum size=0.45\eigo@unit,
		inner sep=0pt,
		outer sep=0pt
	},
	eigo circle symbol/.style={
		circle,
		draw=black,
		fill=none,
		line width=1pt,
		minimum size=0.45\eigo@unit,
		inner sep=0pt,
		outer sep=0pt
	},
	eigo cross/.style={
		cross out,
		draw=black,
		line width=1.5pt,
		minimum size=0.4\eigo@unit,
		inner sep=0pt,
		outer sep=0pt
	}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Hoshi points
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\eigo@is@hoshi#1#2{%
	\def\eigo@hoshi@result{0}%
	\ifnum\value{eigo@size}=19\relax%
	\eigo@check@hoshi@nineteen{#1}{#2}%
	\else\ifnum\value{eigo@size}=13\relax%
	\eigo@check@hoshi@thirteen{#1}{#2}%
	\else\ifnum\value{eigo@size}=9\relax%
	\eigo@check@hoshi@nine{#1}{#2}%
	\fi\fi\fi%
}

\def\eigo@check@hoshi@at#1#2#3#4{%
	\ifnum#3=#1\relax%
	\ifnum#4=#2\relax%
	\def\eigo@hoshi@result{1}%
	\fi%
	\fi%
}

\def\eigo@check@hoshi@nineteen#1#2{%
	\foreach \hoshi in {%
		{4}{4},{4}{10},{4}{16},%
		{10}{4},{10}{10},{10}{16},%
		{16}{4},{16}{10},{16}{16}%
	} {%
		\expandafter\eigo@check@hoshi@at\hoshi{#1}{#2}%
	}%
}

\def\eigo@check@hoshi@thirteen#1#2{%
	\foreach \hoshi in {{4}{4},{4}{10},{7}{7},{10}{4},{10}{10}} {%
		\expandafter\eigo@check@hoshi@at\hoshi{#1}{#2}%
	}%
}

\def\eigo@check@hoshi@nine#1#2{%
	\foreach \hoshi in {{3}{3},{3}{7},{5}{5},{7}{3},{7}{7}} {%
		\expandafter\eigo@check@hoshi@at\hoshi{#1}{#2}%
	}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Coordinate drawing functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Draw column coordinates (a-t)
\def\eigo@drawcolcoords#1#2#3#4{%
	% #1=min col, #2=min row, #3=max col, #4=max row
	\edef\eigo@coord@minx{#1}%
	\edef\eigo@coord@maxx{#3}%
	\edef\eigo@coord@miny{#2}%
	\edef\eigo@coord@maxy{#4}%
	
	% Bottom coordinates
	\foreach \col in {\eigo@coord@minx,...,\eigo@coord@maxx} {%
		\pgfmathsetmacro{\coordy}{\eigo@coord@miny - \eigo@coord@distance}%
		\node[eigo coord, scale=\eigo@coord@scale] at (\col,\coordy) {\eigo@num@to@col{\col}};%
	}%
	
	% Top coordinates
	\foreach \col in {\eigo@coord@minx,...,\eigo@coord@maxx} {%
		\pgfmathsetmacro{\coordy}{\eigo@coord@maxy + \eigo@coord@distance}%
		\node[eigo coord, scale=\eigo@coord@scale] at (\col,\coordy) {\eigo@num@to@col{\col}};%
	}%
}

% Draw row coordinates (1-19)
\def\eigo@drawrowcoords#1#2#3#4{%
	% #1=min col, #2=min row, #3=max col, #4=max row
	\edef\eigo@coord@minx{#1}%
	\edef\eigo@coord@maxx{#3}%
	\edef\eigo@coord@miny{#2}%
	\edef\eigo@coord@maxy{#4}%
	
	% Left coordinates
	\foreach \row in {\eigo@coord@miny,...,\eigo@coord@maxy} {%
		\pgfmathsetmacro{\coordx}{\eigo@coord@minx - \eigo@coord@distance}%
		\node[eigo coord, scale=\eigo@coord@scale] at (\coordx,\row) {\row};%
	}%
	
	% Right coordinates
	\foreach \row in {\eigo@coord@miny,...,\eigo@coord@maxy} {%
		\pgfmathsetmacro{\coordx}{\eigo@coord@maxx + \eigo@coord@distance}%
		\node[eigo coord, scale=\eigo@coord@scale] at (\coordx,\row) {\row};%
	}%
}

% Main coordinate drawing function
\def\eigo@drawcoords#1#2#3#4{%
	\ifeigo@coords%
	\eigo@drawcolcoords{#1}{#2}{#3}{#4}%
	\eigo@drawrowcoords{#1}{#2}{#3}{#4}%
	\fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Board rendering
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Draw grid
\def\eigo@draw@grid#1#2#3#4{%
	\edef\eigo@grid@minx{#1}%
	\edef\eigo@grid@miny{#2}%
	\edef\eigo@grid@maxx{#3}%
	\edef\eigo@grid@maxy{#4}%
	
	% Interior horizontal lines
	\foreach \row in {\eigo@grid@miny,...,\eigo@grid@maxy} {
		\draw[eigo line] (\eigo@grid@minx,\row) -- (\eigo@grid@maxx,\row);
	}
	% Interior vertical lines
	\foreach \col in {\eigo@grid@minx,...,\eigo@grid@maxx} {
		\draw[eigo line] (\col,\eigo@grid@miny) -- (\col,\eigo@grid@maxy);
	}
	
	% Thick border
	\draw[eigo border] (\eigo@grid@minx,\eigo@grid@miny) rectangle (\eigo@grid@maxx,\eigo@grid@maxy);
}

% Draw partial grid with extensions
\def\eigo@draw@partial@grid#1#2#3#4{%
	\edef\eigo@grid@minx{#1}%
	\edef\eigo@grid@miny{#2}%
	\edef\eigo@grid@maxx{#3}%
	\edef\eigo@grid@maxy{#4}%
	
	% Horizontal lines with extensions
	\foreach \row in {\eigo@grid@miny,...,\eigo@grid@maxy} {
		% Left extension
		\ifnum\eigo@grid@minx>1\relax%
		\draw[\eigo@extension@style] (\eigo@grid@minx-\eigo@extend,\row) -- (\eigo@grid@minx,\row);
		\fi%
		% Main line
		\draw[eigo line] (\eigo@grid@minx,\row) -- (\eigo@grid@maxx,\row);
		% Right extension
		\ifnum\eigo@grid@maxx<\value{eigo@size}\relax%
		\draw[\eigo@extension@style] (\eigo@grid@maxx,\row) -- (\eigo@grid@maxx+\eigo@extend,\row);
		\fi%
	}
	
	% Vertical lines with extensions
	\foreach \col in {\eigo@grid@minx,...,\eigo@grid@maxx} {
		% Bottom extension
		\ifnum\eigo@grid@miny>1\relax%
		\draw[\eigo@extension@style] (\col,\eigo@grid@miny-\eigo@extend) -- (\col,\eigo@grid@miny);
		\fi%
		% Main line
		\draw[eigo line] (\col,\eigo@grid@miny) -- (\col,\eigo@grid@maxy);
		% Top extension
		\ifnum\eigo@grid@maxy<\value{eigo@size}\relax%
		\draw[\eigo@extension@style] (\col,\eigo@grid@maxy) -- (\col,\eigo@grid@maxy+\eigo@extend);
		\fi%
	}
}

% Draw hoshi points
\def\eigo@draw@hoshi#1#2#3#4{%
	\eigo@foreach@position{#1}{#2}{#3}{#4}{%
		\eigo@check@and@draw@hoshi%
	}%
}

\def\eigo@check@and@draw@hoshi#1#2{%
	\eigo@is@hoshi{#1}{#2}%
	\ifnum\eigo@hoshi@result=1\relax%
	\node[eigo hoshi] at (#1,#2) {};%
	\fi%
}

% Draw stones
\def\eigo@draw@stones#1#2#3#4{%
	\eigo@foreach@position{#1}{#2}{#3}{#4}{%
		\eigo@draw@stone@at%
	}%
}

\def\eigo@draw@stone@at#1#2{%
	\edef\eigo@current@stone{\eigo@get@stone{#1}{#2}}%
	\ifx\eigo@current@stone\empty\else%
	\edef\eigo@current@num{\eigo@get@num{#1}{#2}}%
	\ifx\eigo@current@num\empty%
	\node[eigo \eigo@current@stone\space stone] at (#1,#2) {};%
	\else%
	\node[eigo \eigo@current@stone\space stone] at (#1,#2) {\eigo@current@num};%
	\fi%
	\fi%
}

% Draw symbols
\def\eigo@draw@symbols#1#2#3#4{%
	\eigo@foreach@position{#1}{#2}{#3}{#4}{%
		\eigo@draw@symbol@at%
	}%
}

\def\eigo@draw@symbol@at#1#2{%
	\edef\eigo@current@symbol{\eigo@get@symbol{#1}{#2}}%
	\ifx\eigo@current@symbol\empty\else%
	% Check if there's a stone at this position
	\edef\eigo@current@stone{\eigo@get@stone{#1}{#2}}%
	\ifx\eigo@current@stone\empty%
	% No stone, draw symbol on empty intersection
	\def\eigo@test@triangle{TRIANGLE}%
	\def\eigo@test@square{SQUARE}%
	\def\eigo@test@circle{CIRCLE}%
	\def\eigo@test@cross{CROSS}%
	\ifx\eigo@current@symbol\eigo@test@triangle%
	% Draw background rectangle first
	\node[rectangle, fill=eigo@board@bg, draw=none, minimum width=0.6\eigo@unit, minimum height=0.6\eigo@unit] at (#1,#2) {};%
	% Then draw triangle
	\node[eigo triangle] at (#1,#2) {};%
	\else\ifx\eigo@current@symbol\eigo@test@square%
	% Draw background rectangle first
	\node[rectangle, fill=eigo@board@bg, draw=none, minimum width=0.6\eigo@unit, minimum height=0.6\eigo@unit] at (#1,#2) {};%
	% Then draw square
	\node[eigo square symbol] at (#1,#2) {};%
	\else\ifx\eigo@current@symbol\eigo@test@circle%
	% Draw background rectangle first
	\node[rectangle, fill=eigo@board@bg, draw=none, minimum width=0.6\eigo@unit, minimum height=0.6\eigo@unit] at (#1,#2) {};%
	% Then draw circle
	\node[eigo circle symbol] at (#1,#2) {};%
	\else\ifx\eigo@current@symbol\eigo@test@cross%
	% Draw background rectangle first
	\node[rectangle, fill=eigo@board@bg, draw=none, minimum width=0.6\eigo@unit, minimum height=0.6\eigo@unit] at (#1,#2) {};%
	% Then draw cross
	\node[eigo cross] at (#1,#2) {};%
	\else%
	% Regular letter or number
	\node[eigo symbol] at (#1,#2) {\eigo@current@symbol};%
	\fi\fi\fi\fi%
	\fi%
	\fi%
}

% Main board drawing function
\def\eigo@draw@board#1#2#3#4{%
	% Check if we're showing the full board
	\def\eigo@is@fullboard{0}%
	\ifnum#1=1\relax%
	\ifnum#2=1\relax%
	\ifnum#3=\value{eigo@size}\relax%
	\ifnum#4=\value{eigo@size}\relax%
	\def\eigo@is@fullboard{1}%
	\fi%
	\fi%
	\fi%
	\fi%
	
	% Update background color based on settings
	\ifnum\eigo@is@fullboard=1\relax%
	\ifeigo@frame%
	\ifeigo@color%
	% Colored background active
	\definecolor{eigo@board@bg}{RGB}{245,222,179}%
	\else%
	% No color - use white
	\definecolor{eigo@board@bg}{RGB}{255,255,255}%
	\fi%
	\else%
	% No frame - always white
	\definecolor{eigo@board@bg}{RGB}{255,255,255}%
	\fi%
	\else%
	% Partial board - always white
	\definecolor{eigo@board@bg}{RGB}{255,255,255}%
	\fi%
	
	\begin{tikzpicture}[eigo goban]
		% Background/frame only if full board AND frame option enabled
		\ifnum\eigo@is@fullboard=1\relax%
		\ifeigo@frame%
		\ifeigo@color%
		\fill[eigo background] 
		(#1-0.5,#2-0.5) rectangle (#3+0.5,#4+0.5);%
		\else%
		% Just border for B&W mode
		\draw[eigo@border, line width=1pt] 
		(#1-0.5,#2-0.5) rectangle (#3+0.5,#4+0.5);%
		\fi%
		\fi%
		\fi%
		
		% Grid (with extensions for partial boards)
		\ifnum\eigo@is@fullboard=1\relax%
		\eigo@draw@grid{#1}{#2}{#3}{#4}%
		\else%
		\eigo@draw@partial@grid{#1}{#2}{#3}{#4}%
		\fi%
		
		% Hoshi points
		\eigo@draw@hoshi{#1}{#2}{#3}{#4}%
		
		% Symbols (drawn BEFORE stones to be in background)
		\eigo@draw@symbols{#1}{#2}{#3}{#4}%
		
		% Stones
		\eigo@draw@stones{#1}{#2}{#3}{#4}%
		
		% Draw coordinates if enabled
		\eigo@drawcoords{#1}{#2}{#3}{#4}%
	\end{tikzpicture}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% User interface
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\showgoban}[1][a1,s19]{%
	\eigo@parse@zone{#1}%
	\eigo@draw@board{\eigo@zone@min@col}{\eigo@zone@min@row}%
	{\eigo@zone@max@col}{\eigo@zone@max@row}%
}

\newcommand{\showfullgoban}{%
	\edef\eigo@temp@size{\arabic{eigo@size}}%
	\eigo@draw@board{1}{1}{\eigo@temp@size}{\eigo@temp@size}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% User commands for options
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Frame commands
\newcommand{\eigoframe}{\eigo@frametrue}
\newcommand{\eigonoframe}{\eigo@framefalse}

% Coordinate commands
\newcommand{\eigocoords}{\eigo@coordstrue}
\newcommand{\eigonocoords}{\eigo@coordsfalse}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Helpers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\theeigobsize}{\arabic{eigo@size}}
\newcommand{\theeigobnum}{\arabic{eigo@boardnum}}

% Initialize
\normalgoban

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% End of package
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\endinput