-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtikz-backgammon.sty
404 lines (380 loc) · 14.7 KB
/
tikz-backgammon.sty
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
% Copyright 2012-2024 by Alan Munn
%
% This package may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or any later version.
% The latest version of this license is in
% http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This package has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this package is Alan Munn.
%
% This package consists of the file tikz-backgammon.sty and tikz-backgammon-dice.sty
% and documentation files tikz-backgammon.tex and tikz-backgammon.pdf
%
% Version 0.9 2024/01/20
%
%
\ProvidesPackage{tikz-backgammon}[2024/01/20 Backgammon game display using TikZ v0.9]
\RequirePackage{etoolbox}
\RequirePackage{tikz}
\usetikzlibrary{positioning,calc}
\RequirePackage{tikz-backgammon-dice} % dice code in a separate package
% Construct the board
%
% alternating colours for the board
\newcommand*{\bk@BW}{
\pgfmathifthenelse{mod(\x,2)==0}{1}{0}\let\bk@fillstyle\pgfmathresult}
\newcommand*{\bk@WB}{
\pgfmathifthenelse{mod(\x,2)==0}{0}{1}\let\bk@fillstyle\pgfmathresult}
% construct a down point
\newcommand*{\bk@dpoint}{%
\bk@BW
\draw[thick,style=\bk@fillstyle]
(\x,0) -- (\x+3/2,15) -- (\x+3,0) -- cycle; }
% construct an up point
\newcommand{\bk@upoint}{%
\bk@WB
\draw[thick,style=\bk@fillstyle]
(\x,32) -- (\x+3/2,17) -- (\x+3,32) -- cycle; }
% set basic tikz parameters for the board points, dice, and stones;
% in order to scale text and nodes, node styles are specified 'transform shape'
%
\tikzset{1/.style={fill=\boardblack},
0/.style={fill=\boardwhite},
stone/.style={transform shape,draw=black,circle,minimum size=3*\defaultxy,inner sep=0pt,font={\sffamily},text={\bk@stonetextcolor}},
sans/.style={transform shape,font={\sffamily},inner sep=0pt,outer sep=0pt},
white double/.style={line color=\black,text=\black},
black double/.style={dice color=\black,text=\white},
neutral double/.style={dice color=\black,text=\white},
}
% initialization
% These should be made key values
% Maybe pass them as tikz options to a {game} environments
% This would be more flexible than having them as package options
\newcommand*{\black}{black} % for the stones
\newcommand*{\white}{white}
\newcommand*{\boardblack}{brown}
\newcommand*{\boardwhite}{olive!50}
\newcommand*{\barcolor}{brown}
\newcommand*{\defaultxy}{.5em}
\newlength{\betweengameskip}
\setlength{\betweengameskip}{2\baselineskip}
% initial state of the doubling cube
\newcommand*{\bk@doublestate}{neutral}
\newcommand*{\bk@doublenum}{64}
% create 24 counters for the point counts
% create 24 macros for the point state (black,white,none)
\foreach \x in {1,...,24}{
\newcounter{bk@pt\x}
\expandafter\gdef\csname bk@state\x\endcsname{none}
}
% create 2 counters state of the bar (we don't need a state)
% this still needs to be implemented
% do we need 2 different commands, one for within a game
% and the other for setting the board manually?
% 0 for white 1 for black
\foreach \x in {0,1}{
\newcounter{bk@bar\x}
}
% create 2 counters and macros for the state of bearing off
% this still needs to be implemented
% this will work similarly to the bar state
% 0 for white 1 for black
\foreach \x in {0,1}{
\newcounter{bk@off\x}
}
% Current code has only one bar state (this is wrong)
%\newcounter{bk@bar}
%\newcommand*{\bk@barstate}{none}
% some utility and debugging commands
\newcommand*\showpoint[1]{\the\csname c@bk@pt#1\endcsname}
\newcommand*\bk@setstate[2]{\expandafter\gdef\csname bk@state#1\endcsname{#2}}
\newcommand*\bk@setcount[2]{\global\csname c@bk@pt#1\endcsname #2\relax}
\newcommand*\bk@barcount[2]{\advance\csname c@bk@bar#1\endscname#2\relax}
%\newcommand*\bk@barset[2]{\expandafter\gdef\csname\bk@barstate#1\endcsname{#2}}
\newcommand*\usestate[1]{\csname bk@state#1\endcsname}
% basic command to draw the board
% all of these numbers probably shouldn't be hard coded
%
\newif\ifbk@black\bk@blacktrue % sets the view of the board
\newif\ifbk@displaydice\bk@displaydicefalse % toggle to display the dice or not
\newcommand*\bk@right{40.5}
\newcommand*\bk@left{-1.5}
\newcommand*\bk@upstart{-1.75}
\newcommand*\bk@downstart{33.75}
\newcommand*\bk@nodelabel{%
\ifbk@black
\bk@nodename
\else
\number\numexpr 25-\bk@nodename\relax
\fi}
\newcommand*\bk@wstart{\ifbk@black\bk@downstart\else\bk@upstart\fi}
\newcommand{\bk@drawboard}{%
% draw the border and the point numbers
% the nodes are numbered internally from the point of view of black
% nodes are labelled according to the view of the person playing
% this is encoded in the conditional \ifbk@black. If this is false
% the nodes will be labelled from white's point of view.
\draw[line width=4pt] (0,0) -- (0,32) -- (39,32) -- (39,0) -- cycle;
\foreach \x in {1,...,6}{
\let\bk@nodename\x
\node[sans] (\bk@nodename) at (\bk@left+\x*3,\bk@upstart) {\bk@nodelabel};
\pgfmathparse{int(\x+6)}\let\bk@nodename\pgfmathresult
\node[sans] (\bk@nodename) at (\bk@left+\x*3+21,\bk@upstart) {\bk@nodelabel};
\pgfmathparse{int(\x+12)}\let\bk@nodename\pgfmathresult
\node[sans] (\bk@nodename) at (\bk@right-\x*3,\bk@downstart) {\bk@nodelabel};
\pgfmathparse{int(\x+18)}\let\bk@nodename\pgfmathresult
\node[sans] (\bk@nodename) at (\bk@right-\x*3-21,\bk@downstart) {\bk@nodelabel};
}
% now draw the first half points
\foreach \x in {0,3,...,15}
\bk@dpoint
\foreach \x in {0,3,...,15}
\bk@upoint
% draw the bar and set the anchors for bar and the doubling cube and dice rolls
\draw[very thick,fill=\barcolor](18,0) -- (18,32) -- (21,32) -- (21,0) -- cycle;
\coordinate (barcenter) at (19.5,14);
\coordinate (whitebar) at (19.5,28);
\coordinate (blackbar) at (19.5,4);
\coordinate (black double) at (42, 4);
\coordinate (white double) at (42, 28);
\coordinate (neutral double) at (42,16);
\coordinate (black dice) at (9,16);
\coordinate (white dice) at (30,16);
\coordinate (caption) at (19.5,-4);
\coordinate (offblack) at (-4,4);
\coordinate (offwhite) at (-4,28);
% draw the other half of the points
\foreach \x in {21,24,...,36}
\bk@dpoint
\foreach \x in {21,24,...,36}
\bk@upoint
}
% commands to place markers on a point and set its state
% for users to make arbitrary board configurations
% first for a black point (user command)
\newcommand{\blackpoint}[2]{%
\bk@setstate{#1}{black}%
\bk@setcount{#1}{#2}%
\ignorespaces
}
% same again for a white point (this is from point in whites view)
\newcommand{\whitepoint}[2]{%
\def\bk@tmp{\numexpr 25-#1\relax}%
\bk@setstate{\the\bk@tmp}{white}%
\bk@setcount{\the\bk@tmp}{#2}%
\ignorespaces
}
% now a generic version of the command for use in displaying the board
% this is an internal command and uses the internal point numbers
% this now correctly displays the excess number of stones on the last
% one;
\newif\ifbk@maxstones
\newcommand{\bk@placepoint}[2]{%
\bk@maxstonesfalse%
\let\ptname#1%
\def\bk@tmpb{black}%
\ifcsstrequal{\usestate{\ptname}}{\bk@tmpb}{\let\bk@stonetextcolor\white}{\let\bk@stonetextcolor\black}
\ifnumcomp{#2}{<}{1}%
{}
{\ifnumcomp{#2}{>}{5}%
{\gdef\bk@tmp{5}\bk@maxstonestrue\xdef\bk@tmpa{#2}}
{\gdef\bk@tmp{#2}}
{\pgfmathparse{ifthenelse(#1>12,"below","above")}\let\bk@pos\pgfmathresult
\foreach \x in {1,...,\bk@tmp}
\node[fill=\usestate{\ptname},style=stone,\bk@pos=(3*\x)-2 of \ptname]
{\ifbk@maxstones
\pgfmathparse{ifthenelse(\x==5,\bk@tmpa,"")}\pgfmathresult
\else\relax\fi};
}}
}
% command to place stones on the bar
\newcommand{\bk@placebar}{%
\foreach \y in {0,...,1}{
\def\bk@barcount{\the\csname c@bk@bar\y\endcsname}
\bk@maxstonesfalse
\ifnumcomp{\y}{=}{0}
{\global\let\bk@stonetextcolor\black
\gdef\bk@stonecolor{white}
\gdef\bk@barpos{whitebar}
\gdef\bk@orient{below}}
{\global\let\bk@stonetextcolor\white
\gdef\bk@stonecolor{black}
\gdef\bk@barpos{blackbar}
\gdef\bk@orient{above}}
\ifnumcomp{\bk@barcount}{>}{3}
{\gdef\bk@tmp{3}\bk@maxstonestrue\xdef\bk@tmpa{\bk@barcount}}
{\gdef\bk@tmp{\bk@barcount}}
\ifnumcomp{\bk@barcount}{>}{0}
{\foreach \x in {1,...,\bk@tmp}
\node[fill=\bk@stonecolor,style=stone,\bk@orient=(3*\x)-2 of \bk@barpos] {\ifbk@maxstones
\pgfmathparse{ifthenelse(\x==3,\bk@tmpa,"")}\pgfmathresult
\else\relax\fi};}{}}
}
% command to display borne off stones
% user command to place pieces on the bar
% syntax is \onbar{colour}{amount}
% this is the manual set command so we don't increment the counter
\newcommand*{\onbar}[2]{%
\ifstrequal{#1}{black}
{\setcounter{bk@bar1}{#2}}
{\ifstrequal{#1}{white}
{\setcounter{bk@bar0}{#2}}
{\PackageError{tikz-backgammon}{Only 'white' or 'black' can be on the bar}{Check your spelling!}}
}
}
% user command to set a double
% syntax is \double{<owner>}{amount}
\newcommand*{\double}[2]{%
\def\bk@doublenum{#2}
\ifstrequal{#1}{neutral}
{\gdef\bk@doublestate{neutral double}}
{\ifstrequal{#1}{white}
{\gdef\bk@doublestate{white double}}
{\gdef\bk@doublestate{black double}}
}
}
% command to place the doubling cube in the correct place
\newcommand*{\bk@placedouble}{%
\node at (\bk@doublestate)
{\drawdblcube[\bk@doublestate]{\bk@doublenum}};} %
% command to display the dice
\newcommand*{\bk@whitedice}[2]{%
\node at (white dice) [left=0pt] (dice)
{\drawdie[dot color=\black,line color=\black]{#1}};
\node at (white dice) [right=0pt] (dice)
{\drawdie[dot color=\black,line color=\black]{#2}};
}
\newcommand*{\bk@blackdice}[2]{%
\node at (black dice) [left=0pt] (dice)
{\drawdie[dot color=\white,dice color=\black]{#1}};
\node at (black dice) [right=0pt] (dice)
{\drawdie[dot color=\white,dice color=\black]{#2}};
}
% This is an internal command to parse a dice role in the form (nm)
% into two separate parts so that the dice can be generated
\def\bk@parsedice(#1#2){\gdef\bk@dice@one{#1}\gdef\bk@dice@two{#2}}
% User commands to display a roll; roll in nm format
%
\newcommand*{\blackroll}[1]{%
\bk@displaydicetrue
\gdef\bk@dicestate{black}
\bk@parsedice(#1)%
}
\newcommand*{\whiteroll}[1]{%
\bk@displaydicetrue
\gdef\bk@dicestate{white}
\bk@parsedice(#1)%
}
% user command to draw a blank board
\newcommand*{\blankboard}[1][]{%
\double{neutral}{64}
\begin{tikzpicture}[x=\defaultxy,y=\defaultxy,#1]
\bk@drawboard;
\end{tikzpicture}
}
% user command to clear the board state
\newcommand*{\clearboard}{%
\double{neutral}{64}
\bk@displaydicefalse
\foreach \x in {1,...,24}{\bk@setcount{\x}{0}}
}
% user commands to set the caption for a board
\newcommand{\boardcaptionformat}[1]{\gdef\bk@captionformat{#1}}
\boardcaptionformat{}
\newcommand{\boardcaption}[1]{\gdef\bk@boardcaption{\bk@captionformat#1\par}}
\boardcaption{}
% command to setup the pieces for a new game without drawing anything
\newcommand*\newpieces{
\foreach \x in {1,12,17,19}
{\bk@setstate{\x}{white}}
\foreach \x in {6,8,13,24}
{\bk@setstate{\x}{black}}
\foreach \x in {1,24}
{\bk@setcount{\x}{2}}
\foreach \x in {12,19,13,6}
{\bk@setcount{\x}{5}}
\foreach \x in {17,8}
{\bk@setcount{\x}{3}}
\double{neutral}{64}
\bk@displaydicefalse
}
% command to set a new game (no longer displays it)
\let\newgame=\newpieces
% commands to move first black, then white
% starred versions are for doubles
% This needs to be rewritten to accept 'bar' and 'off' arguments
\newcommand\blackmove{\@ifstar\bk@blackdouble\bk@blacksingle}
\newcommand\bk@blacksingle[4]{%
\advance\csname c@bk@pt#1\endcsname -1\relax
\ifnumcomp{\the\csname c@bk@pt#1\endcsname}{=}{0}
{\expandafter\gdef\csname bk@state#1\endcsname{none}}
{\expandafter\gdef\csname bk@state#1\endcsname{black}}
\ifnumcomp{#2}{=}{0}{}{\advance\csname c@bk@pt#2\endcsname 1\relax}
\expandafter\gdef\csname bk@state#2\endcsname{black}
\advance\csname c@bk@pt#3\endcsname -1\relax
\ifnumcomp{\the\csname c@bk@pt#3\endcsname}{=}{0}
{\expandafter\gdef\csname bk@state#1\endcsname{none}}
{\expandafter\gdef\csname bk@state#3\endcsname{black}}
\ifnumcomp{#4}{=}{0}{}{\advance\csname c@bk@pt#4\endcsname 1\relax}
\expandafter\gdef\csname bk@state#4\endcsname{black}
}
\newcommand\bk@blackdouble[4]{%
\bk@blacksingle{#1}{#2}{#1}{#2}
\bk@blacksingle{#3}{#4}{#3}{#4}
}
% White moves are normalized so that the input is from white's point of view
% Since the point numbers internally are from black's view, we need to subtract
% each white move from 25 to calculate the correct point number for white.
% Thanks to Enrico Gregorio for help with this code
\newcommand\whitemove{\@ifstar\bk@whitedouble\bk@whitesingle}
\newcommand\bk@whitesingle[4]{
\advance\csname c@bk@pt\number\numexpr 25-#1\relax\endcsname -1\relax
\ifnumcomp{\the\csname c@bk@pt\number\numexpr 25-#1\relax\endcsname}{=}{0}
{\expandafter\gdef\csname bk@state\number\numexpr 25-#1\relax\endcsname{none}}
{\expandafter\gdef\csname bk@state\number\numexpr 25-#1\relax\endcsname{white}}
\ifnumcomp{\numexpr 25-#2\relax}{=}{0}{}{\advance\csname c@bk@pt\number\numexpr 25-#2\relax\endcsname 1\relax}
\expandafter\gdef\csname bk@state\number\numexpr 25-#2\relax\endcsname{white}
\advance\csname c@bk@pt\number\numexpr 25-#3\relax\endcsname -1\relax
\ifnumcomp{\the\csname c@bk@pt\number\numexpr 25-#3\relax\endcsname}{=}{0}
{\expandafter\gdef\csname bk@state\number\numexpr 25-#1\relax\endcsname{none}}
{\expandafter\gdef\csname bk@state\number\numexpr 25-#3\relax\endcsname{white}}
\ifnumcomp{\numexpr 25-#4\relax}{=}{0}{}{\advance\csname c@bk@pt\number\numexpr 25-#4\relax\endcsname 1\relax}
\expandafter\gdef\csname bk@state#4\endcsname{white}
}
\newcommand\bk@whitedouble[4]{%
\bk@whitesingle{#1}{#2}{#1}{#2}
\bk@whitesingle{#3}{#4}{#3}{#4}
}
% black and white moves using nm:to/from,to/from notation
\newcommand*{\blkmove}[1]{\bk@blkparsemove(#1)}
\newcommand*{\whtmove}[1]{\bk@whtparsemove(#1)}
\def\bk@blkparsemove(#1:#2/#3,#4/#5){\blackroll{#1}\blackmove{#2}{#3}{#4}{#5}}
\def\bk@whtparsemove(#1:#2/#3,#4/#5){\whiteroll{#1}\whitemove{#2}{#3}{#4}{#5}}
% command to display the current state of the board
\newcommand*\whiteboard{\bk@blackfalse\bk@displayboard}
\newcommand*\blackboard{\bk@blacktrue\bk@displayboard}
\newcommand{\bk@displayboard}[1][]{%
\par\vspace{\betweengameskip}
\begin{tikzpicture}[x=\defaultxy,y=\defaultxy,#1]
\bk@drawboard
\foreach \x in {1,...,24}{
\bk@placepoint{\x}{\the\csname c@bk@pt\x\endcsname}}
\bk@placedouble
\bk@placebar
\ifbk@displaydice
\csname bk@\bk@dicestate dice\endcsname{\bk@dice@one}{\bk@dice@two}\fi
\node[align=center,anchor=north,inner sep=0pt] at (caption)
{\bk@boardcaption};
\end{tikzpicture}
% after drawing the board we reset the caption and the dice display
\bk@displaydicefalse
\boardcaption{}
}
\endinput
% Still to be added:
% need commands for entering from the bar and bearing off