Editor

Tic-Tac-Toe Winner

#245 · Games and puzzles · Beginner · 35 XP · about 8 min

Two friends left a game of tic-tac-toe on the table. The board is a list of three strings, one per row, and each square is 'X', 'O' or '.' (empty). Write tic_tac_toe(board) that returns how the game stands:

  • 'X' or 'O' if that player has three marks in a row: along a row, down a column, or along one of the two diagonals;
  • 'draw' if nobody has three in a row and the board is full;
  • 'playing' if nobody has three in a row and at least one square is still empty.

The board always comes from a real game that stopped as soon as someone won, so at most one player has three in a row.

Examples

tic_tac_toe(['XXX', 'OO.', '...'])  # returns 'X'
tic_tac_toe(['XOX', 'XOO', 'OXX'])  # returns 'draw'
tic_tac_toe(['X.O', '.X.', 'O..'])  # returns 'playing'
tic_tac_toe(['XXO', 'XO.', 'O..'])  # returns 'O'

In the last example O owns the diagonal that runs from the top right corner to the bottom left one.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.