Exemple #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            point start, end;
            point target;

            int i, tmp, x, y;

            int[,] state;
            string text1 = this.textBox1.Text; //初始状态
            string text2 = this.textBox2.Text; //目标状态

            state = new int[3, 3];

            for (i = 0; i < text2.Length; i++)
            {
                x = i % 3;
                y = i / 3;

                state[x, y] = text2[i] - '0';
            }

            end = new point(state);

            for (i = 0; i < text1.Length; i++)
            {
                x = i % 3;
                y = i / 3;

                state[x, y] = text1[i] - '0';
            }

            start = new point(state);

            start.setG();
            start.setH(end);
            //start.setF();

            if (this.checkBox1.Checked)//执行A*算法
            {
                start.setF();

                Astar_test astar_Test = new Astar_test(start, end);

                if (Astar_test.cangettoTarget(text1, text2))//如果说可以到达
                {
                    Astar_test.converttobuttons(start, buttons);

                    Astar_test.converttobuttons(end, buttons_end);

                    Astar_test.converttobuttons(start, buttons_demo);

                    target = astar_Test.exe();//得到最终节点,反向求得路径

                    this.progressBar1.Value = 0;

                    this.l = out_put(target);

                    this.textBox4.Text = 0 + "";
                }

                else
                {
                    MessageBox.Show("终点无法到达!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else if (this.checkBox2.Checked)//全局择优算法
            {
                start.f = start.h;

                Global_optimun global_Optimun = new Global_optimun(start, end);

                if (Global_optimun.cangettoTarget(text1, text2))//如果说可以到达
                {
                    Global_optimun.converttobuttons(start, buttons);

                    Global_optimun.converttobuttons(end, buttons_end);

                    Global_optimun.converttobuttons(start, buttons_demo);

                    target = global_Optimun.exe();//得到最终节点,反向求得路径

                    this.progressBar1.Value = 0;

                    this.l = out_put(target);

                    this.textBox4.Text = 0 + "";
                }

                else
                {
                    MessageBox.Show("终点无法到达!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else if (this.checkBox3.Checked)//广度优先
            {
                start.f = start.g;

                Width_first width_first = new Width_first(start, end);

                if (Width_first.cangettoTarget(text1, text2))//如果说可以到达
                {
                    Width_first.converttobuttons(start, buttons);

                    Width_first.converttobuttons(end, buttons_end);

                    Width_first.converttobuttons(start, buttons_demo);

                    target = width_first.exe();//得到最终节点,反向求得路径

                    this.progressBar1.Value = 0;

                    this.l = out_put(target);

                    this.textBox4.Text = 0 + "";
                }

                else
                {
                    MessageBox.Show("终点无法到达!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else if (this.checkBox4.Checked)//玩游戏
            {
                Astar_test.converttobuttons(start, buttons);

                Astar_test.converttobuttons(end, buttons_end);

                Astar_test.converttobuttons(start, buttons_demo);
            }
        }