%%
%% This is file `havannah.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% havannah.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2012-2015 by Marcin Ciura
%% 
%% This file 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.
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{havannah}[2010/06/06 LaTeX havannah package]
\RequirePackage{tikz}
\newcommand\HLetterCoordinates{%
  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}
\newcommand\HCoordinateStyle[1]{\sffamily#1}
\newcommand\HMoveNumberStyle[1]{\sffamily#1}
\newcommand\HDrawHex{\shadedraw[
  shading=radial,inner color=gray!30,outer color=gray!70]}
\newcommand\HWhiteStone{\node[
  circle,draw=black,inner sep=0.6pt,fill=white,
  minimum size=\HStoneDiameter]}
\newcommand\HBlackStone{\node[
  circle,draw=black,inner sep=0.6pt,fill=black,text=white,
  minimum size=\HStoneDiameter]}
\newcommand\HTransparentStone{\node[
  circle,draw=black,inner sep=0.6pt,
  minimum size=\HStoneDiameter]}
\newcommand\HBeforeOddMove{}
\newcommand\HBeforeEvenMove{}
\newcommand\HBeforeStone{}
\newcommand{\h@draw@hex}[1]{%
  \HDrawHex (#1)
    ++(-2\h@one@third@hex@wd,0)--
    ++(\h@one@third@hex@wd,-\h@half@hex@ht)--
    ++(2\h@one@third@hex@wd,0)--
    ++(\h@one@third@hex@wd,\h@half@hex@ht)--
    ++(-\h@one@third@hex@wd,\h@half@hex@ht)--
    ++(-2\h@one@third@hex@wd,0)--
    cycle;
}
\newif\ifh@numbered@moves
\newif\ifh@show@coordinates
\newif\ifh@show@hexes
\pgfkeys{%
  /h@havannah@board/.cd,
  board size/.store in=\hv@board@size,
  coordinate style/.is choice,
  coordinate style/classical/.code={%
    \def\h@draw@board{\h@draw@classical@board}},
  coordinate style/little golem/.code={%
    \def\h@draw@board{\h@draw@little@golem@board}},
  hex height/.store in=\h@hex@height,
  prefix/.store in=\h@prefix,
  show coordinates/.is if=h@show@coordinates,
  show hexes/.is if=h@show@hexes,
  x/.store in=\h@xx,
  y/.store in=\h@yy,
  board size=10,
  coordinate style=classical,
  hex height=17.5pt,
  prefix=,
  show coordinates=true,
  show hexes=true,
  x=0,
  y=0,
  /h@hex@board/.cd,
  top left color/.is choice,
  top left color/white/.code={%
    \def\h@top@left@color{\HWhiteStone}%
    \def\h@bottom@left@color{\HBlackStone}%
  },
  top left color/black/.code={%
    \def\h@top@left@color{\HBlackStone}%
    \def\h@bottom@left@color{\HWhiteStone}%
  },
  board size/.store in=\hx@board@size,
  hex height/.store in=\h@hex@height,
  prefix/.store in=\h@prefix,
  relative stone size/.store in=\h@relative@stone@size,
  show coordinates/.is if=h@show@coordinates,
  show hexes/.is if=h@show@hexes,
  x/.store in=\h@xx,
  y/.store in=\h@yy,
  top left color=black,
  board size=11,
  hex height=17.5pt,
  relative stone size=0.75,
  show coordinates=true,
  show hexes=true,
  /h@game/.cd,
  first move label/.store in=\h@first@move@label,
  first player/.is choice,
  first player/white/.code={%
    \def\h@odd@player{\HWhiteStone}%
    \def\h@even@player{\HBlackStone}%
  },
  first player/black/.code={%
    \def\h@odd@player{\HBlackStone}%
    \def\h@even@player{\HWhiteStone}%
  },
  numbered moves/.is if=h@numbered@moves,
  relative stone size/.store in=\h@relative@stone@size,
  first move label=1,
  numbered moves=true,
  relative stone size=0.75,
  /h@stone@group/.cd,
  color/.is choice,
  color/white/.code={\def\h@player{\HWhiteStone}},
  color/black/.code={\def\h@player{\HBlackStone}},
  color/transparent/.code={\def\h@player{\HTransparentStone}},
  label/.store in=\h@label,
  relative stone size/.store in=\h@relative@stone@size,
  relative stone size=0.75,
  /h@hex@group/.cd,
  label/.store in=\h@label,
}
\newcount\h@board@diagonal
\newdimen\h@half@hex@ht
\newdimen\h@one@third@hex@wd
\newenvironment{InnerHavannahBoard}[1][]{%
  \def\h@odd@player{\HWhiteStone}%
  \def\h@even@player{\HBlackStone}%
  \pgfqkeys{/h@havannah@board}{#1}%
  \setlength\h@half@hex@ht{\h@hex@height}%
  \divide\h@half@hex@ht by 2
  \setlength\h@one@third@hex@wd{0.577350269\h@half@hex@ht}%
  \h@board@diagonal=\hv@board@size
  \multiply\h@board@diagonal by 2
  \advance\h@board@diagonal by -1
  \h@draw@board
}
{}
\newenvironment{HavannahBoard}[1][]{%
  \begin{tikzpicture}
  \begin{InnerHavannahBoard}[#1,prefix=,x=0,y=0]
}
{ \end{InnerHavannahBoard}
  \end{tikzpicture}
}
\newcount\h@l
\newcount\h@a@corner
\newcount\h@b@corner
\newcommand\h@draw@classical@board{%
  \h@l=0
  \h@b@corner=\hv@board@size
  \foreach \h@letter in \HLetterCoordinates {%
    \global\advance\h@l by 1
    \ifnum \h@l > \h@board@diagonal
      \breakforeach
    \else
      \global\advance\h@b@corner by 1
      \h@a@corner=\hv@board@size
      \foreach \h@n in {1,...,\h@board@diagonal} {%
        \global\advance\h@a@corner by 1
        \ifnum \h@l < \h@a@corner
        \ifnum \h@n < \h@b@corner
          \coordinate (\h@prefix\h@letter\h@n) at
            (\h@xx+3*\h@n\h@one@third@hex@wd-3*\h@l\h@one@third@hex@wd,
             \h@yy+\h@n\h@half@hex@ht+\h@l\h@half@hex@ht);
          \ifh@show@hexes
            \h@draw@hex{\h@prefix\h@letter\h@n}%
          \fi
        \fi
        \fi
      }%
      \ifh@show@coordinates
        \ifnum \h@l < \hv@board@size\relax
          \node at
            (\h@xx-3*\h@l\h@one@third@hex@wd,\h@yy+\h@l\h@half@hex@ht)
            {\HCoordinateStyle{\h@letter}};
        \else
          \node at
            (\h@xx-3*\hv@board@size\h@one@third@hex@wd,
             \h@yy+2*\h@l\h@half@hex@ht-\hv@board@size\h@half@hex@ht)
            {\HCoordinateStyle{\h@letter}};
        \fi
      \fi
    \fi
  }%
  \ifh@show@coordinates
    \foreach \h@n in {1,...,\h@board@diagonal} {%
      \ifnum \h@n < \hv@board@size
        \node at
          (\h@xx+3*\h@n\h@one@third@hex@wd,\h@yy+\h@n\h@half@hex@ht)
          {\HCoordinateStyle{\h@n}};
      \else
        \node at
          (\h@xx+3*\hv@board@size\h@one@third@hex@wd,
           \h@yy+2*\h@n\h@half@hex@ht-\hv@board@size\h@half@hex@ht)
          {\HCoordinateStyle{\h@n}};
      \fi
    }%
  \fi
}
\newcommand\h@draw@little@golem@board{%
  \h@a@corner=\hv@board@size
  \h@b@corner=\hv@board@size
  \multiply\h@b@corner by 3
  \h@l=0
  \foreach \h@letter in \HLetterCoordinates {%
    \global\advance\h@l by 1
    \ifnum \h@l > \h@board@diagonal
      \breakforeach
    \else
      \global\advance\h@a@corner by 1
      \global\advance\h@b@corner by -1
      \foreach \h@n in {1,...,\h@board@diagonal} {%
        \ifnum \h@n < \h@a@corner
        \ifnum \h@n < \h@b@corner
          \ifnum \h@l < \hv@board@size
            \coordinate (\h@prefix\h@letter\h@n) at
              (\h@xx+3*\h@l\h@one@third@hex@wd,
               \h@yy+2*\hv@board@size\h@half@hex@ht+
               2*\h@n\h@half@hex@ht-\h@l\h@half@hex@ht);
            \ifh@show@hexes
              \h@draw@hex{\h@prefix\h@letter\h@n}%
            \fi
          \else
            \coordinate (\h@prefix\h@letter\h@n) at
              (\h@xx+3*\h@l\h@one@third@hex@wd,
               \h@yy+2*\h@n\h@half@hex@ht+\h@l\h@half@hex@ht);
            \ifh@show@hexes
              \h@draw@hex{\h@prefix\h@letter\h@n}%
            \fi
          \fi
        \fi
        \fi
      }
      \ifh@show@coordinates
        \ifnum \h@l < \hv@board@size
          \node at
            (\h@xx+3*\h@l\h@one@third@hex@wd,
             \h@yy+2*\hv@board@size\h@half@hex@ht-\h@l\h@half@hex@ht)
            {\HCoordinateStyle{\h@letter}};
        \else
          \node at
            (\h@xx+3*\h@l\h@one@third@hex@wd,\h@yy+\h@l\h@half@hex@ht)
            {\HCoordinateStyle{\h@letter}};
        \fi
      \fi
    \fi
  }%
  \ifh@show@coordinates
    \foreach \h@n in {1,...,\h@board@diagonal} {%
      \ifnum \h@n < \hv@board@size
        \node at
          (\h@xx,
           \h@yy+2*\h@n\h@half@hex@ht+
           \h@board@diagonal\h@half@hex@ht+\h@half@hex@ht)
           {\HCoordinateStyle{\h@n}};
        \node at
          (\h@xx+3*\h@board@diagonal\h@one@third@hex@wd+
           3*\h@one@third@hex@wd,
           \h@yy+2*\h@n\h@half@hex@ht+
           \h@board@diagonal\h@half@hex@ht+\h@half@hex@ht)
           {\HCoordinateStyle{\h@n}};
      \else
        \node at
          (\h@xx+3*\h@n\h@one@third@hex@wd-
           3*\hv@board@size\h@one@third@hex@wd,
           \h@yy+\h@n\h@half@hex@ht+3*\hv@board@size\h@half@hex@ht)
           {\HCoordinateStyle{\h@n}};
        \node at
          (\h@xx-3*\h@n\h@one@third@hex@wd+
           9*\hv@board@size\h@one@third@hex@wd,
           \h@yy+\h@n\h@half@hex@ht+3*\hv@board@size\h@half@hex@ht)
           {\HCoordinateStyle{\h@n}};
      \fi
    }%
  \fi
}
\newenvironment{InnerHexBoard}[1][]{%
  \def\h@odd@player{\HBlackStone}%
  \def\h@even@player{\HWhiteStone}%
  \pgfqkeys{/h@hex@board}{#1}%
  \tracingcommands=1
  \setlength\h@half@hex@ht{\h@hex@height}%
  \divide\h@half@hex@ht by 2
  \setlength\h@one@third@hex@wd{0.577350269\h@half@hex@ht}%
  \HStoneDiameter=\h@relative@stone@size\h@half@hex@ht
  \multiply\HStoneDiameter by 2
  \h@l=0
  \foreach \h@letter in \HLetterCoordinates {%
    \global\advance\h@l by 1
    \ifnum \h@l > \hx@board@size
      \breakforeach
    \else
      \foreach \h@n in {1,...,\hx@board@size} {%
        \coordinate (\h@letter\h@n) at
          (3*\h@l\h@one@third@hex@wd+3*\h@n\h@one@third@hex@wd,
           \h@l\h@half@hex@ht-\h@n\h@half@hex@ht);
        \ifh@show@hexes
          \h@draw@hex{\h@letter\h@n}%
        \fi
      }%
      \coordinate (\h@letter) at
        (3*\h@l\h@one@third@hex@wd,
         2\h@half@hex@ht-2\h@half@hex@ht+\h@l\h@half@hex@ht);
      \coordinate (-\h@letter) at
        (3*\hx@board@size\h@one@third@hex@wd+
         3\h@one@third@hex@wd+3*\h@l\h@one@third@hex@wd,
         -\hx@board@size\h@half@hex@ht-
         \h@half@hex@ht+\h@l\h@half@hex@ht);
      \ifh@show@coordinates
        \h@top@left@color at (\h@letter)
          {\HCoordinateStyle{\h@letter}};
        \h@top@left@color at (-\h@letter)
          {\HCoordinateStyle{\h@letter}};
      \fi
    \fi
  }%
  \ifh@show@coordinates
    \foreach \h@n in {1,...,\hx@board@size} {%
      \coordinate (\h@n) at
        (3*\h@n\h@one@third@hex@wd,-1*\h@n\h@half@hex@ht);
      \coordinate (-\h@n) at
        (3*\hx@board@size\h@one@third@hex@wd+3\h@one@third@hex@wd+
         3*\h@n\h@one@third@hex@wd,
         \hx@board@size\h@half@hex@ht+\h@half@hex@ht-\h@n\h@half@hex@ht);
      \h@bottom@left@color at (\h@n)
        {\HCoordinateStyle{\h@n}};
      \h@bottom@left@color at (-\h@n)
        {\HCoordinateStyle{\h@n}};
    }%
  \fi
}
{}
\newenvironment{HexBoard}[1][]{%
  \begin{tikzpicture}
  \begin{InnerHexBoard}[#1,prefix=,x=0,y=0]
}
{ \end{InnerHexBoard}
  \end{tikzpicture}
}
\newcount\h@move@number
\newdimen\HStoneDiameter
\newcommand\HGame[2][]{%
  \pgfqkeys{/h@game}{#1}%
  \HStoneDiameter=\h@relative@stone@size\h@half@hex@ht
  \multiply\HStoneDiameter by 2
  \h@move@number=0
  \ifh@numbered@moves
    \def\h@label{\HMoveNumberStyle{\h@first@move@label}%
      \global\def\h@label{\HMoveNumberStyle{\the\h@move@number}}}
  \else
    \def\h@label{}
  \fi
  \foreach \h@coord in {#2} {%
    \global\advance\h@move@number by 1
    \ifodd\h@move@number
      \HBeforeOddMove
      \h@odd@player at (\h@coord) {\h@label};
    \else
      \HBeforeEvenMove
      \h@even@player at (\h@coord) {\h@label};
    \fi
  }
}
\newcommand\HStoneGroup[2][]{%
  \let\h@player\empty
  \let\h@label\empty
  \pgfqkeys{/h@stone@group}{#1}%
  \ifx\h@player\empty
    \errmessage{No color specified for HStoneGroup}
  \fi
  \HStoneDiameter=\h@relative@stone@size\h@half@hex@ht
  \multiply\HStoneDiameter by 2
  \foreach \h@coord in {#2} {%
    \HBeforeStone
    \h@player at (\h@coord) {\h@label};
  }%
}
\newcommand\HHexGroup[2][]{%
  \let\h@label\empty
  \pgfqkeys{/h@hex@group}{#1}%
  \foreach \h@coord in {#2} {%
    \node at (\h@coord) {\h@label};
    \h@draw@hex{\h@coord}%
  }%
}
\endinput
%%
%% End of file `havannah.sty'.
