%%
%% This is file `hexboard.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% hexboard.dtx  (with options: `hexboard')
%% Copyright (c) 2022 Peter Rowlett and Chris Sangwin
%% 
%% The package is licenced under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0). To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/
\ProvidesPackage{hexboard}[2022/02/19 hexboard 1.0]

\RequirePackage{tikz}
\usetikzlibrary{shapes.geometric}
\RequirePackage{xstring}

\makeatletter
\@ifundefined{c@hexedge}{}
{\PackageWarning{hexboard}{Counter `hexedge' already defined}}
\@ifundefined{c@hexmovecount}{}
{\PackageWarning{hexboard}{Counter `hexmovecount' already defined}}
\@ifundefined{c@hexmoveskips}{}
{\PackageWarning{hexboard}{Counter `hexmoveskips' already defined}}
\@ifundefined{c@hexletternum}{}
{\PackageWarning{hexboard}{Counter `hexletternum' already defined}}
\@ifundefined{hexboard}{}
{\PackageWarning{hexboard}{command `hexboard' already defined}}
\@ifundefined{hexcounter}{}
{\PackageWarning{hexboard}{command `hexcounter' already defined}}
\@ifundefined{hexcellshaded}{}
{\PackageWarning{hexboard}{command `hexcellshaded' already defined}}
\@ifundefined{hexdot}{}
{\PackageWarning{hexboard}{command `hexdot' already defined}}
\@ifundefined{hexconnect}{}
{\PackageWarning{hexboard}{command `hexconnect' already defined}}
\@ifundefined{hexcontent}{}
{\PackageWarning{hexboard}{command `hexcontent' already defined}}
\@ifundefined{hexcell}{}
{\PackageWarning{hexboard}{command `hexcell' already defined}}
\@ifundefined{hexgridnolabel}{}
{\PackageWarning{hexboard}{command `hexgridnolabel' already defined}}
\@ifundefined{hexreducing}{}
{\PackageWarning{hexboard}{command `hexreducing' already defined}}
\@ifundefined{hexshadedsubrow}{}
{\PackageWarning{hexboard}{command `hexshadedsubrow' already defined}}
\@ifundefined{hexgrid}{}
{\PackageWarning{hexboard}{command `hexgrid' already defined}}
\@ifundefined{setcolorA}{}
{\PackageWarning{hexboard}{command `setcolorA' already defined}}
\@ifundefined{setcolorB}{}
{\PackageWarning{hexboard}{command `setcolorB' already defined}}
\@ifundefined{hexscale}{}
{\PackageWarning{hexboard}{command `hexscale' already defined}}
\@ifundefined{xchor}{}
{\PackageWarning{hexboard}{command `xchor' already defined}}
\@ifundefined{ychor}{}
{\PackageWarning{hexboard}{command `ychor' already defined}}
\@ifundefined{colorA}{}
{\PackageWarning{hexboard}{command `colorA' already defined}}
\@ifundefined{colorB}{}
{\PackageWarning{hexboard}{command `colorB' already defined}}
\@ifundefined{hexlinewidth}{}
{\PackageWarning{hexboard}{command `hexlinewidth' already defined}}
\@ifundefined{hexedgewidth}{}
{\PackageWarning{hexboard}{command `hexedgewidth' already defined}}
\@ifundefined{hexthinline}{}
{\PackageWarning{hexboard}{command `hexthinline' already defined}}
\@ifundefined{hexcoord}{}
{\PackageWarning{hexboard}{command `hexcoord' already defined}}
\@ifundefined{hexsize}{}
{\PackageWarning{hexboard}{command `hexsize' already defined}}
\@ifundefined{Ia}{}
{\PackageWarning{hexboard}{command `Ia' already defined}}
\@ifundefined{Ib}{}
{\PackageWarning{hexboard}{command `Ib' already defined}}
\@ifundefined{Iaend}{}
{\PackageWarning{hexboard}{command `Iaend' already defined}}
\@ifundefined{leftofIa}{}
{\PackageWarning{hexboard}{command `leftofIa' already defined}}
\@ifundefined{belowIa}{}
{\PackageWarning{hexboard}{command `belowIa' already defined}}
\@ifundefined{leftofxchor}{}
{\PackageWarning{hexboard}{command `leftofxchor' already defined}}
\@ifundefined{hexgame}{}
{\PackageWarning{hexboard}{command `hexgame' already defined}}
\@ifundefined{hexgamelabels}{}
{\PackageWarning{hexboard}{command `hexgamelabels' already defined}}
\@ifundefined{hexthismover}{}
{\PackageWarning{hexboard}{command `hexthismover' already defined}}
\@ifundefined{hexmove}{}
{\PackageWarning{hexboard}{command `hexmove' already defined}}
\@ifundefined{hexletter}{}
{\PackageWarning{hexboard}{command `hexletter' already defined}}
\@ifundefined{hexsubrow}{}
{\PackageWarning{hexboard}{command `hexsubrow' already defined}}
\@ifundefined{hexskipmove}{}
{\PackageWarning{hexboard}{command `hexskipmove' already defined}}
\@ifundefined{hexmovenumber}{}
{\PackageWarning{hexboard}{command `hexmovenumber' already defined}}
\makeatother

\newcommand{\xchor}{0} % x-coordinate.
\newcommand{\ychor}{0} % y-coordinate.

\newcommand{\colorA}{red}
\newcommand{\colorB}{blue}
\newcommand{\hexlinewidth}{0.07}
\newcommand{\hexedgewidth}{0.15}
\newcommand{\hexthinline}{0.03}
\newcommand{\hexscale}[1]{
    \tikzset{hex/.style={scale=#1,every node/.style={scale=#1}}}
    \pgfmathsetmacro\hexlinewidth{#1 * 0.07}
    \pgfmathsetmacro\hexedgewidth{#1 * 0.15}
    \pgfmathsetmacro\hexthinline{#1 * 0.03}
}
\hexscale{2}

\newcommand{\setcolorA}[1]{\renewcommand{\colorA}{#1}}
\newcommand{\setcolorB}[1]{\renewcommand{\colorB}{#1}}

%% This calculates the values of the hex coordinate grid.
\newcounter{hexletternum}
\newcommand{\hexcoord}[2]{%
    \ifnum0<0#1\relax % is it a number?
    \pgfmathsetmacro\xchor{#1 + 0.5 * #2}
    \else % assume it's a letter
    \setcounter{hexletternum}{0}
    \foreach \hexletter in {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}{%
        \addtocounter{hexletternum}{1}
        \IfEq{\hexletter}{#1}{\breakforeach}{}
    }
    \pgfmathsetmacro\xchor{\value{hexletternum} + 0.5 * #2}
    \fi
    \pgfmathsetmacro\ychor{sqrt(3)/2 * #2}
}

\pgfmathsetmacro\hexsize{2/sqrt(3)} % size of the node we want tikz to draw a hexagon inside

\newcommand{\hexcell}[2]{%
    \hexcoord{#1}{#2}
    \node[regular polygon,draw,regular polygon sides=6,rotate=90,line width=\hexlinewidth em,minimum size=\hexsize em]  at (\xchor em,\ychor em) {};
}

\newcommand{\hexcellshaded}[3][yellow]{%
    \hexcoord{#2}{#3}
    \node[regular polygon,draw,regular polygon sides=6,rotate=90,line width=\hexlinewidth em,minimum size=\hexsize em,fill=#1] at (\xchor em,\ychor em) {};
}

\newcommand{\thiscolor}{\colorB}
\newcommand{\hexcounter}[3]{%
    \renewcommand{\thiscolor}{\colorB}
    \def\testA{A}
    \def\argiii{#3}
    \if\argiii\testA
    \renewcommand{\thiscolor}{\colorA}
    \fi

    \hexcoord{#1}{#2}
    \draw[\thiscolor,fill=\thiscolor] (\xchor em,\ychor em) circle (0.35em);
}
\newcommand{\hexcounterlabel}[4]{%
    \hexcounter{#1}{#2}{#3}
    \hexcontent[white]{#1}{#2}{#4}
}

\newcommand{\hexdot}[2]{%
    \hexcoord{#1}{#2}
    \draw[fill] (\xchor em,\ychor em) circle (0.1em);
}

\newcommand{\hexcontent}[4][black]{%
    \hexcoord{#2}{#3}
    \node[text=#1] at (\xchor em,\ychor em) {{\sf\scriptsize #4}};
}

\newcommand{\hexconnect}[4]{%
    \hexcoord{#1}{#2}
    \node[minimum size=0,inner sep=0,outer sep=0] (A) at (\xchor em,\ychor em) {};
    \hexcoord{#3}{#4}
    \node[minimum size=0,inner sep=0,outer sep=0] (B) at (\xchor em,\ychor em) {};
    \draw[line width=\hexthinline em,line cap=round] (A) -- (B);
}

\newcommand{\hexgridnolabel}[2]{

    \foreach \Ia in {1,...,#1}{
        % Place the hex cells.
        \foreach \Ib in {1,...,#2}{
            \hexcell{\Ia}{\Ib}
        }
    }
    \ifnum#1<3 % don't loop
    \ifnum#1=2
    % Coloured boundary
    \hexcoord{1}{1}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom edge
    \hexcoord{1}{#2}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,0.288675 em) -- (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top edge
    %        \else % 1, hopefully
    %            % Coloured boundary
    %            \hexcoord{1}{1}
    %            \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom edge
    %            \hexcoord{1}{#2}
    %            \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,0.288675 em) -- (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top edge
    \fi
    \else
    \pgfmathsetmacro\Iaend{#1-1}
    \foreach \Ia in {2,...,\Iaend}{ % top to bottom player A
        % Coloured boundary
        \hexcoord{\Ia}{1}
        \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom edge
        \hexcoord{\Ia}{#2}
        \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,0.288675 em) -- (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top edge
    }
    \fi

    % Awkward corners

    \hexcoord{1}{#2}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=butt] (-0.25 em,0.4330125 em) -- (0 em,0.577350 em);
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top left colorA

    \ifnum#1>1
    \hexcoord{#1}{#2}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=butt] (-0.5 em,0.288675 em) -- (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top right colorA
    \hexcoord{1}{1}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom left colorA
    \fi

    \hexcoord{#1}{1}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em); % top right colorA
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=butt] (0.25 em,-0.4330125 em) -- (0 em,-0.577350 em); % awkward bit

    \ifnum#2<3 % don't loop
    \ifnum#2=2
    % Coloured boundary
    \hexcoord{1}{1}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (-0.5 em,0.288675 em) -- (0 em,0.577350 em); % left edge
    \hexcoord{#1}{2}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (0 em,-0.577350 em) -- (0.5 em,-0.288675 em) -- (0.5 em,0.288675 em); % right edge
    %        \else % 1, hopefully
    %            % Coloured boundary
    %            \hexcoord{1}{1}
    %            \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom edge
    %            \hexcoord{1}{#2}
    %            \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,0.288675 em) -- (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top edge
    \fi
    \else
    \pgfmathsetmacro\Iaend{#2-1}
    \foreach \Ia in {2,...,\Iaend}{ % left to right player B
        % Coloured boundary
        \hexcoord{1}{\Ia}
        \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (-0.5 em,0.288675 em) -- (0 em,0.577350 em); % left edge
        \hexcoord{#1}{\Ia}
        \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (0 em,-0.577350 em) -- (0.5 em,-0.288675 em) -- (0.5 em,0.288675 em); % right edge
    }
    \fi

    % Awkward corners
    \hexcoord{#1}{1}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=butt] (0.25 em,-0.4330125 em) -- (0.5 em,-0.288675 em);

    \ifnum#2>1
    \hexcoord{1}{1}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=] (-0.5 em,-0.288675 em) -- (-0.5 em,0.288675 em) -- (0 em,0.577350 em); % bottom left colorB
    \hexcoord{#1}{#2}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (0 em,-0.577350 em) -- (0.5 em,-0.288675 em) -- (0.5 em,0.288675 em); % top right colorB
    \fi

    \hexcoord{#1}{1}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (0.5 em,-0.288675 em) -- (0.5 em,0.288675 em); % bottom right colorB

    \hexcoord{1}{#2}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (-0.5 em,0.288675 em); % top left colorB
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=butt] (-0.25 em,0.4330125 em) -- (-0.5 em,0.288675 em); % awkward bit
}

\newcommand{\hexreducingnoborder}[2]{
    % Place the hex cells.
    \foreach \Ib in {1,...,#2}{
        \pgfmathsetmacro\Iaend{#1+1-\Ib}
        \foreach \Ia in {1,...,\Iaend}{
            \hexcell{\Ia}{\Ib}
        }

    }
}

\newcommand{\hexbottomsubborder}[2]{%
    % Coloured boundary

    % bottom edge
    \foreach \Ia in {#1,...,#2}{
        % Coloured boundary
        \hexcoord{\Ia}{1}
        \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom edge
    }
}

\newcommand{\hexreducing}[2]{
    % Place the hex cells.
    \hexreducingnoborder{#1}{#2}

    % bottom edge
    \hexbottomsubborder{1}{#1}
}

\newcommand{\hexsubrow}[3]{
    \foreach \Ia in {#1,...,#2}{\hexcell{\Ia}{#3}}
}

\newcommand{\hexshadedsubrow}[3]{
    \foreach \Ia in {#1,...,#2}{\hexcellshaded{\Ia}{#3}}
}

\newcounter{hexedge}
\newcommand{\hexgrid}[2]{
    \hexgridnolabel{#1}{#2}

    % labels
    \setcounter{hexedge}{0}
    \foreach \Ia in {1,...,#1}{
        \addtocounter{hexedge}{1}
        \pgfmathsetmacro\leftofIa{\Ia+0.1}
        \pgfmathsetmacro\belowIa{1-1/sqrt(3)-0.2}
        \ifnum\value{hexedge}<27
        \node[scale=0.3] at (\leftofIa em,\belowIa em) {{\sf\color{\colorA}\alph{hexedge}}};
        \else
        \node[scale=0.3] at (\leftofIa em,\belowIa em) {{\sf\color{\colorA}\arabic{hexedge}}};
        \fi
    }
    \setcounter{hexedge}{0}
    \foreach \Ia in {1,...,#2}{
        \addtocounter{hexedge}{1}
        \hexcoord{1}{\Ia}
        \pgfmathsetmacro\leftofxchor{\xchor-0.75}
        \node[scale=0.3] at (\leftofxchor em,\ychor em) {{\sf\color{\colorB}\arabic{hexedge}}};
    }
}

\newcommand{\hexboard}[1]{
    \hexgrid{#1}{#1}
}

\newenvironment{hexpicture}{\begin{tikzpicture}[hex]}{\end{tikzpicture}}

\newcounter{hexmovecount} % keep track of moves
\newcounter{hexmoveskips} % count skipped moves
\newcommand{\hexmovenumber}{0} % label of current move

\newcounter{hexlabelling}
\newenvironment{hexgame}[1][11]{%
    \begin{hexpicture}
        \hexboard{#1}
        \setcounter{hexmovecount}{0}
        \setcounter{hexmoveskips}{0}
        \setcounter{hexlabelling}{0}}
    {\end{hexpicture}}
\newenvironment{hexgamelabels}[1][11]{%
    \begin{hexgame}[#1]
        \setcounter{hexlabelling}{1}}
    {\end{hexgame}}

\newcommand{\hexthismover}{B}

\newcommand{\hexmove}[2]{%
    \addtocounter{hexmovecount}{1}
    \renewcommand{\hexthismover}{B}
    \ifodd\value{hexmovecount}\renewcommand{\hexthismover}{A}\fi

    \pgfmathtruncatemacro\hexmovenumber{\value{hexmovecount}-\value{hexmoveskips}} % move label is moves minus skips
    \ifnum\value{hexlabelling}=1\hexcounterlabel{#1}{#2}{\hexthismover}{\hexmovenumber}\else\hexcounter{#1}{#2}{\hexthismover}\fi
}

\newcommand{\hexskipmove}{%
    \addtocounter{hexmovecount}{1}
    \addtocounter{hexmoveskips}{1}
}
\endinput
%%
%% End of file `hexboard.sty'.
