3

Ok so for my C# programming class I am making an adventure game. I think I understand how to do most of it expect I'm having trouble setting up the "world".

I have a class for the world (World.cs) where I started creating a list for each room. However I'm confused of to add a name and description for each room.

For example if the List (room) is type String I would do room.Add("Prison", "This is a prison). What is the best way to do this?

2 Answers 2

7

That's when you create a class

public class Room {
   public string Name { get; set; }
   public string Description { get; set; }
}

And hold a list of rooms:

List<Room> rooms = new List<Room>();

In order to add anything to the list, simply do this:

rooms.Add(new Room { Name = "Prison", Description = "This is a prison" });

Objects are used to group data together, this will allow for much cleaner code. It is one of the keystones of Object-Oriented Programming.

Sign up to request clarification or add additional context in comments.

2 Comments

Ohhhhh I was missing the object part, awesome. So it would be the same thing to create a list for items that would be in the rooms right?
@user3064291: yes. Create a class Item that holds their properties. Some people might advise you to use a struct instead since it's just holding a few fields for now. While correct, I think it would be best if you'd stay with just classes for now. Just as a heads up.
0

The best thing you could do is to create a class indeed.

public class Room
{
    public string Name { get; set; }
    public string Description { get; set; }
}

Then you can create a list of Rooms

List<Room> rooms = new List<Room>();

To add objects

Room room = new Room { Name = "", Description = "" };
rooms.Add(room);

From this point forward I will talk about things that exist, but that are not necessarily the best thing to do.

Extension methods

You can create an extension method such this one

public static class ListRoomExtender
{
    public static void Add(this List<Room> rooms, string name, string description)
    {
        rooms.Add(new Room { Name = name, Description = description });
    }
}

As I've specified, this method will only exist for lists of rooms. So if I have

List<Room> rooms = new List<Room>();

I can use

rooms.Add("Room name", "Room description");

Other types of "lists"

  • StringDictionary
  • Dictionary<string, string>
    • You can't store more information if needed, only a pair of strings.
    • You don't have semantics, you access data through properties Key and Value.
    • You can't add two items with the same key.
  • List<KeyValuePair<string, string>>
    • You can't store more information if needed, only a pair of strings.
    • You don't have semantics, you access data through properties Key and Value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.