GolfScript, 63 + 27 chars= 90 bytes
I originally submitted the following 27-byte entry that exploited a loophole in the rules (as written at the time of submission) allowing a redundant input encoding:
70&.{~"win""lose"if}"cat"if
The input format for this entry is a string consisting of eight octal digits, each (redundantly) encoding three consecutive board squares:
- The first three digits each encode a single row of the board, from top down and left to right.
- The following three digits each encode a single column of the board, from left to right and top down.
- The final two digits each encode one of the diagonals (first from top left to bottom right, then from bottom left to top right).
To encode a sequence (row / column / diagonal) of three squares as an octal digit, replace every x
in the sequence with a 1 and every o
with a 0, and interpret the resulting sequence of ones and zeros as a binary number between 0 and 7 inclusive.
This input format is quite redundant (all board positions are encoded at least twice, with the center position encoded four times), but it does unambiguously represent any possible state of a completely filled tic-tac-toe board, and does not directly encode the winner into the input.
The input may, optionally, contain spaces or other delimiters between digits. In fact, all the program really cares about is whether or not the input string contains the digits 7
or 0
.
For example, the example board:
|x|x|o|
|x|o|x|
|o|o|x|
may be represented by the input:
651 643 50
For convenienceTo make testing the program above easier, here'sI also provided a 63-byte GolfScript program to convert an ASCII art board layout, as shown in the challenge above, into an input string suitable for this program:
."XOxo"--[{1&!}/]:a[3/.zip"048642"{15&a=}%3/]{{2base""+}%}%" "*
This converter ignores any characters other than x
and o
, in either case, in its input. It produces a single digit string (complete with space delimiters as shown above) suitable for feeding into the win-determining program above, so the concatenation of these two programs can be used to determine the winner directly from the ASCII art board, and thus still qualifies as a valid entry under the concatenation of these two programs can be used to determinecurrent challenge rules:
."XOxo"--[{1&!}/]:a[3/.zip"048642"{15&a=}%3/]{{2base""+}%}%" "*70&.{~"win""lose"if}"cat"if
Of course, the winner directly frominput format converted is not particularly well optimized and the ASCII art boardcombined program could easily be golfed further. However, rather than attempting to re-golf a six-year-old solution, I prefer to just keep it as close to its originally submitted form as current rules permit.
Also, here'sPs. Here's a reverse converter, just to demonstrate that the redundant input format for the 27-byte version indeed does unambiguously represent the board:
.56,48>-- 3<{2base-3>{"ox"=}%n}%"|".@@*+);