6

I need to draw 15-puzzle in LaTeX, that is, a diagram of a 4x4 grid with numbers inside. Could you please recommend me a package for doing this and some example code?

15-puzzle

14

With logicpuzzle

\documentclass{article}
\usepackage{logicpuzzle}

\newenvironment{fifteen}[1][]{%
\begin{logicpuzzle}[rows=4,columns=4,#1]
\begin{puzzleforeground}
\framepuzzle
\end{puzzleforeground}
}{\end{logicpuzzle}}

\begin{document}

\begin{fifteen}
\setrow{4}{13,10,11,6}
\setrow{3}{5,7,4,8}
\setrow{2}{1,12,14,9}
\setrow{1}{3,15,2,}
\end{fifteen}
%
\begin{fifteen}
\setrow{4}{13,10,11,6}
\setrow{3}{1,5,7,4}
\setrow{2}{3,12,14,8}
\setrow{1}{,15,2,9}
\end{fifteen}
\end{document} 

enter image description here

|improve this answer|||||
  • 1
    Congrats to 100k! – Schrödinger's cat Oct 15 '19 at 14:54
  • @Schrödinger'scat Thank you! – Ignasi Oct 15 '19 at 14:59
13

This puts a list in the minimal quadratic grid that is large enough to fit it.

\documentclass{article}
\usepackage{tikz}
\newcommand{\Puzzle}[2][]{\begin{tikzpicture}
\pgfmathtruncatemacro{\mydim}{dim({#2})}%
\pgfmathtruncatemacro{\myrows}{sqrt(\mydim)+0.99}%
\pgfmathtruncatemacro{\mymax}{max(#2)}%
\pgfmathsetmacro{\mysize}{width("\mymax")+8}%
\draw (0,0) grid[step=\mysize pt] (\myrows*\mysize pt,-\myrows*\mysize pt);
\foreach \Z [count=\Cn starting from 0] in {#2}
{\pgfmathtruncatemacro{\Y}{\Cn/\myrows}%
\pgfmathtruncatemacro{\X}{\Cn-\Y*\myrows}%
\unless\ifnum\Z<0
\node[minimum size=\mysize pt,anchor=north west] at (\X*\mysize pt,-\Y*\mysize pt) {\Z};
\fi}
\end{tikzpicture}}
\begin{document}
\Puzzle{13,10,11,6,5,7,4,8,1,12,14,9,3,15,2}

\Puzzle{13,10,11,6,5,7,4,8,1,12,14,9,3,15,2,13,10,11,6,5,7,4,8,1,12,14,9,3,15,2,3,4}

\Puzzle{13,10,-1,11,6,-1,5}

\end{document}

enter image description here

|improve this answer|||||
  • Thank you. However your solution does not allow to place the empty pebble anywhere else than at the last position. – Brikowski Oct 15 '19 at 13:57
  • 1
    @Brikowski Now it is possible. Negative entries will be ignored, i.e. lead to empty cells. – Schrödinger's cat Oct 15 '19 at 14:50
6

A simplified variant on my answer here: Vertically centered material in fixed height rows in tabular environment

\documentclass{article}
\newcount\cellwd
\newcount\cellht
\makeatletter
{\catcode`|=\active \gdef|{\acell}}
\newcommand\acell[1]{\noindent%
  \framebox(\cellwd,\cellht){\centering#1}\kern\@wholewidth\ignorespaces}
\newcommand\cellrowskip{\\[\dimexpr-1pt+\@wholewidth\relax]}
\newenvironment{celltable}[2][\@wholewidth]
  {\cellwd=#2\relax
  \cellht=#2\relax
  \linethickness{#1}%
  \vspace{#1}%
  \par\catcode`\| \active 
}{\par}
\makeatother
\begin{document}
What precedes.

\begin{celltable}{30}% CELL SIZE IN PT
|{13}|{10}|{11}|6\cellrowskip
|5|7|4|8\cellrowskip
|1|{12}|{14}|9\cellrowskip
|3|{15}|2|~
\end{celltable}

What follows.
\end{document}

enter image description here

Of course, if the cell size is of no consequence, a simple tabular will work:

\documentclass{article}
\begin{document}
What precedes.

\begin{tabular}{|c|c|c|c|}
\hline
13 & 10 & 11 & 6\\
\hline
5 & 7 & 4 & 8\\
\hline
1 & 12 & 14 & 9\\
\hline
3 & 15 & 2 &\\
\hline
\end{tabular}

What follows.
\end{document}

enter image description here

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.