首先让我为尺寸道歉,我会尽量保持这个尺寸
在尝试完全按照维基百科上所说的方式构建 prim 算法之后,我发现它不会像我构建的迷宫那样工作。所以我试图做同样的想法来适应我的迷宫,但我看到了一个奇怪的错误,
当我的游戏开始时,它只是没有正确构建我的迷宫,我无法弄清楚为什么这是偶尔发生的事情
其他时候它工作得很好,所以我有一个public Dictionary<int, Dictionary<int, MazeCellState>> maze
这个迷宫,当它开始时,迷宫都是树篱,然后我继续像这样建造路径
private static void buildPath()
{
List<KeyValuePair<Misc.Cord, Misc.Cord>> ends = new List<KeyValuePair<Misc.Cord, Misc.Cord>>();
ends.Add(new KeyValuePair<Misc.Cord, Misc.Cord>(new Misc.Cord() { X = 0, Y = 0 }, new Misc.Cord() { X = 0, Y = 0 }));
Misc.Cord currentPos = null;
while (ends.Count > 0)
{
int posKey = rand.Next(0, ends.Count);
Misc.Cord lastPos = ends[posKey].Key;
currentPos = ends[posKey].Value;
maze[currentPos.X][currentPos.Y] = MazeCellState.Path;
int currentCount = 0;
MovingState moveTo1 = (MovingState)rand.Next(0, 4);
MovingState moveTo2 = (MovingState)rand.Next(0, 4);
while (moveTo1.Equals(moveTo2))
{
moveTo1 = (MovingState)rand.Next(0, 4);
moveTo2 = (MovingState)rand.Next(0, 4);
}
// check left
if (currentPos.X - 2 > 0 && maze[currentPos.X - 2][currentPos.Y] != MazeCellState.Path && currentCount < 2 && (moveTo1 == MovingState.Left || moveTo2 == MovingState.Left))
{
if(!lastPos.Equals(new Misc.Cord() { X = currentPos.X - 2, Y = currentPos.Y }))
{
ends.Add(new KeyValuePair<Misc.Cord, Misc.Cord>(currentPos, new Misc.Cord() { X = currentPos.X - 2, Y = currentPos.Y }));
maze[currentPos.X - 1][currentPos.Y] = MazeCellState.Path;
currentCount++;
}
}
// check right
if (currentPos.X + 2 < maze.Count && maze[currentPos.X + 2][currentPos.Y] != MazeCellState.Path && currentCount < 2 && (moveTo1 == MovingState.Right || moveTo2 == MovingState.Right))
{
if (!lastPos.Equals(new Misc.Cord() { X = currentPos.X + 2, Y = currentPos.Y }))
{
ends.Add(new KeyValuePair<Misc.Cord, Misc.Cord>(currentPos, new Misc.Cord() { X = currentPos.X + 2, Y = currentPos.Y }));
maze[currentPos.X + 1][currentPos.Y] = MazeCellState.Path;
currentCount++;
}
}
// check Up
if (currentPos.Y - 2 > 0 && maze[currentPos.X][currentPos.Y - 2] != MazeCellState.Path && currentCount < 2 && (moveTo1 == MovingState.Up || moveTo2 == MovingState.Up))
{
if(!lastPos.Equals(new Misc.Cord() { X = currentPos.X, Y = currentPos.Y - 2}))
{
ends.Add(new KeyValuePair<Misc.Cord, Misc.Cord>(currentPos, new Misc.Cord() { X = currentPos.X, Y = currentPos.Y - 2 }));
maze[currentPos.X][currentPos.Y - 1] = MazeCellState.Path;
currentCount++;
}
}
// check Down
if (currentPos.Y + 2 < maze[0].Count && maze[currentPos.X][currentPos.Y + 2] != MazeCellState.Path && currentCount < 2 && (moveTo1 == MovingState.Down || moveTo2 == MovingState.Down))
{
if(!lastPos.Equals(new Misc.Cord() { X = currentPos.X, Y = currentPos.Y + 2}))
{
ends.Add(new KeyValuePair<Misc.Cord, Misc.Cord>(currentPos, new Misc.Cord() { X = currentPos.X, Y = currentPos.Y + 2 }));
maze[currentPos.X][currentPos.Y + 1] = MazeCellState.Path;
currentCount++;
}
}
ends.RemoveAt(posKey);
ends = reorderList(ends);
}
maze[0][1] = MazeCellState.Path;
}
我不知道为什么有时我会得到上面的图片,我的理论是它最终会恢复它的自我
一些快速说明,此时 MazeCellState 只能是 2 个选项之一,路径或对冲和 reorderList 将重新索引任何类型的列表,迷宫大小是从屏幕分辨率计算出来的,每个单元格是 64x64 PX,
GraphicsDevice.Viewport.Width * 5 / 64,
GraphicsDevice.Viewport.Height * 5 / 64