Post your shortest code, by character count, to check if a player has won, and if so, which.
Assume you have an integer array in a variable b
(board), which holds the Tic Tac Toe board, and the moves of the players where:
So, given the array b = [ 1, 2, 1, 0, 1, 2, 1, 0, 2 ]
would represent the board
X|O|X
-+-+-
|X|O
-+-+-
X| |O
For that situation, your code should output 1
to indicate player 1 has won. If no-one has won you can output 0
or false
.
My own (Ruby) solution will be up soon.
Edit: Sorry, forgot to mark it as community wiki. You can assume the input is well formed and does not have to be error checked.
Update: Please post your solution in the form of a function. Most people have done this already, but some haven't, which isn't entirely fair. The board is supplied to your function as the parameter. The result should be returned by the function. The function can have a name of your choosing.
Crazy Python solution - 79 characters
max([b[x] for x in range(9) for y in range(x) for z in range(y)
if x+y+z==12 and b[x]==b[y]==b[z]] + [0])
However, this assumes a different order for the board positions in b:
5 | 0 | 7
---+---+---
6 | 4 | 2
---+---+---
1 | 8 | 3
That is, b[5]
represents the top-left corner, and so on.
To minimize the above:
r=range
max([b[x]for x in r(9)for y in r(x)for z in r(y)if x+y+z==12and b[x]==b[y]==b[z]]+[0])
93 characters and a newline.
Update: Down to 79 characters and a newline using the bitwise AND trick:
r=range
max([b[x]&b[y]&b[z]for x in r(9)for y in r(x)for z in r(y)if x+y+z==12])