第三个版本(通过pdflatex使用ClearSans包)

第三个版本(通过pdflatex使用ClearSans包)

我想你们中的一些人可能知道这个游戏2048. 情况有以下形状:

例子

我想创建一个 TikZ 模板,以便我制作类似的图画。

平均能量损失

为了创建以下示例(这是我得到的最好的示例),我使用了以下答案TikZ 可以创建像素艺术图像吗?

\documentclass[tikz]{standalone}
\usepackage{ifthen}
\renewcommand\familydefault{\sfdefault}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\pixels{
{0,2,0,0},
{0,8,0,4},
{2,2,4,16},
{8,16,128,2},
}

% Font color for 2 and 4: #776e65
% Font color for rest: #f9f6f2
% Grid color: #bbada0
% Font family: "Clear Sans", "Helvetica Neue", Arial, sans-serif

\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EEE4DA}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F2B179} % TODO
\definecolor{pixel 64}{HTML}{F2B179}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{F2B179} % TODO
\definecolor{pixel 512}{HTML}{F2B179} % TODO
\definecolor{pixel 1024}{HTML}{F2B179} % TODO
\definecolor{pixel 2048}{HTML}{F2B179} % TODO
\definecolor{pixel 4096}{HTML}{3E3933}
\begin{document}
\begin{tikzpicture}
  \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \draw[fill=pixel \pix] (\x,-\y) rectangle +(1,1);
      \ifthenelse{\equal{0}{\pix}}
           {}
           {\node at ($(\x,-\y) + (0.5,0.5)$)    {\Huge \pix};}
    }
  }

\end{tikzpicture}
\end{document}

渲染结果:

在此处输入图片描述

问题

  • 如何更改字体颜色?
  • 我总是遇到更改字体的问题:在这种情况下,字体应该是粗体。
  • 数字应该带有一些填充,适合单元格。
  • 网格不应该是黑色的,而应该是#bbada0

答案1

第三个版本(通过pdflatex使用ClearSans包)

(注:ClearSans软件包,版本 2014-05-28)

在此处输入图片描述

