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 marks 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. This is done in the following way:

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.

>>> readGrid('tictactoe1.txt')
['OX ', 'OX ', ' X ']
>>> readGrid('tictactoe2.txt')
['OOO', ' XX', 'X  ']
>>> readGrid('tictactoe3.txt')
['XXO', 'OOX', 'XOX']

>>> print(showGrid(['OX ', 'OX ', ' X ']))
O X .
O X .
. X .
>>> showGrid(['OX ', 'OX ', ' X '])
'O X .\nO X .\n. X .'
>>> print(showGrid(['XXO', 'OOX', 'XOX']))
X X O
O O X
X O X

>>> winner(['OX ', 'OX ', ' X '])
'X wins'
>>> winner(['OOO', ' XX', 'X  '])
'O wins'
>>> winner(['XXO', 'OOX', 'XOX'])
'draw'