Ejemplo n.º 1
0
        private void DiscernPath(DiscernPathResult result, MazeNode parent, MazeNode node, MazeNode.Direction direction, int ability, int deep)
        {
            result.Rectangle = result.Rectangle.Union(new Rectangle(node.X, node.Y, 1, 1));
            result.Paths.Add(node);
            foreach (MazeNode next in node.GetEnumeratorExclude(parent))
            {
                int ability2 = ability;
                MazeNode.Direction direction2 = next.ToNodeDirection(node);
                if (direction != direction2)                     // 非直线
                {
                    if (next.IsOnlyOnePath(direction2))          // 无分支
                    {
                        ability2++;
                    }
                    else
                    {
                        ability2 += deep;
                    }
                }
                else
                {
                    if (!next.IsOnlyOnePath(direction2))
                    {
                        ability2++;
                    }
                }

                if (ability2 <= DiscernPathAbility)
                {
                    DiscernPath(result, node, next, direction2, ability2, deep + 1);
                }
            }
        }
Ejemplo n.º 2
0
 public void Answer(int x, int y)
 {
     userPath    = Answer(map[StartX, StartY], map[x, y]);
     userX       = x;
     userY       = y;
     result      = DiscernPath();
     userChanged = true;
 }
Ejemplo n.º 3
0
        public bool Goto(MazeNode.Direction from)
        {
            List <Point> eventArgs = null;

            if (this.Changed != null)
            {
                eventArgs = new List <Point>()
                {
                    new Point(userX, userY)
                }
            }
            ;

            MazeNode temp = GotoNode(map[userX, userY], from);

            if (temp != null)
            {
                userX = temp.X;
                userY = temp.Y;
            }
            else
            {
                return(false);
            }

            MazeNode next;

            do
            {
                next = map[userX, userY];
                if (userPath.Count >= 2 && userPath[userPath.Count - 2] == next)
                {
                    userPath.RemoveAt(userPath.Count - 1);
                }
                else
                {
                    userPath.Add(next);
                }

                if (userX == EndX && userY == EndY && this.Completed != null)
                {
                    this.Completed();
                }

                if (this.Changed != null)
                {
                    eventArgs.Add(new Point(userX, userY));
                }
            } while (IsFindBranch && next.OnlyOnePath(ref userX, ref userY, ref from));

            userChanged = true;
            result      = DiscernPath();
            if (this.Changed != null)
            {
                this.Changed(new MazeEventArgs(eventArgs));
            }
            return(true);
        }
Ejemplo n.º 4
0
 public void Reset()
 {
     userPath.Clear();
     userPath.Add(map[StartX, StartY]);
     userX       = StartX;
     userY       = StartY;
     result      = DiscernPath();
     userChanged = true;
 }
Ejemplo n.º 5
0
        public bool ToDiscernNode(int x, int y)
        {
            if (result != null && result.Paths.Contains(map[x, y]))
            {
                List <MazeNode> path = Answer(map[userX, userY], map[x, y]);
                if (this.Changed != null && path.Count >= 2)
                {
                    this.Changed(new MazeEventArgs(path.Select(node => new Point(node.X, node.Y)).ToList()));
                }
                int removeCount = 0;
                foreach (MazeNode node in path)
                {
                    if (userPath.Count > removeCount)
                    {
                        if (userPath[userPath.Count - 1 - removeCount] == node)
                        {
                            removeCount++;
                        }
                        else
                        {
                            userPath.RemoveRange(userPath.Count - removeCount, removeCount);
                            path.RemoveRange(0, removeCount - 1);
                            userPath.AddRange(path);
                            goto Result;
                        }
                    }
                    else
                    {
                        path.RemoveRange(0, userPath.Count - 1);
                        userPath.Clear();
                        userPath.AddRange(path);
                        goto Result;
                    }
                }
                userPath.RemoveRange(userPath.Count - removeCount + 1, removeCount - 1);
Result:
                userX       = x;
                userY       = y;
                result      = DiscernPath();
                userChanged = true;

                if (userX == EndX && userY == EndY && this.Completed != null)
                {
                    this.Completed();
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 6
0
        private DiscernPathResult DiscernPath()
        {
            MazeNode          userNode = map[userX, userY];
            DiscernPathResult result   = new DiscernPathResult(new HashSet <MazeNode>()
            {
                userNode
            }, new Rectangle(userX, userY, 1, 1));

            foreach (MazeNode node in userNode)
            {
                DiscernPath(result, userNode, node, node.ToNodeDirection(userNode), 0, 1);
            }
            return(result);
        }
Ejemplo n.º 7
0
        public void Create(int rootX, int rootY, bool setSeed = true)
        {
            this.rootX = rootX;
            this.rootY = rootY;

            Resize();

            if (setSeed)
            {
                this.SetRandomSeed();
            }

            DisorderList <MazeNode> tempNodes = new DisorderList <MazeNode>(width * height);             // 已记录节点
            MazeNode header = new MazeNode(-1, -1);
            MazeNode root   = map[RootX, RootY];

            header.AddChild(root);
            tempNodes.Add(root);
            double count = height;

            while (tempNodes.Count > 0)
            {
                int             index      = (int)(random.NextDouble(randomMapper) * tempNodes.Count);
                MazeNode        randomNode = tempNodes[index];
                List <MazeNode> childs     = GetSurroundNodes(randomNode);
                if (childs.Count == 0)
                {
                    tempNodes.RemoveAt(index);
                }
                else
                {
                    MazeNode next = childs.Random();
                    randomNode.AddChild(next);
                    tempNodes.Add(next);
                }
            }

            header.RemoveChild(root);
            userPath.Clear();
            userPath.Add(map[StartX, StartY]);
            userX       = StartX;
            userY       = StartY;
            result      = DiscernPath();
            userChanged = true;
            reupdate    = true;
            IsCreated   = true;
        }