26
1
Let's play some code-golf!
The challenge is to find the winner of a game of Tic-Tac-Toe.
This has been done many times by giving a board that has one clear winner but here is the twist:
The cells are numbered like this:
1|2|3
-+-+-
4|5|6
-+-+-
7|8|9
You get an array of exactly 9 moves like that:
{3, 5, 6, 7, 9, 8, 1, 2, 3}
This is parsed as follows:
- Player 1 marks cell 3
- Player 2 marks cell 5
- Player 1 marks cell 6
- Player 2 marks cell 7
- Player 1 marks cell 9
- Player 1 has won
Note: The game does not stop after one player has won, it may happen that the losing player manages to get three in a row after the winning player, but only the first win counts.
Your job is now to get 9 numbers as input and output the winning player and the round in which the win occured. If no one wins, output something constant of your choice. You can receive input and provide output through any standard mean / format.
Have fun!
Some more examples as requested:
{2,3,4,5,6,7,1,8,9} => Player 2 wins in round 6
{1,2,4,5,6,7,3,8,9} => Player 2 wins in round 8
{1,2,3,5,4,7,6,8,9} => Player 2 wins in round 8
11
Welcome to PPCG! This is a nice first post, but usually we do not like very restrictive input / output formats. Would you consider removing the "Player X wins in round Y" and let us output in any reasonable format, like a list
– Mr. Xcoder – 2018-01-09T10:17:14.153[X, Y]
? In case of a tie, can we output any other consistent value instead? I recommend so, because printing those exact strings aren't really part of golfing. For future challenge ideas, I recommend using the sandbox. :-)Sorry, my bad. I think it is correct now. – Grunzwanzling – 2018-01-09T13:16:28.493
Read the challenge to the end, I say that there may be a draw and that you can output something of your choice when it happens. I return {2,6} when player 2 wins in round 6 and {0,0} when no one wins. – Grunzwanzling – 2018-01-09T13:18:25.780
Can we use 0-indexed everything? (cells, players, rounds) – Arnauld – 2018-01-09T21:56:26.813
1"You get an array of exactly 9 moves like that:
{3, 5, 6, 7, 9, 8, 1, 2, 3}
" - should3
really appear twice? – Jonathan Allan – 2018-01-09T22:09:45.797@JonathanAllan I think it's a typo, maybe they meant to type
4
instead (not editing though). – Erik the Outgolfer – 2018-01-10T12:28:46.293