summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/hexboard/hexboard.sty
blob: 7ac3877c8000a548248b52f810cf59c3669d2c67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
%%
%% This is file `hexboard.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% hexboard.dtx  (with options: `hexboard')
%% Copyright (c) 2022 Peter Rowlett and Chris Sangwin
%% 
%% 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/
\ProvidesPackage{hexboard}[2022/02/19 hexboard 1.0]

\RequirePackage{tikz}
\usetikzlibrary{shapes.geometric}
\RequirePackage{xstring}

\makeatletter
\@ifundefined{c@hexedge}{}
{\PackageWarning{hexboard}{Counter `hexedge' already defined}}
\@ifundefined{c@hexmovecount}{}
{\PackageWarning{hexboard}{Counter `hexmovecount' already defined}}
\@ifundefined{c@hexmoveskips}{}
{\PackageWarning{hexboard}{Counter `hexmoveskips' already defined}}
\@ifundefined{c@hexletternum}{}
{\PackageWarning{hexboard}{Counter `hexletternum' already defined}}
\@ifundefined{hexboard}{}
{\PackageWarning{hexboard}{command `hexboard' already defined}}
\@ifundefined{hexcounter}{}
{\PackageWarning{hexboard}{command `hexcounter' already defined}}
\@ifundefined{hexcellshaded}{}
{\PackageWarning{hexboard}{command `hexcellshaded' already defined}}
\@ifundefined{hexdot}{}
{\PackageWarning{hexboard}{command `hexdot' already defined}}
\@ifundefined{hexconnect}{}
{\PackageWarning{hexboard}{command `hexconnect' already defined}}
\@ifundefined{hexcontent}{}
{\PackageWarning{hexboard}{command `hexcontent' already defined}}
\@ifundefined{hexcell}{}
{\PackageWarning{hexboard}{command `hexcell' already defined}}
\@ifundefined{hexgridnolabel}{}
{\PackageWarning{hexboard}{command `hexgridnolabel' already defined}}
\@ifundefined{hexreducing}{}
{\PackageWarning{hexboard}{command `hexreducing' already defined}}
\@ifundefined{hexshadedsubrow}{}
{\PackageWarning{hexboard}{command `hexshadedsubrow' already defined}}
\@ifundefined{hexgrid}{}
{\PackageWarning{hexboard}{command `hexgrid' already defined}}
\@ifundefined{setcolorA}{}
{\PackageWarning{hexboard}{command `setcolorA' already defined}}
\@ifundefined{setcolorB}{}
{\PackageWarning{hexboard}{command `setcolorB' already defined}}
\@ifundefined{hexscale}{}
{\PackageWarning{hexboard}{command `hexscale' already defined}}
\@ifundefined{xchor}{}
{\PackageWarning{hexboard}{command `xchor' already defined}}
\@ifundefined{ychor}{}
{\PackageWarning{hexboard}{command `ychor' already defined}}
\@ifundefined{colorA}{}
{\PackageWarning{hexboard}{command `colorA' already defined}}
\@ifundefined{colorB}{}
{\PackageWarning{hexboard}{command `colorB' already defined}}
\@ifundefined{hexlinewidth}{}
{\PackageWarning{hexboard}{command `hexlinewidth' already defined}}
\@ifundefined{hexedgewidth}{}
{\PackageWarning{hexboard}{command `hexedgewidth' already defined}}
\@ifundefined{hexthinline}{}
{\PackageWarning{hexboard}{command `hexthinline' already defined}}
\@ifundefined{hexcoord}{}
{\PackageWarning{hexboard}{command `hexcoord' already defined}}
\@ifundefined{hexsize}{}
{\PackageWarning{hexboard}{command `hexsize' already defined}}
\@ifundefined{Ia}{}
{\PackageWarning{hexboard}{command `Ia' already defined}}
\@ifundefined{Ib}{}
{\PackageWarning{hexboard}{command `Ib' already defined}}
\@ifundefined{Iaend}{}
{\PackageWarning{hexboard}{command `Iaend' already defined}}
\@ifundefined{leftofIa}{}
{\PackageWarning{hexboard}{command `leftofIa' already defined}}
\@ifundefined{belowIa}{}
{\PackageWarning{hexboard}{command `belowIa' already defined}}
\@ifundefined{leftofxchor}{}
{\PackageWarning{hexboard}{command `leftofxchor' already defined}}
\@ifundefined{hexgame}{}
{\PackageWarning{hexboard}{command `hexgame' already defined}}
\@ifundefined{hexgamelabels}{}
{\PackageWarning{hexboard}{command `hexgamelabels' already defined}}
\@ifundefined{hexthismover}{}
{\PackageWarning{hexboard}{command `hexthismover' already defined}}
\@ifundefined{hexmove}{}
{\PackageWarning{hexboard}{command `hexmove' already defined}}
\@ifundefined{hexletter}{}
{\PackageWarning{hexboard}{command `hexletter' already defined}}
\@ifundefined{hexsubrow}{}
{\PackageWarning{hexboard}{command `hexsubrow' already defined}}
\@ifundefined{hexskipmove}{}
{\PackageWarning{hexboard}{command `hexskipmove' already defined}}
\@ifundefined{hexmovenumber}{}
{\PackageWarning{hexboard}{command `hexmovenumber' already defined}}
\makeatother

\newcommand{\xchor}{0} % x-coordinate.
\newcommand{\ychor}{0} % y-coordinate.

\newcommand{\colorA}{red}
\newcommand{\colorB}{blue}
\newcommand{\hexlinewidth}{0.07}
\newcommand{\hexedgewidth}{0.15}
\newcommand{\hexthinline}{0.03}
\newcommand{\hexscale}[1]{
    \tikzset{hex/.style={scale=#1,every node/.style={scale=#1}}}
    \pgfmathsetmacro\hexlinewidth{#1 * 0.07}
    \pgfmathsetmacro\hexedgewidth{#1 * 0.15}
    \pgfmathsetmacro\hexthinline{#1 * 0.03}
}
\hexscale{2}

\newcommand{\setcolorA}[1]{\renewcommand{\colorA}{#1}}
\newcommand{\setcolorB}[1]{\renewcommand{\colorB}{#1}}

%% This calculates the values of the hex coordinate grid.
\newcounter{hexletternum}
\newcommand{\hexcoord}[2]{%
    \ifnum0<0#1\relax % is it a number?
    \pgfmathsetmacro\xchor{#1 + 0.5 * #2}
    \else % assume it's a letter
    \setcounter{hexletternum}{0}
    \foreach \hexletter in {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}{%
        \addtocounter{hexletternum}{1}
        \IfEq{\hexletter}{#1}{\breakforeach}{}
    }
    \pgfmathsetmacro\xchor{\value{hexletternum} + 0.5 * #2}
    \fi
    \pgfmathsetmacro\ychor{sqrt(3)/2 * #2}
}

\pgfmathsetmacro\hexsize{2/sqrt(3)} % size of the node we want tikz to draw a hexagon inside

\newcommand{\hexcell}[2]{%
    \hexcoord{#1}{#2}
    \node[regular polygon,draw,regular polygon sides=6,rotate=90,line width=\hexlinewidth em,minimum size=\hexsize em]  at (\xchor em,\ychor em) {};
}

\newcommand{\hexcellshaded}[3][yellow]{%
    \hexcoord{#2}{#3}
    \node[regular polygon,draw,regular polygon sides=6,rotate=90,line width=\hexlinewidth em,minimum size=\hexsize em,fill=#1] at (\xchor em,\ychor em) {};
}

\newcommand{\thiscolor}{\colorB}
\newcommand{\hexcounter}[3]{%
    \renewcommand{\thiscolor}{\colorB}
    \def\testA{A}
    \def\argiii{#3}
    \if\argiii\testA
    \renewcommand{\thiscolor}{\colorA}
    \fi

    \hexcoord{#1}{#2}
    \draw[\thiscolor,fill=\thiscolor] (\xchor em,\ychor em) circle (0.35em);
}
\newcommand{\hexcounterlabel}[4]{%
    \hexcounter{#1}{#2}{#3}
    \hexcontent[white]{#1}{#2}{#4}
}

\newcommand{\hexdot}[2]{%
    \hexcoord{#1}{#2}
    \draw[fill] (\xchor em,\ychor em) circle (0.1em);
}

\newcommand{\hexcontent}[4][black]{%
    \hexcoord{#2}{#3}
    \node[text=#1] at (\xchor em,\ychor em) {{\sf\scriptsize #4}};
}

\newcommand{\hexconnect}[4]{%
    \hexcoord{#1}{#2}
    \node[minimum size=0,inner sep=0,outer sep=0] (A) at (\xchor em,\ychor em) {};
    \hexcoord{#3}{#4}
    \node[minimum size=0,inner sep=0,outer sep=0] (B) at (\xchor em,\ychor em) {};
    \draw[line width=\hexthinline em,line cap=round] (A) -- (B);
}

\newcommand{\hexgridnolabel}[2]{

    \foreach \Ia in {1,...,#1}{
        % Place the hex cells.
        \foreach \Ib in {1,...,#2}{
            \hexcell{\Ia}{\Ib}
        }
    }
    \ifnum#1<3 % don't loop
    \ifnum#1=2
    % Coloured boundary
    \hexcoord{1}{1}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom edge
    \hexcoord{1}{#2}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,0.288675 em) -- (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top edge
    %        \else % 1, hopefully
    %            % Coloured boundary
    %            \hexcoord{1}{1}
    %            \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom edge
    %            \hexcoord{1}{#2}
    %            \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,0.288675 em) -- (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top edge
    \fi
    \else
    \pgfmathsetmacro\Iaend{#1-1}
    \foreach \Ia in {2,...,\Iaend}{ % top to bottom player A
        % Coloured boundary
        \hexcoord{\Ia}{1}
        \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom edge
        \hexcoord{\Ia}{#2}
        \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,0.288675 em) -- (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top edge
    }
    \fi

    % Awkward corners

    \hexcoord{1}{#2}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=butt] (-0.25 em,0.4330125 em) -- (0 em,0.577350 em);
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top left colorA

    \ifnum#1>1
    \hexcoord{#1}{#2}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=butt] (-0.5 em,0.288675 em) -- (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top right colorA
    \hexcoord{1}{1}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom left colorA
    \fi

    \hexcoord{#1}{1}
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em); % top right colorA
    \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=butt] (0.25 em,-0.4330125 em) -- (0 em,-0.577350 em); % awkward bit

    \ifnum#2<3 % don't loop
    \ifnum#2=2
    % Coloured boundary
    \hexcoord{1}{1}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (-0.5 em,0.288675 em) -- (0 em,0.577350 em); % left edge
    \hexcoord{#1}{2}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (0 em,-0.577350 em) -- (0.5 em,-0.288675 em) -- (0.5 em,0.288675 em); % right edge
    %        \else % 1, hopefully
    %            % Coloured boundary
    %            \hexcoord{1}{1}
    %            \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom edge
    %            \hexcoord{1}{#2}
    %            \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,0.288675 em) -- (0 em,0.577350 em) -- (0.5 em,0.288675 em); % top edge
    \fi
    \else
    \pgfmathsetmacro\Iaend{#2-1}
    \foreach \Ia in {2,...,\Iaend}{ % left to right player B
        % Coloured boundary
        \hexcoord{1}{\Ia}
        \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (-0.5 em,0.288675 em) -- (0 em,0.577350 em); % left edge
        \hexcoord{#1}{\Ia}
        \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (0 em,-0.577350 em) -- (0.5 em,-0.288675 em) -- (0.5 em,0.288675 em); % right edge
    }
    \fi

    % Awkward corners
    \hexcoord{#1}{1}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=butt] (0.25 em,-0.4330125 em) -- (0.5 em,-0.288675 em);

    \ifnum#2>1
    \hexcoord{1}{1}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=] (-0.5 em,-0.288675 em) -- (-0.5 em,0.288675 em) -- (0 em,0.577350 em); % bottom left colorB
    \hexcoord{#1}{#2}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (0 em,-0.577350 em) -- (0.5 em,-0.288675 em) -- (0.5 em,0.288675 em); % top right colorB
    \fi

    \hexcoord{#1}{1}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (0.5 em,-0.288675 em) -- (0.5 em,0.288675 em); % bottom right colorB

    \hexcoord{1}{#2}
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (-0.5 em,0.288675 em); % top left colorB
    \draw[\colorB,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=butt] (-0.25 em,0.4330125 em) -- (-0.5 em,0.288675 em); % awkward bit
}

\newcommand{\hexreducingnoborder}[2]{
    % Place the hex cells.
    \foreach \Ib in {1,...,#2}{
        \pgfmathsetmacro\Iaend{#1+1-\Ib}
        \foreach \Ia in {1,...,\Iaend}{
            \hexcell{\Ia}{\Ib}
        }

    }
}

\newcommand{\hexbottomsubborder}[2]{%
    % Coloured boundary

    % bottom edge
    \foreach \Ia in {#1,...,#2}{
        % Coloured boundary
        \hexcoord{\Ia}{1}
        \draw[\colorA,shift={(\xchor em,\ychor em)},line width=\hexedgewidth em,line cap=round] (-0.5 em,-0.288675 em) -- (0 em,-0.577350 em) -- (0.5 em,-0.288675 em); % bottom edge
    }
}

\newcommand{\hexreducing}[2]{
    % Place the hex cells.
    \hexreducingnoborder{#1}{#2}

    % bottom edge
    \hexbottomsubborder{1}{#1}
}

\newcommand{\hexsubrow}[3]{
    \foreach \Ia in {#1,...,#2}{\hexcell{\Ia}{#3}}
}

\newcommand{\hexshadedsubrow}[3]{
    \foreach \Ia in {#1,...,#2}{\hexcellshaded{\Ia}{#3}}
}

\newcounter{hexedge}
\newcommand{\hexgrid}[2]{
    \hexgridnolabel{#1}{#2}

    % labels
    \setcounter{hexedge}{0}
    \foreach \Ia in {1,...,#1}{
        \addtocounter{hexedge}{1}
        \pgfmathsetmacro\leftofIa{\Ia+0.1}
        \pgfmathsetmacro\belowIa{1-1/sqrt(3)-0.2}
        \ifnum\value{hexedge}<27
        \node[scale=0.3] at (\leftofIa em,\belowIa em) {{\sf\color{\colorA}\alph{hexedge}}};
        \else
        \node[scale=0.3] at (\leftofIa em,\belowIa em) {{\sf\color{\colorA}\arabic{hexedge}}};
        \fi
    }
    \setcounter{hexedge}{0}
    \foreach \Ia in {1,...,#2}{
        \addtocounter{hexedge}{1}
        \hexcoord{1}{\Ia}
        \pgfmathsetmacro\leftofxchor{\xchor-0.75}
        \node[scale=0.3] at (\leftofxchor em,\ychor em) {{\sf\color{\colorB}\arabic{hexedge}}};
    }
}

\newcommand{\hexboard}[1]{
    \hexgrid{#1}{#1}
}

\newenvironment{hexpicture}{\begin{tikzpicture}[hex]}{\end{tikzpicture}}

\newcounter{hexmovecount} % keep track of moves
\newcounter{hexmoveskips} % count skipped moves
\newcommand{\hexmovenumber}{0} % label of current move

\newcounter{hexlabelling}
\newenvironment{hexgame}[1][11]{%
    \begin{hexpicture}
        \hexboard{#1}
        \setcounter{hexmovecount}{0}
        \setcounter{hexmoveskips}{0}
        \setcounter{hexlabelling}{0}}
    {\end{hexpicture}}
\newenvironment{hexgamelabels}[1][11]{%
    \begin{hexgame}[#1]
        \setcounter{hexlabelling}{1}}
    {\end{hexgame}}

\newcommand{\hexthismover}{B}

\newcommand{\hexmove}[2]{%
    \addtocounter{hexmovecount}{1}
    \renewcommand{\hexthismover}{B}
    \ifodd\value{hexmovecount}\renewcommand{\hexthismover}{A}\fi

    \pgfmathtruncatemacro\hexmovenumber{\value{hexmovecount}-\value{hexmoveskips}} % move label is moves minus skips
    \ifnum\value{hexlabelling}=1\hexcounterlabel{#1}{#2}{\hexthismover}{\hexmovenumber}\else\hexcounter{#1}{#2}{\hexthismover}\fi
}

\newcommand{\hexskipmove}{%
    \addtocounter{hexmovecount}{1}
    \addtocounter{hexmoveskips}{1}
}
\endinput
%%
%% End of file `hexboard.sty'.