1

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?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Liam Kenneth
  • 982
  • 1
  • 11
  • 21
  • From your code, I don't get what you are saying about `StringBuilder`. Please elaborate. Is there a reason you cannot use a two-dimensional array, or a list which holds instances of `Point`? – K. Berger Dec 15 '16 at 14:14
  • Your vertical and horizontal connectors are swapped, and since you don't have rooms at Y=1, there are no vertical connectors. – Manfred Radlwimmer Dec 15 '16 at 14:15
  • Quick fix for the duplicate Keys issue: Instead of a `SortedList` use a `List>` (Key = X = Item1, Value = Y = Item2). – Manfred Radlwimmer Dec 15 '16 at 14:19
  • Also: Output where? You won't have a lot of fun with text formatting, if you are not using a Console font. – Jens Dec 15 '16 at 14:21
  • Other issues: Slashes are escaped with another slash, not with single-quotes. If you are trying to build the map room by room, you can't just *append* multiple lines at once . – Manfred Radlwimmer Dec 15 '16 at 14:25
  • @jens Don't worry about that. The output is to a webpage within
     tags the Dotnet fiddle outputs to a console so will work there too. @ Manfred Thanks for that just reading up on Tuples now
    – Liam Kenneth Dec 15 '16 at 14:25
  • @ManfredRadlwimmer _ you can't just append multiple lines at once_ I am unsure on the approach to build this grid. Appending lines was my 1st thought, if that's not possible I guess I need to rethink my whole approach – Liam Kenneth Dec 15 '16 at 14:32
  • 1
    Instead of SortedList or List> I'd suggest, just for now to use List (System.Drawing.Point), it gives you predefined X and Y fields – David Dec 15 '16 at 14:37
  • @LiamKenneth Have you considered using a "half-step" Grid instead? Basically a Grid that alternates between Rooms and connectors - [Example](https://i.stack.imgur.com/q20sL.png) – Manfred Radlwimmer Dec 15 '16 at 14:37
  • @ManfredRadlwimmer "half-step" grid looks exactly like what I am trying to do. – Liam Kenneth Dec 15 '16 at 14:40
  • @LiamKenneth [Ugly, non-optimzed, proof-of-concept here](https://dotnetfiddle.net/Widget/p7zhzq) – Manfred Radlwimmer Dec 15 '16 at 14:49

2 Answers2

1

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.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • Thanks, this works very well and gives me a good understanding on how to achieve what I want. You are correct about the custom class I will need one for the things you mentioned as well as denoting when to use a diagonal line to connect rooms I suspect. – Liam Kenneth Dec 15 '16 at 15:56
0

Besides the problems with text-formatting and the console, and also the options Manfred already mentioned, here is an example which uses an array instead of a list. A room is a true-value at any given index, "no room" is represented by false.

Your Rooms()method looks like this then:

public bool[,] Rooms ()
{
    var roomList = new bool[,]{{true,false},{true,true}};
    return roomList;
}

Which makes you need to change IsRoom(int, int)to look like this:

public bool IsRoom(int x, int y)
{
    var rooms = Rooms();

    if(x<rooms.GetLength(0) && y<rooms.GetLength(1))
    {
        return (rooms[x,y]);
    }
    return false;
}

Also there was a line-break missing, which lead to having a vertcal connector on the same line as the next room. I changed it to be

        if (IsRoom(x, y + 1))
        {
            buildMap.Append("\r\n".PadLeft(4) + DrawVerticalConnector().PadRight(4));
            buildMap.Append("\r\n".PadLeft(4) + DrawVerticalConnector().PadRight(4)+ "\r\n".PadLeft(4));
        }
K. Berger
  • 361
  • 1
  • 9