Validate the Tic-Tac-Toe Game State

Published by persolut in

The function is given a list of three strings representing a board. The characters can be "X", "O", " ". The first player writes "X" at first turn. If a player has three marks in a row, column or a diagonal, the game stops. Given the board evaluate if this state can be achieved in line with the rules, return true / false.

Examples

validateTicTacToe(["X  ", "   ", "   "]) ➞ true
// X goes first.

validateTicTacToe(["O  ", "   ", "   "]) ➞ false
// O cannot go first.

validateTicTacToe(["X X", " O ", "   "]) ➞ true
// Two X and one O is a possible state.

validateTicTacToe(["XOX", " X ", "   "]) ➞ false
// Three X and one O is not a possible state.
// Players have to go one after another.

Notes

N/A

Watch a quick demo on how Edabit works.