| 1 |
\documentclass{ltxdoc} |
| 2 |
\title{The \textsf{maze} package\footnote{~~This project is distributed under the \LaTeX~Project Public License, version 1.3c.}} |
| 3 |
\usepackage[margin=2cm]{geometry} |
| 4 |
\usepackage{graphicx} |
| 5 |
\usepackage{listings} |
| 6 |
\lstset{ |
| 7 |
basicstyle=\fontspec{Consolas},numbers=left, |
| 8 |
numberstyle=\small,stepnumber=1,numbersep=5pt |
| 9 |
} |
| 10 |
\usepackage{xcolor} |
| 11 |
\usepackage{fontspec} |
| 12 |
\setmainfont{Times New Roman} |
| 13 |
\usepackage{indentfirst} |
| 14 |
\usepackage[misc]{ifsym} |
| 15 |
\author{Sicheng Du\thanks{~~\Letter~~\href{mailto:[email protected]}{[email protected]}}} |
| 16 |
\date{\today~~~~v1.2} |
| 17 |
\usepackage[colorlinks,linkcolor=purple]{hyperref} |
| 18 |
\usepackage{maze} |
| 19 |
\begin{document} |
| 20 |
\maketitle |
| 21 |
\section{Changes in this version} |
| 22 |
Thanks to the \TeX nicians who kindly gave valuable and earnest suggestions to this package, the version 1.2 is now released. The main changes include |
| 23 |
\begin{enumerate} |
| 24 |
\item Corrected some mistakes in this manual; |
| 25 |
\item Modified the format in the source code to improve compatibility; |
| 26 |
\item Improved the output map of the maze to make it clearer. |
| 27 |
\end{enumerate} |
| 28 |
\section{User instructions} |
| 29 |
The \textsf{maze} package can generate random square mazes of a specified size. You need to start from the bottom-left corner and reach the top-right corner to play it. |
| 30 |
\begin{macro}{\maze} |
| 31 |
\marg{size}\oarg{seed} is the syntax of the command that generates a maze. Thereinto |
| 32 |
\begin{description} |
| 33 |
\item[\marg{size}] controls the density of the walls inside the maze and directly influences its complexity. It must be a positive integer in the range \textbf{[2,100]}. |
| 34 |
|
| 35 |
To have the package produce a satisfactory output, it is recommended to input a number between 20 and 50 into \marg{text}. Over large numbers may cause \TeX~to exhaust its capacity and fail to produce anything. |
| 36 |
\item[\oarg{seed}] is an optional parameter that specifies the seed for random numbers. If it is omitted, the current time (minute) will be used as the seed instead. |
| 37 |
\end{description}\end{macro} |
| 38 |
|
| 39 |
As an example, the mazes in Figure \ref{fig:mazes} can be created by \cs{maze\{30\}[4]}, \cs{maze\{20\}[7]} and \cs{maze\{25\}}\footnote{This maze is likely to look different because of the difference of compiling time.} respectively. |
| 40 |
\begin{figure}[h] |
| 41 |
\centering |
| 42 |
\maze{30}[4]\hfill\maze{20}[7]\hfill\maze{25} |
| 43 |
\caption{Examples of mazes}\label{fig:mazes} |
| 44 |
\end{figure} |
| 45 |
\clearpage |
| 46 |
\section{Code implementation} |
| 47 |
This package uses the \textsf{expl3} programming layer. Under the scope of \cs{ExplSyntaxOn} we first define some variables |
| 48 |
\begin{lstlisting}[firstnumber=7] |
| 49 |
\int_new:N\l_maze_rand_int % the random variable |
| 50 |
\int_new:N\l_maze_old_int \int_new:N\l_maze_new_int |
| 51 |
\dim_const:Nn\g_maze_size_dim{\linewidth} % store the line width |
| 52 |
\intarray_new:Nn\g_maze_map_intarray{10000} % map of the maze |
| 53 |
\intarray_new:Nn\g_walls_v_intarray{9900} % existence of vertical |
| 54 |
\intarray_new:Nn\g_walls_h_intarray{9900} % /horizontal walls |
| 55 |
\end{lstlisting} |
| 56 |
|
| 57 |
The internal command is defined as \cs{m@ze}. Starting with variable initialization, |
| 58 |
\begin{lstlisting}[firstnumber=last] |
| 59 |
\newcommand{\m@ze}[2]{ |
| 60 |
\sys_gset_rand_seed:n{#2} |
| 61 |
\intarray_gzero:N\g_maze_map_intarray |
| 62 |
\intarray_gzero:N\g_walls_v_intarray |
| 63 |
\intarray_gzero:N\g_walls_h_intarray |
| 64 |
\int_step_inline:nn{#1*#1}{ |
| 65 |
\intarray_gset:Nnn\g_maze_map_intarray{##1}{##1} |
| 66 |
} |
| 67 |
\int_step_inline:nn{#1*(#1-1)}{ |
| 68 |
\intarray_gset:Nnn\g_walls_v_intarray{##1}{1} |
| 69 |
\intarray_gset:Nnn\g_walls_h_intarray{##1}{1} |
| 70 |
} |
| 71 |
\end{lstlisting} |
| 72 |
|
| 73 |
Then we apply the \textit{Kruskal} algorithm to the intarray-based variant of the \textit{Union-find set}. Our loop should end when the beginning and finishing cells are connected. (so that a path exists) |
| 74 |
\begin{lstlisting}[firstnumber=last] |
| 75 |
\bool_do_until:nn{ |
| 76 |
\int_compare_p:nNn{\intarray_item:Nn\g_maze_map_intarray{1}} |
| 77 |
={\intarray_item:Nn\g_maze_map_intarray{#1*#1}} |
| 78 |
}{ |
| 79 |
\int_set:Nn\l_maze_rand_int{\int_rand:n{#1*(#1-1)}} |
| 80 |
\int_compare:nNnTF{0}={\intarray_item:Nn\g_walls_v_intarray{\l_maze_rand_int}}{}{ |
| 81 |
\int_compare:nNnTF{ |
| 82 |
\intarray_item:Nn\g_maze_map_intarray{ |
| 83 |
\l_maze_rand_int+\int_div_truncate:nn{\l_maze_rand_int-1}{#1-1} |
| 84 |
} |
| 85 |
}={ |
| 86 |
\intarray_item:Nn\g_maze_map_intarray{ |
| 87 |
1+\l_maze_rand_int+\int_div_truncate:nn{\l_maze_rand_int-1}{#1-1} |
| 88 |
} |
| 89 |
}{}{ |
| 90 |
\int_set:Nn\l_maze_new_int{ |
| 91 |
\intarray_item:Nn\g_maze_map_intarray{ |
| 92 |
1+\l_maze_rand_int+\int_div_truncate:nn{\l_maze_rand_int-1}{#1-1} |
| 93 |
} |
| 94 |
} |
| 95 |
\int_set:Nn\l_maze_old_int{ |
| 96 |
\intarray_item:Nn\g_maze_map_intarray{ |
| 97 |
\l_maze_rand_int+\int_div_truncate:nn{\l_maze_rand_int-1}{#1-1} |
| 98 |
} |
| 99 |
} |
| 100 |
\intarray_gset:Nnn\g_walls_v_intarray{\l_maze_rand_int}{0} |
| 101 |
\int_step_inline:nn{#1*#1}{ |
| 102 |
\int_compare:nNnTF{\l_maze_old_int}={\intarray_item:Nn\g_maze_map_intarray{##1}} |
| 103 |
{\intarray_gset:Nnn\g_maze_map_intarray{##1}{\l_maze_new_int}}{} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
\int_set:Nn\l_maze_rand_int{\int_rand:n{#1*(#1-1)}} |
| 108 |
\int_compare:nNnTF{0}={\intarray_item:Nn\g_walls_h_intarray{\l_maze_rand_int}}{}{ |
| 109 |
\int_compare:nNnTF{\intarray_item:Nn\g_maze_map_intarray{\l_maze_rand_int}} |
| 110 |
={\intarray_item:Nn\g_maze_map_intarray{#1+\l_maze_rand_int}}{}{ |
| 111 |
\int_set:Nn\l_maze_new_int{ |
| 112 |
\intarray_item:Nn\g_maze_map_intarray{#1+\l_maze_rand_int} |
| 113 |
} |
| 114 |
\int_set:Nn\l_maze_old_int{ |
| 115 |
\intarray_item:Nn\g_maze_map_intarray{\l_maze_rand_int} |
| 116 |
} |
| 117 |
\intarray_gset:Nnn\g_walls_h_intarray{\l_maze_rand_int}{0} |
| 118 |
\int_step_inline:nn{#1*#1}{ |
| 119 |
\int_compare:nNnTF{\l_maze_old_int}={\intarray_item:Nn\g_maze_map_intarray{##1}} |
| 120 |
{\intarray_gset:Nnn\g_maze_map_intarray{##1}{\l_maze_new_int}}{} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
\end{lstlisting} |
| 127 |
|
| 128 |
Lastly, we finish off by defining the user command, which outputs a map of the maze. To set the size and draw the boundaries, |
| 129 |
\begin{lstlisting}[firstnumber=last] |
| 130 |
\NewDocumentCommand\maze{mO{\c_sys_minute_int}}{ |
| 131 |
\m@ze{#1}{#2} |
| 132 |
\setlength{\unitlength}{\fp_eval:n{.4/#1}\g_maze_size_dim} |
| 133 |
\begin{picture}(#1,#1)(0,0) |
| 134 |
\put(0,0){\line(0,1){#1}} \put(#1,#1){\line(0,-1){#1}} |
| 135 |
\put(\int_eval:n{#1-1},#1){\line(-1,0){\int_eval:n{#1-1}}} |
| 136 |
\put(1,0){\line(1,0){\int_eval:n{#1-1}}} |
| 137 |
\end{lstlisting} |
| 138 |
|
| 139 |
We extract from \cs{g\_walls\_h\_intarray} and \cs{g\_walls\_v\_intarray} and draw a line wherever a wall exists. |
| 140 |
\begin{lstlisting}[firstnumber=last] |
| 141 |
\int_step_inline:nn{#1*(#1-1)}{ |
| 142 |
\int_compare:nNnTF{0}={\intarray_item:Nn\g_walls_h_intarray{##1}}{}{ |
| 143 |
\put( |
| 144 |
\int_mod:nn{##1-1}{#1}, |
| 145 |
\int_eval:n{1+\int_div_truncate:nn{##1-1}{#1}} |
| 146 |
){\line(1,0){1}} |
| 147 |
} |
| 148 |
\int_compare:nNnTF{0}={\intarray_item:Nn\g_walls_v_intarray{##1}}{}{ |
| 149 |
\put( |
| 150 |
\int_mod:nn{##1}{#1-1}, |
| 151 |
\int_div_truncate:nn{##1-1}{#1-1} |
| 152 |
){\line(0,1){1}} |
| 153 |
} |
| 154 |
} |
| 155 |
\end{picture} |
| 156 |
} |
| 157 |
\ExplSyntaxOff |
| 158 |
% End of package code |
| 159 |
\end{lstlisting} |
| 160 |
\end{document} |