Wednesday, August 3, 2011

Solution to 4Clojure.com tic-tac-toe problem

Ugly but short.

From bottom to top: a vector of all the possible win combinations for a flattened board:

012 is the top row
036 is the first column, etc
ending with
048 and 246 as the two diagonals.

Make the input a vector so I can use map and the vector to find the values at the specific locations.

Then split those values into groups of three and look for all :o's or all :x's and return a nil if none of those combinations exist.

#(some {[:o :o :o] :o [:x :x :x] :x}
(partition 3
(map
(vec (flatten %))
[0 1 2 3 4 5 6 7 8
0 3 6 1 4 7 2 5 8
0 4 8 2 4 6])))