Exemplo n.º 1
0
    private void Awake()
    {
        _buttonStart.onClick.AddListener(OnStartClick);
        _buttonClear.onClick.AddListener(OnClearClick);
        _buttonReset.onClick.AddListener(OnResetClick);

        ResultDataCollector = new ResultDataCollector();
        MazeGenerator       = new MazeGenerator();
        AStarAlgorithm      = new AStarAlgorithm();

        MazeGenerator.GenerateMaze(_mazeMap, _noizeSensitivity);
        _rawImage.texture = MazeGenerator.GenerateTexture();

        SetEditeModeHint(EditMode);
        SetResultHint(string.Empty);

        _uiCanvas.gameObject.SetActive(true);
        _mapCanvas.gameObject.SetActive(true);
    }
Exemplo n.º 2
0
    public void FindThePath(MazeGenerator generator, ResultDataCollector collector)
    {
        Node node;
        var  cost    = 1;
        var  minCost = 0;
        var  counter = 0;
        var  maze    = generator.GetMaze;
        var  open    = new Dictionary <int, Node>()
        {
            { generator.Start.GetHashCode(), generator.Start }
        };
        var closed = new HashSet <Node>();
        var sw     = new System.Diagnostics.Stopwatch();

        collector.Open   = new Dictionary <int, List <Node> >();
        collector.Closed = new Dictionary <int, Node>();

        sw.Start();

        while (!open.ContainsKey(generator.End.GetHashCode()) && open.Any())
        {
            minCost = open.Values.Min(m => m.FCost);
            node    = open.Values.First(n => n.FCost == minCost);

            collector.Closed.Add(counter, node);

            closed.Add(node);
            open.Remove(node.GetHashCode());

            var neighbours = GetNeighbours(node);

            foreach (var item in neighbours)
            {
                var n = open.ContainsKey(item.GetHashCode())
                    ? open[item.GetHashCode()]
                    : item;

                if (closed.Contains(n) || !IsNodeAvailable(n, maze))
                {
                    continue;
                }

                if (collector.Open.ContainsKey(counter))
                {
                    collector.Open[counter].Add(n);
                }
                else
                {
                    collector.Open.Add(counter, new List <Node> {
                        n
                    });
                }

                if (!open.ContainsKey(n.GetHashCode()))
                {
                    n.SetParent(node);
                    n.Estimate(generator.End);
                    n.SetCost(node.GCost + cost);
                    open.Add(n.GetHashCode(), n);
                }
                else if (IsLowerCostWay(node, n, cost))
                {
                    node.SetParent(n);
                    node.SetCost(n.GCost + cost);
                }
            }

            ++counter;
        }

        collector.Passes = counter;
        collector.Path   = new List <Node>();

        if (open.ContainsKey(generator.End.GetHashCode()))
        {
            BuildPath(closed.Last(), collector.Path);
            collector.Result = "End point reached!\nTime: " + sw.ElapsedMilliseconds + "ms\nPasses: " + counter + "\nPath length: " + collector.Path.Count;
        }
        else
        {
            collector.Result = "End point couldn't be reached!";
        }

        sw.Stop();
    }