コード例 #1
0
    public double GetManhattanDistanceToGoal(MazeLocation currentLocation)
    {
        var dx = currentLocation.Column - _finish.Column;
        var dy = currentLocation.Row - _finish.Row;

        return(Math.Abs(dx) + Math.Abs(dy));
    }
コード例 #2
0
    /*
     * 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);
    }
コード例 #3
0
    public double GetEuclideanDistanceToGoal(MazeLocation currentLocation)
    {
        var dx = currentLocation.Column - _finish.Column;
        var dy = currentLocation.Row - _finish.Row;

        return(Math.Sqrt(dx * dx + dy * dy));
    }
コード例 #4
0
    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);
    }
コード例 #5
0
    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);
        }
    }
コード例 #6
0
    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());
    }
コード例 #7
0
    /*
     * 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);
    }
コード例 #8
0
    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;
    }
コード例 #9
0
    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));
    }
コード例 #10
0
    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;
        }
    }
コード例 #11
0
 public bool IsInGoal(MazeLocation location) => _finish.Equals(location);
コード例 #12
0
 public MazeCellEventArgs(MazeExplorer explorer, MazeLocation oldLocation, MazeLocation newLocation)
 {
     OldLocation = oldLocation;
     NewLocation = newLocation;
     Explorer    = explorer;
 }