Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Model the Square

00:00 Model the Square.

00:04 The purpose of a square is to convey information about a particular location in the maze. Therefore, every square should have known coordinates that can determine its position.

00:14 The square should also have a border pattern that describes the maze structure at that location. Depending on the purpose of the square, it may option optionally play a special role—for example, indicating the maze entrance.

00:27 You’ve already done the hard work by offloading most of these responsibilities to helper classes. The only remaining task is to combine them into a final square object.

00:39 Open the square module and enter this code.

00:55 You are using Python’s data class to generate the mundane code for you, which will correctly initialize instances of this class, amongst a few other things.

01:05 Enabling the frozen parameter ensures that square objects become immutable after you create them. There’s no point in changing the values of the instance variables once they’re set.

01:16 Preferring immutable objects over mutable ones where possible is considered a good programming practice, which can prevent subtle bugs caused by unexpected changes in data.

01:26 Note that in addition to storing the square’s row and column indices, you also keep track of its one-dimensional index within a flat sequence of squares. This way, the search algorithm can uniquely identify the squares.

01:43 While you could theoretically infer the index from the row and column, you’d also need to know the width and height of the maze, which are none of the square’s business.

01:52 A little redundancy can sometimes keep data encapsulated. When creating your Square instance, you must specify its index, row, column, and border pattern, but the role is completely optional. If you skip the role in the initializer, then the square will assume Role.NONE by default.

02:13 And that’s it! You successfully modeled the Square data type, so all the building blocks are now in place to build the maze, and that’s what you’ll be doing in the next section of the course.

Become a Member to join the conversation.