Represent the Square Grid of Cells
00:00 Represent the Square Grid of Cells. While some people play variants of tic-tac-toe with a different number of players or different sizes of grids, you’ll stick with the most basic and classic rules.
00:22 Because you represent marks with a single character, you can implement the grid using a string of precisely nine characters corresponding to the cells. A cell can be empty, in which case you’ll fill it with the space character, or it can contain the player’s mark.
00:38 In this course, you’ll store the grid in row-major order by concatenating the rows from top to bottom. For example, with this representation, you could express the three gameplays demonstrated before with a string literal seen on-screen.
01:21 While using strings to represent the grid of cells is straightforward, it falls short in terms of validating its shape and content. Other than that, plain strings can’t provide some extra grid-specific properties that you may be interested in.
However, the benefits of immutable objects, including fault tolerance and improved code readability, far outweigh the cost in modern computers. By default, when you don’t specify any value for the
.cells attribute, it will assume a string of exactly nine spaces to reflect an empty grid.
02:28 However, you can still initialize the grid with the wrong value for cells, ultimately crashing the program. You can prevent this by allowing your objects only to exist if they’re in a valid state.
Otherwise, they won’t be created at all, following the fail-fast and always-valid domain model principles. Data classes take control of object initialization, but they also let you run a post-initialization hook to set derived properties based on the values of other fields, for example. You’ll take advantage of this mechanism to perform cell validation and potentially discard invalid strings before instantiating a
.__post_init__() method uses a regular expression to check whether the given value of the
.cells attribute is exactly nine characters long and contains only the expected characters:
You’ll implement the validation of a particular cell combination’s semantics elsewhere once you gain additional context. At this point, you can add a few extra properties to your
Grid class, which will become handy when determining the state of the game.
and empty cells, respectively. Because your data class is immutable, its state will never change, so you can cache the computed property values with the help of the
@cached_property decorator from the
This will ensure that their code will run at most once no matter how many times you access the properties, for example during validation. On-screen, you can see the
Grid class in action showing some of its features.
05:55 using Python code. You’ve modeled a three-by-three grid of cells, which can contain a particular combination of player’s marks. Now it’s time to model the player’s moves so that artificial intelligence can evaluate and choose the best option, and you’ll be doing that in the next video.
Become a Member to join the conversation.