Click here to Skip to main content
16,011,608 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I am experimenting with my knowledge of programming in C#, and I am likewise trying to develop a simple, text-base game that lets a user navigate through a series of text-described rooms with directional buttons. First, I have three Classes - a setup/control class (the main form), a Player class and a Room Class.

For my player class, I thought I'd like to have a list of rooms the player is surrounded by, including the room the player is currently in. This would limit the "view" of the player to just the cardinal directions around him/her.

public class Player
    {
        private Int32 _playerCurrentRoom;
        ArrayList rooms = new ArrayList();
        public Player(Int32 playerCurrentRoom)
        {
            _playerCurrentRoom = playerCurrentRoom;
        }
        public Int32 MovePlayer(String direction, Room currentRoom)
        {
            Int32 heading;
            switch (direction)
            {
                case "North":
                    heading = currentRoom.ConnectNorth;
                case "East":
                    heading = currentRoom.ConnectEast;
                    break;
                case "South":
                    heading = currentRoom.ConnectSouth;
                    break;
                case "West":
                    heading = currentRoom.ConnectWest;
                    break;
                default:
                    heading = currentRoom.RoomNumber;
                    break;
            }
            return heading;
        }
    }


For my Room class, I indicate the room's number and indicate the rooms around it with an integer. The Room's field "_roomNumber" and the Player's field "_playerCurrentRoom" should also be the same, such that the textBox of the form is updated with the description of the room each time a player moves into it. Also, note that the MovePlayer method takes a String direction which is handed down from the button that the user clicks on the form.

public class Room : IPrintable
    {
        // assignents for what rooms surround room you are currently in
        private Int32 _connectNorth;
        private Int32 _connectEast;
        private Int32 _connectSouth;
        private Int32 _connectWest;
        private Int32 _roomNumber;
        private String _description;
        private String _roomItems;
        private String _roomActors;
        ArrayList itemsInRoom = new ArrayList();
        // Initalize a Room with essential values
        public Room(Int32 connectNorth, Int32 connectEast, Int32 connectSouth, Int32 connectWest, Int32 roomNumber, String description)
        {
            _connectNorth = connectNorth;
            _connectEast = connectEast;
            _connectSouth = connectSouth;
            _connectWest = connectWest;
            _roomNumber = roomNumber;
            _description = description;
        }
        // properties to be used for editor
        public Int32 ConnectNorth   { get; set; }
        public Int32 ConnectEast    { get; set; }
        public Int32 ConnectSouth   { get; set; }
        public Int32 ConnectWest    { get; set; }
        public Int32 RoomNumber     { get; set; }
        public String Description   { get; set; }
        public ArrayList ItemsInRoom { get; set; }

}

Where my problem lies is how to link the two together such that I can populate the Player's ArrayList (or generic List) with Room objects and iterate over the list to maneuver the player. Basically, I don't know how to achieve this. So I am looking for some input. And it may be that my classes are built wrong as well. Whatever the case, let me know what your thoughts are.

As always, I appreciate the help.
Posted
Updated 25-May-11 9:20am
v2

You are struggling with design issues here. Unless someone knows the game you are creating, cannot really provide a good solution. Please update your question to tell us what exactly are player and rooms.

Now, let's see the code you already have:

1. There is no need of MoverPlayer method. When the user clicks on the direction buttons, you can directly set the value for heading rather than sending a string and then doing switch case.
2. You are using ArrayList. Please stop. We are not in that era anymore. Use Collection or List (generic).
3. In the Room class, there is no connection between the private members and there public properties. You do not need private members since we have automatic properties now.
4. Do you really need them to be integers? Could an enumeration help?
5. Again, ArrayList!
6. Why IPrintable? And what is it by the way? Is the devexpress one? Why do you need it?

You should really stop writing code for now and rethink about the design. If you continue like this, you may end up nowhere. Spend some time, may be away from this code/application and start fresh. It helps sometimes. :)
 
Share this answer
 
Comments
Isaiah83 25-May-11 16:14pm    
Thanks for the thoughts. I need to go back and review each of your assessments here before posting again. I am having a major design issue, where perhaps less is more (and usaually is). FYI, IPrintable is an interface that holds properties for objects that are added to an array, whose backing stores I want to expose when I iterate through them.
dan!sh 25-May-11 16:21pm    
I think this should tell you the importance of naming variables, classes etc. I would re-state: rethink the design before you get in to place from where there is no comeback. :)
Isaiah83 25-May-11 17:28pm    
Thanks to your #2 & #5, I am learning now about generics. Ive read about them in the past, but I still couldn't see why they are better. I see performance is one argument for them.
This is much simpler than you're making it. Note I typed this code right in this code block so beware of stupid typos.

First off, you're not using types, but instead the classes.
Int32
is the class reference for the type of
int
. The same for
String
and
string
.


If your room class is simplified down to:

public class Room : IPrintable
{
    public int RoomID { get; set; }
    public string RoomDescription {get;set;}
    public List<room> ConnectedRooms {get;set;}

    //This should be used to get the description for a specific room.
    private string GetDescriptionById(int roomID)
    {
         return "A big scary room!";
    }
    //Used to get the connected Roomclasses (and their subsequent connected rooms)
    private List<room> GetConnectedRooms(int roomID)
    {
        return someDatabaseObject.GetRoomsList();
    }