\documentclass[tikz]{standalone}
\usepackage{ClearSans}
\usepackage[T1]{fontenc}
\usepackage{tikz}
%
\usetikzlibrary{fit,backgrounds}
%
\def\gamefont{\bfseries\sffamily}
%
\definecolor{grid color}{HTML}{BBADA0}
\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EDE0C8}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F67C5F}
\definecolor{pixel 64}{HTML}{F65E3B}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{EDCC61}
\definecolor{pixel 512}{HTML}{EDC850}
\definecolor{pixel 1024}{HTML}{EDC53F}
\definecolor{pixel 2048}{HTML}{EDC22E}
\definecolor{pixel 4096}{HTML}{3E3933}
%
\definecolor{small color}{HTML}{776E65}
\definecolor{big color}{HTML}{F9F6F2}
%
\tikzset{
  case 2048 base/.style={
    minimum size=9mm,rounded corners=.3mm,text=#1,inner sep=0,line width=0,
  },
  %
  case 2048 Large/.style={font=\Large\gamefont,case 2048 base=#1},
  case 2048 large/.style={font=\large\gamefont,case 2048 base=#1},
  case 2048 normal/.style={font=\normalsize\gamefont,case 2048 base=#1},
  %
  case 2048 0/.style={case 2048 Large=black,fill=pixel 0,node contents={}},
  case 2048 2/.style={case 2048 Large=small color,fill=pixel 2,node contents={2}},
  case 2048 4/.style={case 2048 Large=small color,fill=pixel 4,node contents={4}},
  case 2048 8/.style={case 2048 Large=big color,fill=pixel 8,node contents={8}},
  case 2048 16/.style={case 2048 Large=big color,fill=pixel 16,node contents={16}},
  case 2048 32/.style={case 2048 Large=big color,fill=pixel 32,node contents={32}},
  case 2048 64/.style={case 2048 Large=big color,fill=pixel 64,node contents={64}},
  case 2048 128/.style={case 2048 large=big color,fill=pixel 128,node contents={128}},
  case 2048 256/.style={case 2048 large=big color,fill=pixel 256,node contents={256}},
  case 2048 512/.style={case 2048 large=big color,fill=pixel 512,node contents={512}},
  case 2048 1024/.style={case 2048 normal=big color,fill=pixel 1024,node contents={1024}},
  case 2048 2048/.style={case 2048 normal=big color,fill=pixel 2048,node contents={2048}},
  case 2048 4096/.style={case 2048 normal=big color,fill=pixel 4096,node contents={4096}},
}
\begin{document}
\begin{tikzpicture}
  \def\pixels{
    {2,4,0,2048},
    {8,0,2,4096},
    {8,16,32,64},
    {1024,512,256,128},
  }

  \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \path (\x,-\y) node[name=c2048-\x-\y,case 2048 \pix];
    }
  }

  \begin{scope}[on background layer]
    \node[fill=grid color,fit=(c2048-1-1)(c2048-4-4),
    inner sep=1mm,rounded corners=.3mm]{};
  \end{scope}
\end{tikzpicture}
\end{document}

第二版(使用 Intel ClearSans 字体并借助lualatex

在此处输入图片描述

\documentclass[tikz]{standalone}
\usepackage{fontspec}
\usepackage{tikz}
%
\usetikzlibrary{fit,backgrounds}
%
\newfontfamily\clearsansfont{ClearSans}
\def\gamefont{\bfseries\clearsansfont}
%
\definecolor{grid color}{HTML}{BBADA0}
\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EDE0C8}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F67C5F}
\definecolor{pixel 64}{HTML}{F65E3B}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{EDCC61}
\definecolor{pixel 512}{HTML}{EDC850}
\definecolor{pixel 1024}{HTML}{EDC53F}
\definecolor{pixel 2048}{HTML}{EDC22E}
\definecolor{pixel 4096}{HTML}{3E3933}
%
\definecolor{small color}{HTML}{776E65}
\definecolor{big color}{HTML}{F9F6F2}
%
\tikzset{
  case 2048 base/.style={
    minimum size=9mm,rounded corners=.3mm,text=#1,inner sep=0,line width=0,
  },
  %
  case 2048 Large/.style={font=\Large\gamefont,case 2048 base=#1},
  case 2048 large/.style={font=\large\gamefont,case 2048 base=#1},
  case 2048 normal/.style={font=\normalsize\gamefont,case 2048 base=#1},
  %
  case 2048 0/.style={case 2048 Large=black,fill=pixel 0,node contents={}},
  case 2048 2/.style={case 2048 Large=small color,fill=pixel 2,node contents={2}},
  case 2048 4/.style={case 2048 Large=small color,fill=pixel 4,node contents={4}},
  case 2048 8/.style={case 2048 Large=big color,fill=pixel 8,node contents={8}},
  case 2048 16/.style={case 2048 Large=big color,fill=pixel 16,node contents={16}},
  case 2048 32/.style={case 2048 Large=big color,fill=pixel 32,node contents={32}},
  case 2048 64/.style={case 2048 Large=big color,fill=pixel 64,node contents={64}},
  case 2048 128/.style={case 2048 large=big color,fill=pixel 128,node contents={128}},
  case 2048 256/.style={case 2048 large=big color,fill=pixel 256,node contents={256}},
  case 2048 512/.style={case 2048 large=big color,fill=pixel 512,node contents={512}},
  case 2048 1024/.style={case 2048 normal=big color,fill=pixel 1024,node contents={1024}},
  case 2048 2048/.style={case 2048 normal=big color,fill=pixel 2048,node contents={2048}},
  case 2048 4096/.style={case 2048 normal=big color,fill=pixel 4096,node contents={4096}},
}
\begin{document}
\begin{tikzpicture}
  \def\pixels{
    {2,4,0,2048},
    {8,0,2,4096},
    {8,16,32,64},
    {1024,512,256,128},
  }

  \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \path (\x,-\y) node[name=c2048-\x-\y,case 2048 \pix];
    }
  }

  \begin{scope}[on background layer]
    \node[fill=grid color,fit=(c2048-1-1)(c2048-4-4),
    inner sep=1mm,rounded corners=.3mm]{};
  \end{scope}
\end{tikzpicture}
\end{document}

第一个版本(默认字体和pdflatex

这是一个没有ifthen包的(完整的)解决方案:

在此处输入图片描述

\documentclass[tikz]{standalone}
\renewcommand\familydefault{\sfdefault}
\usepackage{tikz}
\usetikzlibrary{fit,backgrounds}
%
\definecolor{grid color}{HTML}{BBADA0}
\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EDE0C8}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F67C5F}
\definecolor{pixel 64}{HTML}{F65E3B}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{EDCC61}
\definecolor{pixel 512}{HTML}{EDC850}
\definecolor{pixel 1024}{HTML}{EDC53F}
\definecolor{pixel 2048}{HTML}{EDC22E}
\definecolor{pixel 4096}{HTML}{3E3933}
%
\definecolor{small color}{HTML}{776E65}
\definecolor{big color}{HTML}{F9F6F2}
%
\tikzset{
  case 2048 base/.style={minimum size=9mm,rounded corners=.3mm,text=#1,inner sep=0},
  %
  case 2048 LARGE/.style={font=\LARGE\bfseries\sffamily,case 2048 base=#1},
  case 2048 Large/.style={font=\Large\bfseries\sffamily,case 2048 base=#1},
  case 2048 large/.style={font=\large\bfseries\sffamily,case 2048 base=#1},
  case 2048 normal/.style={font=\normalsize\bfseries\sffamily,case 2048 base=#1},
  %
  case 2048 0/.style={case 2048 Large=black,fill=pixel 0,node contents={}},
  case 2048 2/.style={case 2048 Large=small color,fill=pixel 2,node contents={2}},
  case 2048 4/.style={case 2048 Large=small color,fill=pixel 4,node contents={4}},
  case 2048 8/.style={case 2048 Large=big color,fill=pixel 8,node contents={8}},
  case 2048 16/.style={case 2048 Large=big color,fill=pixel 16,node contents={16}},
  case 2048 32/.style={case 2048 Large=big color,fill=pixel 32,node contents={32}},
  case 2048 64/.style={case 2048 Large=big color,fill=pixel 64,node contents={64}},
  case 2048 128/.style={case 2048 large=big color,fill=pixel 128,node contents={128}},
  case 2048 256/.style={case 2048 large=big color,fill=pixel 256,node contents={256}},
  case 2048 512/.style={case 2048 large=big color,fill=pixel 512,node contents={512}},
  case 2048 1024/.style={case 2048 normal=big color,fill=pixel 1024,node contents={1024}},
  case 2048 2048/.style={case 2048 normal=big color,fill=pixel 2048,node contents={2048}},
  case 2048 4096/.style={case 2048 normal=big color,fill=pixel 4096,node contents={4096}},
}
\begin{document}
\begin{tikzpicture}
  \def\pixels{
    {0,2,32,64},
    {256,8,512,4},
    {1024,2048,4,16},
    {4096,16,128,2},
  }

  \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \path (\x,-\y) node[name=c2048-\x-\y,case 2048 \pix];
    }
  }

  \begin{scope}[on background layer]
    \node[fill=grid color,fit=(c2048-1-1)(c2048-4-4),
    inner sep=1mm,rounded corners=.3mm]{};
  \end{scope}
\end{tikzpicture}
\end{document}

答案2

这是一个可能的解决方案,目的是尽可能减少对原始代码的侵入性。

修改涉及每个单元的实现方式:以前的\drawing 机制已成为\node。事实上,使用扩展因子\grshift来分隔单元,有必要准确知道背景的位置,因此具有名称的节点使生活变得更轻松。

\documentclass[tikz,border=10pt]{standalone}
\usepackage{ifthen}
\renewcommand\familydefault{\sfdefault}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}
\def\pixels{
{0,2,0,0},
{0,8,0,4},
{2,2,4,16},
{8,16,4096,2},% changed one value for testing purposes
}

% Font color for 2 and 4: #776e65
% Font color for rest: #f9f6f2
% Grid color: #bbada0
% Font family: "Clear Sans", "Helvetica Neue", Arial, sans-serif

\definecolor{gridc}{HTML}{BBADA0}

\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EEE4DA}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F2B179} % TODO
\definecolor{pixel 64}{HTML}{F2B179}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{F2B179} % TODO
\definecolor{pixel 512}{HTML}{F2B179} % TODO
\definecolor{pixel 1024}{HTML}{F2B179} % TODO
\definecolor{pixel 2048}{HTML}{F2B179} % TODO
\definecolor{pixel 4096}{HTML}{3E3933}

