Let's play some code golf!
Given a tic‐tac‐toe board state, for example:
$$
\begin{bmatrix}
❌ & ❌ & ⭕ \\
❌ & ⭕ & ❌ \\
⭕ & ⭕ & ❌
\end{bmatrix}
$$
Determine whether a game is a win
, a lose
or cat
. Your code should output any of these options given a state. The above game should output lose
.
Just to be clear:
- A win is defined as any 3 X's in a row (diagonal, horizontal, vertical).
- A lose is 3 O's in a row, while
- a cat game is if neither 3 X's nor 3 O's appear in a row.
To make things interesting, you get to determine your input structure for the state – which you must then explain.
- For instance
'xxoxoxoox'
is a valid state as seen above where each of the characters is read from left to right, top to bottom. [['x','x','o'],['x','o','x'],['o','o','x']]
is the game in multidimensional array read in a similar way.- While
0x1a9
which is hexadecimal for110101001
binary might work as a suitable compression where1
can be manipulated for X’s and0
can be manipulated for O's.
But these are just some ideas. I'm sure you might have many of your own.
Ground rules:
- Your program must be able to accept any viable state.
- The form of input must be able to represent any state.
- The input cannot be redundant, meaning each cell must only appear in one location.
- "The win state must be determined from the board."
- Assume a complete board.
Win
beforelose
. For instance in the case $$ \begin{bmatrix} ❌ & ❌ & ❌ \\ ⭕ & ⭕ & ⭕ \\ ❌ & ❌ & ❌ \end{bmatrix} $$
The lowest character count wins.
win
,lose
,cat
. For input it must be from a human (not randomly generated). Thus usingdef q(x):#magic code here
works just as well as(/*magic code */)(prompt())
\$\endgroup\$