Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learn Python the Hard Way Exercise 43

Tags:

python

I do not understand how the script gets the next room, and generally how the "Engine" and "Map" classes work. Here's an excerpt:

Class Map(object):

    scenes = {
        'central_corridor': CentralCorridor(),
        'laser_weapon_armory': LaserWeaponArmory(),
        'the_bridge': TheBridge(),
        'escape_pod': EscapePod(),
        'death': Death()
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        return Map.scenes.get(scene_name)

    def opening_scene(self):
        return self.next_scene(self.start_scene)

class Engine(object):

    def __init__(self, scene_map):
        self.scene_map = scene_map

    def play(self):
        current_scene = self.scene_map.opening_scene()

        while True:
            print "\n--------"
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

I simply don't understand how these portions work. I know how classes and object instances and attributes and all that other OOP stuff works, but for some reason this portion of the code I don't get. Mainly the Map class. If someone could explain it it would be awesome.

Also (this may require reading the exercise), why is it required to have these two classes anyway? Couldn't you just do it with class methods instead (i.e. methods without self as a param.)? Then you could just call, for example, CentralCorridor.enter(). In fact, that is how I solved it before reading the answer, and it worked out fine.

Sorry, my main question is how the Engine and Map classes work. The other thing is secondary.

Thanks in advance!

like image 307
Aristides Avatar asked May 17 '26 02:05

Aristides


1 Answers

The Map object is a Class which maps your scenes. It has some scenes saved in an array.

scenes = {
    'central_corridor': CentralCorridor(),
    'laser_weapon_armory': LaserWeaponArmory(),
    'the_bridge': TheBridge(),
    'escape_pod': EscapePod(),
    'death': Death()
}

When an object of Map is made you also give it an opening scene as seen in the constructor

def __init__(self, start_scene):
    self.start_scene = start_scene

This creates a variable in Map called start_scene containing your opening scene.

Furthermore Map has 2 methods

# This one returns a scene based on its name or key in the scenes array
def next_scene(self, scene_name):
    return Map.scenes.get(scene_name)


# And this one  returns the opening scene which is set when you create the map.
def opening_scene(self):
    return self.next_scene(self.start_scene)

The Engine seems to be controlling the scenes when to play and what to play.

# When creating an Engine object you give the map containing scenes to its constructor
def __init__(self, scene_map):
        self.scene_map = scene_map

# The method which starts playing the scenes
def play(self):

    # the opening scene from the map is selected as the current scene
    current_scene = self.scene_map.opening_scene()

     # You loop all the scenes probably, conditions of this loop are unknown because you haven't posted it entirely.
     while True:
         print "\n--------"
         # It seems the next scene name is known in the current scene
         next_scene_name = current_scene.enter()

         # It replaces current scene with the next scene from the map
         current_scene = self.scene_map.next_scene(next_scene_name)

why is it required to have these two classes anyway?

It isn't, unless it's required according to your assignment

Like you said it's possible to do it without, BUT there are good reasons to do so.

This way you make 2 separate classes with their own responsibilities. This way code is more readable when the application gets bigger and bigger. And it's easy to navigate through the application. You can easily change parts of your app etc etc. My advice is to keep practicing and reading about OOP, you will notice why you do the things you see.

like image 196
Timmetje Avatar answered May 19 '26 15:05

Timmetje



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!