14
0
Background
Skip to "Task" if you are familiar with Tic-Tac-Toe (I think most are!)
Tic-Tac-Toe is a famous two-player game. It consists of a 3x3 board that is filled gradually by two players (clarifications below); The first player uses the character X
and the other one uses O
. The winner is the first to get 3 consecutive and identical characters (X
or O
), either horizontally, vertically or diagonally. In case the board is filled and none of the players managed to get three consecutive characters as decribed above, the game ends in a tie. Note that there might be empty spots at the end of the game, in case either of the players wins in less than 9 moves in total (this cannot happen in case of a tie).
Task
Given a Tic-Tac-Toe board at the end of a game (in the form of a string, a matrix, a flat list of 9 ordered values, any other decent format), determine who wins the game.
The input will consist of distinct and consistent values, one for
X
, one forO
and another one that represents an empty spot.Your program should be able to output 3 distinct, consistent and non-empty values: one in case
X
wins, another one in caseO
wins or another if the players are tied.Please specify these values in your answer. You can assume that the input will be a valid Tic-Tac-Toe board.
Test Cases
X
, O
, _
are the input values here; X wins
, O wins
and Tie
are for the output.
X O X
O X _
O _ X
Output: X wins
.
X _ O
X O _
X O X
Output: X wins
.
X O X
_ O X
_ O _
Output: O wins
.
X O X
O O X
X X O
Output: Tie
.
As usual, all our standard rules apply. This is code-golf, the shortest code in bytes in every language wins!
2Get out of my brain! Literally just had an idea for a Noughts & Crosses challenge that I was gonna Sanbox on Monday. Then I crack open the site and see this! – Shaggy – 2017-09-30T17:58:59.657
1@Shaggy To cite someone from the "Fast and Furious" series: Too slow! ;p – Mr. Xcoder – 2017-09-30T18:00:26.533
It's OK, my idea was for a playable version, assuming that hasn't been done already. – Shaggy – 2017-09-30T18:01:41.313
4@Laikoni I don't think it's a dupe, since this has way more flexible input and output and also has empty boxes too, and this also lets you assume the input is a valid board. – Erik the Outgolfer – 2017-09-30T18:16:22.650
Playing the game is a harder challenge, which obviously involves testing for winning and tied conditions. – dmckee --- ex-moderator kitten – 2017-09-30T20:03:18.043
1@Joshua That’s about making a Tic-tac-toe game. This is about grading one. – DonielF – 2017-10-01T03:57:50.097
@Joshua Apart from the very idea of Tic-Tac-Toe, this has nothing to do with that one. And that shouldn't be a problem anyway since we have a [tag:tic-tac-toe] tag :-) – Mr. Xcoder – 2017-10-01T08:33:30.620
@Mr.Xcoder: Read dmkee's comment. That's what I acted on. – Joshua – 2017-10-01T13:27:33.723
This should be reopened—it’s different enough from the alleged duplicates – Jonah – 2017-10-01T13:54:42.200
@Titus Thanks for your consideration! – Mr. Xcoder – 2017-10-01T14:05:21.120
Could you add a test case where either X or O wins with a diagonal from the top-right to the bottom-left? I succeeded in all test cases, but had only added a check for the top-left to bottom-right diagonal instead of both. – Kevin Cruijssen – 2017-10-02T14:38:10.497
What if we don't know who wins? Say, the input is an empty board. – RedClover – 2017-10-02T15:06:40.070
@Soaku Consider reading the Task section again. The empty board is NOT *a valid board at the end of a game* – Mr. Xcoder – 2017-10-02T16:32:34.190
@KevinCruijssen I will add one later, thanks! – Mr. Xcoder – 2017-10-02T16:33:40.053
@Mr.Xcoder that's true, sorry. I didn't understood what you meant by "at the end of a game", now I get it. – RedClover – 2017-10-02T16:35:40.917