\definecolor{font 0}{HTML}{F9F6F2}
\definecolor{font 2}{HTML}{776E65}
\definecolor{font 4}{HTML}{776E65}
\definecolor{font 8}{HTML}{F9F6F2}
\definecolor{font 16}{HTML}{F9F6F2}
\definecolor{font 32}{HTML}{F9F6F2}
\definecolor{font 64}{HTML}{F9F6F2}
\definecolor{font 128}{HTML}{F9F6F2}
\definecolor{font 256}{HTML}{F9F6F2}
\definecolor{font 512}{HTML}{F9F6F2}
\definecolor{font 1024}{HTML}{F9F6F2}
\definecolor{font 2048}{HTML}{F9F6F2}
\definecolor{font 4096}{HTML}{F9F6F2}

\tikzset{shift value/.store in=\grshift,
  shift value=1.2,
}

\begin{document}
\begin{tikzpicture}
  \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \node [draw=none,fill=pixel \pix,
            rounded corners,minimum size=1cm]
            (\x\y) at (\grshift*\x,-\grshift*\y) {};
      % putting text
      \ifthenelse{\equal{0}{\pix}}
           {}
           {\node[font=\bfseries,text=font \pix] at (\x\y)  {\pix};}
    }
  }

\begin{scope}[on background layer]
\fill[gridc,rounded corners]
  ($(11)+(-.65*\grshift,.65*\grshift)$)
 rectangle
  ($(44)+(.65*\grshift,-.65*\grshift)$);
