Debian Codegraph @

texmf-dist/source/latex/tictactoe/tictactoe.dtx

raw
1 %\iffalse meta-comment
2 %Copyright (c) 2025 Peter Rowlett
3 %
4 %The package is licenced under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0). To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/
5 %\fi
6 %\renewcommand*{\thefootnote}{\fnsymbol{footnote}}
7 %\title{tictactoe v1.0}
8 %\author{Peter Rowlett}
9 %\maketitle
10 %
11 %\noindent
12 %\tictactoe{X,,,,O,X,,}{}
13 %\renewcommand{\tictocgridlinescol}{magenta}
14 %\renewcommand{\tictocfullcol}{cyan}
15 %\renewcommand{\tictocfaintcol}{lime}
16 %\tictactoe{o,X,o,X,X,o,X,o,X}{}
17 %\renewcommand{\tictocgridlinescol}{black}
18 %\renewcommand{\tictocfullcol}{black}
19 %\renewcommand{\tictocfaintcol}{lightgray}
20 %\tictactoe{O,X,,X,O,X,X,O,O}{7}
21 %\tictactoe{n,n,X,n,O,n,n,n,n}{}
22 %
23 %\tableofcontents
24 %
25 %\section{Introduction}
26 %
27 %This package provides commands for drawing grids for the game known variously as tic-tac-toe\footnote{I noticed Martin Gardner writes it ticktacktoe, and I learn from Wikipedia that Americans also spell it tick-tack-toe, tick-tat-toe, and tit-tat-toe.}, Noughts and Crosses\footnote{Alternatively, I'm sorry to report, Naughts and Crosses.}, Xs and Os, and so on.
28 %
29 %I have chosen the name \verb|tictactoe| even though I would prefer something based on Noughts and Crosses as I think it is well-recognised and I think command names based on \verb|\noughtsandcrosses| are too unwieldy. The package also provides an option to enable commands based on \verb|\ox|, though I think this is quite short for a command name and am concerned about clashes.
30 %
31 %\section{Drawing a basic board}
32 %
33 %The basic command is \verb|\tictactoe{grid positions}{winning line}|. The \verb|grid positions| are a comma-separated list of nine items, any of which can be blank. The \verb|winning line| is explained later; for now, since it can be blank we will leave it blank.
34 %
35 %For example, to produce a blank board, use \verb|\tictactoe{,,,,,,,,}{}| (left below). To put \nought in one of the cells, put the letter `O' in that position, and put `X' for \cross. For example, \\
36 %\verb|\tictactoe{X,,,,O,,,,}{}| produces the board on the right below.
37 %
38 %\noindent\tictactoe{,,,,,,,,}{}
39 %\tictactoe{X,,,,O,,,,}{}
40 %
41 %\section{Winning lines}
42 %
43 %The second input to the command is which line to mark as the winning line, if any. Winning lines are all a little askew just for the look of thing, I felt it looked odd to be straight through the centre of each cell, especially when drawing diagonally through a winning line of \cross's.
44 %
45 %The rows are numbered 1, 2, and 3. For example the first board here is\\
46 %\verb|\tictactoe{X,X,X,,,,,,}{1}|.
47 %
48 %\noindent\tictactoe{X,X,X,,,,,,}{1}
49 %\tictactoe{,,,X,X,X,,,}{2}
50 %\tictactoe{,,,,,,X,X,X}{3}
51 %
52 %\noindent\tictactoe{O,O,O,,,,,,}{1}
53 %\tictactoe{,,,O,O,O,,,}{2}
54 %\tictactoe{,,,,,,O,O,O}{3}
55 %
56 %The columns are numbered 4, 5, and 6. For example the first board here is\\
57 %\verb|\tictactoe{X,,,X,,,X,,}{4}|.
58 %
59 %\noindent\tictactoe{X,,,X,,,X,,}{4}
60 %\tictactoe{,X,,,X,,,X,}{5}
61 %\tictactoe{,,X,,,X,,,X}{6}
62 %
63 %\noindent\tictactoe{O,,,O,,,O,,}{4}
64 %\tictactoe{,O,,,O,,,O,}{5}
65 %\tictactoe{,,O,,,O,,,O}{6}
66 %
67 %The diagonals are numbered 7 and 8. For example the first board here is\\
68 %\verb|\tictactoe{X,,,,X,,,,X}{7}|.
69 %
70 %\noindent\tictactoe{X,,,,X,,,,X}{7}
71 %\tictactoe{,,X,,X,,X,,}{8}
72 %\tictactoe{O,,,,O,,,,O}{7}
73 %\tictactoe{,,O,,O,,O,,}{8}
74 %
75 %There is no restriction on which line can be marked, for example it does not need to hold three matching symbols. For example \verb|\tictactoe{O,X,X,,O,,,,X}{3}| produces
76 %
77 %\noindent\tictactoe{O,X,X,,O,,,,X}{3}
78 %
79 %\section{Highlighting a move}
80 %
81 %It is sometimes desirable to highlight the current move against the rest of the board. By default (see section \ref{colour} for colour options), the symbols produced by inputting `X' and `O' are black and those produced by `x' and `o' are light grey. For example, \verb|\tictactoe{O,x,,x,o,x,x,o,o}{7}| produces
82 %
83 %\noindent\tictactoe{O,x,,x,o,x,x,o,o}{7}
84 %
85 %\section{Numbering the empty cells}
86 %
87 %It is possible to place a faint cell number in empty cells by putting an `n' in that position. For example \verb|\tictactoe{X,n,n,n,O,n,n,n,n}{}| produces
88 %
89 %\noindent\tictactoe{X,n,n,n,O,n,n,n,n}{}
90 %
91 %\section{\nought and \cross symbols in text}
92 %
93 %Inline in text, write \verb|\nought| and \verb|\cross|. These are by default 1em square and therefore will respond (somewhat) to the font size.
94 %
95 %{\verb|\tiny|: \tiny Inline in text, write \nought and \cross.}
96 %
97 %{\verb|\scriptsize|: \scriptsize Inline in text, write \nought and \cross.}
98 %
99 %{\verb|\footnotesize|: \footnotesize Inline in text, write \nought and \cross.}
100 %
101 %{\verb|\normalsize|: \normalsize Inline in text, write \nought and \cross.}
102 %
103 %{\verb|\large|: \large Inline in text, write \nought and \cross.}
104 %
105 %{\verb|\Large|: \Large Inline in text, write \nought and \cross.}
106 %
107 %{\verb|\huge|: \huge Inline in text, write \nought and \cross.}
108 %
109 %{\verb|\Huge|: \Huge Inline in text, write \nought and \cross.}
110 %
111 %\section{Boards with labels (e.g. scores)}
112 %
113 %For some purposes, it is useful to provide labels under each board, for example these might be scores in a game tree.
114 %
115 %The command \verb|\tictactoescore| is used to add a label below each board. For example, \verb|\tictactoescore{,X,,X,O,X,,O,}{}{-1}| produces
116 %
117 %\noindent\tictactoescore{,X,,X,O,X,,O,}{}{-1}
118 %
119 %\newpage
120 %
121 %The label can in principle be any text, for example \verb|\tictactoescore{,X,,X,O,X,,O,}{}{A}| and \verb|\tictactoescore{,X,,X,O,X,,O,}{}{Peter's Noughts and Crosses game}| produce
122 %
123 %\noindent\tictactoescore{,X,,X,O,X,,O,}{}{A}
124 %\tictactoescore{,X,,X,O,X,,O,}{}{Peter's Noughts and Crosses game}
125 %
126 %By default here there is a box around the grid and corresponding score to associate them. It is possible to draw these without the surrounding box, for example using\\
127 %\verb|\tictactoescorenobox{,X,,X,O,X,,O,}{}{-1}| produces
128 %
129 %\noindent\tictactoescorenobox{,X,,X,O,X,,O,}{}{-1}
130 %
131 %One possible use for boards with scores is in a game tree using the \verb|forest| package. Width is of course an issue, here the nodes further down the tree are sized smaller accordingly (see section \ref{size}).\bigskip
132 %
133 %\noindent\begin{forest}
134 % [{\tictactoescore[0.5]{,X,,X,O,X,,O,}{}{-1}}
135 % [{\tictactoescore[0.3]{O,x,,x,o,x,,o,}{}{0}}
136 % [{\tictactoescore[0.25]{o,x,X,x,o,x,,o,}{}{-1}}
137 % [{\tictactoescore[0.15]{o,x,x,x,o,x,O,o,}{}{+1}}
138 % [{\tictactoescore[0.15]{o,x,x,x,o,x,o,o,X}{6}{+1}}]
139 % ]
140 % [{\tictactoescore[0.15]{o,x,x,x,o,x,,o,O}{7}{-1}}]
141 % ]
142 % [{\tictactoescore[0.25]{o,x,,x,o,x,X,o,}{}{-1}}
143 % [{\tictactoescore[0.15]{o,x,O,x,o,x,x,o,}{}{0}}
144 % [{\tictactoescore[0.15]{o,x,o,x,o,x,x,o,X}{}{0}}]
145 % ]
146 % [{\tictactoescore[0.15]{o,x,,x,o,x,x,o,O}{7}{-1}}]
147 % ]
148 % [{\tictactoescore[0.25]{o,x,,x,o,x,,o,X}{}{0}}
149 % [{\tictactoescore[0.15]{o,x,O,x,o,x,,o,x}{}{0}}
150 % [{\tictactoescore[0.15]{o,x,o,x,o,x,X,o,x}{}{0}}]
151 % ]
152 % [{\tictactoescore[0.15]{o,x,,x,o,x,O,o,x}{}{+1}}
153 % [{\tictactoescore[0.15]{o,x,X,x,o,x,o,o,x}{6}{+1}}]
154 % ]
155 % ]
156 % ]
157 % [{\tictactoescore[0.3]{,x,,x,o,x,O,o,}{}{-1}}
158 % [{\tictactoescore[0.25]{X,x,,x,o,x,o,o,}{}{-1}}
159 % [{\tictactoescore[0.15]{x,x,O,x,o,x,o,o,}{8}{-1}}]
160 % [{\tictactoescore[0.15]{x,x,,x,o,x,o,o,O}{3}{-1}}]
161 % ]
162 % [{\tictactoescore[0.25]{,x,X,x,o,x,o,o,}{}{-1}}
163 % [{\tictactoescore[0.15]{O,x,x,x,o,x,o,o,}{}{+1}}
164 % [{\tictactoescore[0.15]{o,x,x,x,o,x,o,o,X}{6}{+1}}]
165 % ]
166 % [{\tictactoescore[0.15]{,x,x,x,o,x,o,o,O}{3}{-1}}]
167 % ]
168 % [{\tictactoescore[0.25]{,x,,x,o,x,o,o,X}{}{-1}}
169 % [{\tictactoescore[0.15]{O,x,,x,o,x,o,o,x}{}{+1}}
170 % [{\tictactoescore[0.15]{o,x,X,x,o,x,o,o,x}{6}{+1}}]
171 % ]
172 % [{\tictactoescore[0.15]{,x,O,x,o,x,o,o,x}{8}{-1}}]
173 % ]
174 % ]
175 % ]
176 %\end{forest}
177 %
178 %The code that produced this diagram is below.
179 %
180 %\begin{verbatim}\begin{forest}
181 %[{\tictactoescore[0.5]{,X,,X,O,X,,O,}{}{-1}}
182 % [{\tictactoescore[0.3]{O,x,,x,o,x,,o,}{}{0}}
183 % [{\tictactoescore[0.25]{o,x,X,x,o,x,,o,}{}{-1}}
184 % [{\tictactoescore[0.1]{o,x,x,x,o,x,O,o,}{}{+1}}
185 % [{\tictactoescore[0.1]{o,x,x,x,o,x,o,o,X}{6}{+1}}]
186 % ]
187 % [{\tictactoescore[0.1]{o,x,x,x,o,x,,o,O}{7}{-1}}]
188 % ]
189 % [{\tictactoescore[0.25]{o,x,,x,o,x,X,o,}{}{-1}}
190 % [{\tictactoescore[0.1]{o,x,O,x,o,x,x,o,}{}{0}}
191 % [{\tictactoescore[0.1]{o,x,o,x,o,x,x,o,X}{}{0}}]
192 % ]
193 % [{\tictactoescore[0.1]{o,x,,x,o,x,x,o,O}{7}{-1}}]
194 % ]
195 % [{\tictactoescore[0.25]{o,x,,x,o,x,,o,X}{}{0}}
196 % [{\tictactoescore[0.1]{o,x,O,x,o,x,,o,x}{}{0}}
197 % [{\tictactoescore[0.1]{o,x,o,x,o,x,X,o,x}{}{0}}]
198 % ]
199 % [{\tictactoescore[0.1]{o,x,,x,o,x,O,o,x}{}{+1}}
200 % [{\tictactoescore[0.1]{o,x,X,x,o,x,o,o,x}{6}{+1}}]
201 % ]
202 % ]
203 % ]
204 % [{\tictactoescore[0.3]{,x,,x,o,x,O,o,}{}{-1}}
205 % [{\tictactoescore[0.25]{X,x,,x,o,x,o,o,}{}{-1}}
206 % [{\tictactoescore[0.1]{x,x,O,x,o,x,o,o,}{8}{-1}}]
207 % [{\tictactoescore[0.1]{x,x,,x,o,x,o,o,O}{3}{-1}}]
208 % ]
209 % [{\tictactoescore[0.25]{,x,X,x,o,x,o,o,}{}{-1}}
210 % [{\tictactoescore[0.1]{O,x,x,x,o,x,o,o,}{}{+1}}
211 % [{\tictactoescore[0.1]{o,x,x,x,o,x,o,o,X}{6}{+1}}]
212 % ]
213 % [{\tictactoescore[0.1]{,x,x,x,o,x,o,o,O}{3}{-1}}]
214 % ]
215 % [{\tictactoescore[0.25]{,x,,x,o,x,o,o,X}{}{-1}}
216 % [{\tictactoescore[0.1]{O,x,,x,o,x,o,o,x}{}{+1}}
217 % [{\tictactoescore[0.1]{o,x,X,x,o,x,o,o,x}{6}{+1}}]
218 % ]
219 % [{\tictactoescore[0.1]{,x,O,x,o,x,o,o,x}{8}{-1}}]
220 % ]
221 % ]
222 %]
223 %\end{forest}\end{verbatim}
224 %
225 %\newpage
226 %
227 %\section{Customisation}
228 %
229 %\subsection{Size}\label{size}
230 %
231 %Different sizes are possible by passing a scale factor when calling commands. For example, \verb|\tictactoe[scale]{O,X,,X,O,X,X,O,O}{7}| produces, for \verb|0.2|, \verb|0.5|, \verb|3|,
232 %
233 %\noindent\tictactoe[0.2]{O,X,,X,O,X,X,O,O}{7}
234 %\tictactoe[0.5]{O,X,,X,O,X,X,O,O}{7}
235 %\tictactoe[3]{O,X,,X,O,X,X,O,O}{7}
236 %
237 %It is possible to pass a scale factor to \verb|\tictactoe|, \verb|\tictactoescore|, and \verb|\tictactoescorenobox|. It is also possible to pass a scale factor to \verb|\nought| and \verb|\cross|, but note these do not align\footnote{This is because \texttt{\textbackslash tictactoe} etc. are based on the default TikZ unit (cm) so they don't respond when the font size changes, while \texttt{\textbackslash nought} and \texttt{\textbackslash cross} are based on em, so they do. Thus \texttt{\textbackslash nought} and \texttt{\textbackslash cross} have dimensions 1em, and scaling this doesn't scale as quickly as 1cm.}, meaning for example \verb|\cross[2]| doesn't produce a cross as big as a cross in \verb|\tictactoe[2]{X,,,,,,,,}{}|. \smallskip
238 %
239 %\begin{minipage}[t]{0.5\linewidth}
240 % Here's \verb|\cross[2]|: \cross[2]
241 %\end{minipage}\begin{minipage}[t]{0.5\linewidth}
242 % Here's \verb|\tictactoe[2]{X,,,,,,,,}{}|:
243 %
244 % \noindent\tictactoe[2]{X,,,,,,,,}{}
245 %\end{minipage}
246 %
247 %\subsection{Colour}\label{colour}
248 %
249 %Four colours are defined by commands.
250 %
251 %\begin{tabular}{ll}
252 % \verb|\tictocfullcol| & full colour pieces\\
253 % \verb|\tictocfaintcol| & faint colour pieces\\
254 % \verb|\tictocwincol| & winning lines\\
255 % \verb|\tictocgridlinescol| & gridlines\\
256 %\end{tabular}
257 %
258 %The default colours are defined like this.
259 %
260 %\begin{verbatim}\newcommand{\tictocfullcol}{black}
261 %\newcommand{\tictocfaintcol}{lightgray}
262 %\newcommand{\tictocwincol}{gray}
263 %\newcommand{\tictocgridlinescol}{black}\end{verbatim}
264 %
265 %It is therefore possible to change these by redefining these commands. For example
266 %
267 %\begin{verbatim}\renewcommand{\tictocfullcol}{lime}
268 %\renewcommand{\tictocfaintcol}{cyan}
269 %\renewcommand{\tictocwincol}{yellow}
270 %\renewcommand{\tictocgridlinescol}{magenta}
271 %\tictactoe{X,x,O,o,n,X,X,X,X}{3}\end{verbatim}
272 %
273 %produces
274 %
275 %\renewcommand{\tictocfullcol}{lime}
276 %\renewcommand{\tictocfaintcol}{cyan}
277 %\renewcommand{\tictocwincol}{yellow}
278 %\renewcommand{\tictocgridlinescol}{magenta}
279 %
280 %\noindent\tictactoe{X,x,O,o,n,X,X,X,X}{3}
281 %
282 %\renewcommand{\tictocfullcol}{black}
283 %\renewcommand{\tictocfaintcol}{lightgray}
284 %\renewcommand{\tictocwincol}{gray}
285 %\renewcommand{\tictocgridlinescol}{black}
286 %
287 %As well as just changing the colouring, one possible application is to colour \nought's and \cross's in different colours, if you then use uppercase for one and lowercase for the other, and if you don't wish to highlight a move. For example
288 %
289 %\begin{verbatim}\renewcommand{\tictocfullcol}{red}
290 %\renewcommand{\tictocfaintcol}{blue}
291 %\tictactoe{o,X,o,X,X,o,X,o,X}{}\end{verbatim}
292 %
293 %produces
294 %
295 %\renewcommand{\tictocfullcol}{red}
296 %\renewcommand{\tictocfaintcol}{blue}
297 %
298 %\noindent\tictactoe{o,X,o,X,X,o,X,o,X}{}
299 %
300 %\renewcommand{\tictocfullcol}{black}
301 %\renewcommand{\tictocfaintcol}{lightgray}
302 %
303 %\newpage
304 %
305 %\subsection{Different symbols}
306 %
307 %The commands like \verb|\tictactoe| expect input to be one of the characters `X', `x', `O', `o', or `n'. As a fallback (and not really intended as a feature), if the input is none of these, it tries to place the input into the cell directly. For example, the first of these is produced by \verb"\tictactoe{\#,*,\&,\%,5,;,:,=,!}{}".
308 %
309 %\noindent\tictactoe{\#,*,\&,\%,5,;,:,=,!}{}%
310 %\tictactoe{,,Z,,Y,,?,,}{}%
311 %\tictactoe[2.5]{corner,edge,corner,edge,centre,edge,corner,edge,corner}{}
312 %
313 %\subsubsection{Letters that are not permitted, and how to permit them}
314 %
315 %
316 %If you want to put a standard letter `X', `x', `O', `o', or `n' in the grid, you cannot because the package will draw its own symbol or number instead. However you can get around this by redefining commands.
317 %
318 %For example, input `X' is tested against the value of \verb|\tictacX| and if it matches, it draws a cross. (And there are similar commands for the other inputs.) If after loading \verb|tictactoe| you redefine \verb|\tictacX| to some other letter, then your letter becomes the trigger for drawing a cross and you can put an `X' in the grid without obstruction. For example, the code below redefines these commands to use `P', `p', `Q', `q', and `r', allowing `X', `x', `O', `o', and `n' in the grid.
319 %
320 %\begin{verbatim}\documentclass{article}
321 %\usepackage{tictactoe}
322 %\renewcommand{\tictacX}{P}
323 %\renewcommand{\tictacx}{p}
324 %\renewcommand{\tictacO}{Q}
325 %\renewcommand{\tictaco}{q}
326 %\renewcommand{\tictacn}{r}
327 %\begin{document}
328 % \tictactoe{X,O,x,o,n,X,X,X,X}{3}
329 % \tictactoe{P,p,Q,q,r,P,P,P,P}{3}
330 %\end{document}\end{verbatim}
331 %
332 %\newpage
333 %
334 %This will produce
335 %
336 %\renewcommand{\tictacX}{P}
337 %\renewcommand{\tictacx}{p}
338 %\renewcommand{\tictacO}{Q}
339 %\renewcommand{\tictaco}{q}
340 %\renewcommand{\tictacn}{r}
341 %\tictactoe{X,x,O,o,n,X,X,X,X}{3}
342 %\tictactoe{P,p,Q,q,r,P,P,P,P}{3}
343 %\renewcommand{\tictacX}{X}
344 %\renewcommand{\tictacx}{x}
345 %\renewcommand{\tictacO}{O}
346 %\renewcommand{\tictaco}{o}
347 %\renewcommand{\tictacn}{n}
348 %
349 %To restore the default values use
350 %
351 %\begin{verbatim}\renewcommand{\tictacX}{X}
352 %\renewcommand{\tictacx}{x}
353 %\renewcommand{\tictacO}{O}
354 %\renewcommand{\tictaco}{o}
355 %\renewcommand{\tictacn}{n}\end{verbatim}
356 %
357 %\subsection{\nought{}s and \cross{s} and \nought's and \cross's}
358 %
359 %The commands \verb|\nought| and \verb|\cross| use the \verb|xspace| package to place a space after them or not depending on context. For example in the code
360 %
361 %\begin{verbatim}Place in the grid either \nought or \cross.\end{verbatim}
362 %
363 %it knows to put a space after the \nought but not after the \cross.
364 %
365 %One issue with this is that sometimes you do want another character straight after the command. Sometimes this works fine, for example if you want ``\nought's'', you simply use \verb|\nought's|. However, a letter after the symbol is more problematic: \verb|\noughts| is not valid, and \verb|\nought s| produces a space, like ``\nought s''. The solution is to use an empty set of braces, for example \verb|\nought{}s| produces \nought{}s.
366 %
367 %\subsection{Non-American English command options}\label{ox}
368 %
369 %Instead of \verb|\tictactoe|, \verb|\tictactoescore|, and \verb|\tictactoescorenobox|, you can use \verb|\ox|, \verb|\oxscore|, and \verb|\oxscorenobox| if you enable the \verb|ox| option when loading the package.
370 %
371 %\begin{verbatim}\usepackage[ox]{tictactoe}\end{verbatim}
372 %
373 %These are used in exactly the same way, including optional scale factor (in fact, the \verb|\tictactoe| versions are still defined, just the \verb|\ox| commands pass your input onto their American cousins). For example, \verb|\ox{O,x,,x,o,x,x,o,o}{7}| produces (provided the \verb|ox| option was loaded)
374 %
375 %\noindent\ox{O,x,,x,o,x,x,o,o}{7}
376 %
377 %If you get a clash because \verb|\ox| is already used for something else, simply remove the \verb|[ox]| option, grit your teeth, and use \verb|\tictactoe|.
378 %
379 %\newpage
380 %
381 %\appendix
382 %
383 %\section{Command and related names used by this package}
384 %
385 %I tried to name non-user commands, counters, and TikZ style with the prefix \verb|tictac|, to hopefully avoid clashes. Details below.
386 %
387 %\subsection{Counters}
388 %
389 %\begin{verbatim}tictacrowmakercounter
390 %tictacrowmakermax
391 %tictaccellcounter\end{verbatim}
392 %
393 %\subsection{Commands}
394 %
395 %\begin{verbatim}\tictacX
396 %\tictacx
397 %\tictacO
398 %\tictaco
399 %\tictacn
400 %\tictacscale
401 %\tictocfullcol
402 %\tictocfaintcol
403 %\tictocwincol
404 %\tictocgridlinescol
405 %\tictaccellmaker
406 %\tictaccellmarker
407 %\tictacrowmaker
408 %\tictactoegrid
409 %\cross
410 %\nought
411 %\tictactoe
412 %\tictactoescore
413 %\tictactoescorenobox
414 %\tictacscorewidth
415 %\tictacfontsize
416 %\tictacfontspacing
417 %\ox
418 %\oxscore
419 %\oxscorenobox\end{verbatim}
420 %
421 %\subsection{TikZ style names}
422 %
423 %Within TikZ, the following styles are defined:
424 %\begin{verbatim}tictacfullcirc
425 %tictacfullcross
426 %tictacfaintcirc
427 %tictacfaintcross
428 %tictacwin\end{verbatim}
429 %
430 %\subsection{Other}
431 %
432 %The package also creates an \verb|\iftictactoeox|.
433 %\iffalse
434 %<*documentation>
435 \documentclass[a4paper]{article}
436 \usepackage{a4wide}
437 \usepackage[ox]{tictactoe}
438 \usepackage{doc}
439 \usepackage{forest}
440 \usepackage{upquote}
441 \begin{document}
442 \DocInput{tictactoe.dtx}
443 \end{document}
444 %</documentation>
445 %\fi
446 %\newpage
447 %
448 % \section{Implementation}
449 %
450 % \begin{macrocode}
451 %<*tictactoe>
452 \ProvidesPackage{tictactoe}[2024/06/20 tictactoe 1.0]
453
454 % dependencies
455 \RequirePackage{tikz}
456 \RequirePackage{xspace}
457
458 \makeatletter
459 \@ifundefined{c@tictacrowmakercounter}{}
460 {\PackageWarning{tictactoe}{counter `tictacrowmakercounter' already defined}}
461 \@ifundefined{c@tictacrowmakermax}{}
462 {\PackageWarning{tictactoe}{counter `tictacrowmakermax' already defined}}
463 \@ifundefined{c@tictaccellcounter}{}
464 {\PackageWarning{tictactoe}{counter `tictaccellcounter' already defined}}
465 \@ifundefined{tictacX}{}
466 {\PackageWarning{tictactoe}{command `tictacX' already defined}}
467 \@ifundefined{tictacx}{}
468 {\PackageWarning{tictactoe}{command `tictacx' already defined}}
469 \@ifundefined{tictacO}{}
470 {\PackageWarning{tictactoe}{command `tictacO' already defined}}
471 \@ifundefined{tictaco}{}
472 {\PackageWarning{tictactoe}{command `tictaco' already defined}}
473 \@ifundefined{tictacn}{}
474 {\PackageWarning{tictactoe}{command `tictacn' already defined}}
475 \@ifundefined{tictacscale}{}
476 {\PackageWarning{tictactoe}{command `tictacscale' already defined}}
477 \@ifundefined{tictocfullcol}{}
478 {\PackageWarning{tictactoe}{command `tictocfullcol' already defined}}
479 \@ifundefined{tictocfaintcol}{}
480 {\PackageWarning{tictactoe}{command `tictocfaintcol' already defined}}
481 \@ifundefined{tictocwincol}{}
482 {\PackageWarning{tictactoe}{command `tictocwincol' already defined}}
483 \@ifundefined{tictocgridlinescol}{}
484 {\PackageWarning{tictactoe}{command `tictocgridlinescol' already defined}}
485 \@ifundefined{tictaccellmaker}{}
486 {\PackageWarning{tictactoe}{command `tictaccellmaker' already defined}}
487 \@ifundefined{tictaccellmarker}{}
488 {\PackageWarning{tictactoe}{command `tictaccellmarker' already defined}}
489 \@ifundefined{tictacrowmaker}{}
490 {\PackageWarning{tictactoe}{command `tictacrowmaker' already defined}}
491 \@ifundefined{tictactoegrid}{}
492 {\PackageWarning{tictactoe}{command `tictactoegrid' already defined}}
493 \@ifundefined{cross}{}
494 {\PackageWarning{tictactoe}{command `cross' already defined}}
495 \@ifundefined{nought}{}
496 {\PackageWarning{tictactoe}{command `nought' already defined}}
497 \@ifundefined{tictactoe}{}
498 {\PackageWarning{tictactoe}{command `tictactoe' already defined}}
499 \@ifundefined{tictactoescore}{}
500 {\PackageWarning{tictactoe}{command `tictactoescore' already defined}}
501 \@ifundefined{tictactoescorenobox}{}
502 {\PackageWarning{tictactoe}{command `tictactoescorenobox' already defined}}
503 \@ifundefined{tictacscorewidth}{}
504 {\PackageWarning{tictactoe}{command `tictacscorewidth' already defined}}
505 \@ifundefined{tictacfontsize}{}
506 {\PackageWarning{tictactoe}{command `tictacfontsize' already defined}}
507 \@ifundefined{tictacfontspacing}{}
508 {\PackageWarning{tictactoe}{command `tictacfontspacing' already defined}}
509 \makeatother
510
511 % non-US spelling option
512 \newif\iftictactoeox
513 \tictactoeoxfalse
514 \DeclareOption{ox}{\tictactoeoxtrue}
515 \ProcessOptions\relax
516 \iftictactoeox % add the extra options
517 \makeatletter
518 \@ifundefined{ox}{}{\PackageWarning{tictactoe}{command `ox' already defined}}
519 \@ifundefined{oxscore}{}{\PackageWarning{tictactoe}{command `oxscore' already defined}}
520 \@ifundefined{oxscorenobox}{}{\PackageWarning{tictactoe}{command `oxscorenobox' already defined}}
521 \makeatother
522 \newcommand{\ox}[3][1]{\tictactoe[#1]{#2}{#3}}
523 \newcommand{\oxscore}[4][1]{\tictactoescore[#1]{#2}{#3}{#4}}
524 \newcommand{\oxscorenobox}[4][1]{\tictactoescorenobox[#1]{#2}{#3}{#4}}
525 \fi
526
527 % for tracking position while drawing grids
528 \newcounter{tictacrowmakercounter}
529 \newcounter{tictacrowmakermax}
530 \newcounter{tictaccellcounter}
531
532 % these are the symbols that trigger drawing in the grid
533 \newcommand{\tictacX}{X} % full cross
534 \newcommand{\tictacx}{x} % faint cross
535 \newcommand{\tictacO}{O} % full nought
536 \newcommand{\tictaco}{o} % faint nought
537 \newcommand{\tictacn}{n} % number of the cell
538
539 % scale factor and sizes (given values by user commands)
540 \newcommand{\tictacscale}{}
541 \newcommand{\tictacscorewidth}{}
542 \newcommand{\tictacfontsize}{}
543
544 % colours
545 \newcommand{\tictocfullcol}{black}
546 \newcommand{\tictocfaintcol}{lightgray}
547 \newcommand{\tictocwincol}{gray}
548 \newcommand{\tictocgridlinescol}{black}
549
550 % internal command: draws a cell with a little picture of a nought or a cross
551 \newcommand{\tictaccellmaker}[2]{
552 \setcounter{tictaccellcounter}{#1}
553 \if\thetictaccellcounter0
554 \path (0.5*\tictacscale*0.667,2.5*\tictacscale*0.667) pic{#2};
555 \else\if\thetictaccellcounter1
556 \path (1.5*\tictacscale*0.667,2.5*\tictacscale*0.667) pic{#2};
557 \else\if\thetictaccellcounter2
558 \path (2.5*\tictacscale*0.667,2.5*\tictacscale*0.667) pic{#2};
559 \else\if\thetictaccellcounter3
560 \path (0.5*\tictacscale*0.667,1.5*\tictacscale*0.667) pic{#2};
561 \else\if\thetictaccellcounter4
562 \path (1.5*\tictacscale*0.667,1.5*\tictacscale*0.667) pic{#2};
563 \else\if\thetictaccellcounter5
564 \path (2.5*\tictacscale*0.667,1.5*\tictacscale*0.667) pic{#2};
565 \else\if\thetictaccellcounter6
566 \path (0.5*\tictacscale*0.667,0.5*\tictacscale*0.667) pic{#2};
567 \else\if\thetictaccellcounter7
568 \path (1.5*\tictacscale*0.667,0.5*\tictacscale*0.667) pic{#2};
569 \else\if\thetictaccellcounter8
570 \path (2.5*\tictacscale*0.667,0.5*\tictacscale*0.667) pic{#2};
571 \fi\fi\fi\fi\fi\fi\fi\fi\fi
572 }
573
574 % internal command: draws a cell as a node with text in
575 \newcommand{\tictaccellmarker}[4][1]{
576 \pgfmathsetmacro{\tictacfontsize}{#1*#1*20}
577 \setcounter{tictaccellcounter}{#3}
578 \if\thetictaccellcounter0
579 \node[#2] at (0.5*\tictacscale*0.667,2.5*\tictacscale*0.667)
580 {\sffamily\fontsize{\tictacfontsize pt}{0pt}\selectfont #4};
581 \else\if\thetictaccellcounter1
582 \node[#2] at (1.5*\tictacscale*0.667,2.5*\tictacscale*0.667)
583 {\sffamily\fontsize{\tictacfontsize pt}{0pt}\selectfont #4};
584 \else\if\thetictaccellcounter2
585 \node[#2] at (2.5*\tictacscale*0.667,2.5*\tictacscale*0.667)
586 {\sffamily\fontsize{\tictacfontsize pt}{0pt}\selectfont #4};
587 \else\if\thetictaccellcounter3
588 \node[#2] at (0.5*\tictacscale*0.667,1.5*\tictacscale*0.667)
589 {\sffamily\fontsize{\tictacfontsize pt}{0pt}\selectfont #4};
590 \else\if\thetictaccellcounter4
591 \node[#2] at (1.5*\tictacscale*0.667,1.5*\tictacscale*0.667)
592 {\sffamily\fontsize{\tictacfontsize pt}{0pt}\selectfont #4};
593 \else\if\thetictaccellcounter5
594 \node[#2] at (2.5*\tictacscale*0.667,1.5*\tictacscale*0.667)
595 {\sffamily\fontsize{\tictacfontsize pt}{0pt}\selectfont #4};
596 \else\if\thetictaccellcounter6
597 \node[#2] at (0.5*\tictacscale*0.667,0.5*\tictacscale*0.667)
598 {\sffamily\fontsize{\tictacfontsize pt}{0pt}\selectfont #4};
599 \else\if\thetictaccellcounter7
600 \node[#2] at (1.5*\tictacscale*0.667,0.5*\tictacscale*0.667)
601 {\sffamily\fontsize{\tictacfontsize pt}{0pt}\selectfont #4};
602 \else\if\thetictaccellcounter8
603 \node[#2] at (2.5*\tictacscale*0.667,0.5*\tictacscale*0.667)
604 {\sffamily\fontsize{\tictacfontsize pt}{0pt}\selectfont #4};
605 \fi\fi\fi\fi\fi\fi\fi\fi\fi
606 }
607
608 % internal command: draws a row of three cells based on #3 starting from position #2
609 \newcommand{\tictacrowmaker}[3][1]{%
610 \setcounter{tictacrowmakercounter}{#2}
611 \setcounter{tictacrowmakermax}{#2}
612 \addtocounter{tictacrowmakermax}{2} % start + 2 makes three
613 \foreach \l [count = \i from 0] in {#3} {
614 \if\i\thetictacrowmakercounter
615 \if\l\tictacX % full cross
616 \tictaccellmaker{\i}{tictacfullcross};
617 \else\if\l\tictacO % full nought
618 \tictaccellmaker{\i}{tictacfullcirc};
619 \else\if\l\tictaco % faint nought
620 \tictaccellmaker{\i}{tictacfaintcirc};
621 \else\if\l\tictacx % faint cross
622 \tictaccellmaker{\i}{tictacfaintcross};
623 \else\if\l\tictacn % number of the cell
624 \tictaccellmarker[#1]{\tictocfaintcol}{\i}{\pgfmathparse{int(\i+1)}\pgfmathresult}
625 \else % just try to draw the symbol that has been provided as input
626 \tictaccellmarker[#1]{\tictocfullcol}{\i}{\l}
627 \fi\fi\fi\fi\fi
628 \if\thetictacrowmakercounter\thetictacrowmakermax\else
629 \addtocounter{tictacrowmakercounter}{1} % if we haven't done three, keep outputting
630 \fi
631 \fi
632 }
633 }
634
635 % internal command: draw a 3x3 grid
636 \newcommand{\tictactoegrid}[3][1]
637 {%
638 \renewcommand{\tictacscale}{#1*1.5}%
639 \begin{tikzpicture}[line width=\tictacscale*0.07 cm]
640 \tikzset{ % adapted from https://tex.stackexchange.com/a/634705
641 tictacfullcirc/.pic={\draw[\tictocfullcol] circle (0.2*\tictacscale);},
642 tictacfullcross/.pic={\clip (-0.2*\tictacscale,-0.2*\tictacscale) rectangle
643 (0.2*\tictacscale,0.2*\tictacscale);
644 \draw[\tictocfullcol] (-0.2*\tictacscale,-0.2*\tictacscale) --
645 (0.2*\tictacscale,0.2*\tictacscale) (-0.2*\tictacscale,0.2*\tictacscale) --
646 (0.2*\tictacscale,-0.2*\tictacscale);},
647 tictacfaintcirc/.pic={\draw[\tictocfaintcol] circle (0.2*\tictacscale);},
648 tictacfaintcross/.pic={\draw[\tictocfaintcol] (-0.2*\tictacscale,-0.2*\tictacscale) --
649 (0.2*\tictacscale,0.2*\tictacscale) (-0.2*\tictacscale,0.2*\tictacscale) --
650 (0.2*\tictacscale,-0.2*\tictacscale);},
651 tictacwin/.style={\tictocwincol,opacity=0.7,line width=\tictacscale*0.07 cm} % winning line
652 }
653
654 % grid
655 \phantom{\draw (-0.1*\tictacscale,-0.1*\tictacscale) rectangle
656 (2.1*\tictacscale,2.1*\tictacscale);} % make each pic the same size
657 \foreach \i in {1,2}{\draw[line width=\tictacscale*0.03 cm,\tictocgridlinescol]
658 (\i*\tictacscale*0.667,0) -- (\i*\tictacscale*0.667,\tictacscale*2)
659 (0,\i*\tictacscale*0.667) -- (\tictacscale*2,\i*\tictacscale*0.667);}
660
661 % cells
662 \foreach \y in {0,3,6}{
663 \tictacrowmaker[#1]{\y}{#2}
664 }
665
666 % winning lines
667 \foreach \l in {#3}
668 {
669 \if\l1
670 \draw[tictacwin] (0,\tictacscale*1.733) -- (2*\tictacscale,\tictacscale*1.633);
671 \else\if\l2
672 \draw[tictacwin] (0,\tictacscale*0.95) -- (2*\tictacscale,\tictacscale*1.05);
673 \else\if\l3
674 \draw[tictacwin] (0,\tictacscale*0.367) -- (2*\tictacscale,\tictacscale*0.267);
675 \else\if\l4
676 \draw[tictacwin] (\tictacscale*0.267,2*\tictacscale) -- (\tictacscale*0.367,0);
677 \else\if\l5
678 \draw[tictacwin] (\tictacscale*1.05,2*\tictacscale) -- (\tictacscale*0.95,0);
679 \else\if\l6
680 \draw[tictacwin] (\tictacscale*1.633,2*\tictacscale) -- (\tictacscale*1.733,0);
681 \else\if\l7
682 \draw[tictacwin] (0,1.8*\tictacscale) -- (2*\tictacscale,0.2*\tictacscale);
683 \else\if\l8
684 \draw[tictacwin] (0,0.2*\tictacscale) -- (2*\tictacscale,1.8*\tictacscale);
685 \fi\fi\fi\fi\fi\fi\fi\fi
686 }
687 \end{tikzpicture}%
688 }
689
690 % user commands: nought and cross in text
691 \newcommand{\cross}[1][1]{%
692 \renewcommand{\tictacscale}{#1}\begin{tikzpicture}[line width=\tictacscale*0.12 em, baseline]
693 \clip (-0.33*\tictacscale em,0) rectangle (0.33*\tictacscale em,0.66*\tictacscale em);
694 \draw[\tictocfullcol] (-0.33*\tictacscale em,0) -- (0.33*\tictacscale em,0.66*\tictacscale em)
695 (-0.33*\tictacscale em,0.66*\tictacscale em) -- (0.33*\tictacscale em,0);
696 \end{tikzpicture}\xspace}
697 \newcommand{\nought}[1][1]{%
698 \renewcommand{\tictacscale}{#1}\begin{tikzpicture}[line width=\tictacscale*0.12 em, baseline]
699 \draw[\tictocfullcol] (0,0.33*\tictacscale em) circle (0.33*\tictacscale em);
700 \end{tikzpicture}\xspace}
701
702 % user command: draw a grid
703 \newcommand{\tictactoe}[3][1]{%
704 \pgfmathsetmacro{\tictacscorewidth}{#1*3.5}%
705 \begin{minipage}{\tictacscorewidth cm}\centering
706 \tictactoegrid[#1]{#2}{#3}
707 \end{minipage}%
708 }
709
710 % user command: draw a grid with a number underneath in a box
711 \newcommand{\tictactoescore}[4][1]{%
712 \pgfmathsetmacro{\tictacscorewidth}{#1*3.4}%
713 \pgfmathsetmacro{\tictacfontsize}{#1*18}%
714 \pgfmathsetmacro{\tictacfontspacing}{#1*15}%
715 \fbox{\begin{minipage}{\tictacscorewidth cm}\centering
716 \tictactoegrid[#1]{#2}{#3}\\
717 \fontsize{\tictacfontsize pt}{\tictacfontspacing pt}\selectfont #4
718 \end{minipage}}%
719 }
720
721 % user command: draw a grid with a number underneath but no box
722 \newcommand{\tictactoescorenobox}[4][1]{%
723 \pgfmathsetmacro{\tictacscorewidth}{#1*3.4}%
724 \pgfmathsetmacro{\tictacfontsize}{#1*18}%
725 \pgfmathsetmacro{\tictacfontspacing}{#1*15}%
726 \begin{minipage}{\tictacscorewidth cm}\centering
727 \tictactoegrid[#1]{#2}{#3}\\
728 \fontsize{\tictacfontsize pt}{\tictacfontspacing pt}\selectfont #4
729 \end{minipage}%
730 }
731 %</tictactoe>
732 % \end{macrocode}