Validate the Tic-Tac-Toe Game State

Published by Mubashir Hassan in

The function is given an array 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

validate_tic_tac_toe(["X  ", "   ", "   "]) ➞ true
# X goes first.

validate_tic_tac_toe(["O  ", "   ", "   "]) ➞ false
# O cannot go first.

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

validate_tic_tac_toe(["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.