48
11
Challenge
Given a tic-tac-toe board in any format, determine if it is valid or not. If a board can be the result of a tic-tac-toe game, then it is valid. For example, this board is valid:
X O X O X O X O XOn the contrary, this board is invalid:
X X X X X O O O O
Input
- A full (9/9) tic tac toe board (the outcome, not the game).
Rules
- The input format must be able to depict all 512 possible input boards. It must be specified, along with the instructions to create it if it is obscure/unclear. You must state the marks of the board individually though.
- There must be two possible outputs, one for validity and one for invalidity.
- You can assume the board does not have empty spots.
Test cases
Valid:
X O X O X O X O X X O X X O X O X O X O O O O X O X X O X O X O X O X O
Invalid:
X X X X X X X X X O O O O O O O O O X X X O O O X X X O O O O O X X X X X X O O X O O O X
A little help?
A board is considered valid (for this challenge) if and only if the following two conditions hold:
- There are 5 X and 4 O, or 4 X and 5 O. For example,
X X X O X O X X X
is considered invalid, because there are 7 Xs and 2 Os. - Only the player with 5 marks has won, or none of them have won. For example,
X X X O O O O O X
is considered invalid, since either the row ofO
s or the row ofX
s will be formed first. The two players can't have their turn simultaneously.
The current winner is...
...ais523's Jelly answer, at an astounding 26 bytes!
what if some of the board states are null? EG: a player got 3 in a row, thus the game was ended, and some spaces are left blank – tuskiomi – 2016-12-12T15:24:47.257
@tuskiomi *A full (9/9) tic tac toe board (the outcome, not the game).* – Erik the Outgolfer – 2016-12-12T15:26:09.247
2Maybe also add a test-case like
O O O
X O X
X O X
, to show that the same player may have both a horizontal and vertical row. – smls – 2016-12-12T18:56:37.7232You must state the marks of the board individually though. I'm not sure to understand that part. Could you provide a counterexample? – Arnauld – 2016-12-12T20:30:38.283
1So
XXX~OOE~EEE
isn't valid input whereE
is empty? Can we assume or do we have to explicitly check that all squares are filled; in other words, can the program error on partial boards? – Magic Octopus Urn – 2016-12-12T21:40:40.470A normal tic-tac-toe game can never have 4 X and 5 O since X goes first. – David Conrad – 2016-12-12T23:12:50.907
1In the list of invalid test cases, I don't understand why the last one is invalid. The X has 5 marks, and has won. – Tim – 2016-12-13T03:12:59.947
3@Tim X has 4 marks. – Martin Ender – 2016-12-13T07:44:51.927
@MartinEnder "There are 5 X and 4 O, or 4 X and 5 O." – Sparr – 2016-12-13T09:34:05.347
2@Sparr "Only the player with 5 marks has won, or none of them have won." – Martin Ender – 2016-12-13T09:37:46.847
@carusocomputing As I replied to tuskiomi, the input will always be a full board. You do not have to test that. – Erik the Outgolfer – 2016-12-13T10:59:30.637
@Arnauld I mean that parts of the input itself must contain the states (i.e.
375
is not acceptable, but[[0,1,0],[1,1,0],[0,0,1]]
is, because the latter contains the states in itself). – Erik the Outgolfer – 2016-12-13T11:07:49.620@downvoter(s): Why did you downvote? – Erik the Outgolfer – 2016-12-13T17:21:16.220
1Why is a board considered invalid if only the player with 4 marks wins? – Kevin – 2016-12-13T23:00:07.287
1Why aren't boards with fewer than 9 marks total included? There is a third state, unused, that a tile can be in, and if a player wins in fewer than 9 turns, one or more of the tiles will be in that state at the end of a game. – Kevin – 2016-12-13T23:00:15.607
2@Kevin (Reply to 1st comment) Because no 9/9 board will ever be completed if the second player (player with 4 marks) wins. – Erik the Outgolfer – 2016-12-14T11:03:59.137
1@Kevin (Reply to 2nd comment) So as to not make it awfully difficult. – Erik the Outgolfer – 2016-12-14T11:04:28.783