如何创建基于网格的地图?
How to create a grid based map?
我正在尝试用 C# 制作这样的地图:
0 1
0 [ ]---[ ]
| |
| |
1 [ ]---[ ]
房间位于 (0,0)
、(1,0)
、(0,1)
和 (1,1)
的简单网格
我试过这样做,这里有一个例子https://dotnetfiddle.net/3qzBhy
但我的输出是:
[ ]|||| [ ]
我不明白为什么,也不确定在 StringBuilder 上调用 .ToString()
是否会丢失其格式,例如换行。
我也找不到存储坐标的方法
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)
重复,因为键 0
和 1
已被使用。如何存储坐标列表?
除了文本格式和控制台的问题以及 Manfred 已经提到的选项之外,这里还有一个使用数组而不是列表的示例。房间是任何给定索引的 true
值,"no room" 表示为 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));
}
我不会通过评论来传播我的观点,而是将它们全部转储在这里作为答案:
I also had trouble finding a way to store coordinates
SortedLists
、Dictionaries
等都不行。最好只使用常规 List
填充 Tuples
、Points
或您自己的 class,直到找到更好的解决方案。
由于这些房间可能不会空着,您可以编写自己的 classes,例如:
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.
没有。您的示例中没有任何换行符,因为所有房间都在 Y = 0
如果您一次绘制多条线并将它们串在一起,当前的尝试将无效。相反,您可以使用类似 "half-step" 网格的东西。
您可以找到一个小的(丑陋的、未优化的、临时的)示例 here as a fiddle。
我正在尝试用 C# 制作这样的地图:
0 1
0 [ ]---[ ]
| |
| |
1 [ ]---[ ]
房间位于 (0,0)
、(1,0)
、(0,1)
和 (1,1)
我试过这样做,这里有一个例子https://dotnetfiddle.net/3qzBhy
但我的输出是:
[ ]|||| [ ]
我不明白为什么,也不确定在 StringBuilder 上调用 .ToString()
是否会丢失其格式,例如换行。
我也找不到存储坐标的方法
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)
重复,因为键 0
和 1
已被使用。如何存储坐标列表?
除了文本格式和控制台的问题以及 Manfred 已经提到的选项之外,这里还有一个使用数组而不是列表的示例。房间是任何给定索引的 true
值,"no room" 表示为 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));
}
我不会通过评论来传播我的观点,而是将它们全部转储在这里作为答案:
I also had trouble finding a way to store coordinates
SortedLists
、Dictionaries
等都不行。最好只使用常规 List
填充 Tuples
、Points
或您自己的 class,直到找到更好的解决方案。
由于这些房间可能不会空着,您可以编写自己的 classes,例如:
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.
没有。您的示例中没有任何换行符,因为所有房间都在 Y = 0
如果您一次绘制多条线并将它们串在一起,当前的尝试将无效。相反,您可以使用类似 "half-step" 网格的东西。
您可以找到一个小的(丑陋的、未优化的、临时的)示例 here as a fiddle。