Quantcast
Channel: Forum Pasja Informatyki - Najnowsze pytania bez odpowiedzi
Viewing all articles
Browse latest Browse all 21942

Dublowanie wartości w kolejce przy dodawaniu nowych obiektów

$
0
0

Dzień dobry.

Piszę program, który będzie generował labirynt i do tego potrzebna mi jest kolejka. Oto fragment mojego kodu:


private bool AnyUnvisitedCells()
        {
            foreach (var item in Cells)
            {
                if (!item.Visited)
                    return true;
            }
            return false;
        }
        public List<Direction> AnyUnvisitedNeighbors(Point currentCell)
        {
            List<Direction> unvisitedNeighbors = new List<Direction>();
            if (currentCell.Y - 1 >= 0)
                if (!_cells[currentCell.X, currentCell.Y - 1].Visited) unvisitedNeighbors.Add(Direction.left);
            if (currentCell.Y + 1 < _dim.X)
                if (!_cells[currentCell.X, currentCell.Y + 1].Visited) unvisitedNeighbors.Add(Direction.right);
            if (currentCell.X - 1 >= 0)
                if (!_cells[currentCell.X - 1, currentCell.Y].Visited) unvisitedNeighbors.Add(Direction.up);
            if (currentCell.X + 1 < _dim.Y)
                if (!_cells[currentCell.X + 1, currentCell.Y].Visited) unvisitedNeighbors.Add(Direction.down);
            return unvisitedNeighbors;
        }
        private Direction ChooseNeighbor(List<Direction> neighbors)
        {
            return neighbors[RandomInt(0, neighbors.Count)];
        }
        private Point RandomUnvisitedCell()
        {
            Point myPoint;
            for (int i = 0; i < _dim.Y; i++)
            {
                for (int j = 0; j < _dim.X; j++)
                {
                    if (!_cells[i, j].Visited)
                    {
                        myPoint = new Point(i, j);
                        return myPoint;
                    }
                }
            }
            myPoint = new Point();
            return myPoint;
        }
        private bool UnvisitedCellFromQueue(ref Point currentCell, ref Queue<Point> queue)
        {
            Point temp = new Point();
            do
            {
                if (queue.Count > 0)
                {
                    temp = queue.Dequeue();
                }
                else
                    return false;
            } while (AnyUnvisitedNeighbors(temp).Count <= 0);
            currentCell = temp;
            return true;
        }
        private void BreakWalls(ref Point currentCell, Direction dir)
        {
            if (dir == Direction.left)
            {
                _cells[currentCell.X, currentCell.Y].WestWall = false;
                _cells[currentCell.X, currentCell.Y - 1].EastWall = false;
                currentCell.Y--;
            }
            else if (dir == Direction.right)
            {
                _cells[currentCell.X, currentCell.Y].EastWall = false;
                _cells[currentCell.X, currentCell.Y + 1].WestWall = false;
                currentCell.Y++;
            }
            else if (dir == Direction.up)
            {
                _cells[currentCell.X, currentCell.Y].NorthWall = false;
                _cells[currentCell.X - 1, currentCell.Y].SouthWall = false;
                currentCell.X--;
            }
            else if (dir == Direction.down)
            {
                _cells[currentCell.X, currentCell.Y].SouthWall = false;
                _cells[currentCell.X + 1, currentCell.Y].NorthWall = false;
                currentCell.X++;
            }
        }
        public void GenerateMaze()
        {
            Point currentCell = new Point(_start.Y, _start.X);
            Queue<Point> queue = new Queue<Point>();
            List<Direction> unvisitedNeighbors = new List<Direction>();
            Direction dir;

            Step2:
            queue.Enqueue(currentCell);
            _cells[queue.Peek().X, queue.Peek().Y].Visited = true;

            Step4:
            unvisitedNeighbors = AnyUnvisitedNeighbors(currentCell);

            if (unvisitedNeighbors.Count <= 0) // All the neighbors of current cell have been visited
            {
                if (UnvisitedCellFromQueue(ref currentCell, ref queue))
                    goto Step4;
                else
                    return;
            }
            else
            {
                dir = ChooseNeighbor(unvisitedNeighbors);
                BreakWalls(ref currentCell, dir);
                goto Step2;
            }
        }

Na początku zawsze startuje z punktu (0,4), czyli komórka labiryntu (tablica _cells[4,0]) zostanie zaznaczona jako odwiedzona. Mam problem z dodawaniem punktów do kolejki punktów. Na początku w kolejce znajduje się punkt [4,0], a później, gdy algorytm wybierze drogę w prawo to moja kolejka wygląda następująco: {[4,1],[4,1]}. Później, gdy wybierze drogę np. w górę to kolejka prezentuje się: {[3,1],[3,1],[3,1]}. Moje pytanie brzmi: dlaczego te wartości z początku się zmieniają i jak zrobić żeby się nie zmieniały? Chce aby wyglądała tak: {[4,0],[4,1],[3,1]}. Proszę o pomoc.

PS Wiem, że nie powinienem stosować goto ale chcę aby to działało, a później zastanowię się jak to napisać, żeby nie było goto.


Viewing all articles
Browse latest Browse all 21942

Trending Articles


TRX Antek AVT - 2310 ver 2,0


Автовишка HAULOTTE HA 16 SPX


POTANIACZ


Zrób Sam - rocznik 1985 [PDF] [PL]


Maxgear opinie


BMW E61 2.5d błąd 43E2 - klapa gasząca a DPF


Eveline ➤ Matowe pomadki Velvet Matt Lipstick 500, 506, 5007


Auta / Cars (2006) PLDUB.BRRip.480p.XviD.AC3-LTN / DUBBING PL


Peugeot 508 problem z elektroniką


AŚ Jelenia Góra