[Math] Determine the winner of a tic tac toe board with a single matrix expression

game theorymatrices

Assume a tic-tac-toe board's state is stored in a matrix.
$$
S=\begin{bmatrix}
-1 & 0 & 1 \\
1 & -1 & 0 \\
1 & 0 & -1 \\
\end{bmatrix}
$$

Here, $X$ is mapped to $1$, $O$ is mapped to $-1$ and an empty state is mapped to zero, but any other numeric mapping will do if there is one more suitable for solving the problem. Is it possible to create some single expression involving the matrix $S$ which will indicate whether the board is in a winning state? For the above matrix, the expression should show a win for $O$.

I recognize that there are more direct programmatic approaches to this, so this is more of an academic question.

Edit: I have been asked what to do if the board shows two winners. You could either:

  1. Assume only valid board states. Since gameplay would stop after once side wins, it is not possible to have a board with two winners.
  2. Alternatively (or equivalently?), your expression could arbitrarily pick a winner in a board that has two.

Best Answer

Sure, here's one way to do it with linear-algebra primitives. Define column vectors $e_1=(1,0,0)^T$, $e_2=(0,1,0)^T$, $e_3=(0,0,1)^T$, and a row vector $a=(1,1,1)$.

  • You can detect if a row or column of $S$ is a winner by testing $a S e_i$ and $a S^T e_i$ for $\pm 3$. (Exercise: Which expression detects winning rows and which detects winning columns?)
  • You can detect if the main diagonal is a winner by testing the trace of $S$.
  • Finally, let $R$ be the matrix that permutes rows $1$ and $3$; then you can detect if the other diagonal is a winner by testing the trace of $RS$.

In order to combine all eight tests into a single expression, you'll have to specify what you want to do in case of an over-determined matrix. For example, if one row shows a win for $+$ and another row shows a win for $-$, what's the desired behavior?

Edit: Okay, assuming only valid board states, it's not too hard. We're just going to have to introduce some unusual notation. Define a slightly arbitrary function $$ \max^*(a,b)=\begin{cases} a& \text{if } |a| \geq |b|\\ b& \text{otherwise } \end{cases} $$ Then $\max^*$ is an associative operation, so we can extend it iteratively to any number of arguments, and the winner of the game is $$w(S)=\left\lfloor\frac13\max^*\left(\max^*_i(a S e_i), \max^*_i(a S^T e_i), \mathrm{tr}(S),\mathrm{tr}(RS)\right)\right\rceil$$ where $\lfloor x\rceil$ is the round-towards-zero function, so that $w(S)=0$ means nobody wins.

(If $S$ is an invalid state in which both players "win", $w(S)$ will pick the first winner according to the order of expressions tested.)

Edit 2: Here's a theoretical approach that technically uses only matrix multiplication on $S$... but then shifts all the work to a scalar.

Let $a=(1,3,3^2)$ and $b=(1,3^3, 3^6)^T=(1,27,729)^T$. Then $aSb$ is an integer which encodes $S$, and it has absolute value $\leq (3^9-1)/2=9841$. So there exists a polynomial $p$ of degree $\leq 3^9=19683$ such that $w(S)=p(aSb)$.

In fact, we can choose the even coefficients of $p$ to be zero. The odd coefficients are slightly harder to compute. :)

Related Question