【问题标题】:Rendering a random generated maze in WinForms.NET在 WinForms.NET 中渲染随机生成的迷宫
【发布时间】:2010-04-06 19:37:56
【问题描述】:

我正在尝试创建一个迷宫生成器,为此我在 C# 中实现了 Randomized 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);
}

这是一个关于渲染结果的示例:

Rendered Maze 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
    }
}

【问题讨论】:

  • 似乎找不到算法有什么问题。问题一定出在其他地方……

标签: c# winforms maze


【解决方案1】:

这只是一个想法:

即使您没有发布将墙添加到单元格的代码,我相信这是逻辑错误。一个单元格有 4 面墙,但两个相邻的单元格只有 7 面墙。不是 8。

在移除randomWall时,您还需要从另一个单元格中移除相应的墙。

再详细一点: 如果您的 AddWallsToMaze 算法为每个单元格添加 4 面墙,则存在重复的墙。

例如:
CellA 与 CellB 有一堵墙。我们称之为 Wall1。
CellB 与 CellA 有一堵墙。这是 Wall2 而不是 Wall1。
应该是 Wall1。

【讨论】:

  • 不错的萨尼。您的建议与我尝试使用上述代码添加墙壁时遇到的相同问题。 MazeCell 类的 addwall 的实现需要检查是否已经从另一个单元格添加了墙(“MazeCell b”参数)。我想这就是你的答案,克劳斯。
  • 嗯,好吧。它与添加的引用相同,我只是将非通道墙取出(注意 lambda),但我会调查它。
【解决方案2】:

解决方案是更正单元格绘制方式中的渲染错误。我在做右&下墙,并渲染左&上墙。

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

我对结果并不十分满意。我想我会尝试另一个算法,这似乎没有我希望的那么好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多