\end{scope}
\end{tikzpicture}
\end{document}

结果:

在此处输入图片描述

(0,0)然而,由于 ,以前的解决方案并未完全对齐\grshift。考虑到这一点,改进版本:

\documentclass[tikz,border=10pt]{standalone}
\renewcommand\familydefault{\sfdefault}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}

\definecolor{gridc}{HTML}{BBADA0}

\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EDE0C8}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F67C5F}
\definecolor{pixel 64}{HTML}{F65E3B}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{EDCC61}
\definecolor{pixel 512}{HTML}{EDC850}
\definecolor{pixel 1024}{HTML}{EDC53F}
\definecolor{pixel 2048}{HTML}{EDC22E}
\definecolor{pixel 4096}{HTML}{3E3933}

\definecolor{font 0}{HTML}{F9F6F2}
\definecolor{font 2}{HTML}{776E65}
\definecolor{font 4}{HTML}{776E65}
\definecolor{font 8}{HTML}{F9F6F2}
\definecolor{font 16}{HTML}{F9F6F2}
\definecolor{font 32}{HTML}{F9F6F2}
\definecolor{font 64}{HTML}{F9F6F2}
\definecolor{font 128}{HTML}{F9F6F2}
\definecolor{font 256}{HTML}{F9F6F2}
\definecolor{font 512}{HTML}{F9F6F2}
\definecolor{font 1024}{HTML}{F9F6F2}
\definecolor{font 2048}{HTML}{F9F6F2}
\definecolor{font 4096}{HTML}{F9F6F2}

