I'm another newb programmer going through Learn Python the Hard Way and encountering something that I don't quite understand.
I preface this by saying that although I really enjoy his lesson plan, one unfortunate side effect seems to be that I don't understand a lot of technical talk related to the programming world. It took me two days to figure out what an “instance” is, and even now I only think that I get it, hehe. Therefore, I hope that the answer to my request has not yet been asked in a more technically descriptive way, if I wish it was superfluous.
Anyway, what I'm trying to do here is a full text adventure, using classes to describe each room. What I'm actually doing is importing my old work into this new product, more than "OOP" - doing something - converting a program that had essentially 980 lines of function calls.
So I used to have these “rooms” inside each function that would check if you met certain game goals to describe the room for you. Example:
def room_one(): if "flashlight" in pack: print "You see an exit to the NORTH and a bear to the EAST." elif "flashlight" not in pack: print "It too dark to see."
A boring room, but a suitable description. Therefore, trying to do this with classes, I seem to hit a wall. I will show you the code that I use to define the runner game and room classes.
class Runner(object): def __init___(self): self.goals = []
To make sure that an array of “targets” was added, I set up a simple target verification function in the parent class of Room, and I'm pretty sure that the game.goals file has been added. But the if-else statement doesn't seem to do anything; no matter what in the array of goals, I always get "alienation" when I return to the room.
In fairness, to put an if-else in the init room, as if it were some kind of shot in the dark, in fact, I was surprised that I did not receive an error message telling me that it was not where it was! But the fact that he doesn't seem to care about the contents of the game.goals array tells me that this is not the best way to do this. How do I implement this?
As a subquery - with such a program, if the game mode is changed enough so that the contents of the room are drastically changed, is it better that I have a whole class for the changed room? Is it more important to maintain a “card” and say that RoomThree (Room) is always RoomThree regardless of what changes have occurred in the game system, or if by the time the game returns to RoomThree the place is broken, plundered, full of vampires and smells of salad, should it to be just completely different? I hope not, because it seems prudent that each room is simply “that room,” and let the contents of things like game.goals and game.pack change the class instance instead of just creating a new class for when they are executed certain conditions.
Anyway, it was a long time. tl; dr - Am I doing this weird?
EDIT AGAIN: Removed the main question code to meet the new code suggested by people here, also added the parent class "Room" to find out if this is a problem. There is no solution yet, but I believe the quality of the code has improved :) whee
FINAL EDIT (I hope): Yeah, the Stone answer was again most helpful. Since I was returning “the same” to my runner, he used an old copy of Office. I needed to create a completely new instance to recognize this change; returning "Office ()" instead of "SAME", I did this. Thanks to everyone for helping to not only solve the problem, but also to make the code more readable, and also to help me change my mindset regarding my if-else verbal statements. Mwah: D