13
3
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
Definition of line is unclear to me. Does right-right-up&right-right count? I think current def. might allow for that as well.. – lorro – 2016-07-18T15:33:39.853
How are you defining the topology of dimensions n>3 for determining what a straight lines along a diagonal is? For example, does any line through any 3 adjacent vertices constitute a win on a 3⁵ board? Are the middle squares of each 3² plane connected to every point of another plane that shares an edge with it on the n-cube? – Comintern – 2014-02-26T04:29:07.773
1@Comintern How's that (I probably butchered the explanation. Could definitely be simpler). – Justin – 2014-02-26T05:43:25.823
Note: the definitions you gave for adjacent tiles are not equivalent (i.e. it is not manhattan distance equals one). – Howard – 2014-02-26T06:26:08.567
Moreover you should define that it takes
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.) – Howard – 2014-02-26T06:30:12.717@Howard Ugh. I'm so confused. What do you call distance when traveling diagonally across a unit-right triangle is the same distance as traveling across the leg? – Justin – 2014-02-26T06:31:27.253
I'm no mathematician - maybe maximum metric? – Howard – 2014-02-26T06:33:05.413
@Howard Got it. The two points are both on the same unit cube. – Justin – 2014-02-26T06:34:09.423
That makes a lot more sense now. I was envisioning more as rotating the 3D board 90° through 4D and 5D space. – Comintern – 2014-02-26T16:18:11.747
1I feel like there should be a very short solution in Prolog... – Nate Eldredge – 2014-03-05T04:31:29.297