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.
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. This is done in the following way:
Write a function readGrid that takes the location of a text file containing the configuration of a tic-tac-toe game as its argument. The function must read the information from the file, and return a list containing three strings. Each of these strings contains the three characters from the corresponding line in the text file.
Write a function showGrid that takes the configuration of the grid after having played a tic-tac-toe game. This configuration is represented as a list taking the form of the lists returned by the function readGrid. The function showGrid must return a string containing a formatted representation of the given configuration. 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 (.).
Write a function winner that takes the configuration of the grid after having played a tic-tac-toe game. This configuration is represented as a list taking the form of the lists returned by the function readGrid. The function 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.
>>> 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'