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.