# Is it possible to generate sudoku games with LaTeX?

I am a fan of sudoku and I was wondering if it is possible to randomly generate sudoku tables with their solutions in LaTeX. I see a few problems in the generation of numbers in the tables, but, sincerely, I wouldn't know where to start.

-
The first step is to have an algorithm to produce Sudoku tables. If you have that, in pseudo-code, or any language you like if that's easier, then someone will probably step forward an implement it. – Bruno Le Floch Feb 2 '12 at 19:53
I suggest to use the sudoku package. – Thorsten Donig Feb 2 '12 at 20:03

a package, sudokubundle, by peter wilson is on ctan. this appears to do what you're asking. it requires the random package to generate puzzle data.

-
I will look into it. – azetina Feb 2 '12 at 20:05
I'm glad to see some answers get votes even if they don't include screenshots. (The default seems to be one has to include them in order to get votes....) – Marc van Dongen Feb 3 '12 at 1:02

For typesetting Sudokus (well, LaTeX is mainly for typesetting, while calculations can be done for example with Lua, or pgf which provides a random number generator), Roberto Bonvallet has contributed a Sudoku example to the TikZ gallery.

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\usepackage{tikz}
\usepackage{mathpazo}
\PreviewEnvironment{tikzpicture}
\newcounter{row}
\newcounter{col}

\newcommand\setrow[9]{
\setcounter{col}{1}
\foreach \n in {#1, #2, #3, #4, #5, #6, #7, #8, #9} {
\edef\x{\value{col} - 0.5}
\edef\y{9.5 - \value{row}}
\node[anchor=center] at (\x, \y) {\n};
\stepcounter{col}
}
\stepcounter{row}
}

\begin{document}
\begin{tikzpicture}[scale=.5]

\begin{scope}
\draw (0, 0) grid (9, 9);
\draw[very thick, scale=3] (0, 0) grid (3, 3);

\setcounter{row}{1}
\setrow { }{2}{ }  {5}{ }{1}  { }{9}{ }
\setrow {8}{ }{ }  {2}{ }{3}  { }{ }{6}
\setrow { }{3}{ }  { }{6}{ }  { }{7}{ }

\setrow { }{ }{1}  { }{ }{ }  {6}{ }{ }
\setrow {5}{4}{ }  { }{ }{ }  { }{1}{9}
\setrow { }{ }{2}  { }{ }{ }  {7}{ }{ }

\setrow { }{9}{ }  { }{3}{ }  { }{8}{ }
\setrow {2}{ }{ }  {8}{ }{4}  { }{ }{7}
\setrow { }{1}{ }  {9}{ }{7}  { }{6}{ }

\node[anchor=center] at (4.5, -0.5) {Unsolved Sudoku};
\end{scope}

\begin{scope}[xshift=12cm]
\draw (0, 0) grid (9, 9);
\draw[very thick, scale=3] (0, 0) grid (3, 3);

\setcounter{row}{1}
\setrow { }{2}{ }  {5}{ }{1}  { }{9}{ }
\setrow {8}{ }{ }  {2}{ }{3}  { }{ }{6}
\setrow { }{3}{ }  { }{6}{ }  { }{7}{ }

\setrow { }{ }{1}  { }{ }{ }  {6}{ }{ }
\setrow {5}{4}{ }  { }{ }{ }  { }{1}{9}
\setrow { }{ }{2}  { }{ }{ }  {7}{ }{ }

\setrow { }{9}{ }  { }{3}{ }  { }{8}{ }
\setrow {2}{ }{ }  {8}{ }{4}  { }{ }{7}
\setrow { }{1}{ }  {9}{ }{7}  { }{6}{ }

\node[anchor=center] at (4.5, -0.5) {Solved Sudoku};

\begin{scope}[blue, font=\sffamily\slshape]
\setcounter{row}{1}
\setrow {4}{ }{6}  { }{7}{ }  {3}{ }{8}
\setrow { }{5}{7}  { }{9}{ }  {1}{4}{ }
\setrow {1}{ }{9}  {4}{ }{8}  {2}{ }{5}

\setrow {9}{7}{ }  {3}{8}{5}  { }{2}{4}
\setrow { }{ }{3}  {7}{2}{6}  {8}{ }{ }
\setrow {6}{8}{ }  {1}{4}{9}  { }{5}{3}

\setrow {7}{ }{4}  {6}{ }{2}  {5}{ }{1}
\setrow { }{6}{5}  { }{1}{ }  {9}{3}{ }
\setrow {3}{ }{8}  { }{5}{ }  {4}{ }{2}
\end{scope}

\end{scope}

\end{tikzpicture}
\end{document}

-
I guess this only displays the sudoku but does not generate it. Its a good idea though. Thanks for the input. – azetina Feb 2 '12 at 22:00

For typesetting Sudokus, you can also use the logicpuzzle bundle [CTAN].

\begin{center}
\begin{lpsudoku}
\setrow{9}{{},2,6,{},{},{},{},{},{}}
\setrow{8}{{},{},{},{},{},{},1,7,{}}
\setrow{7}{{},{},3,1,{},6,{},{},{}}
\setrow{6}{{},6,{},{},5,{},8,{},3}
\setrow{5}{{},{},9,2,6,1,7,{},{}}
\setrow{4}{5,{},4,{},8,{},{},6,{}}
\setrow{3}{{},{},{},8,{},4,3,{},{}}
\setrow{2}{{},4,8,{},{},{},{},{},{}}
\setrow{1}{{},{},{},{},{},{},9,4,{}}
\end{lpsudoku}
\hspace{1.5cm}
\begin{lpsudoku}
\setrow{9}{1,2,6,5,7,8,4,3,9}
\setrow{8}{4,8,5,9,3,2,1,7,6}
\setrow{7}{7,9,3,1,4,6,5,8,2}
\setrow{6}{2,6,1,4,5,7,8,9,3}
\setrow{5}{8,3,9,2,6,1,7,5,4}
\setrow{4}{5,7,4,3,8,9,2,6,1}
\setrow{3}{6,5,2,8,9,4,3,1,7}
\setrow{2}{9,4,8,7,1,3,6,2,5}
\setrow{1}{3,1,7,6,2,5,9,4,8}
\end{lpsudoku}
\end{center}


You can use the createlpsudoku bash script to transfer exported Sudokus from e.g. HoDoKu or QQwing into lpsudoku environments. See: How to create a Sudoku magazine

With the lpsmag bash script, you can even produce your own Sudoku magazines with solutions! Whenever you run the script, you have a new magazine.

-