[Tutor] designing POOP

Kent Johnson kent37 at tds.net
Sat Feb 9 17:43:53 CET 2008


bhaaluu wrote:

> When you're designing a program, how do you go about dealing
> with text descriptions, like the descriptions for a room? Here is an
> example of a room description:
> 
> THIS IS THE AUDIENCE CHAMBER
> THERE IS A WINDOW TO THE WEST. BY LOOKING TO THE RIGHT
> THROUGH IT YOU CAN SEE THE ENTRANCE TO THE CASTLE.
> DOORS LEAVE THIS ROOM TO THE NORTH, EAST AND SOUTH.

That would probably be an attribute of the Room class.

> The player reads the description, and presses "W". There is no west door.
> The program's output is:
> 
> YOU CANNOT MOVE THROUGH SOLID STONE
> 
> and back to the main prompt. Pressing "S" moves the player through
> the South door into another room, where the status of the player is
> displayed, and the description of the new room.

It might make sense for Room to have a move(direction) method that 
returns the resulting Room. Then room.move('W') would print the above 
message and return self. room.move('S') would return the room to the south.

This requires that the rooms be linked together - each room knows the 
rooms that are adjacent to it.

Another reasonable design is to have some kind of a Map object that 
holds the linkages between all the rooms, and a method move(from, 
direction) that takes both a Room and a direction.

I don't know which of these would work better for your game.

> The data.py file, with the room descriptions is the biggest file in the
> procedural program. I've been told that a data class with a lot of random
> data in it is a CodeSmell. Is a data class that has room descriptions in
> it, with a function for each room, considered a bad design?

A data file is fine.
> 
> And, since the setup table and the function that decides which room
> description is closely related, couldn't they also be a part of that data
> class? Is a data class a CodeSmell when all the functions are related?

I'm not sure I follow you. Are you talking about Room as a data class or 
some other class? What functions do you mean? A class with data and 
related functions is not a data class, it is good design.

> Or, should I have a room class that can instantiate 19 room objects
> which simply accesses the room functions from the data.py file as
> it is?

Maybe you could give a short example of what you are doing.

Kent


More information about the Tutor mailing list