TTTBoard: A Tic-Tac-Toe Game State Representation

The TTTBoard class is a crucial component in representing the state of a tic-tac-toe game. It is designed to track the position of pieces on the board and determine whose turn it is to play. This class is structured to be immutable, meaning that it generates a new board state for each move, rather than modifying the existing state. This immutability is particularly beneficial for search algorithms, as it prevents inadvertent changes to board positions that are still under analysis.

Class Definition and Initialization

The TTTBoard class inherits from a base class Board and is initialized with two main pieces of state: the position of the pieces on the board and the player's turn. The position is represented by a one-dimensional list of TTTPiece objects, and the turn is tracked by a TTTPiece indicating the current player.

[Listing 8.3: Initialization of TTTBoard](https://livebook.manning.com/book/classic-computer-science-problems-in-python/chapter-8/22)

class TTTBoard(Board):
    def __init__(self, position: List[TTTPiece] = [TTTPiece.E] * 9, turn:
     TTTPiece = TTTPiece.X) -> None:
        self.position: List[TTTPiece] = position
        self._turn: TTTPiece = turn

    @property
    def turn(self) -> Piece:
        return self._turn

In this listing, the constructor for TTTBoard initializes a default board with no moves made (an empty board) and sets X as the player to move first, which is the standard in tic-tac-toe. The _turn instance variable and the turn property ensure that all subclasses of Board will keep track of whose turn it is, even though Python does not have a direct mechanism to enforce instance variables in abstract base classes.

Immutability and Move Generation

The TTTBoard class is designed to be informally immutable. This means that instead of modifying the board directly, a new TTTBoard is created with the updated position whenever a move is made. This approach is advantageous for algorithms that explore different game states, as it avoids unintended modifications to the board state.

[Listing 8.4: Making a Move](https://livebook.manning.com/book/classic-computer-science-problems-in-python/chapter-8/22)

def move(self, location: Move) -> Board:
    temp_position: List[TTTPiece] = self.position.copy()
    temp_position[location] = self._turn
    return TTTBoard(temp_position, self._turn.opposite)

In this listing, the move method demonstrates how a new board state is generated. A temporary copy of the current position is created, the move is applied, and a new TTTBoard is returned with the updated position and the turn switched to the opposite player. This method ensures that each move results in a new, distinct board state.

Legal Moves

A legal move in tic-tac-toe is any move to an empty square. The TTTBoard class includes a property, legal_moves, which uses a list comprehension to generate all potential moves for a given board position. This feature is essential for determining the available actions a player can take at any point in the game.