⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jiugongform.cs

📁 这是有一个解决八数码问题的程序! 主要应用了宽度优先搜索法! 是用C#开发的
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;

namespace JiuGong
{
    public partial class JiuGongForm : Form
    {
        //
        public struct PB
        {
            public PictureBox Box;
            public int imageIndex;
        }
        public PB[] Bigs = new PB[9];
        public PB[] Smalls = new PB[9];
        //最小步骤

        public string InitState="";
        public string AimState="";

        //
        public int currStep = 0;

        //
        public TreeArithmetic JGG = null;
        public JiuGongForm()
        {
            InitializeComponent();
            SetBigs();
            SetSmalls();
        }
        //产生0~8随机数列
        public int[] GetRandomArrange()
        {
            int[] Array = { -1, -1, -1, -1, -1, -1, -1, -1, -1 };
            Random r=new Random();
            int i = 0;
            while(i<9)
            {
                int rNumber = r.Next(0, 10);
                if(IsNumInArray(Array,rNumber)!=true&&rNumber<9)
                {
                    Array[i] = rNumber;
                    ++i;
                }
            }
            return Array;
        }
        //判断一个数字是否在数组中
        public bool IsNumInArray(int[] Array, int Num)
        {
            bool bIn = false;
            for (int i = 0; i < Array.Length; i++)
            {
                if (Array[i] == Num)
                {
                    bIn = true;
                    break;
                }
            }
            return bIn;
        }
        #region  设置图片
        public void SetBigs()
        {
            this.Bigs[0].Box = this.PicBoxInit1;
            this.Bigs[1].Box = this.PicBoxInit2;
            this.Bigs[2].Box = this.PicBoxInit3;
            this.Bigs[3].Box= this.PicBoxInit4;
            this.Bigs[4].Box = this.PicBoxInit5;
            this.Bigs[5].Box = this.PicBoxInit6;
            this.Bigs[6].Box = this.PicBoxInit7;
            this.Bigs[7].Box= this.PicBoxInit8;
            this.Bigs[8].Box = this.PicBoxInit9;

        }
        public void SetBigsImage(int [] RandomArray)
        {
            PictureBox Box = null;
            for (int i = 0; i < 9; i++)
            {
                Box = Bigs[i].Box;
                Box.Image = this.imageListBig.Images[RandomArray[i]];
                Bigs[i].imageIndex = RandomArray[i];
            }
        }
        public void SetBigsImage(string  Step)
        {
            for (int i = 0; i < 9; i++)
            {
                int imageIndex = Int32.Parse(Step[i].ToString());
                Bigs[i].Box.Image = this.imageListBig.Images[imageIndex];
            }
        }

        public void SetSmalls()
        {
            this.Smalls[0].Box = this.PicBoxAim0;
            this.Smalls[1].Box = this.PicBoxAim1;
            this.Smalls[2].Box = this.PicBoxAim2;
            this.Smalls[3].Box = this.PicBoxAim3;
            this.Smalls[4].Box = this.PicBoxAim4;
            this.Smalls[5].Box = this.PicBoxAim5;
            this.Smalls[6].Box = this.PicBoxAim6;
            this.Smalls[7].Box = this.PicBoxAim7;
            this.Smalls[8].Box = this.PicBoxAim8;

        }
        public void SetSmallImage(int[] RandomArray)
        {
            PictureBox Box = null;
            for (int i = 0; i < 9; i++)
            {
                Box = Smalls[i].Box;
                Box.Image = this.imageListSmall.Images[RandomArray[i]];
                Smalls[i].imageIndex = RandomArray[i];
            }
        }
        public void SetSmallImage(string Step)
        {
           
            for (int i = 0; i < 9; i++)
            {
                int imageIndex = Int32.Parse(Step[i].ToString());
                if (Smalls[i].imageIndex != imageIndex)
                {
                    Smalls[i].Box.Image = this.imageListSmall.Images[imageIndex];
                }
            }
        }
        #endregion
        #region 设置初始目标状态字符串
        public void SetState(int[] Array,bool bInit)
        {
            string temp = "";
            for (int i = 0; i < Array.Length; i++)
            {
                temp += Array[i].ToString();
            }
            if (bInit)
            {
                this.InitState = temp;
            }
            else
            {
                this.AimState = temp;
            }
        }
        #endregion
        #region 设置控件的状态
        public void SetControlState(bool state)
        {
            this.btnAuto.Enabled = state;
            this.btnNext.Enabled = state;
            this.btnPrev.Enabled = state;
            if (state == false)
            {
                this.lblMinSteps.Text = "";
            }
            this.lblMinSteps.Enabled = state;
            this.btnStart.Enabled = !state;

        }
        #endregion

