下面我们看一个绘制好的例子:sudokugrid-1sudokugrid2-1代码实现如下:

\documentclass{standalone}
    \usepackage{tikz}
    \usetikzlibrary{backgrounds}
    \usepackage{graphicx}

    \pagenumbering{gobble}
    
% Some customizable styles
    \tikzset {
        highlight/.style = {gray, opacity=0.3},
        digit/.style = {minimum height = 5mm, minimum width=5mm, anchor=center },
        circle/.style = {draw=black!80!black, dotted, very thick},
        cross/.style = {red, opacity=.5, shorten >=1mm, shorten <=1mm, very thick, line cap=round},
        hint/.style={black, font=\sf, minimum width=3mm, minimum height=3mm}
    }

% Original code-----------------------------------------------------------
% Modified the \node to give a unique name to each one, which is the
% row number, a dash and the column number. E.g: 1-1, 4-5, etc.
    \newcounter{row}
    \newcounter{col}

    \newcommand\setrow[9]{
        \setcounter{col}{1}
        \foreach \n in {#1, #2, #3, #4, #5, #6, #7, #8, #9} {
            \edef\x{\value{col} - 0.5}
            \edef\y{9.5 - \value{row}}
            \node[digit,name={\arabic{row}-\arabic{col}}] at (\x, \y) {\n};
            \stepcounter{col}
        }
        \stepcounter{row}
    }

% New code -------------------------------------------------------------
    \def\highlightcell#1#2{
        \fill[highlight] (#1-#2.north west) rectangle (#1-#2.south east);
    }

    \def\circlecell#1#2{
        \draw[circle] (#1-#2) circle(4mm);
    }

    \def\crosscell#1#2{
        \draw[cross] (#1-#2.north west) -- (#1-#2.south east);
        \draw[cross] (#1-#2.north east) -- (#1-#2.south west);
    }

    \def\highlightrow#1{
       \fill[highlight] (#1-1.north west) rectangle (#1-9.south east);
    }

    \def\highlighcolumn#1{
        \fill[highlight] (1-#1.north west) rectangle (9-#1.south east);
    }

    \def\hintcell#1#2#3{
        \node at (#1-#2) {\hintbox{#3}};
    }

    \def\highlightrectangle#1#2#3#4{
        \begin{pgfonlayer}{background}
        \fill[highlight] (#1-#2.north west) rectangle (#3-#4.south east);
        \end{pgfonlayer}
    }

% UGLY code. Do not read :-)
\def\hintbox#1{
    \resizebox{4.5mm}{4.5mm}{%
        \tikz[scale=0.3]{%
            \def\auxc{0}
            \foreach \m in {1,...,9} {
                \pgfmathparse{mod(\auxc,3)}
                \xdef\x{\pgfmathresult}
                \pgfmathparse{-floor(\auxc/3)}
                \xdef\y{\pgfmathresult}
                \xdef\hintprinted{0}
                \foreach \n in {#1} {
                    \ifnum\n=\m
                        \node[hint] at (\x,\y) {\n};
                        \xdef\hintprinted{1}
                    \fi
                }
                \ifnum\hintprinted=0
                      \node[hint, opacity=0] at (\x,\y) {\m};
                \fi
                \pgfmathparse{\auxc+1}
                \xdef\auxc{\pgfmathresult}
            }
        }%
    }
}


\begin{document}

    \begin{tikzpicture}[scale=.5]
        \begin{scope}
            \draw (0, 0) grid (9, 9);
            \draw[very thick, scale=3] (0, 0) grid (3, 3);
            \setcounter{row}{1}

    % Single entries
            \setrow { }{ }{9}  { }{ }{ }  {2}{6}{ }
            \setrow {2}{ }{ }  { }{8}{4}  { }{ }{ }
            \setrow {5}{ }{ }  {2}{ }{ }  { }{ }{1}

            \setrow { }{6}{ }  {1}{ }{9}  {7}{ }{ }
            \setrow { }{9}{ }  { }{7}{ }  { }{4}{ }
            \setrow { }{ }{7}  {4}{ }{3}  { }{9}{ }

            \setrow {3}{ }{ }  { }{ }{6}  { }{ }{5}
            \setrow { }{ }{ }  {5}{3}{ }  { }{ }{8}
            \setrow { }{2}{5}  { }{ }{ }  {3}{ }{ }

            \highlightrectangle{1}{4}{3}{6}

            \highlightrectangle{4}{1}{6}{3}
            \highlightrectangle{4}{7}{6}{9}
            
            \highlightrectangle{7}{4}{9}{6}

        \end{scope}
\end{tikzpicture}

\end{document}

代码摘自一个关于数独的报告,https://github.com/ithanium/sudoku_report。 当然还有一个宏包也可以绘制数独游戏的,http://www.ctan.org/pkg/sudoku,这一个宏包的输入比较可视化、直观,不过要花点时间填写内容。有这类兴趣的用户可以自己测试下。sudoku

下载区

本站下载:sudoku_report-master

点赞(1)

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部