Ex47 Study Drills

So I’ve run through all the tasks in ex47 and understand (passively, at least) all the code given and how everything is working. I’m able to set up my own script with variables, functions, classes, and modules that do simple things (arithmetic, print strings with variables, etc.) and make an automated test that successfully tests them with pytest. I think that I’m successfully doing parameterization–testing the functions, modules, and classes with different inputs and checking their outputs, including raising specific errors. For example:

# mymod.py

a = "Balalalala"
b = "Wholalalala"

def modfunc(arg):
    print(f"The arg is {arg}")
    return f"The arg is {arg}"

class Modfunc(object):

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

    def funcfun(self):
        print(f"This works: {self.arg}")
        return f"This works: {self.arg}"

def mathfunc(arg):
    answer = int(arg) + 2
    print(answer)
    return answer

tested by

#mymod_test.py

import pytest
from mymod.mymod import *

def test_modfunc():
    assert modfunc("Hi") == "The arg is Hi"

def test_Modfunc():
    banana = Modfunc("banana")
    assert banana.arg == "banana"
    assert banana.funcfun() == "This works: banana"

def test_str():
    g = Modfunc(a)
    assert g.funcfun() == "This works: Balalalala"
    h = Modfunc(b)
    assert h.funcfun() == "This works: Wholalalala"

def test_math():
    assert mathfunc(3) == 5

def test_math_bad():
    with pytest.raises(ValueError):
        mathfunc("apple")

However, I’m getting completely stuck when I come to the third study drill for ex47: Make your room more advanced, and then use it to rebuild your game yet again, but this time unit test as you go.

The first part, make your room more advanced, wasn’t too much of an issue for me. I did that and started to build another class to manage objects:


class Room(object):
    """ Takes the name of the room as a string and a description of the room, stores the directions
    the player can take out of the room, and provides a function to move in the direction the
    player wants.
    """
    def __init__(self, name, description, object):
        # Takes the name and description of the room, stores the paths the player can take in a dict
        self.name = name
        self.description = description
        self.object = object
        self.paths = {}
        print(f"Welcome to {self.name}. {self.description}.")

    def go(self, direction):
        # Pops keys into the self.paths dict to get the variable out and returns the variable
        return self.paths.get(direction, None)

    def add_paths(self, paths):
        # Updates the paths dict with strings as keys and variables as values
        self.paths.update(paths)

class Things(object):
    """Anything you find and carry around with you."""

    bag = []

    def __init__(self, object, function):
        self.object = object
        self.function = function
        if self.object not in Things.bag:
            Things.bag.append(self.object)

    def remove_object(self):
        Things.bag.remove(self.object)
        return Things.bag

…but the second part, use it to rebuild your game yet again, but this time unit test as you go, is getting me. I’m assuming this is the game from ex43. The game I built didn’t use a geographical space-based map, but rather a task map (the dict keys/values were manila folders/tasks), so it was hard for me to conceptualize how to use the Room class in it. Instead, I tried putting the Room class into the Gothon game, but as soon as I open the gothon code I’m just… stuck.

I understand that the Room class, once properly implemented, would allow the scenes to function like those in Zork, where I can go back and forth through a sort of 2D map (instead of linearly forward as they are written in ex43), but I don’t understand the relationship between the Room class, the Engine class, and the Map class. I can’t conceptualize how to integrate the Room class into the code, I can’t figure out whether I would make each room a class that inherits from Room or just functions that use instances of Room, I can’t figure out how I would start the game once it’s integrated, and I certainly have no idea how I would test it.

I feel like this is just an issue of scaling up and integrating previously studied concepts, but it seems to me the magnitude of scaling is just a bit too much. I searched around for other people posting their code from ex47, and nobody seems to have messed with the ex43 game (I just found a lot of examples of people adding another class to the ex47.py and testing it). Am I misunderstanding the study drill? Am I missing something simple? Or do I just need to spend more time staring at the code until I sort it out? Any kind of nudge in the right direction, or little bit of info to bridge me to where I should be (some sample code?), would be appreciated.