Пример #1
0
        public void BuildDistanceTable(Cell start)
        {
            LastRebuildTarget = start;

            List<Cell>[] distanceTable = new List<Cell>[width * height];

            int i = 0;

            start.Visited = true;
            start.Distance = i;

            distanceTable[i] = new List<Cell>();
            distanceTable[i].Add(start);

            while (distanceTable[i].Count > 0)
            {
                distanceTable[i + 1] = new List<Cell>();
                foreach (Cell t in distanceTable[i])
                {
                    List<Cell> neighbours = FindNeighbours(t, State.Path);
                    foreach (Cell o in neighbours)
                    {
                        o.Visited = true;
                        o.Distance = i + 1;
                        distanceTable[i + 1].Add(o);
                    }
                }
                i++;
            }

            i = 0;
            while (distanceTable[i].Count > 0)
            {
                foreach (Cell t in distanceTable[i])
                {
                    t.Visited = false;
                }
                distanceTable[i].Clear();
                i++;
            }
        }
Пример #2
0
        private Cell FindRandomStart()
        {
            DateTime tstart = DateTime.Now;
            int x, y;

            Cell start = new Cell();
            bool found = false;

            do
            {
                int[] randomCell = PickRandomCell();
                x = randomCell[0];
                y = randomCell[1];

                List<Cell> neighbours = FindNeighbours(Cells[x + y * width], State.Wall);

                if (neighbours.Count == 3 && Cells[x + y * width].Distance > GameSettings.MinimumDistance[difficulty] && Cells[x + y * width].Distance < GameSettings.MaximumDistance[difficulty])
                {
                    start = Cells[x + y * width];
                    start.State = State.Start;
                    found = true;
                }
            }
            while (found == false);

            return start;
        }
Пример #3
0
        private Cell FindRandomGoal()
        {
            int x, y;

            Cell goal = new Cell();
            bool found = false;

            do
            {
                int[] randomCell = PickRandomCell();
                x = randomCell[0];
                y = randomCell[1];

                List<Cell> neighbours = FindNeighbours(Cells[x + y * width], State.Wall);

                if (neighbours.Count == 3)
                {
                    goal = Cells[x + y * width];
                    goal.State = State.Treasure;
                    found = true;
                }
            }
            while (found == false);

            return goal;
        }
Пример #4
0
        private List<Cell> FindNeighbours(Cell t, State state)
        {
            int d;
            int[] dx = { 0, 0, -1, 1 };
            int[] dy = { -1, 1, 0, 0 };

            List<Cell> neighbours = new List<Cell>();

            for (d = 0; d < 4; d++)
            {
                if (t.X + dx[d] > 0 && t.X + dx[d] < width - 1 && t.Y + dy[d] > 0 && t.Y + dy[d] < height - 1 && Cells[(t.X + dx[d]) + (t.Y + dy[d]) * width].State == state)
                {
                    Cell o = Cells[(t.X + dx[d]) + (t.Y + dy[d]) * width];

                    if (o.Visited == false)
                    {
                        neighbours.Add(o);
                    }
                }
            }

            return neighbours;
        }
Пример #5
0
        public void Generate()
        {
            int x, y, n;

            int[][] todo = new int[width * height][];
            int todonum = 0;

            Cells = new Cell[width * height];

            List<int[]> neighbours;

            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    // Mark all even cells as Wall.
                    if (x % 2 == 0 || y % 2 == 0)
                    {
                        Cells[x + y * width] = new Cell();
                        Cells[x + y * width].X = x;
                        Cells[x + y * width].Y = y;
                        Cells[x + y * width].State = State.Wall;
                    }
                    // Mark the rest as Out.
                    else
                    {
                        Cells[x + y * width] = new Cell();
                        Cells[x + y * width].X = x;
                        Cells[x + y * width].Y = y;
                        Cells[x + y * width].State = State.Out;
                    }
                }
            }

            // Pick a random cell and mark it Path.
            int[] randomCell = PickRandomCell();
            x = randomCell[0];
            y = randomCell[1];

            Cells[x + y * width].X = x;
            Cells[x + y * width].Y = y;
            Cells[x + y * width].State = State.Path;

            // Mark all neighbours of current cell as Frontier.
            neighbours = FindNeighbours(x, y, State.Out);
            foreach (int[] neighbour in neighbours)
            {
                todo[todonum++] = neighbour;
                Cells[neighbour[0] + neighbour[1] * width].State = State.Frontier;
            }

            while (todonum > 0)
            {
                // Pick a random cell from Frontier and mark it as Path.
                n = random.Next(todonum);
                x = todo[n][0];
                y = todo[n][1];

                todo[n] = todo[--todonum];

                // Mark current cell as State.Path
                Cells[x + y * width].State = State.Path;

                // Pick a random neighbour of current cell that is Path and mark the cell between it as Path.
                int[] randomNeighbour = FindRandomNeighbour(x, y, State.Path);
                Cells[(((x + randomNeighbour[0] / 2) + (y + randomNeighbour[1] / 2) * width))].State = State.Path;

                // Mark all neighbours of current cell as State.Frontier
                neighbours = FindNeighbours(x, y, State.Out);
                foreach (int[] neighbour in neighbours)
                {
                    todo[todonum++] = neighbour;
                    Cells[neighbour[0] + neighbour[1] * width].State = State.Frontier;
                }
            }

            PlaceRandomItems();

            // Find random goal  location enclosed with 3 walls
            GoalCell = FindRandomGoal();

            BuildDistanceTable(GoalCell);

            // Find random starting location enclosed with 3 walls
            StartCell = FindRandomStart();
        }
Пример #6
0
        public void FindPath(Cell start, Cell goal)
        {
            int i;

            // Clear previous path.
            ClearPath();

            if (start == goal)
            {
                return;
            }

            i = goal.Distance;
            Cell c = goal;

            solutionPath.Add(c);

            while (i >= 0)
            {
                c.IsSolution = true;
                List<Cell> neighbours = FindNeighbours(c, State.Path);
                foreach (Cell o in neighbours)
                {
                    if (o.Distance < i)
                    {
                        solutionPath.Add(o);
                        c = o;
                        break;
                    }
                }
                i--;
            }
        }