Problème
A 3×3 board is given as three strings of X, O and . (empty). A player wins with three of their marks in a row, column or diagonal. Boards never have two winners.
Implement tic_tac_toe_winner(board).
Input
board: Three rows of three characters each.
Output
Return "X" or "O" for the winner, "Draw" if the board is full with no winner, or "Pending" if empty cells remain and nobody has won.
Contraintes
board has exactly 3 rows of length 3
Exemples
Entrée :
board = ["XOX", ".X.", "O.X"]
Sortie attendue :
expected = "X"
Entrée :
std::vector<std::string> board = {"XOX", ".X.", "O.X"};
Sortie attendue :
std::string expected = "X";
Entrée :
string[] board = new string[] {"XOX", ".X.", "O.X"};
Sortie attendue :
string expected = "X";
Explication
X holds the diagonal from top-left to bottom-right, so X wins.
Entrée :
board = ["XOX", "XOO", "OXX"]
Sortie attendue :
expected = "Draw"
Entrée :
std::vector<std::string> board = {"XOX", "XOO", "OXX"};
Sortie attendue :
std::string expected = "Draw";
Entrée :
string[] board = new string[] {"XOX", "XOO", "OXX"};
Sortie attendue :
string expected = "Draw";
Explication
The board is full and no line has three of a kind: Draw.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Vérifie chaque ligne, chaque colonne et les deux diagonales pour trois marques identiques non vides.
Indice 2
Garde aussi la trace des cases vides pendant un seul parcours du plateau.
Indice 3
Rappelle-toi qu’un plateau plein sans gagnant est une égalité, sinon la partie est en attente.
Complexité attendue
temps O(1), espace supplémentaire O(1)
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.