创建图并将其用于路径查找算法

时间:2014-04-12 14:58:16

标签: c# algorithm graph path-finding

我正在创建一个随机创建迷宫的程序,其中每个方块都可以从另一个方块到达。我希望在此程序中包含的下一步是将路径从一个方块连接到另一个方块,最好使用Dijkstra's Shortest Path Algorithm

但是,这意味着我需要将MAze转换为图形,如下所示: enter image description here

我如何对此进行编码,我之前从未使用过这样的算法,并且没有这方面的经验,所以我真的需要几乎从图a到图解的每一步都要向我解释。 / p>

以下是迷宫创建的代码:

public void GenerateMaze()
{
    for (int x = 0; x < mazeWidth; x++)
        for (int z = 0; z < mazeHeight; z++)
        {
            MazeCells[x, z].Walls[0] = true;
            MazeCells[x, z].Walls[1] = true;
            MazeCells[x, z].Walls[2] = true;
            MazeCells[x, z].Walls[3] = true;
            MazeCells[x, z].Visited = false;
        }
    MazeCells[0, 0].Visited = true;
    EvaluateCell(new Vector2(0, 0));
}

public void resetMaze()
{
    for (int x = 0; x < mazeWidth; x++)
        for (int z = 0; z < mazeHeight; z++)
        {
            MazeCells[x, z].Visited = false;
        }

    RandomWalls(new Vector2(0, 0));
}

private void EvaluateCell(Vector2 cell)
{
    List<int> neighborCells = new List<int>();
    neighborCells.Add(0);
    neighborCells.Add(1);
    neighborCells.Add(2);
    neighborCells.Add(3);

    while (neighborCells.Count > 0)
    {
        int pick = rand.Next(0, neighborCells.Count);
        int selectedNeighbor = neighborCells[pick];
        neighborCells.RemoveAt(pick);

        Vector2 neighbor = cell;

        switch (selectedNeighbor)
        {
            case 0: neighbor += new Vector2(0, -1);
                break;
            case 1: neighbor += new Vector2(1, 0);
                break;
            case 2: neighbor += new Vector2(0, 1);
                break;
            case 3: neighbor += new Vector2(-1, 0);
                break;
        }

        if (
            (neighbor.X >= 0) &&
            (neighbor.X < mazeWidth) &&
            (neighbor.Y >= 0) &&
            (neighbor.Y < mazeHeight)
            )
        {
            if (!MazeCells[(int)neighbor.X, (int)neighbor.Y].Visited)
            {
                MazeCells[(int)neighbor.X, (int)neighbor.Y].Visited = true;
                MazeCells[(int)cell.X, (int)cell.Y].Walls[selectedNeighbor] = false;
                MazeCells[(int)neighbor.X, (int)neighbor.Y].Walls[(selectedNeighbor + 2) % 4] = false;
                EvaluateCell(neighbor);
            }
        }
    }
}

//Removes random walls
private void RandomWalls(Vector2 cell)
{
    List<int> neighborCells = new List<int>();
    neighborCells.Add(0);
    neighborCells.Add(1);
    neighborCells.Add(2);
    neighborCells.Add(3);

    while (neighborCells.Count > 0)
    {
        int pick = rand.Next(0, neighborCells.Count);
        int selectedNeighbor = neighborCells[pick];
        neighborCells.RemoveAt(pick);

        Vector2 neighbor = cell;

        switch (selectedNeighbor)
        {
            case 0: neighbor += new Vector2(0, -1);
                break;
            case 1: neighbor += new Vector2(1, 0);
                break;
            case 2: neighbor += new Vector2(0, 1);
                break;
            case 3: neighbor += new Vector2(-1, 0);
                break;
        }

        //Ensures that end piece is not deleted
        if (
            (neighbor.X >= 0) &&
            (neighbor.X < mazeWidth) &&
            (neighbor.Y >= 0) &&
            (neighbor.Y < mazeHeight)
            )
        {

            //if cell was not visited
            if (!MazeCells[(int)neighbor.X, (int)neighbor.Y].Visited)
            {
                Random random = new Random();

                MazeCells[(int)neighbor.X, (int)neighbor.Y].Visited = true;

                //if random number is >= a certain number, removes the walls on both ends
                if (random.Next(20) >= 15 && removed <= 100)
                {
                    //MazeCells[(int)neighbor.X, (int)neighbor.Y].Visited = true;
                    MazeCells[(int)cell.X, (int)cell.Y].Walls[selectedNeighbor] = false;
                    MazeCells[(int)neighbor.X, (int)neighbor.Y].Walls[(selectedNeighbor + 2) % 4] = false;
                    removed++;
                }

                RandomWalls(neighbor);
            }
        }
    }
}

我对代码缺乏注释表示道歉,但这就是迷宫最终呈现后的样子:

enter image description here

0 个答案:

没有答案