    public Room()
    {
        //Empty constructor logic.  We need to initialize the List no matter what.
        //to avoid object not set to instance of object Exceptions.
        ConnectedRooms = new List<room>();
    }
    public Room(int _id)
    {
        //Let's construct a room by ID.
        ID = _id;
        Description = GetDescriptionById(_id);
        ConnectedRooms = GetConnectedRoomsById(_id);
    }
}
</room></room></room>


Now, because we have that empty constructor, when getting the RoomList you can do some nice magic that has the EMPTY connected room list. That way you don't have to store a huge LinkedLIst type construction.



EDIT: Also notice how (with the anonymous getter and setters) I don't need the private variables explicitly. That's handy.

By attaching the ConnectedRooms to your room class directly, you can specify the List of Connected Rooms directly from any instance of Room.

var r = new Room();
r.ConnectedRooms(); //Will be empty.  This is an orphan room only GMs can get into.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-May-11 15:59pm    
Surely this is much simpler. My 5.
For another aspect of it, please take a look at the previous OP's question and my answer.

Unfortunately, lack of adequate OP's response (and a re-post) caused me to add my answer to the question on this page, pretty bitter one.
--SA
Isaiah83 25-May-11 16:17pm    
Thanks, J for the insight. I am going to take what you offered here and work on my design again before re-commenting again. I am still getting used to the mechanics of this particular forum, and I am new at programming. I haven't quite gotten around to considering pattern's yet, so for those (SA) whose requisites for a kind/helpful post are knowledge of "patterns / anti-patterns" and constant attention to posts, I apologize for not meeting them...but seriously, SA, ease up.
Jpuckett 25-May-11 16:52pm    
Don't sweat the small stuff.

As for working on your design. Do that. Then do it again. The beauty of programming is finding the simplest solution to the most complex of problems. I quickly realized you were new because you've been playing with ArrayLists and were incorrectly using types. You're self-taught and showing initiative. That's a good sign.

In this line of work you'll meet many types of folks, and you have to know how to take constructive criticism because there's ALWAYS a guy out there who knows more than you. SA was mostly right in his comments, but the internet does make it seem really rude.

If you want to continue working on this, I encourage it. Just post a message over on my personal profile and I'll help you out as I get time. EDIT: heck. We might even make it open source for noobies on what to do and lessons learned. :)
Isaiah83 25-May-11 17:26pm    
Hey, its great to meet someone that's genuine in their approach and care of questions like mine. A big thanks. I will be working on this project until I learn everything I can about what it is I ultimately want to do; this includes using sockets, and some design patterns I am learning. Since I am new to this forum and how it works, will you instruct me where to post a message at in your profile? I still don't see a place to do it.

In response to your thought on making it open source, I will add that this project is indeed a way for me to teach programming to myself. I continue to learn from creating mini experiments around it to test why things are breaking and to test new ideas.
OK, you completely ignored my solution I've given you to your past question. Please see. You did not ask me any follow-up questions. You re-posted the question, not 100% the same, but this is essentially a re-post.

You still using this switch statement based on immediate string constants (hard-coded). You repeat code. This is not how programming works in principle. This is not fixable.

You have only two choices: change your mind and start learning or leave the industry now.

[EDIT]
I expected the down-votes on this answer. It this is a vote by OP (isn't it? you may not answer if you don't want), it would only makes me even more sure about my assessment. I can smell such cases.

At the same time I would be more happy to know that I was wrong. But I can only see this if OP changes this attitude and starts respond to criticism constructively and actually start learning something. It it is not happening, leaving the industry would be the only viable solution. Either one way or another, no more options.

Sorry,
—SA
 
Share this answer
 
v2
Comments
Isaiah83 25-May-11 16:09pm    
Funny how you are so helpful one minute and just as easily could send someone straight to hell the next. Had you not a thick skull, you would have drilled down to some deeper intuition and realized I hadn't read your response to my question, by which I had already accepted another solution to, and thought not to look back at it. Thanks for the warm invite to the forum. I am just staring out as a programmer. Next time, be less of a dick.
Sergey Alexandrovich Kryukov 25-May-11 16:59pm    
I would be so happy to know that I'm wrong! I'll take into account your advice. But note, I've never being so rude as you are. (Short personal advice: never use such words to people. Could you find one word like that I've ever written?)

This is not about variant of solutions. Yes, they can be different. You need to get understanding. Are you protecting what are you doing? Why are you so stubborn. If you don't want to listen to an advice, don't. Who is the looser anyway.

If I may: you should not using the fact that you're a beginner as an excuse. You need learning, not excuses. It you're a beginner, do begin. If you don't trust my technical expertize, it's understandable. I gave you my opinion, use your own judgment now. I knew so many people at your position, so I know that you're at risk right now. You need to select a right road in terms of your attitude. Only hard work and constructive response to criticism will work for you.

Now, if you try to apply logic to what you observe, you should understand that my withes for you are most positive, personally. Think by yourself, why would I spend so disproportionally long time with you otherwise. This is because I really feel you need serious help. You decide if you accept it.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900