// Display the maze in the TextBox. private void DisplayMazeText(MazeNode[,] nodes) { int hgt = nodes.GetLength(0); int wid = nodes.GetLength(1); string maze = ""; for (int y = 0; y < hgt; y++) { // mevcut hucre ve sağ duvar for (int x = 0; x < wid; x++) { MazeNode current = nodes[y, x]; maze += "0 "; var east = current.Neighbors[MazeNode.East]; if (east == null) { continue; } if (east.Predecessor == current || east == current.Predecessor) { maze += "0 "; } else { maze += "1 "; } // maze += current.ToString() + " "; } maze += Environment.NewLine; // altduvar for (int x = 0; x < wid; x++) { MazeNode current = nodes[y, x]; var south = current.Neighbors[MazeNode.South]; if (south == null) { continue; } if (south.Predecessor == current || south == current.Predecessor) { maze += "0 1 "; } else { maze += "1 1 "; } // maze += current.ToString() + " "; } maze = maze.Remove(maze.Length - 2); maze += Environment.NewLine; } txtMaze.Text = maze; }
// Build a spanning tree with the indicated root node. private void FindSpanningTree(MazeNode root) { Random rand = new Random(); // Set the root node's predecessor so we know it's in the tree. root.Predecessor = root; // Make a list of candidate links. List <MazeLink> links = new List <MazeLink>(); // Add the root's links to the links list. foreach (MazeNode neighbor in root.Neighbors) { if (neighbor != null) { links.Add(new MazeLink(root, neighbor)); } } // Add the other nodes to the tree. while (links.Count > 0) { int link_num; // Pick a random link. link_num = rand.Next(0, links.Count); MazeLink link = links[link_num]; links.RemoveAt(link_num); // Add this link to the tree. MazeNode to_node = link.ToNode; link.ToNode.Predecessor = link.FromNode; // Remove any links from the list that point // to nodes that are already in the tree. // (That will be the newly added node.) for (int i = links.Count - 1; i >= 0; i--) { if (links[i].ToNode.Predecessor != null) { links.RemoveAt(i); } } // Add to_node's links to the links list. foreach (MazeNode neighbor in to_node.Neighbors) { if ((neighbor != null) && (neighbor.Predecessor == null)) { links.Add(new MazeLink(to_node, neighbor)); } } } }
// Make the network of MazeNodes. private MazeNode[,] MakeNodes(int wid, int hgt) { // Make the nodes. MazeNode[,] nodes = new MazeNode[hgt, wid]; for (int r = 0; r < hgt; r++) { int y = Ymin + CellHgt * r; for (int c = 0; c < wid; c++) { int x = Xmin + CellWid * c; nodes[r, c] = new MazeNode( x, y, CellWid, CellHgt); } } // Initialize the nodes' neighbors. for (int r = 0; r < hgt; r++) { for (int c = 0; c < wid; c++) { if (r > 0) { nodes[r, c].Neighbors[MazeNode.North] = nodes[r - 1, c]; } if (r < hgt - 1) { nodes[r, c].Neighbors[MazeNode.South] = nodes[r + 1, c]; } if (c > 0) { nodes[r, c].Neighbors[MazeNode.West] = nodes[r, c - 1]; } if (c < wid - 1) { nodes[r, c].Neighbors[MazeNode.East] = nodes[r, c + 1]; } } } // Return the nodes. return(nodes); }
public MazeLink(MazeNode from_node, MazeNode to_node) { FromNode = from_node; ToNode = to_node; }