public IPathFindingAlgorithm GetPathFindingAlgorithm(PathFindingAlgorithms pathFindingAlgorithm) { switch (pathFindingAlgorithm) { case PathFindingAlgorithms.AStar: return(new AStarPathFindingAlgorithm()); case PathFindingAlgorithms.None: default: return(null); } }
/// <summary> /// This method calls to search of the optimal path for the Path Finding algorithms. Currently there is only 1. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_Find_Click(object sender, EventArgs e) { // Do few check beforehand // 1. Does the start and end exist (they can only exist if the maze exists, so we do not need to check for it additionally)? if ((this.current_key[0] == 0 && this.current_key[1] == 0) || (this.current_gate[0] == 0 && this.current_gate[1] == 0)) { return; } // 2. Now we do need to update the template through which the search will be done. As it needs to see new start and end points String[][] rooms_Path = TemplateConverter.StartFinishAdder(given_Template: this.rooms_template_redux, key: this.current_key, gate: this.current_gate); PathFindingAlgorithms pathFindingAlgorithms = new PathFindingAlgorithms(); // Find directions we need to go through to reach our destination point String answer_Path = pathFindingAlgorithms.BreadthFirstSearch(maze: rooms_Path, key: this.current_key, gate: this.current_gate); // Check if the path exists, and if so, draw it. Otherwise, let us know that it does not exist. if (answer_Path == "There is no answer!") { lbl_Status.Text = "Status: No Path"; return; } List <int> x_coordList = pathFindingAlgorithms.DrawPath_X(path: answer_Path, key: this.current_key); List <int> y_coordList = pathFindingAlgorithms.DrawPath_Y(path: answer_Path, key: this.current_key); // Let's use the green brush to draw over our fields Cell cell = new Cell(); MainField field = new MainField(); Graphics g = this.CreateGraphics(); Brush myAquamarineBrush = new SolidBrush(color: Color.Aquamarine); for (int i = 0; i < x_coordList.Count - 1; i++) { int x_FieldPosition = field.x + cell.width * x_coordList[i]; int y_FieldPosition = field.y + cell.height * y_coordList[i]; g.FillRectangle(brush: myAquamarineBrush, x: x_FieldPosition, y: y_FieldPosition, width: cell.width, height: cell.height); } lbl_Status.Text = "Status: Solved"; }