Code Golf: Tic Tac Toe
- by Aistina
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:
0 = nothing set
1 = player 1 (X)
2 = player 2 (O)
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.