当前位置: 首页>>代码示例>>C#>>正文


C# Square.RemoveSide方法代码示例

本文整理汇总了C#中Square.RemoveSide方法的典型用法代码示例。如果您正苦于以下问题:C# Square.RemoveSide方法的具体用法?C# Square.RemoveSide怎么用?C# Square.RemoveSide使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Square的用法示例。


在下文中一共展示了Square.RemoveSide方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BuildMaze

        public void BuildMaze()
        {
            // pick a random square
            int c = 0;
            int r = random.Next(maze.Height);
            Square startingSquare = maze.SquareAt(r, c);

            // set current square to starting square
            currentSquare = startingSquare;

            // remove LEFT wall of current square
            currentSquare.RemoveSide(Square.Direction.LEFT);

            // set last side to left
            lastSide = Square.Direction.LEFT;

            // while there remains any blocked square in the grid
            while (maze.IsAnyBlocked()) {
                // set bool addingSquare to false
                addingSquareFlag = false;

                // create a random sequence of the 0, 1, 2, or 3 remaining walls for currentSquare
                // exclude sides that lead to unblocked squares (squares that are already in the maze)
                directions.Clear();
                if (currentSquare.HasSide(Square.Direction.LEFT)) {
                    if (currentSquare.AdjacentSquare(Square.Direction.LEFT) != null &&
                        currentSquare.AdjacentSquare(Square.Direction.LEFT).IsBlocked()) {
                        directions.Add(Square.Direction.LEFT);
                    }
                }

                if (currentSquare.HasSide(Square.Direction.RIGHT)) {
                    if (currentSquare.AdjacentSquare(Square.Direction.RIGHT) != null &&
                        currentSquare.AdjacentSquare(Square.Direction.RIGHT).IsBlocked()) {
                        directions.Add(Square.Direction.RIGHT);
                    }
                }

                if (currentSquare.HasSide(Square.Direction.TOP)) {
                    if (currentSquare.AdjacentSquare(Square.Direction.TOP) != null &&
                        currentSquare.AdjacentSquare(Square.Direction.TOP).IsBlocked()) {
                        directions.Add(Square.Direction.TOP);
                    }
                }

                if (currentSquare.HasSide(Square.Direction.BOTTOM)) {
                    if (currentSquare.AdjacentSquare(Square.Direction.BOTTOM) != null &&
                        currentSquare.AdjacentSquare(Square.Direction.BOTTOM).IsBlocked()) {
                        directions.Add(Square.Direction.BOTTOM);
                    }
                }

                // exclude lastSide
                directions.Remove(lastSide);

                // shuffle them
                Shuffle(directions);

                foreach (Square.Direction dir in directions) {
                    // let nextSide be the current element in this sequence
                    Square.Direction nextSide = dir;

                    // if square adjacent to the current square on side nextSide exists and is blocked
                    if (currentSquare.AdjacentSquare(nextSide) != null && currentSquare.AdjacentSquare(nextSide).IsBlocked()) {
                        // remove walls that separate currentSquare and adjacentSquare
                        Square adjacent = currentSquare.AdjacentSquare(nextSide);
                        currentSquare.RemoveSide(nextSide);
                        adjacent.RemoveSide(Square.OppositeSide(nextSide));

                        // add path to adjacency matrix
                        int u = ConvertToNode(currentSquare.Row, currentSquare.Column);
                        int v = ConvertToNode(adjacent.Row, adjacent.Column);
                        adjacentMatrix[u, v] = true;
                        adjacentMatrix[v, u] = true;

                        // adjacentSquare becomes currentSquare
                        currentSquare = adjacent;

                        // set boolean addingSquareFlag to true
                        addingSquareFlag = true;

                        // set lastSide to the opposite of nextSide
                        lastSide = Square.OppositeSide(nextSide);
                        break;
                    }
                }

                // this means we couldn't find any adjacent square to move to in the for loop above
                // we now have to backtrack by picking a random unblocked square elsewhere in the maze
                // and try to build a path from that point
                if (!addingSquareFlag) {
                    // pick an unblocked square (already in the maze) from the grid at
                    // random, optionally ensuring that it is adjacent to at least one
                    // blocked square (not already in the maze)
                    // this becomes the current square
                    currentSquare = maze.SelectRandomUnblockedSquare();
                    if (currentSquare == null)
                        break;

                    // randomly pick an open side (one with wall already removed) of the
//.........这里部分代码省略.........
开发者ID:channguyen,项目名称:maze-builder-windows-form,代码行数:101,代码来源:WallMaze.cs


注:本文中的Square.RemoveSide方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。