Thread: Why OOP?
View Single Post
Old Oct 5th, 2007, 12:51 PM   #19
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
It's my belief that you're thinking a little too directly, nisim777. You've created some code to build a specific adventure game, when what you really want to do is to take a step back and consider the problem more generally.

You've divided each room into a specific function, but since each room will have many common attributes, I think you'll find that as your program grows more complex, you'll find yourself repeating yourself more and more.

Instead of beginning by building a specific room like a kitchen or a foyer, start by considering the behaviour of a generic room.

python Syntax (Toggle Plain Text)
  1. class Room:
  2. def __init__(self, description):
  3. self.description = description
  4. self.exits = {}
A class is like a template. We can use this template to create more specific objects:

python Syntax (Toggle Plain Text)
  1. foyer = Room("You are in the foyer of the house...")
  2. kitchen = Room("You step into a dark kitchen...")
  3.  
  4. foyer.exits["west"] = kitchen
  5. kitchen.exits["east"] = foyer
In order to interact with these objects, we need a player:

python Syntax (Toggle Plain Text)
  1. class Player:
  2. def __init__(self, room):
  3. self.room = room
  4.  
  5. def look(self):
  6. print self.room.description
  7.  
  8. def move(self, direction):
  9. self.room = self.room.exits[direction]
  10.  
  11. me = Player(foyer)
Now we have two classes (Room and Player), and three objects (foyer, kitchen and me). The neatest thing about this arrangement is that we can actually play the game via Python's interactive interpreter:

>>> me.look()
You are in the foyer of the house...
>>> me.move("west")
>>> me.look()
You step into a dark kitchen...
>>> me.move("east")
>>> me.look()
You are in the foyer of the house...
Okay, so eventually we'd need to create our own command handler (so we could type "look" rather than "me.look()"), but you can see that the functionality is already there. In fact, we can play and debug the game at the same time:
>>> me.move("up")

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    me.move("up")
  File "C:/Python25/t.py", line 20, in move
    self.room = self.room.exits[direction]
KeyError: 'up'
Oops! Looks like we could use some better error handling in the "move" function:
python Syntax (Toggle Plain Text)
  1. def move(self, direction):
  2. if direction in self.room.exits:
  3. self.room = self.room.exits[direction]
  4. else:
  5. print "You cannot go in that direction!"
Now let's try the interactive interpreter:
>>> me.move("up")
You cannot go in that direction!
Much better! Essentially, try representing physical things, such as rooms and items, as Python objects, and try representing commands, such as "look" and "move", as methods.
Arevos is offline   Reply With Quote