%%
%% Copyright (C) 2013 by:
%% Josef Kleber
%% <josef.kleber@gmx.de>
%%
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3 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.3 or later is part of all distributions of LaTeX version
%% 2003/12/01 or later.
%%
%% This work has the LPPL maintenance status "author-maintained".
%%
%% The Current Maintainer of this work is Josef Kleber.
%%
%% This work consists of all files listed in manifest.txt.
%%
\ProvidesPackage{logicpuzzle}[2013/03/16 logicpuzzle.sty v1.2a - Josef Kleber (C) 2013]%
%
\RequirePackage{xkeyval}%
\RequirePackage{ifthen}%
\RequirePackage{ragged2e}%
\RequirePackage{etoolbox}%
\RequirePackage{marginnote}%
\RequirePackage{tikz}%
\usetikzlibrary{decorations.pathmorphing}%
\pgfdeclarelayer{LPbgcolor}%
\pgfdeclarelayer{LPbackgroundtwo}%
\pgfdeclarelayer{LPbackground}%
\pgfdeclarelayer{LPforeground}%
\pgfdeclarelayer{LPforegroundtwo}%
\pgfsetlayers{LPbgcolor,LPbackgroundtwo,LPbackground,main,LPforeground,LPforegroundtwo}%
%
\newcounter{LP@counti}%
\newcounter{LP@countii}%
\newcounter{LP@whiledo}%
\newcounter{LP@count@ig@i}%
\newcounter{LP@count@ig@ii}%
\newcounter{LP@count@ig@iii}%
\newcounter{LP@count@ig@iv}%
\newcounter{LP@puzzlecounter}%
\setcounter{LP@puzzlecounter}{1}%
%
\newcommand*\LP@counterstyle{none}%
\newcommand*\LP@titleformat{}%
\gdef\LP@env@prefix{}%
\gdef\LP@package{}%
\gdef\LP@rows{}%
\gdef\LP@columns{}%
\gdef\LP@scale{1}%
\gdef\LP@color{black}%
\gdef\LP@fontsize\Large%
\gdef\LP@cvoffset{0pt}%
%
\newcommand*\LP@Pfive{.5}%
%
\newcommand*\LP@ingrid[5]%
{%
  \setcounter{LP@count@ig@i}{#1}% column
  \setcounter{LP@count@ig@ii}{#2}% row
  \setcounter{LP@count@ig@iii}{#3}% max column
  \setcounter{LP@count@ig@iv}{#4}% max row
  %#5 style
  %
  \ifnum\value{LP@count@ig@i}<1%
    \PackageError{#5}%
                 {element outside of the grid}%
                 {You tried to set an element at (#1,#2),\MessageBreak which is outside the grid (1,1) .. (#3,#4)}%
  \fi%
  \ifnum\value{LP@count@ig@ii}<1%
    \PackageError{#5}%
                 {element outside of the grid}%
                 {You tried to set an element at (#1,#2),\MessageBreak which is outside the grid (1,1) .. (#3,#4)}%
  \fi%
  \ifnum\value{LP@count@ig@i}>\value{LP@count@ig@iii}%
    \PackageError{#5}%
                 {element outside of the grid}%
                 {You tried to set an element at (#1,#2),\MessageBreak which is outside the grid (1,1) .. (#3,#4)}%
  \fi%
  \ifnum\value{LP@count@ig@ii}>\value{LP@count@ig@iv}%
    \PackageError{#5}%
                 {element outside of the grid}%
                 {You tried to set an element at (#1,#2),\MessageBreak which is outside the grid (1,1) .. (#3,#4)}%
  \fi%
}%
%
% #1 grid x min
% #2 grid y min
% #3 grid x max
% #4 grid y max
% #5 step
\newcommand*\LP@drawgrid[5]%
{%
  \setcounter{LP@counti}{#3}% max column
  \setcounter{LP@countii}{#4}% max row
  \stepcounter{LP@counti}%
  \stepcounter{LP@countii}%
  \draw[step=#5] (#1,#2) grid (\value{LP@counti},\value{LP@countii});%
}%
%
\newcommand*\LP@drawsudokugrid%
{%
  \draw[ultra thick] (1,1) -- (1,10);%
  \draw[ultra thick] (4,1) -- (4,10);%
  \draw[ultra thick] (7,1) -- (7,10);%
  \draw[ultra thick] (10,1) -- (10,10);%
  \draw[ultra thick] (1,1) -- (10,1);%
  \draw[ultra thick] (1,4) -- (10,4);%
  \draw[ultra thick] (1,7) -- (10,7);%
  \draw[ultra thick] (1,10) -- (10,10);%
}%
%
% #1 grid x min
% #2 grid y min
% #3 grid x max
% #4 grid y max
% #5 bgcolor
\newcommand*\LP@drawbackground[5]%
{%
  \ifthenelse{\equal{#5}{}}%
  {}% no bgcolor
  {%
    \setcounter{LP@counti}{#3}% max column
    \setcounter{LP@countii}{#4}% max row
    \stepcounter{LP@counti}%
    \stepcounter{LP@countii}%
    \begin{pgfonlayer}{LPbgcolor}%
      \draw[fill=#5] (#1,#2) rectangle (\value{LP@counti},\value{LP@countii});%
    \end{pgfonlayer}{LPbgcolor}%
  }%
}%
%
% #1 column
% #2 row
% #3 content
\newcommand*\LP@setcellcontent[3]%
{%
  \node at (#1\LP@Pfive,#2\LP@Pfive){#3};%
}%
%
% #1 csv list
% #2 column
% #3 row
\newcommand*\LP@setrowcontents[3]%
{%
  \setcounter{LP@counti}{#2}%
  \setcounter{LP@countii}{#3}%
  \forcsvlist\LP@setrowcontents@i{#1}{\LP@env@prefix}%
}%
%
\newcommand*\LP@setrowcontents@i[1]%
{%
  \LP@setrowcontents@ii{#1}{\LP@env@prefix}%
}%
%
\newcommand*\LP@setrowcontents@ii[2]%
{%
  \LP@setcellcontent{\arabic{LP@counti}}{\arabic{LP@countii}}{\csname #2@fontsize\endcsname#1}%
  \stepcounter{LP@counti}%
}%
%
\newcommand*\setrow[2]%
{%
  \LP@setrowcontents{#2}{1}{#1}%
}%
%
% #1 csv list
% #2 column
% #3 row
\newcommand*\LP@setcolumncontents[3]%
{%
  \setcounter{LP@counti}{#2}%
  \setcounter{LP@countii}{#3}%
  \forcsvlist\LP@setcolumncontents@i{#1}%
}%
%
\newcommand*\LP@setcolumncontents@i[1]%
{%
  \LP@setcolumncontents@ii{#1}{\LP@env@prefix}%
}%
%
\newcommand*\LP@setcolumncontents@ii[2]%
{%
  \LP@setcellcontent{\arabic{LP@counti}}{\arabic{LP@countii}}{\csname #2@fontsize\endcsname#1}%
  \stepcounter{LP@countii}%
}%
%
\newcommand*\setcolumn[2]%
{%
  \LP@setrowcontents{#2}{#1}{1}%
}%
%
\newcommand*\setcell[3]%
{%
  \LP@set@LP@scale{\LP@env@prefix}%
  \LP@set@LP@fontsize{\LP@env@prefix}%
  \LP@set@LP@rows{\LP@env@prefix}%
  \LP@set@LP@columns{\LP@env@prefix}%
  \LP@ingrid{#1}{#2}{\LP@columns}{\LP@rows}{\LP@package}%
  \LP@setcellcontent{#1}{#2}{\LP@fontsize#3}%
}%
%
\newcommand*\LP@bottomrow[1]%
{%
  \LP@setrowcontents{#1}{1}{0}%
}%
%
\newcommand*\LP@leftcolumn[1]%
{%
  \LP@setcolumncontents{#1}{0}{1}%
}%
%
\newcommand*\LP@toprow[1]%
{%
  \LP@toprow@i{#1}{\LP@env@prefix}%
}%
%
\newcommand*\LP@toprow@i[2]%
{%
  \LP@setrowcontents{#1}{1}{\csname the#2@rows\endcsname}%
}%
%
\newcommand*\LP@rightcolumn[1]%
{%
  \LP@rightcolumn@i{#1}{\LP@env@prefix}%
}%
%
\newcommand*\LP@rightcolumn@i[2]%
{%
  \LP@setcolumncontents{#1}{\csname the#2@columns\endcsname}{1}%
}%
%
\newcommand*\titleformat[1]%
{%
  \renewcommand*\LP@titleformat{#1}%
}%
\titleformat{\centering\Large\color{blue}}%
%
\newcommand*\puzzlecounter{\theLP@puzzlecounter}%
%
\newcommand*\setpuzzlecounter[1]%
{%
  \setcounter{LP@puzzlecounter}{#1}%
}%
%
% #1 counterstyle
\newcommand*\LP@drawcounter[1]%
{%
  \LP@set@LP@cvoffset{\LP@env@prefix}%
  \csname LP@cs@#1\endcsname%
}%
%
\newcommand*{\definecounterstyle}[2]%
{%
  \expandafter\gdef\csname LP@cs@#1\endcsname{#2}%
}%
%
\definecounterstyle{none}{}%
\definecounterstyle{left}{\begingroup\reversemarginpar\marginnote{\tikz\node[shape=rectangle,fill=yellow!40,inner sep=7pt,draw,rounded corners=3pt,thick]{\Huge\puzzlecounter};}[\LP@cvoffset]\endgroup}%
\definecounterstyle{right}{\marginnote{\tikz\node[shape=rectangle,fill=yellow!40,inner sep=7pt,draw,rounded corners=3pt,thick]{\Huge\puzzlecounter};}[\LP@cvoffset]}%
%
\newcommand*\LP@Block{\tikz[scale=\LP@scale]\draw[fill=\LP@color] (0,0) rectangle (1,1);}%
%
\newcommand*\fillcell[2]%
{%
  \LP@set@LP@scale{\LP@env@prefix}%
  \LP@set@LP@color{\LP@env@prefix}%
  \LP@set@LP@rows{\LP@env@prefix}%
  \LP@set@LP@columns{\LP@env@prefix}%
  \LP@ingrid{#1}{#2}{\LP@columns}{\LP@rows}{\LP@package}%
  \LP@setcellcontent{#1}{#2}{\LP@Block}%
}%
%
% #1 row
% #2 csv list
\newcommand*\fillrow[2]%
{%
  \setcounter{LP@counti}{1}%
  \setcounter{LP@countii}{#1}%
  \forcsvlist\LP@fillrow@i{#2}%
}%
%
\newcommand*\LP@fillrow@i[1]%
{%
  \ifthenelse{\equal{#1}{1}}%
  {\fillcell{\arabic{LP@counti}}{\arabic{LP@countii}}}%
  {}%
  \stepcounter{LP@counti}%
}%
%
% #1 column
% #2 csv list
\newcommand*\fillcolumn[2]%
{%
  \setcounter{LP@counti}{#1}%
  \setcounter{LP@countii}{1}%
  \forcsvlist\LP@fillcolumn@i{#2}%
}%
%
\newcommand*\LP@fillcolumn@i[1]%
{%
  \ifthenelse{\equal{#1}{1}}%
  {\fillcell{\arabic{LP@counti}}{\arabic{LP@countii}}}%
  {}%
  \stepcounter{LP@countii}%
}%
%
\newcommand*\LP@set@LP@scale[1]%
{%
  \expandafter\xdef\expandafter\LP@scale{\csname #1@scale\endcsname}%
}%
%
\newcommand*\LP@set@LP@color[1]%
{%
  \expandafter\xdef\expandafter\LP@color{\csname #1@color\endcsname}%
}%
%
\newcommand*\LP@set@LP@rows[1]%
{%
  \expandafter\xdef\expandafter\LP@rows{\csname #1@rows\endcsname}%
}%
%
\newcommand*\LP@set@LP@columns[1]%
{%
  \expandafter\xdef\expandafter\LP@columns{\csname #1@columns\endcsname}%
}%
%
\newcommand*\LP@set@LP@cvoffset[1]%
{%
  \expandafter\xdef\expandafter\LP@cvoffset{\csname #1@cvoffset\endcsname}%
}%
%
\newcommand*\LP@set@LP@fontsize[1]%
{%
  % can't expand \Large
  \expandafter\gdef\expandafter\LP@fontsize{\csname #1@fontsize\endcsname}%
}%
%
\newcommand*\LP@set@env@prefix[1]%
{%
  \gdef\LP@env@prefix{#1}%
}%
%
\newcommand*\LP@set@package[1]%
{%
  \gdef\LP@package{#1}%
}%
%
\newcommand*\framearea[2]%
{%
  \draw[ultra thick,color=#1] #2;%
}%
%
\newcommand*\fillarea[2]%
{%
  \draw[ultra thick,fill=#1] #2 ;%
}%
%
\newenvironment{puzzlebackground}%
{%
  \begin{pgfonlayer}{LPbackground}%
}%
{%
  \end{pgfonlayer}%
}%
%
\newenvironment{puzzleforeground}%
{%
  \begin{pgfonlayer}{LPforeground}%
}%
{%
  \end{pgfonlayer}%
}%
%
\endinput%