\tikzset{shift value/.store in=\grshift,
  shift value=1.2cm,
  module/.style={minimum size=1cm},
  every module/.code={\tikzset{module/.append style={#1}}},
  game/.code={%
    \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \node [draw=none,fill=pixel \pix,
            rounded corners,
            module]
            (gm-\x-\y) 
            at ([xshift=-.45*\grshift,yshift=.45*\grshift]\grshift*\x,-\grshift*\y) {};%
      % putting text
      \ifnum\pix=0\relax%
      \else%
        \node[font=\bfseries,text=font \pix] at (gm-\x-\y)  {\pix};%
      \fi%
    }%
  }%

  \begin{scope}[on background layer]
  \fill[gridc,rounded corners]
    ($(gm-1-1)+(-.55*\grshift,.55*\grshift)$)
   rectangle
    ($(gm-4-4)+(.55*\grshift,-.55*\grshift)$);
  \end{scope}%
  },%
}%

% that's just an alias for \node
\makeatletter
\newcommand{\drawgame}[1][game]{\tikz@path@overlay{node}[#1]}
\makeatother


\begin{document}
\def\pixels{
 {0,2,32,64},
 {256,8,512,4},
 {1024,2048,4,16},
 {4096,16,128,2},
}

\begin{tikzpicture}
\drawgame{};
\end{tikzpicture}

\begin{tikzpicture}
\drawgame[shift value=1.5cm, 
 every module={minimum size=1.2cm},
 game]% specify as last option the actual code ``game''
 {};
\draw(0,0)--(7,0);
\end{tikzpicture}
\end{document}

第二张图片提供:

在此处输入图片描述

答案3

另一个起点。这个使用matrix和自定义样式来处理每种类型的注释。最终颜色是从 Claudio 的答案中偷来的。

下一个代码定义了一个命令\drawboard{},当指示行时,该命令可以创建一个空板或一个已经填充的板\drawboard{{2,0,16},{64,128,0,32},{0,4},{2,4,4}}(不需要填充所有位置)。

命令用指示的\drawpixel{x-y}{value}填充棋盘位置。一旦创建 ,即可使用。x-yvalueboard

\documentclass[tikz, border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
% Font color for 2 and 4: #776e65
% Font color for rest: #f9f6f2
% Grid color: #bbada0
% Font family: "Clear Sans", "Helvetica Neue", Arial, sans-serif

\definecolor{board}{HTML}{BBADA0}
\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EDE0C8}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F67C5F}
\definecolor{pixel 64}{HTML}{F65E3B}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{EDCC61}
\definecolor{pixel 512}{HTML}{EDC850}
\definecolor{pixel 1024}{HTML}{EDC53F}
\definecolor{pixel 2048}{HTML}{EDC22E}
\definecolor{pixel 4096}{HTML}{3E3933}

\definecolor{font 0}{HTML}{F9F6F2}
\definecolor{font 2}{HTML}{776E65}
\definecolor{font 4}{HTML}{776E65}
\definecolor{font 8}{HTML}{F9F6F2}
\definecolor{font 16}{HTML}{F9F6F2}
\definecolor{font 32}{HTML}{F9F6F2}
\definecolor{font 64}{HTML}{F9F6F2}
\definecolor{font 128}{HTML}{F9F6F2}
\definecolor{font 256}{HTML}{F9F6F2}
\definecolor{font 512}{HTML}{F9F6F2}
\definecolor{font 1024}{HTML}{F9F6F2}
\definecolor{font 2048}{HTML}{F9F6F2}
\definecolor{font 4096}{HTML}{F9F6F2}

\tikzset{
    pixel/.style={rounded corners, minimum width=1cm, minimum height=1cm, 
                   anchor=center, fill=#1, font=\sffamily},
    pixel 0/.style={pixel=pixel 0, text=font 0,node contents={~}},
    pixel 2/.style={pixel=pixel 2, text=font 2, node contents={2}},
    pixel 4/.style={pixel=pixel 4, text=font 4, node contents={4}},
    pixel 8/.style={pixel=pixel 8, text=font 8, node contents={8}},
    pixel 16/.style={pixel=pixel 16, text=font 16, node contents={16}},
    pixel 32/.style={pixel=pixel 32, text=font 32, node contents={32}},
    pixel 64/.style={pixel=pixel 64, text=font 64, node contents={64}},
    pixel 128/.style={pixel=pixel 128, text=font 128, node contents={128}},
    pixel 256/.style={pixel=pixel 256, text=font 256, node contents={256}},
    pixel 512/.style={pixel=pixel 512, text=font 512, node contents={512}},
    pixel 1024/.style={pixel=pixel 1024, text=font 1024, node contents={1024}},
    pixel 2048/.style={pixel=pixel 2048, text=font 2048, node contents={2048}},
    board/.style={matrix of nodes, nodes={pixel=pixel 0}, fill=board, 
                         column sep=2mm, row sep=2mm, nodes in empty cells, 
                         rounded corners=1mm}
    }

\def\pixels{{0,2,0,32},{2,8,2,4},{2,2,4,16},{8,16,128,2}}

\newcommand{\drawboard}[1]{
    \matrix (board) [board, ampersand replacement=\&]%
            { \& \& \& \\ \& \& \& \\ \& \& \& \\ \& \& \& \\};
    \foreach \line [count =\y] in {#1} { 
        \foreach \pix [count = \x] in \line {
            \node[pixel \pix, at=(board-\y-\x)];
        }
    }   
}

\newcommand{\drawpixel}[2]{
    \node[pixel #2, at=(board-#1)];
}

\begin{document}   
\begin{tikzpicture}
\drawboard{{0,2,0,32},{2,8},{2},{8,16,128,2}}

\drawpixel{1-2}{4}
\drawpixel{3-3}{1024}

\end{tikzpicture}
\end{document}

2048 棋盘

答案4

以下是基于环境的另一种解决logicpuzzle方案hitori

\documentclass{standalone}
\usepackage{logicpuzzle}
\renewcommand\familydefault{\sfdefault}
\makeatletter
% defining colors LP@c@romannumeral
\definecolor{LP@c@zero}{HTML}{CCC0B3}
\definecolor{LP@c@grid}{HTML}{BBADA0}
\definecolor{LP@c@numcoli}{HTML}{776E65}
\definecolor{LP@c@numcolii}{HTML}{F9F6F2}
\definecolor{LP@c@ii}{HTML}{EEE4DA}
\definecolor{LP@c@iv}{HTML}{EEE4DA}
\definecolor{LP@c@viii}{HTML}{F2B179}
\definecolor{LP@c@xvi}{HTML}{F59563}
\definecolor{LP@c@xxxii}{HTML}{F2B179}
\definecolor{LP@c@lxiv}{HTML}{F2B179}
\definecolor{LP@c@cxxviii}{HTML}{EDCF72}
\definecolor{LP@c@cclvi}{HTML}{F2B179}
%
%redefine \setcolorrow for using different zero color
\renewcommand*\setcolorrow[2]%
{%
  \setcounter{LP@counti}{1}%
  \setcounter{LP@countii}{#1}%
  \gdef\LP@fontcolor{LP@c@numcolii}%
  \foreach \LP@element in {#2}%
  {%
    \ifthenelse{\equal{\LP@element}{0}}%
    {%
      \gdef\LP@HT@color{LP@c@zero}%
      \begin{puzzlebackground}%
        \fillcell{\arabic{LP@counti}}{\arabic{LP@countii}}%
      \end{puzzlebackground}%
    }%
    {%
      \expandafter\gdef\expandafter\LP@HT@color{LP@c@\romannumeral\LP@element}%
      \begin{puzzlebackground}%
        \fillcell{\arabic{LP@counti}}{\arabic{LP@countii}}%
      \end{puzzlebackground}%
      \ifthenelse{\equal{\LP@element}{2}}%
        {\def\LP@fontcolor{LP@c@numcoli}}{}%
      \ifthenelse{\equal{\LP@element}{4}}%
        {\def\LP@fontcolor{LP@c@numcoli}}{}%
      \node[color=\LP@fontcolor,font=\bfseries] at (\arabic{LP@counti}.5,\arabic{LP@countii}.5){\LP@element};%
    }%
    \stepcounter{LP@counti}%
  }%
}%
%
% redefining grid color and line width
\renewcommand*\LP@drawgrid[5]%
{%
  \setcounter{LP@counti}{#3}% max column
  \setcounter{LP@countii}{#4}% max row
  \stepcounter{LP@counti}%
  \stepcounter{LP@countii}%
  \draw[color=LP@c@grid,step=#5,line width=2.4pt,\LP@grid@linestyle,%
        draw opacity=\LP@draw@opacity,line join=round,line cap=round]%
        (#1,#2) grid (\value{LP@counti},\value{LP@countii});%
}%
\let\mypuzzle\hitori
\let\endmypuzzle\endhitori
\makeatother
\begin{document}
\begin{mypuzzle}[rows=4,columns=4,width=4.3cm]
\setcolorrow{4}{0,2,0,0}
\setcolorrow{3}{0,8,0,4}
\setcolorrow{2}{2,2,4,16}
\setcolorrow{1}{8,16,128,2}
\end{mypuzzle}
\end{document}

在此处输入图片描述

相关内容