Пример #1
0
    public WeightedDistances FindDistanceForAllReachableLinkedCells()
    {
        WeightedDistances       weights  = new WeightedDistances(this);
        List <WeightedMazeCell> frontier = new List <WeightedMazeCell>()
        {
            this
        };

        // Will expands until last connected cell found
        while (frontier.Count > 0)
        {
            //frontier.Sort((x, y) => x.weight.CompareTo(y.weight)); frontier.Reverse();
            //or
            //frontier.Sort((x, y) => -1 * x.weight.CompareTo(y.weight)); //descending order

            frontier.Sort((x, y) => weights[x].CompareTo(weights[y])); //ascending orders
            WeightedMazeCell currentCell = frontier[0];
            frontier.Remove(currentCell);

            foreach (var neighbour in currentCell.Links())
            {
                int totalWeight = weights[currentCell] + neighbour.weight;

                if (!weights.ContainsKey(neighbour) || totalWeight < weights[neighbour])
                {
                    frontier.Add(neighbour);
                    weights[neighbour] = totalWeight;
                }
            }
        }

        return(weights);
    }
Пример #2
0
 public bool IsLinked(WeightedMazeCell cell)
 {
     if (cell == null) //don't forget!
     {
         return(false);
     }
     return(links.ContainsKey(cell));
 }
Пример #3
0
 public WeightedMazeCell Unlink(WeightedMazeCell cell, bool isBidirectional = true)
 {
     links.Remove(cell);
     if (isBidirectional)
     {
         cell.Unlink(this, false);
     }
     return(this);
 }
Пример #4
0
 public WeightedMazeCell Link(WeightedMazeCell cell, bool isBidirectional = true)
 {
     links[cell] = true;
     if (isBidirectional)
     {
         cell.Link(this, false);
     }
     return(this);
 }
Пример #5
0
 public IEnumerable <WeightedMazeCell[]> EachRow(Direction vertical, Direction horizontal)
 {
     if (vertical == Direction.North)
     {
         if (horizontal == Direction.East)
         {
             for (int z = 0; z < height; z++)
             {
                 WeightedMazeCell[] cellRow = new WeightedMazeCell[width];
                 for (int x = 0; x < width; x++)
                 {
                     cellRow[x] = values[x, z];
                 }
                 yield return(cellRow);
             }
         }
         else
         {
             for (int z = 0; z < height; z++)
             {
                 WeightedMazeCell[] cellRow = new WeightedMazeCell[width];
                 for (int x = width - 1; x >= 0; x--)
                 {
                     cellRow[width - 1 - x] = values[x, z];
                 }
                 yield return(cellRow);
             }
         }
     }
     else
     {
         if (horizontal == Direction.East)
         {
             for (int z = height - 1; z >= 0; z--)
             {
                 WeightedMazeCell[] cellRow = new WeightedMazeCell[width];
                 for (int x = 0; x < width; x++)
                 {
                     cellRow[x] = values[x, z];
                 }
                 yield return(cellRow);
             }
         }
         else
         {
             for (int z = height - 1; z >= 0; z--)
             {
                 WeightedMazeCell[] cellRow = new WeightedMazeCell[width];
                 for (int x = width - 1; x >= 0; x--)
                 {
                     cellRow[width - 1 - x] = values[x, z];
                 }
                 yield return(cellRow);
             }
         }
     }
 }
Пример #6
0
 public IEnumerable <WeightedMazeCell[]> EachRow()
 {
     for (int z = height - 1; z >= 0; z--)
     {
         WeightedMazeCell[] cellRow = new WeightedMazeCell[width];
         for (int x = 0; x < width; x++)
         {
             cellRow[x] = values[x, z];
         }
         yield return(cellRow);
     }
 }
Пример #7
0
    public void PrepareGrid()
    {
        values = new WeightedMazeCell[width, height];

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                values[j, i] = new WeightedMazeCell(j, i);
                Debug.Log("Cell.gridX = " + j + "; Cell.gridY = " + i);
            }
        }
        Debug.Log("WidthCells = " + values.GetLength(0) + "; HeightCells = " + values.GetLength(1));
    }
Пример #8
0
    public void BraidMaze(float chanceToCullDeadend)
    {
        List <WeightedMazeCell> deadends = GetDeadends();

        deadends = Shuffle(deadends, (int)(Random.value * int.MaxValue));
        foreach (WeightedMazeCell deadendCell in deadends)
        {
            if (deadendCell.Links().Count == 1 || Random.value < chanceToCullDeadend)
            {
                List <WeightedMazeCell> neighbours = deadendCell.Neighbours.FindAll(c => !deadendCell.IsLinked(c));
                List <WeightedMazeCell> neighboursThatAreDeadends = neighbours.FindAll(c => c.Links().Count == 1);

                WeightedMazeCell chosenNeighbour = neighboursThatAreDeadends.Count != 0 ? neighboursThatAreDeadends[Random.Range(0, neighboursThatAreDeadends.Count)] : neighbours[Random.Range(0, neighbours.Count)];
                deadendCell.Link(chosenNeighbour);
            }
        }
    }
Пример #9
0
 // содержимое клетки
 public virtual string ContentsOfCell(WeightedMazeCell cell)
 {
     return("   ");
 }