        private void btnNewInitState_Click(object sender, EventArgs e)
        {
            int[] RandomArrage = GetRandomArrange();
            SetBigsImage(RandomArrage);
            SetState(RandomArrage, true);
            //
            this.JGG.Init = this.InitState;
            SetControlState(false);
        }

        private void btnNextAim_Click(object sender, EventArgs e)
        {
            int[] RandomArrage = GetRandomArrange();
            SetSmallImage(RandomArrage);
            SetState(RandomArrage, false);
            //
            this.JGG.Aim = this.AimState;
            SetControlState(false);
        }

        private void btnMinSteps_Click(object sender, EventArgs e)
        {
            int Count = this.JGG.BestPath.Count;
            if (Count <30)
            {
                this.lblMinSteps.Text = (this.JGG.BestPath.Count - 1).ToString();
            }
            else
            {
                this.lblMinSteps.Text = "不能到达!";
                SetControlState(false);
            }

        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            
            if (this.currStep-1 >= 0)
            {
                this.currStep--;
                string Step = JGG.BestPath[currStep].ToString();
                SetBigsImage(Step);
                if (this.currStep == 0)
                {
                    this.btnPrev.Enabled = false;
                }
                if (this.currStep < this.JGG.BestPath.Count - 1)
                {
                    this.btnNext.Enabled = true;
                }
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            
            if (this.currStep+1< JGG.BestPath.Count)
            {
                this.currStep++;
                string Step = JGG.BestPath[currStep].ToString();
                SetBigsImage(Step);
                if (this.currStep > 0)
                {
                    this.btnPrev.Enabled = true;
                }
                if (this.currStep == this.JGG.BestPath.Count - 1)
                {
                    this.btnNext.Enabled = false;
                }

            }
        }
        private void btnAuto_Click(object sender, EventArgs e)
        {
            this.currStep = 0;
            this.timer1.Interval = 500;
            this.timer1.Start();
        }

        private void JiuGongForm_Load(object sender, EventArgs e)
        {
            int[] RandomArray = GetRandomArrange();
            SetBigsImage(RandomArray);
            SetState(RandomArray, true);
            Thread.Sleep(1);
            RandomArray = GetRandomArrange();
            SetSmallImage(RandomArray);
            SetState(RandomArray, false);
            //调试用代码
            //int[] InitArray ={   2,4,1,  5,0,3,  7, 8, 6 };
            //SetBigsImage(InitArray);
            //SetState(InitArray, true);
            //int[] AimArray ={ 1, 2, 3, 4, 5, 6, 7, 8, 0 };
            //SetSmallImage(AimArray);
            //SetState(AimArray, false);

            this.JGG = new TreeArithmetic(this.InitState, this.AimState, 3, 3);
            SetControlState(false);
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            this.lblMinSteps.Text = "";
            JGG.ComputeFeel();
            //
            int Count = JGG.BestPath.Count;
            if (Count !=0&&Count <29)
            {
                this.lblMinSteps.Text =(Count-1).ToString();
                SetControlState(true);
            }
            else
            {
                this.lblMinSteps.Text = "不能到达目标!";
            }    
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.currStep + 1 < this.JGG.BestPath.Count)
            {
                this.currStep++;
                string Step = JGG.BestPath[currStep].ToString();
                SetBigsImage(Step);
            }
            else
            {
                this.timer1.Stop();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -