Exemplo n.º 1
0
 private void stepBtn_Click(object sender, EventArgs e)
 {
     if (statusTb.Text != string.Empty && statusTb.Text != "Cancelled")
     {
         if (MessageBox.Show("Algorithm has been finished.Wish to start over ? ", "Start",
                             MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
         {
             Reset();
         }
         else
         {
             return;
         }
     }
     else if (statusTb.Text == "Cancelled")
     {
         Reset();
     }
     if (fullexec)
     {
         Reset();
     }
     step++;
     bystep = true;
     if (firststep)
     {
         MetroTextBox test = null;
         if (puzzlemode == 8)
         {
             test = testTb;
         }
         if (puzzlemode == 15)
         {
             test = testTb2;
         }
         GetPuzzle(puzzle8);
         GetPuzzle(puzzle15);
         if (test.BackColor == Color.FromArgb(0, 177, 224))
         {
             List <int> puzzle = new List <int>();
             if (puzzleTc.SelectedIndex == 0)
             {
                 puzzle = new List <int>(puzzle8);
             }
             if (puzzleTc.SelectedIndex == 1)
             {
                 puzzle = new List <int>(puzzle15);
             }
             initNode = new Node(puzzle.ToArray());
             search   = new Search();
             GetSolution();
             firststep = false;
         }
         else
         {
             if (MessageBox.Show("Puzzle is not solvable. Continue?", "Start",
                                 MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
             {
                 List <int> puzzle = new List <int>();
                 if (puzzleTc.SelectedIndex == 0)
                 {
                     puzzle = new List <int>(puzzle8);
                 }
                 if (puzzleTc.SelectedIndex == 1)
                 {
                     puzzle = new List <int>(puzzle15);
                 }
                 initNode = new Node(puzzle.ToArray());
                 search   = new Search();
                 GetSolution();
                 firststep = false;
             }
         }
     }
     else
     {
         GetSolution();
     }
     try
     {
         if (solution.Count > 0)
         {
             solution.Reverse();
             workTb.Clear();
             for (int i = 0; i < solution.Count; i++)
             {
                 solution[i].PrintPuzzle(workTb);
             }
             info           = search.GetInfo();
             statesTb.Text  = info[0].ToString();
             ustatesTb.Text = info[1].ToString();
             dstatesTb.Text = info[2].ToString();
             depthTb.Text   = info[3].ToString();
             countChart.Series[0].Points.Clear();
             infoCount.Clear();
             infoCount.Add("Generated states", info[0]);
             infoCount.Add("Added states", info[1]);
             infoCount.Add("Discarded states", info[2]);
             countChart.Series[0].Points.DataBindXY(infoCount.Keys, infoCount.Values);
             dependCount = search.GetDepend();
             if (dependCount.Count > 3)
             {
                 optimalDepth.Clear();
                 for (int i = 3; i <= dependCount.Count; i++)
                 {
                     optimalDepth.Add(i, dependCount[i] - dependCount[i - 1]);
                 }
                 var minvalue = optimalDepth.OrderBy(kvp => kvp.Value).First();
                 var optdepth = optimalDepth.OrderBy(x => x.Value == minvalue.Value).Last();
                 optdepthTb.Text = optdepth.Key.ToString() + "; " + optdepth.Value.ToString();
             }
             dependChart.Series[0].Points.DataBindXY(dependCount.Keys, dependCount.Values);
             if (dependChart.Series[0].Points.Count > 5)
             {
                 dependChart.ChartAreas[0].AxisX.ScaleView.Size     = 5;
                 dependChart.ChartAreas[0].AxisX.ScaleView.Position = dependChart.Series[0].Points.Count - 5;
             }
             dependChart.ChartAreas[0].AxisX.Interval = 1;
             if ((Math.Floor(Math.Log10(dependCount.Values.Max()) + 1) > 4))
             {
                 dependChart.Series[0].Font = new Font("Consolas", 9.75f);
             }
         }
     }
     catch (NullReferenceException)
     {
     }
 }
Exemplo n.º 2
0
        private void GetSolution()
        {
            List <int> puzzle = new List <int>();

            if (puzzlemode == 8)
            {
                puzzle = new List <int>(puzzle8);
            }
            if (puzzlemode == 15)
            {
                puzzle = new List <int>(puzzle15);
            }
            if (astep != -1)
            {
                if (step != 0 && bystep)
                {
                    if (algochoice == 0)
                    {
                        solution = search.BreadthFirstSearch(initNode, statusTb, workTb, step, bystep);
                    }
                    if (algochoice == 1)
                    {
                        solution = search.DepthFirstSearch(initNode, statusTb, workTb, step, bystep, maxdepth);
                    }
                    if (algochoice == 2)
                    {
                        solution = search.AStarSearch(initNode, statusTb, workTb, step, bystep, maxdepth, heuristic);
                    }
                }
                else if (step != 0 && !bystep)
                {
                    if (!(testTb.BackColor == Color.FromArgb(0, 177, 224)))
                    {
                        if (algochoice == 0)
                        {
                            solution = search.BreadthFirstSearch(initNode, statusTb, workTb, astep, bystep);
                        }
                        if (algochoice == 1)
                        {
                            solution = search.DepthFirstSearch(initNode, statusTb, workTb, astep, bystep, maxdepth);
                        }
                        if (algochoice == 2)
                        {
                            solution = search.AStarSearch(initNode, statusTb, workTb, step, bystep, maxdepth, heuristic);
                        }
                    }
                    else
                    {
                        initNode = new Node(puzzle.ToArray());
                        search   = new Search();
                        if (algochoice == 0)
                        {
                            solution = search.BreadthFirstSearch(initNode, statusTb, workTb, token);
                        }
                        if (algochoice == 1)
                        {
                            solution = search.DepthFirstSearch(initNode, statusTb, workTb, maxdepth, token);
                        }
                        if (algochoice == 2)
                        {
                            solution = search.AStarSearch(initNode, statusTb, workTb, maxdepth, heuristic, token);
                        }
                    }
                }
                else
                {
                    initNode = new Node(puzzle.ToArray());
                    search   = new Search();
                    if (algochoice == 0)
                    {
                        solution = search.BreadthFirstSearch(initNode, statusTb, workTb, astep, bystep);
                    }
                    if (algochoice == 1)
                    {
                        solution = search.DepthFirstSearch(initNode, statusTb, workTb, astep, bystep, maxdepth);
                    }
                    if (algochoice == 2)
                    {
                        solution = search.AStarSearch(initNode, statusTb, workTb, step, bystep, maxdepth, heuristic);
                    }
                }
            }
            else
            {
                initNode = new Node(puzzle.ToArray());
                search   = new Search();
                if (algochoice == 0)
                {
                    solution = search.BreadthFirstSearch(initNode, statusTb, workTb, token);
                }
                if (algochoice == 1)
                {
                    solution = search.DepthFirstSearch(initNode, statusTb, workTb, maxdepth, token);
                }
                if (algochoice == 2)
                {
                    solution = search.AStarSearch(initNode, statusTb, workTb, maxdepth, heuristic, token);
                }
            }
        }