Search code examples
c#mud

How to create a grid based map?


I'm trying to make a map like this in C#:

   0     1    
0 [ ]---[ ]   
   |     |    
   |     |    
1 [ ]---[ ]

Simple grid with rooms at (0,0), (1,0), (0,1) and (1,1)

I have tried doing this and have an example here https://dotnetfiddle.net/3qzBhy

But my output is: [ ]|||| [ ]

I don't get why and not sure if calling .ToString() on a StringBuilder makes it lose its formatting such as new lines.

I also had trouble finding a way to store coordinates

Public SortedList<int, int> Rooms ()
    {
        var roomList = new SortedList<int, int>(); 

        roomList.Add(0,0);
        roomList.Add(1,0);
        //roomList.Add(0,1);
        //roomList.Add(1,1);

        return roomList;
    }

roomList.Add(0,1) and roomList.Add(1,1) are duplicates because the keys 0 and 1 are already used. How can I store a list of coordinates?


Solution

  • Instead of spreading my opinions via comments I'll just dump em all here as an answer:

    I also had trouble finding a way to store coordinates

    SortedLists, Dictionaries, etc. won't work. It would be best to just use a regular List filled with Tuples, Points or a class of your own until you find a better solution.

    Since those rooms maybe won't stay empty you could write your own classes, e.g.:

    class Tile
    {
        public int X { get; set; }
        public int Y { get; set; }
    
        public virtual void Draw(StringBuilder map)
        {
            map.Append("   ");
        }
    }
    
    class Room : Tile
    { 
        public int EnemyType { get; set; }
        public int Reward { get; set; }
    
        public override void Draw(StringBuilder map)
        {
            map.Append("[ ]");
        }
    }
    
    // etc.
    

    I don't get why and not sure if calling .ToString() on a StringBuilder makes it lose its formatting such as new lines.

    It doesn't. You didn't have any newlines in your example because all rooms are at Y = 0

    The current attempt won't work if you draw multiple lines at once and string them together. Instead you could use something like a "half-step" grid.

    enter image description here

    You can find a small (ugly, non-optimzed, makeshift) example here as a fiddle.