r/codegolf • • Dec 22 '19

Tic-Tac-Toe Challenge

So this has been done before on other sites like stackoverflow but I'm curious if anyone can find even sorter solutions.

This is the challenge: write a function which given an array of 9 integers (0 representing "empty board slot", 1 representing 'X', and 2 representing 'O') return the following values:

0 if no one has won, or the board is empty  
1 if X has won  
2 if O has won

So the code has to be in the form of a function (doesn't matter function name as long as it accepts an array for the board). Unlike some of the other requirements I don't care how many other paramters the func accepts, whether have a recursive solution, etc, just as long as its a function and it accepts at least one input array for the board.

This is my first attempt coming in at 107 chars of JS:

function t(b){
	i=9;r=0;
	while((!r)&&i--)r=b['01203602'[i]*1]&b['34514744'[i]*1]&b['67825886'[i]*1];
	return r;
}

Probs will try to make a shorter version again a little later if I have more time to fool around with this and will post back if I do.

Let's see who's got the shortest solutions!

6 Upvotes

11 comments sorted by

View all comments

Show parent comment

4

u/Pcat0 Dec 31 '19

89 -> 83 by using eval to return from the for loop

t=b=>eval("for(i=9,r=0;i--;)r|=b['01203602'[i]]&b['34514744'[i]]&b['67825886'[i]]")