/// <summary> /// /// /// Function for Getting Possible nodes and visiting the next node once every time it is called /// this functions also updates the values for the GUI panel /// function is recursive till the path to end point is not found /// /// </summary> /// <param name="Cells"></param> /// <param name="CurrentNode"></param> /// <param name="colsize"></param> /// <param name="rowsize"></param> public void A_FindPath(Activity_1.Cell[,] Cells, Activity_1.Cell CurrentNode, int colsize, int rowsize) { //collect possible nodes List <Activity_1.Cell> PossibleNodes = PathFinder.PossibleNodes(CurrentNode, colsize, rowsize, Cells); //assign cost to each possible node for current node PossibleNodes = PathFinder.CostOfOpenNodes(PossibleNodes, StartPos, EndPos, PathType); foreach (Activity_1.Cell Node in PossibleNodes) { bool existing = false; //loop to check if the new possible nodes already exists in the open node list or not if (Node.visited == false && !ClosedNodes.Contains(Node) && !OpenNodes.Contains(Node)) { OpenNodes.Add(Node); ChangeColor(Node.CellPos.x, Node.CellPos.y, Color.Blue); } } //mark current node as visited //add the node to closed node list //remove the node from open node list //The least Expensive Node will be Selected CurrentNode.visited = true; ClosedNodes.Add(CurrentNode); OpenNodes.Remove(CurrentNode); ChangeColor(CurrentNode.CellPos.x, CurrentNode.CellPos.y, Color.Red); Cells[CurrentNode.CellPos.x, CurrentNode.CellPos.y] = CurrentNode; //Select next node wiht lowest cost from the open node list CurrentNode = PathFinder.SelectNextNode(OpenNodes); CurrentCell = CurrentNode; //show the path when end point is found if (CurrentCell.CellPos.x == EndPos.x && CurrentCell.CellPos.y == EndPos.y) { ShowPath(CurrentNode, ClosedNodes); pathFound = true; MessageBox.Show("Path Found"); return; } else { Thread.Sleep(200); FindPath(); } }
//fucntion Responsible for Initialising a grid for the algorithm and greate a 2D matrix to store the value public void CreateGrid(int row, int col) { OpenNodes = new List <Activity_1.Cell>(); ClosedNodes = new HashSet <Activity_1.Cell>(); Path = new List <Activity_1.Cell>(); activity_1.columnsize = col; activity_1.rowsize = row; Cells = new Activity_1.Cell[row, col];//initialise the the cell in 2D matrix with the input row and col size for (int x = 0; x < row; x++) { for (int y = 0; y < col; y++) { Cells[x, y] = new Activity_1.Cell(x, y, Activity_1.Box.W0);//initiates a matrix to store the values with all blocks as walkable as default until user make changes with input } } }
/// <summary> /// /// Function finds the shortest path that reaches the /// /// Function is responsible to Display the shortest path been created using the A* algorithm in the GUI Format using the Visited Node list /// /// </summary> /// <param name="CurrentNode"></param> /// <param name="ClosedNodes"></param> public void ShowPath(Activity_1.Cell CurrentNode, HashSet <Activity_1.Cell> ClosedNodes) { PrevNode = CurrentNode.Parent; Path.Add(PrevNode); foreach (Activity_1.Cell Node in ClosedNodes) { if (PrevNode.CellPos.x == Node.CellPos.x && PrevNode.CellPos.y == Node.CellPos.y) { CurrentNode = Node; break; } } if (PrevNode.CellPos.x == StartPos.x && PrevNode.CellPos.y == StartPos.y) { foreach (Activity_1.Cell Node in Path) { ChangeColor(Node.CellPos.x, Node.CellPos.y, Color.Green); } } else { ShowPath(CurrentNode, ClosedNodes); } }
private void panel1_MouseMove(object sender, MouseEventArgs e) { int mousePoseX = (int)((e.X / 10) - ((e.X / 10) * 0.1f)); int mousePoseY = (int)((e.Y / 10) - ((e.Y / 10) * 0.1f)); if (MouseIsDown) { if (mousePoseX >= 0 && mousePoseX < row && mousePoseY >= 0 && mousePoseY < col) { if (CurrentOption == Activity_1.Box.S) { ChangeColor(mousePoseX, mousePoseY, Color.AntiqueWhite); Cells[mousePoseX, mousePoseY].box = new Activity_1.Box(); Cells[mousePoseX, mousePoseY].box = Activity_1.Box.S; CurrentCell = new Activity_1.Cell(mousePoseX, mousePoseY, Activity_1.Box.S); StartPos.x = mousePoseX; StartPos.y = mousePoseY; } else if (CurrentOption == Activity_1.Box.E) { ChangeColor(mousePoseX, mousePoseY, Color.Red); Cells[mousePoseX, mousePoseY].box = Activity_1.Box.E; EndPos.x = mousePoseX; EndPos.y = mousePoseY; } else if (CurrentOption == Activity_1.Box.O) { ChangeColor(mousePoseX, mousePoseY, Color.SandyBrown); Cells[mousePoseX, mousePoseY].box = Activity_1.Box.O; } else if (CurrentOption == Activity_1.Box.W) { ChangeColor(mousePoseX, mousePoseY, Color.Salmon); Cells[mousePoseX, mousePoseY].box = Activity_1.Box.W; } else if (CurrentOption == Activity_1.Box.W0) { ChangeColor(mousePoseX, mousePoseY, Color.BurlyWood); Cells[mousePoseX, mousePoseY].box = Activity_1.Box.W0; } else if (CurrentOption == Activity_1.Box.W90) { ChangeColor(mousePoseX, mousePoseY, Color.DarkSlateGray); Cells[mousePoseX, mousePoseY].box = Activity_1.Box.W90; } else if (CurrentOption == Activity_1.Box.W120) { ChangeColor(mousePoseX, mousePoseY, Color.GreenYellow); Cells[mousePoseX, mousePoseY].box = Activity_1.Box.W120; } else if (CurrentOption == Activity_1.Box.Wg) { ChangeColor(mousePoseX, mousePoseY, Color.LawnGreen); Cells[mousePoseX, mousePoseY].box = Activity_1.Box.Wg; } else if (CurrentOption == Activity_1.Box.Wr) { ChangeColor(mousePoseX, mousePoseY, Color.Gray); Cells[mousePoseX, mousePoseY].box = Activity_1.Box.Wr; } else if (CurrentOption == Activity_1.Box.Ww) { ChangeColor(mousePoseX, mousePoseY, Color.Azure); Cells[mousePoseX, mousePoseY].box = Activity_1.Box.Ww; } } } }