public Node(int x, int y, Node parent) { this.x = x; this.y = y; this.parent = parent; manhattenDistanceToGoal = NOT_CALCULATED; childNodes = null; }
public void removeChildNode(Node childNode) { if (childNodes == null) { Console.WriteLine("removeChildNode => childNodes is empty."); } childNodes.Remove(childNode); }
public void Launch(Node selectedNode, MacroExpander macros) { ProcessStartInfo info = new ProcessStartInfo(); macros.Target = selectedNode as File; info.FileName = Program; info.Arguments = macros.Expand(Arguments); Fork(info); }
public virtual void Add(Node node) { if(!Contents.Contains(node)) { if(node is OtherFile) node = new File(node as OtherFile); else if(node is OtherFilesFolder) node = new Folder(node as OtherFilesFolder); AddCore(node); } }
//size == dimension * dimension public RandomMaze(int dimension, int seed) { //atleast be half dimension apart startAndDestDist = dimension; astar = new AstarSearch(); this.gridNeedToFill = (int)(dimension * dimension * roadPercentage); this.dimension=dimension; this.seed = seed; random = new Random(seed); random.Next(Int32.MaxValue); maze = new float[dimension,dimension]; astar.initailize2DArrayToValue(maze, WALL); //setStartPointAndDestPoint(dimension); startPoint = null; destPoint = null; }
public bool isEqual(Node n) { if (n.x == x && n.y == y) return true; else return false; }
public Node generatePossibleDestPoint(List<Node> edgePoints, Node startPoint) { Node destPoint = null; Node tempDestPoint = null; if (edgePoints.Count == 0 || edgePoints == null) { //Console.WriteLine("generatePossibleDestPoint => no edge points can be chosen."); return destPoint; } while (true) { if (edgePoints.Count == 0 || edgePoints == null) { break; //Console.WriteLine("generatePossibleDestPoint => edgePoints is empty"); } tempDestPoint = edgePoints.ElementAt(randomIndex(edgePoints.Count)); edgePoints.Remove(tempDestPoint); if (Math.Sqrt(Math.Pow(Math.Abs(startPoint.x - tempDestPoint.x),2)+ Math.Pow(Math.Abs(startPoint.y - tempDestPoint.y),2)) >= startAndDestDist) { destPoint = tempDestPoint; break; } } return destPoint; }
public void setStartPointAndDestPoint() { //List<Node> edgePoints; List<Node> path = null; edgePoints = getEdgePoints(dimension); List<Node> edgePointsTemp = new List<Node>(); while (path==null) { destPoint = null; if (edgePoints.Count == 0) { break; } //this 'if' statement can be deleted . the logic flaw in here is that //if many seeds does not have a path and due to edgepoint.Count == 0 , it will exit //this loop before setting the previous start and end point back to WALL. if (startPoint != null && destPoint != null) { //reset to wall maze[startPoint.y, startPoint.x] = WALL; maze[destPoint.y, destPoint.x] = WALL; } startPoint = edgePoints.ElementAt(randomIndex(edgePoints.Count)); edgePoints.Remove(startPoint); //while (true) //{ //edgePoints.Clear(); //edgePointsTemp.AddRange(edgePoints); destPoint = generatePossibleDestPoint(edgePoints, startPoint); if (destPoint == null) { return; } edgePointsTemp.Remove(destPoint); maze[startPoint.y, startPoint.x] = ROAD; maze[destPoint.y, destPoint.x] = ROAD; path=astar.FindPath(astar.Float2DtoInt(maze), (int)WALL, startPoint.x, startPoint.y, destPoint.x, destPoint.y); if (path == null) { maze[startPoint.y, startPoint.x] = WALL; maze[destPoint.y, destPoint.x] = WALL; } //Console.WriteLine("find possible start end."); //} } if (path == null) { //Console.WriteLine("maze has no start and dest point abort program "); //System.Environment.Exit(1); return; } //set path startX = startPoint.x; startY = startPoint.y; destX = destPoint.x; destY = destPoint.y; }
private void StartFile(Node node) { if(node is File) (node as File).Start(); else if (node is OtherFile) (node as OtherFile).Start(); else if (node is OtherFilesFolder) (node as OtherFilesFolder).Start(); }
//assign a random length then pop random node from queue ? public void GenerateMaze() { List<Node> roads = new List<Node>(); List<Node> fourNeighbors; //generae maze start at 1,1 Node currentPoint = new Node(1, 1); Node tempNode = null; //Node tempCurrentPoint = null; maze[currentPoint.y, currentPoint.x]=ROAD; byte[,] visited = new byte[dimension,dimension]; astar.initailize2DArrayToValue(visited,NOT_VISITED); while(true) { visited[currentPoint.y, currentPoint.x] = VISITED; //Console.WriteLine(currentPoint.x + ", " + currentPoint.y+" road len =>"+roads.Count); if (currentPoint.childNodes == null) { //Console.WriteLine("seek children"); fourNeighbors = getFourNeighbors2(currentPoint.x, currentPoint.y, dimension, maze,visited); currentPoint.childNodes = fourNeighbors; } if (currentPoint.childNodes == null ||currentPoint.childNodes.Count==0) { //Console.WriteLine("no valid children"); if (roads.Count <= 1) { break; } tempNode = currentPoint; //dont need to remove current in this position is not been added yet. roads.Remove(currentPoint); currentPoint = roads.Last(); //currentPoint.childNodes.Remove(tempNode); tempNode.freeNode(); continue; } maze[currentPoint.y, currentPoint.x] = ROAD; //Console.WriteLine("remove current Point children"); tempNode = currentPoint.childNodes.ElementAt(randomIndex(currentPoint.childNodes.Count)); currentPoint.childNodes.Remove(tempNode); //Console.WriteLine("add current Point"); roads.Add(currentPoint); currentPoint = tempNode; } //byte[,] visitedPositions= new byte[dimension,dimension]; //astar.initailize2DArrayToValue(visitedPositions,NOT_VISITED); }
public void freeNode() { parent = null; childNodes = null; }
public bool Contains(Node node) { bool contains = false; if(this.Equals(node)) contains = true; else if(node != null) contains = Contains(node.Parent); return contains; }
public void setStartPointAndDestPoint(int dimension) { //List<Node> edgePoints; edgePoints = getEdgePoints(dimension); startPoint = null; destPoint = null; startPoint = edgePoints.ElementAt(random.Next() % edgePoints.Count); Console.WriteLine("startPoint => " + startPoint); edgePoints.Remove(startPoint); destPoint=generatePossibleDestPoint(edgePoints,startPoint); Console.WriteLine("destPoint => " + destPoint); startX = startPoint.x; startY = startPoint.y; destX = destPoint.x; destY = destPoint.y; }
public void Remove(Node node) { node.Changed -= OnContentsChanged; node.Parent = null; Contents.Remove(node); OnPropertyChanged(new DependencyPropertyChangedEventArgs( ContentsProperty, Contents, Contents)); }
//after every dumbTimes it will make one right movement closer to Dest public List<Node> generateDumbPathToDestPoint(int startX,int startY,int destX,int destY,int dumbTimes,int smartTimes) { float[,] visited = new float[dimension, dimension]; astar.initailize2DArrayToValue(maze, WALL); Node tempNode = null; Node tempCurrentPoint =null; Node currentPoint = new Node(startX,startY); List<Node> dumbPath = new List<Node>(); List<Node> fourNeighbors = new List<Node>(); int currentDumbTimes = dumbTimes; int currentSmartTimes = 0; while(true) { //visited[currentPoint.y, currentPoint.x] = VISITED; //road == visited visited[currentPoint.y, currentPoint.x] = ROAD; fourNeighbors = getFourNeighbors(currentPoint.x,currentPoint.y,dimension,maze); //need to go to previous node .if no neibours if(currentPoint.x==destX&¤tPoint.y==destY) { //Console.WriteLine("generateDumbPathToDestPoint => found dest"); dumbPath.Insert(0, currentPoint); break; } if(fourNeighbors==null) { // Console.WriteLine("generateDumbPathToDestPoint => neighbors null"); if(dumbPath.Count==0) { // Console.WriteLine("generateDumbPathToDestPoint => dumbPath 0"); break; } tempCurrentPoint = currentPoint; currentPoint=dumbPath.First(); dumbPath.Remove(currentPoint); tempCurrentPoint.freeNode(); continue; } astar.FillManhattenDistance(fourNeighbors, destX, destY); //addind dumb path if(currentSmartTimes==0&¤tDumbTimes==0) { currentSmartTimes = smartTimes; currentDumbTimes=dumbTimes; } if (currentSmartTimes != 0) { currentSmartTimes--; currentPoint.childNodes = fourNeighbors; dumbPath.Insert(0, currentPoint); tempNode = currentPoint.LeastManhattenChildNode(); currentPoint.childNodes.Remove(tempNode); currentPoint = tempNode; if (currentSmartTimes == 0) { currentDumbTimes = dumbTimes; } continue; } if(currentDumbTimes!=0) { currentDumbTimes--; currentPoint.childNodes = fourNeighbors; dumbPath.Insert(0,currentPoint); tempNode = currentPoint.childNodes.ElementAt(randomIndex(currentPoint.childNodes.Count)); currentPoint.childNodes.Remove(tempNode); currentPoint = tempNode; if (currentDumbTimes == 0) { currentSmartTimes = smartTimes; } continue; } } if(dumbPath.Count==0||dumbPath==null) { //Console.WriteLine(" generateDumbPathToDestPoint => fail to find dumb path to goal point. "); } return dumbPath; }
float heuristic(Node n1, Node n2) { return Math.Abs(n1.x - n2.x) + Math.Abs(n1.y - n2.y); }
//This is the function that the enemy class could call to get the path to the player List<Node> getPathBetween(Node start, Node end) { //Reset f, g, h and parent of each node foreach (Node node in nodes) { node.f = node.g = node.h = 0; node.parent = -1; } List<Node> path = new List<Node>(); //Final path of nodes List<Node> openList = new List<Node>(); //Open list to be evaluated List<Node> closedList = new List<Node>(); //Closed list of evaluated nodes openList.Add(start); while (openList.Count > 0) { //Get node in openList with the lowest f value float fLow = 1000; int iLow = -1; foreach (Node pnode in openList) { if (pnode.f < fLow) { fLow = pnode.f; iLow = openList.IndexOf(pnode); } } Node currentNode = openList[iLow]; //Is the current node the end node? if (currentNode.isEqual(end)) { path.Add(currentNode); while (currentNode.parent != -1) { currentNode = nodes[currentNode.parent]; path.Add(currentNode); } return path; } openList.Remove(currentNode); closedList.Add(currentNode); List<int> nodeNeighbours = neighbours[nodes.IndexOf(currentNode)]; foreach (int i in nodeNeighbours) { if (closedList.Contains(nodes[i])) continue; float gScore = currentNode.g + 1; if (!openList.Contains(nodes[i])) //neighbour not visited { openList.Add(nodes[i]); nodes[i].h = heuristic(nodes[i], end); nodes[i].g = -1; } //If this iteration has a better gScore or neighbour has not been visited, update its f, g and parent values. if (gScore < nodes[i].g || nodes[i].g == -1) { nodes[i].parent = nodes.IndexOf(currentNode); nodes[i].g = gScore; nodes[i].f = nodes[i].g + nodes[i].h; } } } return path; }
protected void AddCore(Node node) { if(node.Parent != null) { node.Parent.Remove(node); } node.SetProject(Project); node.Changed += OnContentsChanged; Contents.InsertSorted(node, CompareNodes); node.Parent = this; OnPropertyChanged(new DependencyPropertyChangedEventArgs( ContentsProperty, Contents, Contents)); }
private int CompareNodes(Node left, Node right) { if (left is OtherFilesFolder && right is OtherFile) return -1; else if (left is OtherFilesFolder && !(right is OtherFilesFolder)) return 1; else if (right is OtherFilesFolder && left is OtherFile) return 1; else if (right is OtherFilesFolder && !(left is OtherFilesFolder)) return -1; else if (left is Folder && !(right is Folder)) return -1; else if (left is File && !(right is File)) return 1; else return left.Name.CompareTo(right.Name); }
public override void Add(Node node) { AddCore(node); }
public void setStartPointAndDestPoint() { //List<Node> edgePoints; List<Node> path = null; edgePoints = getEdgePoints(dimension); List<Node> edgePointsTemp = new List<Node>(); while (path==null) { destPoint = null; if (edgePoints.Count == 0) { break; } if (startPoint != null && destPoint != null) { //reset to wall maze[startPoint.y, startPoint.x] = WALL; maze[destPoint.y, destPoint.x] = WALL; } startPoint = edgePoints.ElementAt(randomIndex(edgePoints.Count)); edgePoints.Remove(startPoint); //while (true) //{ //edgePoints.Clear(); //edgePointsTemp.AddRange(edgePoints); destPoint = generatePossibleDestPoint(edgePoints, startPoint); if (destPoint == null) { return; } edgePointsTemp.Remove(destPoint); maze[startPoint.y, startPoint.x] = ROAD; maze[destPoint.y, destPoint.x] = ROAD; path=astar.FindPath(astar.Float2DtoInt(maze), (int)WALL, startPoint.x, startPoint.y, destPoint.x, destPoint.y); //Console.WriteLine("find possible start end."); //} } if (path == null) { //Console.WriteLine("maze has no start and dest point abort program "); //System.Environment.Exit(1); return; } //set path startX = startPoint.x; startY = startPoint.y; destX = destPoint.x; destY = destPoint.y; }