private List <AnyCell> GetNeighbours(IMaze maze, AnyCell currentCell, int distance) { var neighbourCells = new List <AnyCell>(); neighbourCells.Add(maze[currentCell.X, currentCell.Y - distance]); neighbourCells.Add(maze[currentCell.X + distance, currentCell.Y]); neighbourCells.Add(maze[currentCell.X, currentCell.Y + distance]); neighbourCells.Add(maze[currentCell.X - distance, currentCell.Y]); return(neighbourCells.Where(x => x != null && !x.Visited).ToList()); }
private bool Is3WallNeighbours(IMaze maze, AnyCell currentCell) { if (CountingWall(maze[currentCell.X, currentCell.Y - 1]) + CountingWall(maze[currentCell.X + 1, currentCell.Y]) + CountingWall(maze[currentCell.X, currentCell.Y + 1]) + CountingWall(maze[currentCell.X - 1, currentCell.Y]) > 2) { return(true); } return(false); }
private int CountingWall(AnyCell cell) { int countWall = 0; if (cell is Wall) { countWall++; } return(countWall); }
private void RemoveWall(AnyCell first, AnyCell second, IMaze maze) { int xDiff = second.X - first.X; int yDiff = second.Y - first.Y; int addX, addY; addX = (xDiff != 0) ? (xDiff / Math.Abs(xDiff)) : 0; addY = (yDiff != 0) ? (yDiff / Math.Abs(yDiff)) : 0; maze[first.X + addX, first.Y + addY] = new Ground(first.X + addX, first.Y + addY); maze[first.X + addX, first.Y + addY].Visited = true; }
public void TryToStep(Direction direction) { AnyCell cell = null; var hero = Hero.GetHero; cell = CheckDirection(direction, hero, cell); if (cell?.TryToStep(this) ?? false) { hero.X = cell.X; hero.Y = cell.Y; } if (cell?.CallAfterStep != null) { cell.CallAfterStep(); } DescLastAction = cell?.CellMessage; }
public Maze GetMaze() { maze = new Maze(Width, Height); Hero.GetHero.X = 1; Hero.GetHero.Y = 1; startCell = maze[1, 1]; startCell.Visited = true; do { var neighbours = GetNeighbours(maze, startCell, 2); if (neighbours.Any()) { neighbourCell = neighbours[random.Next(0, neighbours.Count)]; stackCells.Push(startCell); RemoveWall(startCell, neighbourCell, maze); startCell = neighbourCell; neighbourCell.Visited = true; } else if (stackCells.Any()) { startCell = stackCells.Pop(); } else { List <AnyCell> unvisitedCells = maze.Cells.Where(cell => (cell is Ground) && (!cell.Visited)).ToList <AnyCell>(); startCell = unvisitedCells[random.Next(0, unvisitedCells.Count)]; } }while (stackCells.Count != 0); DropCoin(maze); maze[1, 1] = new StartCell(); PlaceFinishCell(maze); return(maze); }
public AnyCell CheckDirection(Direction direction, Hero hero, AnyCell cell) { switch (direction) { case Direction.Up: cell = this[hero.X - 1, hero.Y]; break; case Direction.Right: cell = this[hero.X, hero.Y + 1]; break; case Direction.Down: cell = this[hero.X + 1, hero.Y]; break; case Direction.Left: cell = this[hero.X, hero.Y - 1]; break; } return(cell); }