1

C# で次のようなマップを作成しようとしています。

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

(0,0)(1,0)(0,1)およびに部屋がある単純なグリッド(1,1)

私はこれを試してみましたが、ここに例があります https://dotnetfiddle.net/3qzBhy

しかし、私の出力は次のとおりです。 [ ]|||| [ ]

理由がわかりません.ToString()し、 StringBuilder を呼び出すと改行などの書式が失われるかどうかもわかりません。

座標を保存する方法を見つけるのにも苦労しました

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)キーとが既に使用されているため、とroomList.Add(1,1)は重複しています。座標のリストを保存するにはどうすればよいですか?01

4

2 に答える 2

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.

于 2016-12-15T14:55:44.480 に答える
0

テキストの書式設定とコンソールの問題、および Manfred が既に述べたオプションに加えて、リストの代わりに配列を使用する例を次に示します。部屋はtrue任意のインデックスの値であり、「部屋がない」は で表されfalseます。

メソッドRooms()は次のようになります。

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

IsRoom(int, int)これにより、次のように変更する必要があります。

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;
}

また、改行が欠落していたため、隣の部屋と同じ行に垂直コネクタがありました。に変更しました

        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));
        }
于 2016-12-15T14:35:39.853 に答える