题
我试图创建一个迷宫发电机,并为此我在C#中实现的随机Prim算法。
然而,该位产生的结果是无效的。我想不通,如果是我的渲染,或者是无效的执行情况。因此,对于初学者来说,我想有一个人看看执行:
迷宫是单元的矩阵。
var cell = maze[0, 0];
cell.Connected = true;
var walls = new HashSet<MazeWall>(cell.Walls);
while (walls.Count > 0)
{
var randomWall = walls.GetRandom();
var randomCell = randomWall.A.Connected ? randomWall.B : randomWall.A;
if (!randomCell.Connected)
{
randomWall.IsPassage = true;
randomCell.Connected = true;
foreach (var wall in randomCell.Walls)
walls.Add(wall);
}
walls.Remove(randomWall);
}
下面是关于所呈现的结果的示例:
渲染迷宫http://dl.dropbox.com/u/1744224 /Upload/primrecur.png
修改确定,让我们看看渲染部分则:
private void MazePanel_Paint(object sender, PaintEventArgs e)
{
int size = 20;
int cellSize = 10;
MazeCell[,] maze = RandomizedPrimsGenerator.Generate(size);
mazePanel.Size = new Size(
size * cellSize + 1,
size * cellSize + 1
);
e.Graphics.DrawRectangle(Pens.Blue, 0, 0,
size * cellSize,
size * cellSize
);
for (int y = 0; y < size; y++)
for (int x = 0; x < size; x++)
{
foreach(var wall in maze[x, y].Walls.Where(w => !w.IsPassage))
{
if (wall.Direction == MazeWallOrientation.Horisontal)
{
e.Graphics.DrawLine(Pens.Blue,
x * cellSize, y * cellSize,
x * cellSize + cellSize,
y * cellSize
);
}
else
{
e.Graphics.DrawLine(Pens.Blue,
x * cellSize,
y * cellSize, x * cellSize,
y * cellSize + cellSize
);
}
}
}
}
和我想,明白这一点,我们需要看到MazeCell和MazeWall类:
namespace MazeGenerator.Maze
{
class MazeCell
{
public int Column
{
get;
set;
}
public int Row
{
get;
set;
}
public bool Connected
{
get;
set;
}
private List<MazeWall> walls = new List<MazeWall>();
public List<MazeWall> Walls
{
get { return walls; }
set { walls = value; }
}
public MazeCell()
{
this.Connected = false;
}
public void AddWall(MazeCell b)
{
walls.Add(new MazeWall(this, b));
}
}
enum MazeWallOrientation
{
Horisontal,
Vertical,
Undefined
}
class MazeWall : IEquatable<MazeWall>
{
public IEnumerable<MazeCell> Cells
{
get
{
yield return CellA;
yield return CellB;
}
}
public MazeCell CellA
{
get;
set;
}
public MazeCell CellB
{
get;
set;
}
public bool IsPassage
{
get;
set;
}
public MazeWallOrientation Direction
{
get
{
if (CellA.Column == CellB.Column)
{
return MazeWallOrientation.Horisontal;
}
else if (CellA.Row == CellB.Row)
{
return MazeWallOrientation.Vertical;
}
else
{
return MazeWallOrientation.Undefined;
}
}
}
public MazeWall(MazeCell a, MazeCell b)
{
this.CellA = a;
this.CellB = b;
a.Walls.Add(this);
b.Walls.Add(this);
IsPassage = false;
}
#region IEquatable<MazeWall> Members
public bool Equals(MazeWall other)
{
return (this.CellA == other.CellA) && (this.CellB == other.CellB);
}
#endregion
}
}
解决方案
这仅仅是一个想法:
即使你没有张贴在您添加壁细胞的代码,我认为是逻辑上的错误。单细胞有4周壁,但两个相邻的小区具有唯一7的壁。不是8。
您需要除去randomWall何时从其它细胞藏汉删除相应的壁。
要阐述中更详细一点:
如果AddWallsToMaze
算法增加4周壁给每个小区则存在重复的壁。
<强>实施例强>结果 CELLA有CellB墙。让我们把它Wall1。结果 CellB有地窖墙壁。这是Wall2不Wall1。结果 它应该是Wall1。
其他提示
的解决方案是校正在细胞如何绘制的绘制错误。我正在做右&底壁,和呈现左与顶壁。
private static void RenderCell(PaintEventArgs e, int cellSize, int y, int x, MazeWall wall, Pen pen)
{
if (wall.Direction == MazeWallOrientation.Horisontal)
{
e.Graphics.DrawLine(pen,
x * cellSize,
y * cellSize + cellSize,
x * cellSize + cellSize,
y * cellSize + cellSize
);
}
else
{
e.Graphics.DrawLine(pen,
x * cellSize + cellSize,
y * cellSize,
x * cellSize + cellSize,
y * cellSize + cellSize
);
}
}
我不是结果非常高兴。我想我会尝试另一个algorihm,这似乎并没有因为简洁(wellformed)为我所希望的。
不隶属于 StackOverflow