Skip to the content

The game of tic-tac-toe

Problem

Rosalba, the sixth-grade teacher, keeps finding games of tic-tac-toe doodled in the margins of her notebook. She goes over them one at a time: some are already finished, some are halfway through and some could never have happened.

Write a function that takes board, a list of three strings of three characters each: every square holds an X, an O, or a space if the square is free. Lining up three means getting three of the same in a row, in a column or in one of the two diagonals.

In tic-tac-toe X goes first and from there they take turns, so there are never more O than X, nor two more X than O. And the moment somebody lines up three, the game ends right there: after that move nobody plays again, neither the one who lined up nor the other.

Return one of these four strings. "impossible" if the drawing could not have come out of a game like that, and there are three reasons for it: that the counts of X and O do not fit the turns, that both lined up three, or that somebody lined up three and the board still shows moves that no longer fit after that one. "win" if the drawing is possible and somebody lined up three. "draw" if nobody lined up three and no square is free. "ongoing" if nobody lined up three and some square still is. It does not matter who won, only that somebody did: with ["XOO", "X ", "X "] the left column is all X, so you return "win". With ["O ", " X ", " "] the game has barely started: "ongoing". With ["OOX", " ", " "] there are two O and a single X, and with those turns that could not have happened: "impossible". With ["XXX", "OO ", "O "] the X closed the top row and the game ended on that move, yet the drawing has O moves that no longer fit: it is "impossible" too.

Examples

  • X lined up the left column

    ["XOO", "X ", "X "] → "win"

  • The game has barely started

    ["O ", " X ", " "] → "ongoing"

  • Two O and a single X

    ["OOX", " ", " "] → "impossible"

  • X lined up and O played afterwards

    ["XXX", "OO ", "O "] → "impossible"

  • O lined up the middle column

    ["XOX", " OX", " O "] → "win"

  • X lined up the top row

    ["XXX", "XOO", "O "] → "win"

  • O lined up the bottom row

    ["XOX", " XX", "OOO"] → "win"

  • X lined up the falling diagonal

    ["XOO", " X ", " X"] → "win"

  • O lined up the rising diagonal

    [" O", " OX", "OXX"] → "win"

  • The board filled up with nobody lining up

    ["XOX", "XXO", "OXO"] → "draw"

  • Another full board with no winner

    ["XXO", "OXX", "XOO"] → "draw"

  • Five moves in and the game goes on

    ["X ", " XO", "OX "] → "ongoing"

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def game_state(board):
    pass

JavaScript

function gameState(board) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →