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); }
private MazeNode GotoNode(MazeNode node, MazeNode.Direction from) { switch (from) { case MazeNode.Direction.Left: return(node.Right ? map[node.X + 1, node.Y] : null); case MazeNode.Direction.Right: return(node.Left ? map[node.X - 1, node.Y] : null); case MazeNode.Direction.Up: return(node.Down ? map[node.X, node.Y + 1] : null); case MazeNode.Direction.Down: return(node.Up ? map[node.X, node.Y - 1] : null); default: return(null); } }
public bool WiseGoto(MazeNode.Direction from) { if (Goto(from)) { return(true); } else { MazeNode user = map[userX, userY]; MazeNode.Direction direction1 = from.ROL(1); MazeNode.Direction direction2 = from.ROL(3); MazeNode node1 = GotoNode(user, direction1); MazeNode node2 = GotoNode(user, direction2); if (node1 != null) { if (node2 != null) { MazeNode node3 = GotoNode(node1, from); MazeNode node4 = GotoNode(node2, from); if (node3 != null && node4 == null) { return(Goto(direction1)); } if (node3 == null && node4 != null) { return(Goto(direction2)); } return(false); } else { return(Goto(direction1)); } } else { if (node2 != null) { return(Goto(direction2)); } else { return(false); } } } }
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; }
private void Draw(Graphics graphics, int x1, int y1, int x2, int y2) { graphics.Clear(this.BackColor); Pen pen = new Pen(this.ForeColor); SolidBrush brush = new SolidBrush(pen.Color); if (IsShowStartPoint && StartX >= x1 && StartX <= x2 && StartY >= y1 && StartY <= y2) { graphics.FillRectangle(new SolidBrush(this.StartPointColor), new Rectangle(StartX * weight, StartY * weight, weight, weight)); } if (IsShowEndPoint && EndX >= x1 && EndX <= x2 && EndY >= y1 && EndY <= y2) { graphics.FillRectangle(new SolidBrush(this.EndPointColor), new Rectangle(EndX * weight, EndY * weight, weight, weight)); } int wx0 = x1 * weight, wy0 = y1 * weight; for (int x = x1, wx = wx0; x <= x2; x++, wx += weight) { for (int y = y1, wy = wy0; y <= y2; y++, wy += weight) { MazeNode node = map[x, y]; Point point = new Point(wx, wy); if (node.Type == MazeNode.MazeNodeType.空白) { //if (node.Parent == null && node != map[rootX, rootY]) continue; if (!node.Up) { graphics.DrawLine(pen, point, new Point(point.X + weight, point.Y)); } if (!node.Left) { graphics.DrawLine(pen, point, new Point(point.X, point.Y + weight)); } } else { graphics.FillRectangle(brush, point.X, point.Y, weight, weight); } } } graphics.DrawLine(pen, new Point(0, height * weight), new Point(width * weight, height * weight)); graphics.DrawLine(pen, new Point(width * weight, 0), new Point(width * weight, height * weight)); }
public List <MazeNode> GetSurroundNodes(MazeNode node) { List <MazeNode> output = new List <MazeNode>(); if (node.X > 0) { MazeNode n = map[node.X - 1, node.Y]; if (n.Parent == null && n.Type == MazeNode.MazeNodeType.空白) { output.Add(n); } } if (node.Y > 0) { MazeNode n = map[node.X, node.Y - 1]; if (n.Parent == null && n.Type == MazeNode.MazeNodeType.空白) { output.Add(n); } } if (node.X < width - 1) { MazeNode n = map[node.X + 1, node.Y]; if (n.Parent == null && n.Type == MazeNode.MazeNodeType.空白) { output.Add(n); } } if (node.Y < height - 1) { MazeNode n = map[node.X, node.Y + 1]; if (n.Parent == null && n.Type == MazeNode.MazeNodeType.空白) { output.Add(n); } } return(output); }