Problem
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.
Einschränkungen
board has exactly 3 rows of length 3
Beispiele
Eingabe:
board = ["XOX", ".X.", "O.X"]
Erwartete Ausgabe:
expected = "X"
Eingabe:
std::vector<std::string> board = {"XOX", ".X.", "O.X"};
Erwartete Ausgabe:
std::string expected = "X";
Eingabe:
string[] board = new string[] {"XOX", ".X.", "O.X"};
Erwartete Ausgabe:
string expected = "X";
Erläuterung
X holds the diagonal from top-left to bottom-right, so X wins.
Eingabe:
board = ["XOX", "XOO", "OXX"]
Erwartete Ausgabe:
expected = "Draw"
Eingabe:
std::vector<std::string> board = {"XOX", "XOO", "OXX"};
Erwartete Ausgabe:
std::string expected = "Draw";
Eingabe:
string[] board = new string[] {"XOX", "XOO", "OXX"};
Erwartete Ausgabe:
string expected = "Draw";
Erläuterung
The board is full and no line has three of a kind: Draw.
So gehst du vor
Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.
Hinweis 1
Prüfe jede Zeile, jede Spalte und die beiden Diagonalen auf drei gleiche, nicht leere Markierungen.
Hinweis 2
Behalte beim einmaligen Durchlauf des Spielfelds im Blick, ob noch leere Felder übrig sind.
Hinweis 3
Denk daran: Ein volles Spielfeld ohne Sieger ist ein Unentschieden, sonst ist das Spiel noch offen.
Erwartete Komplexität
Zeit O(1), zusätzlicher Speicher O(1)
Referenzen
Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.