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

📄 mainform.cs

📁 编写一个“密室寻宝”算法(以下简称算法)
💻 CS
📖 第 1 页 / 共 2 页
字号:
            AutoCreateMap();
        }

        private void AutoCreateMap()
        {
            if (this.Map == null || this.Map.ReadOnly)
            {
                return;
            }
            Random r = new Random(DateTime.Now.Millisecond);
            Tag[] tags = new Tag[this.Map.Size.Width * this.Map.Size.Height * 3 / 2];
            int p = 0;
            int w = 0;
            switch (this.Map.Level)
            {
                case Level.Easy:
                    for (int i = 0; i < 10; i++)
                    {
                        tags[i] = GrapeCity.Competition.TreasureHouse.Tag.Coin;
                    }
                    p = 10;
                    w = 30;
                    this.Map.MaxStep = 30 + r.Next(20);
                    break;
                case Level.Normal:
                    for (int i = 0; i < 24; i++)
                    {
                        tags[i] = GrapeCity.Competition.TreasureHouse.Tag.Coin;
                    }
                    for (int i = 24; i < 30; i++)
                    {
                        tags[i] = GrapeCity.Competition.TreasureHouse.Tag.Gem;
                    }
                    p = 30;
                    w = 35;
                    this.Map.MaxStep = 45 + r.Next(55);
                    break;
                case Level.Hard:
                    for (int i = 0; i < 28; i++)
                    {
                        tags[i] = GrapeCity.Competition.TreasureHouse.Tag.Coin;
                    }
                    for (int i = 28; i < 40; i++)
                    {
                        tags[i] = GrapeCity.Competition.TreasureHouse.Tag.Gem;
                    }
                    for (int i = 40; i < 43; i++)
                    {
                        tags[i] = GrapeCity.Competition.TreasureHouse.Tag.HolyA;
                    }
                    for (int i = 43; i < 46; i++)
                    {
                        tags[i] = GrapeCity.Competition.TreasureHouse.Tag.HolyB;
                    }
                    for (int i = 46; i < 49; i++)
                    {
                        tags[i] = GrapeCity.Competition.TreasureHouse.Tag.HolyC;
                    }
                    p = 49;
                    w = 40;
                    this.Map.MaxStep = 80 + r.Next(120);
                    break;
            }
            for (int i = p; i < (tags.Length - p) * w / 100; i++)
            {
                tags[i] = GrapeCity.Competition.TreasureHouse.Tag.Wall;
            }

            for (int i = 0; i < tags.Length; i++)
            {
                int t = r.Next(tags.Length);
                Tag temp = tags[t];
                tags[t] = tags[i];
                tags[i] = temp;
            }

            tags[0] = GrapeCity.Competition.TreasureHouse.Tag.Path;

            for (int x = 0; x < this.Map.Size.Width; x++)
            {
                for (int y = 0; y < this.Map.Size.Height; y++)
                {
                    this.Map[x, y] = tags[x * this.Map.Size.Height + y];
                }
            }
        }

        private void toolStripEditorTag_Click(object sender, EventArgs e)
        {
            ToolStripButton button = sender as ToolStripButton;
            if (button != null)
            {
                if (button == this.toolStripClear)
                {
                    CurrentEditor = GrapeCity.Competition.TreasureHouse.Tag.Path;
                }
                else if (button == this.toolStripWall)
                {
                    CurrentEditor = GrapeCity.Competition.TreasureHouse.Tag.Wall;
                }
                else if (button == this.toolStripCoin)
                {
                    CurrentEditor = GrapeCity.Competition.TreasureHouse.Tag.Coin;
                }
                else if (button == this.toolStripGem)
                {
                    CurrentEditor = GrapeCity.Competition.TreasureHouse.Tag.Gem;
                }
                else if (button == this.toolStripHolyA)
                {
                    CurrentEditor = GrapeCity.Competition.TreasureHouse.Tag.HolyA;
                }
                else if (button == this.toolStripHolyB)
                {
                    CurrentEditor = GrapeCity.Competition.TreasureHouse.Tag.HolyB;
                }
                else if (button == this.toolStripHolyC)
                {
                    CurrentEditor = GrapeCity.Competition.TreasureHouse.Tag.HolyC;
                }
                else
                {
                    CurrentEditor = null;
                }
            }
        }

        private void mapViewer1_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.Map != null && !this.Map.ReadOnly && this.CurrentEditor != null)
            {
                Point position = mapViewer1.HitTest(e.Location);

                Tag? tag = null;
                if (e.Button == MouseButtons.Left)
                {
                    tag = this.CurrentEditor;
                }
                else if (e.Button == MouseButtons.Right)
                {
                    tag = GrapeCity.Competition.TreasureHouse.Tag.Path;
                }

                if (tag.HasValue)
                {
                    Tag old = this.Map[position.X, position.Y];
                    this.Map[position.X, position.Y] = tag.Value;
                    if (this.Map.TreasureCount > this.Map.MaxTreasureCount || this.Map.TreasureValue > this.Map.MaxTreasureValue)
                    {
                        this.Map[position.X, position.Y] = old;
                    }
                }
            }
        }

        private void toolStripMaxStep_TextChanged(object sender, EventArgs e)
        {
            if (this.Map != null && !this.Map.ReadOnly)
            {
                int value;
                if (Int32.TryParse(this.toolStripMaxStep.Text, out value))
                {
                    this.Map.MaxStep = value;
                }
                else
                {
                    this.Map.MaxStep = 0;
                }
            }
        }
        #endregion

        #region Explorer

        private IExplorer _explorer;
        public IExplorer Explorer
        {
            get
            {
                return this._explorer;
            }
            set
            {
                if (this._explorer != value)
                {
                    this._explorer = value;
                    this.OnExplorerChanged();
                }
            }
        }

        private void OnExplorerChanged()
        {
            if (this.Explorer != null)
            {
                this.buttonRun.Enabled = true;
            }
            else
            {
                this.buttonRun.Enabled = false;
            }
            this.Result = null;
        }
        private void buttonLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "*.dll|*.dll";
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                this.LoadExplorer(dialog.FileName);
            }
        }

        public void LoadExplorer(string file)
        {
            Assembly asm = null;
            IExplorer exp = null;
            try
            {
                //asm = Assembly.LoadFile(file);
                asm = Assembly.LoadFrom(file);
            }
            catch (Exception e)
            {
                MessageBox.Show("Cannot load the file " + file + Environment.NewLine + e.Message);
            }
            if (asm != null)
            {
                try
                {
                    Type[] types = asm.GetTypes();
                    foreach (Type t in types)
                    {
                        if (t.GetInterface(typeof(IExplorer).FullName) != null)
                        {
                            exp = Activator.CreateInstance(t) as IExplorer; 
                            break;
                        }
                    }
                    
                }
                catch
                {
                }
                if (exp == null)
                {
                    MessageBox.Show("Cannot create instance for IExplorer");
                }
                else
                {
                    this.Explorer = exp;
                }
            }
        }
        #endregion

        #region Result

        private Result _result;
        private Result Result
        {
            get
            {
                return this._result;
            }
            set
            {
                if (this._result != value)
                {
                    this._result = value;
                    this.OnResultChanged();
                }
            }
        }

        private void OnResultChanged()
        {
            if (this.Result != null)
            {
                this.propertyGridResult.SelectedObject = this.Result;
                this.buttonReplay.Enabled = true;
                this.mapViewer1.Positions = this.Result.Positions;
            }
            else
            {
                this.propertyGridResult.SelectedObject = null;
                this.buttonReplay.Enabled = false;
                this.mapViewer1.Positions = null;
            }
        }

        private void buttonRun_Click(object sender, EventArgs e)
        {
            if (this.Explorer == null)
            {
                MessageBox.Show("You need load Explorer first.");
            }
            else if (this.Map == null)
            {
                MessageBox.Show("You need load/create Map first.");
            }
            else
            {
                this.Cursor = Cursors.WaitCursor;
                Stopwatch sw = Stopwatch.StartNew();
                try
                {
                    List<Direction> list = this.Explorer.Explore(this.Map);
                    sw.Stop();
                    this.Result = new Result(this.Map, list, sw.Elapsed);
                }
                catch (Exception exception)
                {
                    ThreadExceptionDialog dialog = new ThreadExceptionDialog(exception);
                    dialog.ShowDialog(this);

                    this.Result = null;
                }
                finally
                {
                    this.Cursor = null;
                }
            }
        }

        private void buttonReplay_Click(object sender, EventArgs e)
        {
            this.mapViewer1.Replay();
        }

        #endregion
    }
}

⌨️ 快捷键说明

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