![使用 LaTeX/TikZ 创建不带表格的井字游戏棋盘](https://linux22.com/image/446388/%E4%BD%BF%E7%94%A8%20LaTeX%2FTikZ%20%E5%88%9B%E5%BB%BA%E4%B8%8D%E5%B8%A6%E8%A1%A8%E6%A0%BC%E7%9A%84%E4%BA%95%E5%AD%97%E6%B8%B8%E6%88%8F%E6%A3%8B%E7%9B%98.png)
答案1
编辑
我认为抛出一个矩阵将您想要的内容放入单元格并获得结果,因此这是一个改进的版本:
\documentclass{article}
\usepackage{tikz}
\newcounter{num}
\newcommand{\tictactoe}[1]
{
\begin{tikzpicture}[line width=2pt]
\def\r{3mm}
\tikzset{
circ/.pic={\draw circle (\r);},
cross/.pic={\draw (-\r,-\r) -- (\r,\r) (-\r,\r) -- (\r,-\r);},
opt/.pic={\draw[opacity=0.2] (-\r,-\r) -- (\r,\r) (-\r,\r) -- (\r,-\r);}
}
% The grid
\foreach \i in {1,2} \draw (\i,0) -- (\i,3) (0,\i) -- (3,\i);
% Numbering the cells
\setcounter{num}{0}
\foreach \y in {0,...,2}
\foreach \x in {0,...,2}
{
\coordinate (\thenum) at (\x+0.5,2-\y+0.5);
%\node[opacity=0.5] at (\thenum) {\sffamily\thenum}; % Uncomment to see numbers in the cells
\addtocounter{num}{1}
}
\def\X{X} \def\x{x} \def\O{O} \def\n{n}
\foreach \l [count = \i from 0] in {#1}
{
\if\l\X \path (\i) pic{cross};
\else
\if\l\O \path (\i) pic{circ};
\else
\if\l\x \path (\i) pic{opt};
\else
\if\l\n \node[opacity=0.5] at (\i) {\sffamily\i};
\fi
\fi
\fi
\fi
}
\end{tikzpicture}
}
\begin{document}
\tictactoe{O,x,n,
x,X,x,
n,x,O}
\vspace*{1cm}
\tictactoe{ ,O,X,
O,X, ,
X, ,O}
\end{document}