在使用包xskak
和chessboard
初始化游戏中,如何确定给定棋盘的白棋和黑棋数量\newgame
以及如何输出这些棋子在相应棋盘上的实际位置?我知道有一个参数\getpieceslist
,\chessboard
但是如何借助 TeX 命令总结白棋和黑棋的棋子以及如何借助输出这些列表figure
?
答案1
选项(不是命令)getpiecelists
给出了逗号分隔的位置列表。您必须解析此列表,例如使用以下命令\@for
来计算它们:
\documentclass{article}
\usepackage{xskak,chessboard}
\begin{document}
\newchessgame
\chessboard[getpiecelists]%
\newcounter{countrook}
\setcounter{countrook}{0}
\makeatletter
\@for\templist:=\cblistr \do {\stepcounter{countrook}}
\makeatother
Number of black rooks: \thecountrook. They are at the positions \cblistr.
\end{document}
编辑:您可以尝试这个来获取各种计数(我也更改了命令,以便\cblist...
现在它也为不在棋盘上的棋子定义):
\documentclass{article}
\usepackage{xskak,chessboard}
\makeatletter
\renewcommand\board@do@getpiecelists{%
\setcounter{cnt@board@file}{\board@val@minfilenum}%
\setcounter{cnt@board@rank}{\board@val@minranknum}%
\edef\board@temp@curlist{% changed 3.2.2012 to parse all pieces.
\board@skak@piececharlist}
\@for\board@temp@piecechar:=\board@temp@curlist
\do {%
\expandafter\xdef\csname cblist\board@temp@piecechar\endcsname{}% 3.2.2012 global
\expandafter\xdef\csname cblistnum\board@temp@piecechar\endcsname{0}% 3.2.2012 new
\expandafter\def\csname board@temp@\board@temp@piecechar @comma\endcsname{}}%
\xdef\cblistempty{}%
\xdef\cblistnumempty{}% 3.2.2012
\def\board@temp@empty@comma{}%
\whiledo
{\value{cnt@board@file}<\numexpr \board@val@maxfilenum+1\relax}%
{\whiledo
{\value{cnt@board@rank}<\numexpr \board@val@maxranknum+1\relax}%
{\edef\board@temp@piecechar{%
\csname
board@val@f\the\c@cnt@board@file r\the\c@cnt@board@rank @piecechar\endcsname}%
\edef\board@temp@curlist{%
\csname cblist\board@temp@piecechar\endcsname}%
\edef\board@temp@comma{%
\csname board@temp@\board@temp@piecechar @comma\endcsname}%
\expandafter\xdef\csname cblist\board@temp@piecechar \endcsname{%
\board@temp@curlist
\board@temp@comma
\alph{cnt@board@file}\the\c@cnt@board@rank}%
\expandafter\xdef\csname cblistnum\board@temp@piecechar\endcsname{\the\numexpr \csname cblistnum\board@temp@piecechar\endcsname +1}% 3.2.2012
\expandafter\def
\csname board@temp@\board@temp@piecechar @comma\endcsname
{,}%
\stepcounter{cnt@board@rank}}%
\setcounter{cnt@board@rank}{\board@val@minranknum}%
\stepcounter{cnt@board@file}}}
\makeatother
\begin{document}
\newchessgame
\chessboard[setpieces={Ke1,Ra1,Ra3,Ra5},getpiecelists,marginleft=false]
White king: \cblistnumK
White rooks: \cblistnumR
White pieces: \the\numexpr \cblistnumK + \cblistnumQ +\cblistnumR
+\cblistnumB + \cblistnumN + \cblistnumP\relax
empty fields: \cblistnumempty
\end{document}