Public Group

# A query on how to design a bit of class based code for a text adventure game.

This topic is 2865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been learning C# over the summer and now feel like making a small project out of what I've done so far. I've decided on a sort of text based adventure game.

The basic structure of the game will involve having a number of sectors(or rooms). Upon entry into a room, a description will be outputted and a number of actions and such you may take; the ability to examine, pick up, use stuff in that room; possibly a battle system, etc etc. A sector may be connected up to 4 other sectors.

Anyway, scribbling ideas on paper on how to design the code for this, I'm scratching my head over the structure of part of my code.

I've decided on a player class, and a 'level' class that represents a level/dungeon/area. This level class would consist of a number of interconnected 'sectors'. At any given time, the player would be present in one certain sector in the level.

So here's the confusion:

Logically, one would expect a method such as
 player.Move(Dir d)

Such a method should change the "current sector" field in the level object. This means class Player would need to know about class Level. Hmmm.
And Level may have to manipulate the Player object (eg. player enters room, ambushed by something, loses something from inventory.) So now Level also needs to hold a reference to the Player object?

This doesn't feel nice, having everything hold a reference to everything else.

At this point I remembered reading about delegates from the book I'm using. Though I know about function pointers from C++, the chapter on delegates was presented with examples with a sort of 'event based' programming viewpoint, with which I did not have much enlightenment about.

That gave me the idea to design the classes as such:

Player:
class Player{    //...    public delegate void Movement(Dir d);   //enum Dir{NORTH, ...}    public event Movement PlayerMoved;    public void Move(Dir d)    {                PlayerMoved(d);        //Other code...    }}

Level:
class Level{    private Sector currSector;    private Player p;    //etc etc...    private void OnMove(Dir d)    {        switch (d)        {            case Dir.NORTH:                //change currSector                //other code                break;                //other cases        }    }    public Level(Player p)    {        p.PlayerMoved += OnMove;  //this is 'plus equals'. Donno why it's not showing in preview.        currSector = START_SECTOR;        //other code    }    //etc...}

So firstly, is this an alright way to do this?
Secondly, if the delegate chapter was not presented the way it was, I would not have thought of using such 'events'. So what would be a good way to implement this without using callbacks?

I have a habit of making highly detailed posts... sorry v__v

[Edited by - ManiacalAce on July 16, 2010 1:54:07 PM]

##### Share on other sites
Well, in case anyone else is pondering along the lines of my first post, or is just curious about some viewpoints regarding it, I later asked the question on StackOverflow as well. (That place is damn fast!)

• 10
• 18
• 14
• 18
• 15