首先,让我为篇幅道歉,我会尽量保持简短。
在尝试按照维基百科上的指示构建普里姆算法后,我发现它无法适应我的迷宫构建方式。因此,我尝试了同样的思路来适应我的迷宫,但是我遇到了一个奇怪的错误。
当游戏开始时,迷宫构建不正确,我无法找出原因。以下是偶尔发生的情况:
有时它可以完美地工作,所以我有一个 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此时只能是路径或栅栏两个选项,并且reorderList将重新索引任何类型的列表,迷宫大小是根据屏幕分辨率计算的,每个单元格为64x64像素。
GraphicsDevice.Viewport.Width * 5 / 64,
GraphicsDevice.Viewport.Height * 5 / 64