// Build a spanning tree with the indicated root node. public static 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) { // Pick a random link. int 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; ScreenOrginizer.last = to_node; 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)); } } } }
private bool CheckWhetherValidMovement(MazeNode goingTo) { if (goingTo == null) { return(false); } else if (goingTo.Predecessor == playerCurrentLocation || goingTo == playerCurrentLocation.Predecessor) { return(true); } return(false); }
private void DrawSolution() { if (ScreenOrginizer.last != null) { MazeNode node = ScreenOrginizer.last; while (node.Predecessor != node) { node.Bounds.Fill = new SolidColorBrush(System.Windows.Media.Colors.Orange); node = node.Predecessor; } node.Bounds.Fill = new SolidColorBrush(System.Windows.Media.Colors.Yellow); } }
/// <summary> /// Main method for movement. Create a movement illusion for the player. Also verefying wheter the movement is valid. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnKeyDownHandler(object sender, KeyEventArgs e) { if (!hasFinished) { if (e.Key == Key.Left) { if (CheckWhetherValidMovement(playerCurrentLocation.Neighbors[MazeNode.West])) { playerCurrentLocation.Bounds.Fill = new SolidColorBrush(System.Windows.Media.Colors.White); playerCurrentLocation = playerCurrentLocation.Neighbors[MazeNode.West]; playerCurrentLocation.Bounds.Fill = new SolidColorBrush(System.Windows.Media.Colors.Orange); } } else if (e.Key == Key.Right) { if (CheckWhetherValidMovement(playerCurrentLocation.Neighbors[MazeNode.East])) { playerCurrentLocation.Bounds.Fill = new SolidColorBrush(System.Windows.Media.Colors.White); playerCurrentLocation = playerCurrentLocation.Neighbors[MazeNode.East]; playerCurrentLocation.Bounds.Fill = new SolidColorBrush(System.Windows.Media.Colors.Orange); } } else if (e.Key == Key.Up) { if (CheckWhetherValidMovement(playerCurrentLocation.Neighbors[MazeNode.North])) { playerCurrentLocation.Bounds.Fill = new SolidColorBrush(System.Windows.Media.Colors.White); playerCurrentLocation = playerCurrentLocation.Neighbors[MazeNode.North]; playerCurrentLocation.Bounds.Fill = new SolidColorBrush(System.Windows.Media.Colors.Orange); } } else if (e.Key == Key.Down) { if (CheckWhetherValidMovement(playerCurrentLocation.Neighbors[MazeNode.South])) { playerCurrentLocation.Bounds.Fill = new SolidColorBrush(System.Windows.Media.Colors.White); playerCurrentLocation = playerCurrentLocation.Neighbors[MazeNode.South]; playerCurrentLocation.Bounds.Fill = new SolidColorBrush(System.Windows.Media.Colors.Orange); } } EndPointArrival(); // Handle the win condition. } }
// Make the network of MazeNodes. private void MakeNodes() { // Make the nodes. nodes = new MazeNode[cols, rows]; for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { nodes[i, j] = new MazeNode(); } } Console.WriteLine(nodes); // Initialize the nodes' neighbors. for (int c = 0; c < cols; c++) { for (int r = 0; r < rows; r++) { if (r > 0) { nodes[c, r].Neighbors[MazeNode.North] = nodes[c, r - 1]; } if (r < rows - 1) { nodes[c, r].Neighbors[MazeNode.South] = nodes[c, r + 1]; } if (c > 0) { nodes[c, r].Neighbors[MazeNode.West] = nodes[c - 1, r]; } if (c < cols - 1) { nodes[c, r].Neighbors[MazeNode.East] = nodes[c + 1, r]; } } } MainWindow.playerCurrentLocation = nodes[0, 0]; }
public MazeLink(MazeNode from_node, MazeNode to_node) { FromNode = from_node; ToNode = to_node; }