Example #1
0
        private void DrawGrid(SquareGrid grid, AStarSearch astar)
        {
            string _output = "";

            // Print out the cameFrom array
            for (var y = 0; y < grid.height; y++)
            {
                for (var x = 0; x < grid.width; x++)
                {
                    Location id  = new Location(x, y);
                    Location ptr = id;
                    Tile     t   = Instantiate(tilePrefab, new Vector3(x, y), Quaternion.identity, transform) as Tile;

                    if (!astar.cameFrom.TryGetValue(id, out ptr))
                    {
                        ptr = id;
                    }
                    if (astar.cameFrom.TryGetValue(id, out ptr))
                    {
                        t.MarkTile();
                    }
                    if (grid.walls.Contains(id))
                    {
                        _output = "##";
                        t.SetColor(Color.black);
                    }
                    else if (ptr.x == x + 1)
                    {
                        _output += "\u2192 ";// Console.Write("\u2192 ");
                    }
                    else if (ptr.x == x - 1)
                    {
                        _output += "\u2190 ";// Console.Write("\u2190 ");
                    }
                    else if (ptr.y == y + 1)
                    {
                        _output += "\u2193 ";// Console.Write("\u2193 ");
                    }
                    else if (ptr.y == y - 1)
                    {
                        _output += "\u2191 ";// Console.Write("\u2191 ");
                    }
                    else
                    {
                        _output += "* ";//Console.Write("* ");
                    }
                }
                //        Debug.Log(_output);//.WriteLine();
            }
        }
Example #2
0
        private void Start()
        {
            // Make "diagram 4" from main article
            SquareGrid grid = new SquareGrid(10, 10);

            for (var x = 1; x < 4; x++)
            {
                for (var y = 7; y < 9; y++)
                {
                    grid.walls.Add(new Location(x, y));
                }
            }
            grid.forests = new HashSet <Location>
            {
                new Location(3, 4), new Location(3, 5),
                new Location(4, 1), new Location(4, 2),
                new Location(4, 3), new Location(4, 4),
                new Location(4, 5), new Location(4, 6),
                new Location(4, 7), new Location(4, 8),
                new Location(5, 1), new Location(5, 2),
                new Location(5, 3), new Location(5, 4),
                new Location(5, 5), new Location(5, 6),
                new Location(5, 7), new Location(5, 8),
                new Location(6, 2), new Location(6, 3),
                new Location(6, 4), new Location(6, 5),
                new Location(6, 6), new Location(6, 7),
                new Location(7, 3), new Location(7, 4),
                new Location(7, 5)
            };

            // Run A*
            AStarSearch astar = new AStarSearch(grid, new Location(0, 0),
                                                new Location(1, 1));

            DrawGrid(grid, astar);
        }