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.
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.
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:
An initialization method that takes the location of a text file containing the configuration of a tic-tac-toe game. The method must read the information from the file, store it in the attribute grid of the newly created object. The attribute grid must refer to a list containing three strings, where each string contains the three characters from the corresponding line in the text file.
An implementation of the built-in method __str__ that returns a string representation of the grid of the tic-tac-toe game. This string representation must contain a formatted representation of the grid. The formatted string must contain three lines, representing the rows of the grid. The columns of the grid are separated by a single space in the formatted string. Empty space are represented by a dot (.).
A method winner that returns who won the tic-tac-toe game. The method must return the string X wins if the player marking crosses has won the game, the string O wins if the player marking noughts has won the game, and the string draw if the game has ended in a draw.
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'