如何获得 4 x 4 的棋盘?或者任何其他尺寸的棋盘。

如何获得 4 x 4 的棋盘?或者任何其他尺寸的棋盘。

我见过一些在 LaTeX 上绘制棋盘的软件包,例如skakchessboard。但是,在阅读文档后,它们似乎不支持更改棋盘大小。我说的是棋盘上的方格数,而不是视觉大小。

如果你好奇的话,我正在尝试提出解决方案n 皇后问题

所以,我正在寻找一种简单的方法来绘制任意大小的棋盘和一些皇后。此外,在棋盘侧面写上数字并在棋盘下方写上一些特定字母的方法也很棒。

编辑:我使用了 Mike Renfro 的解决方案,但我对其进行了一些修改,以获取底部标签上的变量并删除移动方块:

\documentclass{article}
\usepackage{chessboard}
\storechessboardstyle{4x4}{maxfield=d4}
\begin{document}
    \def\mylabelformat{%
    {\makebox[0pt][c]{%
    {$x_\arabic{filelabel}$}}}}

    \begin{tabular}{cc}
        \chessboard[style=4x4,setwhite={Qa3,Qb1,Qc4, Qd2}, labelbottomformat=\mylabelformat, showmover=false]
        &  
        \chessboard[style=4x4,setwhite={Qa3,Qb1,Qc4, Qd2}, labelbottomformat=\mylabelformat, showmover=false]
    \end{tabular}
\end{document}

4-Queens 的解决方案

答案1

摘自棋盘手册(并根据以下评论进行编辑):

\documentclass{article}
\usepackage{chessboard}
\storechessboardstyle{4x4}{maxfield=d4}
\begin{document}
\chessboard[style=4x4,setwhite={Qa1,Qd2},showmover=false]
\end{document}

4x4 板

答案2

这是一个使用节点的快速解决方案。当然,您可以根据需要进行调整。通过更改宏\boardsize,您可以根据需要更改板的大小。

\begin{tikzpicture}
    \pgfmathsetmacro{\boardsize}{4}

    \def\letters{{"","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"}}

    \foreach \i in {1,...,\boardsize}{
        \foreach \j in {1,...,\boardsize}{
            \pgfmathsetmacro{\weight}{(1 + (-1)^(\i+\j))*50};
            \node[rectangle,fill=gray!\weight,minimum size=1cm] (node\i-\j) at (\i,\j) {};
        }
    }
    \foreach \j in {1,...,\boardsize}{
        node[left=2mm of node1-\j] {\j};
    }

    \foreach \i in {1,...,\boardsize}{
        \node[below=4mm of node\i-1,anchor=base] {\pgfmathparse{\letters[\i]}\pgfmathresult};
    }

    \end{tikzpicture}

相关内容