public double GetManhattanDistanceToGoal(MazeLocation currentLocation) { var dx = currentLocation.Column - _finish.Column; var dy = currentLocation.Row - _finish.Row; return(Math.Abs(dx) + Math.Abs(dy)); }
/* * Creates a maze with the given dimensions * @param sizeRows Height of the maze * @param sizeCols Width of the maze * @return Maze data */ public MazeLocation[,] FromDimensions(int sizeRows, int sizeCols) { MazeLocation[,] maze = new MazeLocation[sizeRows, sizeCols]; int maxRows = maze.GetUpperBound(0); int maxColumns = maze.GetUpperBound(1); for (int i = 0; i <= maxRows; i++) { for (int j = 0; j <= maxColumns; j++) { if (i == 0 || j == 0 || i == maxRows || j == maxColumns) { maze[i, j] = MazeLocation.WALL; } else if (i % 2 == 0 && j % 2 == 0) { if (Random.value > placementThreshold) { maze[i, j] = MazeLocation.WALL; int a = (Random.value < .5) ? 0 : (Random.value < .5 ? -1 : 1); int b = (a != 0) ? 0 : (Random.value < .5 ? -1 : 1); maze[i + a, j + b] = MazeLocation.WALL; } } } } return(maze); }
public double GetEuclideanDistanceToGoal(MazeLocation currentLocation) { var dx = currentLocation.Column - _finish.Column; var dy = currentLocation.Row - _finish.Row; return(Math.Sqrt(dx * dx + dy * dy)); }
public void Move(Direction direction) { MazeLocation oldLocation = Location.Clone(); switch (direction) { case Direction.Right: Location.Column++; break; case Direction.Down: Location.Row++; break; case Direction.Left: Location.Column--; break; case Direction.Up: Location.Row--; break; } SetPosition(); Moved?.Invoke(this, oldLocation); }
public bool CanMove(MazeLocation currentLocation, Direction direction) { switch (direction) { case Direction.Right: return(currentLocation.Column < ColumnCount - 1 && GetCell(currentLocation.Column + 1, currentLocation.Row).IsWalkable); case Direction.Down: return(currentLocation.Row < RowCount - 1 && GetCell(currentLocation.Column, currentLocation.Row + 1).IsWalkable); case Direction.Left: return(currentLocation.Column > 0 && GetCell(currentLocation.Column - 1, currentLocation.Row).IsWalkable); case Direction.Up: return(currentLocation.Row > 0 && GetCell(currentLocation.Column, currentLocation.Row - 1).IsWalkable); case Direction.None: default: return(false); } }
public IReadOnlyCollection <MazeLocation> GetPossibleSuccessors(MazeLocation current) { var onLeft = new MazeLocation(current.Row - 1, current.Column); var onRight = new MazeLocation(current.Row + 1, current.Column); var onTop = new MazeLocation(current.Row, current.Column - 1); var onBottom = new MazeLocation(current.Row, current.Column + 1); return(new[] { onLeft, onRight, onTop, onBottom } .Where(location => location.IsValid(_height, _width)) .Where(location => _grid[location.Row, location.Column] != Cell.Blocked) .ToArray()); }
/* * Returns a valid (FREE) position * @param newMazeLocation Maze location * @return Valid position */ private Position GenerateValidPosition(MazeLocation newMazeLocation) { if (!isInitialized) { throw new System.Exception("Respawn system not initialized"); } Position selectedPosition = freePositions[Random.Range(0, freePositions.Count)]; mazeData[selectedPosition.row, selectedPosition.column] = newMazeLocation; freePositions.Remove(selectedPosition); return(selectedPosition); }
public Maze(int height, int width, MazeLocation start, MazeLocation finish, double sparseness) { _height = height; _width = width; _start = start; _finish = finish; _grid = new Cell[height, width]; FillInGrid(sparseness); _grid[start.Row, start.Column] = Cell.Start; _grid[finish.Row, finish.Column] = Cell.Goal; }
private void Interceptor_Moved(MazeExplorer interceptor, MazeLocation location) { if (interceptor.Location == Player.Location) { OnGameOver(); return; } GetCell(location.Column, location.Row).Explorers.Remove(interceptor); GetCell(interceptor.Location.Column, interceptor.Location.Row).Explorers.Add(interceptor); OnCaseCouranteChanged(new MazeCellEventArgs(interceptor, location, interceptor.Location)); }
private void Player_Moved(MazeExplorer interceptor, MazeLocation location) { if (GetCell(Player.Location.Column, Player.Location.Row).Explorers.Any(x => x.ExplorerType == UserType.NPC)) { OnGameOver(); return; } OnCaseCouranteChanged(new MazeCellEventArgs(Player, location, Player.Location)); if (Player.Location.Row == RowCount - 1 && Player.Location.Column == (_isLeft ? ColumnCount - 2 : 1)) { OnEndReached(); return; } }
public bool IsInGoal(MazeLocation location) => _finish.Equals(location);
public MazeCellEventArgs(MazeExplorer explorer, MazeLocation oldLocation, MazeLocation newLocation) { OldLocation = oldLocation; NewLocation = newLocation; Explorer = explorer; }