Tic-tac-toe (also called Xs and Os or noughts and crosses) is a simple game for two players. It is played with paper and pencil on a $$3 \times 3$$ grid. Initially, all spaces in the grid are empty. One player marks spaces with crosses (here represented by the letter X) and the other player marks spaces with noughts (here represented by the letter O). Player in turn mark an empty space in the grid with their own symbol. The player who marks crosses always begins the game. The player who succeeds in placing three respective marks in a horizontal, vertical or diagonal row wins the game. If all places are filled up without any player winning, the game ends in a draw. The following game is won by the player marking crosses.

boter-kaas-en-eiren

Players soon discovered that best play for both parties leads to a draw. Hence, tic-tac-toe is most often played by young children. Especially for beginners, the middle square is the most important square in the grid. In total there are 764 essentially different states of the grid, without taking into account the empty grid and without taking into account positions that are the same after rotation and/or mirroring the grid. There are 2096 valid ways players can mark empty spaces starting from the first mark and successive marks after that. As such, there are three possible opening marks: a mark in the middle, a mark in one of the corners and a mark in the middle of one of the borders.

Assignment

A given text file contains the configuration of a tic-tac-toe game that has just finished. The file contains three lines, each containing three characters. The possible characters are i) a space representing an empty space, ii) a letter X used as a marker by the first player, or iii) a letter O used as a marker by the second player. Your task is to determine which player has won the game. In order to do so, you need to define a class TicTacToe, that at least supports the following three methods:

Example

In the following interactive session we assume that the text files tictactoe1.txt1, tictactoe2.txt2 and tictactoe3.txt3 are located in the current directory.

>>> game = TicTacToe('tictactoe1.txt')
>>> game.grid
['OX ', 'OX ', ' X ']
>>> print(game)
O X .
O X .
. X .
>>> game.winner()
'X wins'

>>> game = TicTacToe('tictactoe2.txt')
>>> game.grid
['OOO', ' XX', 'X  ']
>>> print(game)
O O O
. X X
X . .
>>> game.winner()
'O wins'

>>> game = TicTacToe('tictactoe3.txt')
>>> game.grid
['XXO', 'OOX', 'XOX']
>>> print(game)
X X O
O O X
X O X
>>> game.winner()
'draw'