In this video, learn about the software classic "Design Patterns." Then, learn how to create the maze labyrinth game from the "Design Patterns" book in Python using OOP. To begin, you won't use any patterns, so you can see and understand the value of using them.
- [Instructor] Hello, and Welcome to Python projects. In this video, we will be exploring design patterns, using Python. We will create the Maze Labyrinth game. The design of the maze game looks like this. We have an abstract class called MapSite, which defines an abstract from the inherited method called Enter.
We have three subclasses, Room, Wall, and Door. Each of them are inheriting the Enter method from the abstract base class. So Room has the Enter method via inheritance, so does Wall, and so does Door. Because the Enter method of the MapSite abstract class is abstract, virtual, it needs to be overwritten in every of the subclasses.
So Room overwrites Enter, so does Wall, so does Door, and according to the book of design patterns, what happens when you enter a room or wall or a door is defined within these three subclasses which implement the abstract base class. In this design, by definition, a room consists of sides and exactly of four sides.
Each side inherits and implements the MapSite abstract class. So what this means is a room can have a wall, or four walls, and it can have a door, or four doors, and that makes up a room. A game of Maze is made up of rooms. So a Maze can add a room, and it can retrieve a room by its room number, which in a user bot will return the room number private property.
The game of Maze consists of several rooms. Now there are different ways to create those rooms, and this is where design patterns come in. The first thing we do is to create the abstract class MapSite. In Python, we will use the keyword abstract, just declare the class, and we declare our method within in this class. And here we say raise NotImplementedError because it's an abstract class and a virtual method.
So now when we go down here to our main self testing section we can create an instance of this abstract class, assign to a variable, and call the Enter method on that instance and run it. So when we run it, guess what, it blows up. Yeah, it says raise NotImplementedError, abstract base class method needs to be implemented. The next step is to create an ennumeration of directions.
In order to do so in Python, we import Enum from the Enum module. This allows us to create names, like North, East, South, and West. And underneath the hood, those relate to numbers. So North is zero, East is one, and so on. Next, we create some classes that inherit from MapSite. We create the Room class, the Wall class, and the door class.
Each of them inherit and override the Enter method. With those classes in place, we can create a Maze, and we can create a Maze game. We can simply create a Maze by instantiating a Maze, instantiating a Room, giving it a Room number, one, instantiating another Room, two, and then a Door, a Door requires a Room on each side of the Door, and then we can add those two Rooms to the Maze, and next we're going to set the four sides of each of those two Rooms.
So we create a Wall, and a Door, and some more Walls, and some more Doors. Now one thing that is interesting, which might not be obvious from the beginning, is that even though the Room consists of Doors and Walls, the Door knows the Rooms it's connected to. So when we create a Door, we pass in Room One, Room Two, we save this in private members here, and later we can figure out, by calling this Door method, which Room is on the other side of our Door.
Now we are ready to create a game of Maze. We do so by first creating an instance of the Maze game and then immediately, we call the CreateMaze method on it and store it in the Maze object. After that, we create a list that will store the Rooms the Maze has. We just choose five as a number, so we are trying to figure out if our Maze consists of five Rooms.
So we try it to get the Room number by calling the RoomNo method on the Maze object. And if this doesn't work, we simply fall into the exception handler and say there's no Room of this number. If that Room number does exists, we'll fall into the access Room handler, so we are trying to enter this Room. And we got to append the Room to our list of Maze Rooms. Next, we are interested in the existing Room having four sides.
So we retrieve the sides, get rid of a little bit of syntactic sugar here, and then what we are really interested in, for as we try to enter the side, remember a side can be a Door or a Wall, and if it's a Door, then we figure out if the Door's open or if it's not open, we got to open the Door, and then we enter it. And then we find out what's on the other side of the Door, which Room is on the other side of the Door.
Now we can run our Maze game. We check if there are five Rooms available. The first Room will be called zero, but there's no Room with the number zero, next within the loop, we check for Room number one, and yes, our Maze has a Room number one. We enter it, and we successfully entered it. Now each Room has four sides. So we are looking for the sides, North, East, South, West.
On the North, there's a Wall, we try to enter the Wall, and we bang our head, we just ran into a Wall. On the East direction of Room One, there is a Door, so we are trying to enter it. But it's not open yet. So we can't get through it. So we'll open it, and then it tells us, yes, we have opened the Door and passed through it, and the Door object is this one here.
That object will change whenever you run it. And it tells us that this door is a side of Room One. And on the other side, which is a method of the Door class, Door instance knows which rooms it is connected to. On the other side of this Room is Room Two. Then within the loop we bang our head some more. And we continue the loop to find more Rooms. So in Room Two, we have the same thing.
We find a Door here, and what's interesting is that the Door object, which in my case you happen to have eight, nine, is the same as this one, which really proves that the Door is connecting our two Rooms. So there's no Room number three. There's no Room number four. So we summarize that there are two Rooms in our Maze and that the two Rooms have a Door, which is the same object.
The Door is the same object in those two Rooms, and they are combined or connected by East and West sides.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Running Python programs within Eclipse
- Setting breakpoints
- Using the PyDev debugger
- Using design patterns
- Creating a GUI application with Tkinter
- Writing a Windows scheduling service