Create the shortest program to check who has won in an nd tic tac toe game.
Your program should work when n
(width) and d
(dimension number) are in these ranges:
n∈[3,6]∩ℕ ie a number from this list: 3,4,5,6
d∈[2,5]∩ℕ ie a number from this list: 2,3,4,5
n = 3; d = 2
(32 ie 3 by 3):
[][][]
[][][]
[][][]
n = 3; d = 3
(33 ie 3 by 3 by 3):
[][][]
[][][]
[][][]
[][][]
[][][]
[][][]
[][][]
[][][]
[][][]
n = 6; d = 2
(62 ie 6 by 6):
[][][][][][]
[][][][][][]
[][][][][][]
[][][][][][]
[][][][][][]
[][][][][][]
And so on.
Winning (If you've played enough multi-dimensional tic tac toe, this is the same.)
In order for there to be a win, one player must have all the adjacent squares along a line. That is, that player must have n
moves on a line to be a winner.
Adjacent:
- each tile is a point; for example (0,0,0,0,0) is a point in
d=5
- adjacent tiles are tiles such they are both points on the same unit d-cube. In other words, the Chebyshev distance between the tiles is 1.
- in other words, if a point
p
is adjacent to a pointq
, then every coordinate inp
s corresponding coordinate inq
differs from it by no more than one. Additionally, at least on coordinate pair differs by exactly one.
Lines:
- Lines are defined by vectors and a tile. A line is each tile hit by the equation:
p0 + t
<
some vector with the same number of coordinates as p0>
Input:
Input will be to STDIN. The first line of input will be two numbers, n
and d
in the form n,d
.
After this will be a line consisting of coordinates specifying the moves that have been done. Coordinates will be listed in the form: 1,1;2,2;3,3
. The upper left corner is the origin ( 0,0 for 2D). In the general case, this list will be like 1,2,...,1,4;4,0,...,6,0;...
where the first number represents left-right-ness, the second up-down-ness, the third through the 3rd dimension, etc. Note that the first coordinate is X
s first turn, the second is O
s first turn, ....
The input will be followed by a newline.
Output:
Output will be to STDOUT. Simply indicate who won if someone won, or if it is a tie. If it is neither a tie nor a win, don't output anything.
Additionally, indicate if there is a move clash, that is, if there are at least two moves in the same location.
If there was a win/draw before the input ended, your program can do whatever it wants.
Test cases (anyone want to suggest any more?):
Input:
4,3
0,0,0;1,1,1;1,0,1;2,0,2;0,0,1;2,0,0;2,0,1;3,0,2;3,0,1
Example Output:
X wins
Another possible output (requires explanation):
1
n
moves on a line to be a winner. (Sorry for not posting these remarks in the sandbox but I didn't even have time to even see it there because it was posted so soon after sandboxing.) \$\endgroup\$