% This work may be distributed and/or modified under the conditions of the LaTeX project public license (LPPL), version 1.3c.
% The details of this license's version is in https://www.latex-project.org/lppl/lppl-1-3c.txt
\ProvidesPackage{multi-sudoku}[2025/7/29 multi-sudoku package version 1.00]

% Required packages
\RequirePackage{pgfkeys}
\RequirePackage{array}
\RequirePackage{xparse}
\RequirePackage{calc}
\RequirePackage{kvoptions}
\RequirePackage{ifthen}

% Set up kvoptions for this package
\SetupKeyvalOptions{
	family=multisudoku,
	prefix=multisudoku@
}

% Declare user-settable package options with defaults
\DeclareStringOption[9x9]{size}
\DeclareStringOption[1.5em]{length}
\DeclareStringOption[1.5pt]{thickness}
\DeclareStringOption[\small]{fontsize}

% Process package options
\ProcessKeyvalOptions*

% Internal lengths and state
\newlength\@thickness
\newlength\cell@length
\newlength\font@height
\newlength\extra@height
\def\sudoku@fontsize{\small}


% Apply thickness from package option
\setlength\@thickness{\multisudoku@thickness}

% Define column types for tabular rendering
\newcolumntype{S}{
	@{}
	>{\centering \rule[-0.5\extra@height]{0pt}{\font@height + \extra@height}%
		\begin{minipage}{\cell@length}\centering}
		p{\cell@length}
		<{\end{minipage}}
	@{}
}
\newcolumntype{T}{!{\vrule width \@thickness}}

% Grid styling helpers
\newcommand{\@subgrid}{\noalign{\hrule height \@thickness}}
\newcommand\nl{\tabularnewline\hline}
\newcommand\NL{\tabularnewline\@subgrid}


% Define environment-level keys using pgfkeys
\pgfkeys{
	/sudoku/.is family, /sudoku,
	default/.style = {
		size=\multisudoku@size,
		fontsize=\multisudoku@fontsize,
		length=\multisudoku@length,
		thickness=\multisudoku@thickness
	},
	size/.estore in = \sudoku@size,
	fontsize/.code = \renewcommand{\sudoku@fontsize}{#1},
	length/.code = \setlength{\cell@length}{#1},
	thickness/.code = \setlength{\@thickness}{#1}
}

% Sudoku environment
\newenvironment{sudoku}[1][]{%
	% Load keys from defaults and optional argument
	\pgfkeys{/sudoku, default, #1}
	%
	% Set font size
	\sudoku@fontsize
	%
	% Cell height adjustments
	\settoheight\font@height{A}%
	\setlength{\extra@height}{\cell@length - \font@height}%
	%
	% Choose tabular layout by size
	\ifthenelse{\equal{\sudoku@size}{1x1}}%
		{\begin{tabular}{T S T}}{%
	\ifthenelse{\equal{\sudoku@size}{2x2}}%
		{\begin{tabular}{T S T S T}}{%
	\ifthenelse{\equal{\sudoku@size}{4x4}}%
		{\begin{tabular}{T S|S T S|S T}}{%
	\ifthenelse{\equal{\sudoku@size}{9x9}}%
		{\begin{tabular}{T S|S|S T S|S|S T S|S|S T}}{%
	\ifthenelse{\equal{\sudoku@size}{16x16}}%
		{\begin{tabular}{T S|S|S|S T S|S|S|S T S|S|S|S T S|S|S|S T}}{%
	\ifthenelse{\equal{\sudoku@size}{25x25}}%
		{\begin{tabular}{T S|S|S|S|S T S|S|S|S|S T S|S|S|S|S T S|S|S|S|S T S|S|S|S|S T}}{%
	\ifthenelse{\equal{\sudoku@size}{36x36}}%
		{\begin{tabular}{T S|S|S|S|S|S T S|S|S|S|S|S T S|S|S|S|S|S T S|S|S|S|S|S T S|S|S|S|S|S T S|S|S|S|S|S T}}{%
	\ifthenelse{\equal{\sudoku@size}{49x49}}%
		{\begin{tabular}{T S|S|S|S|S|S|S T S|S|S|S|S|S|S T S|S|S|S|S|S|S T S|S|S|S|S|S|S T S|S|S|S|S|S|S T S|S|S|S|S|S|S T S|S|S|S|S|S|S T}}%
	{\PackageError{multi-sudoku}%
		{Unsupported sudoku size '\sudoku@size'}%
		{Valid sizes: 1x1, 2x2, 4x4, 9x9, 16x16, 25x25, 36x36, 49x49.\MessageBreak
		You used an unsupported sudoku size in \string\sudoku\space from the multi-sudoku package.}}%
	}}}}}}}%
		\@subgrid
	}{%
		\NL\end{tabular}%
	}

