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

📄 mainform.cs

📁 编写一个“密室寻宝”算法(以下简称算法)
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Reflection;
using System.Diagnostics;

namespace GrapeCity.Competition.TreasureHouse
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.MapChanged();
        }

        #region Map
        public Map Map
        {
            get
            {
                return this.mapViewer1.Map;
            }
            set
            {
                if (this.mapViewer1.Map != value)
                {
                    if (this.mapViewer1.Map != null)
                    {
                        this.mapViewer1.Map.PropertyChanged -= new PropertyChangedEventHandler(Map_PropertyChanged);
                    }
                    this.mapViewer1.Map = value;
                    if (this.mapViewer1.Map != null)
                    {
                        this.mapViewer1.Map.PropertyChanged += new PropertyChangedEventHandler(Map_PropertyChanged);
                    }
                    this.MapChanged();
                }
            }
        }

        void Map_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.MapPropertyChanged();
        }

        private void easyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Map = new Map(Level.Easy);
        }
        private void normalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Map = new Map(Level.Normal);
        }
        private void hardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Map = new Map(Level.Hard);
        }
        private void toolStripLoad_Click(object sender, EventArgs e)
        {
            this.LoadMap();
        }
        private void toolStripSave_Click(object sender, EventArgs e)
        {
            if (this.Map != null)
            {
                this.SaveMap();
            }
        }

        private void toolStripGridline_CheckedChanged(object sender, EventArgs e)
        {
            this.mapViewer1.ShowGridLine = this.toolStripGridline.Checked;
        }

        private void SaveMap()
        {
            Map map = this.Map;
            if (map != null)
            {
                SaveFileDialog dialog = new SaveFileDialog();
                dialog.Filter = "*.map|*.map";
                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    Stream s = dialog.OpenFile();
                    this.SaveMap(map, s);
                    s.Flush();
                    s.Close();
                }
                dialog.Dispose();
            }
        }

        private void SaveMap(Map map, Stream s)
        {
            XmlTextWriter writer = new XmlTextWriter(s, Encoding.ASCII);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("Map");

            writer.WriteStartAttribute("Level");
            writer.WriteValue(map.Level.ToString());
            writer.WriteEndAttribute();

            writer.WriteStartAttribute("MaxStep");
            writer.WriteValue(map.MaxStep);
            writer.WriteEndAttribute();

            for (int x = 0; x < map.Size.Width; x++)
            {
                for (int y = 0; y < map.Size.Height; y++)
                {
                    writer.WriteStartElement("Tag");
                    writer.WriteValue(map[x, y].ToString());
                    writer.WriteEndElement();
                }
            }

            writer.WriteEndElement();
            writer.Flush();
        }

        private void LoadMap()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "*.map|*.map";
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                this.LoadMap(dialog.FileName);
            }
        }

        public void LoadMap(string filename)
        {
            Map map = null;
            Stream s = null;
            try
            {
                s = File.OpenRead(filename);
                map = this.LoadMap(s);
                if (map != null)
                {
                    string message = null;
                    if (map.MaxStep < 0 || map.MaxStep > map.MaxMaxStep)
                    {
                        message = "MaxStep is invalid.";
                    }
                    if (map.TreasureCount > map.MaxTreasureCount || map.MaxTreasureValue > map.MaxTreasureValue)
                    {
                        message = "Treasure is invalid.";
                    }
                    if (message != null)
                    {
                        MessageBox.Show("Load Map failed!" + Environment.NewLine + message);
                        map = null;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Load Map failed!" + Environment.NewLine + e.Message);
            }
            finally
            {
                s.Close();
            }

            if (map != null)
            {
                this.Map = map;
            }
        }

        private Map LoadMap(Stream s)
        {
            XmlTextReader reader = new XmlTextReader(s);
            reader.ReadToFollowing("Map");
            //reader.ReadStartElement("Map");

            string level = reader.GetAttribute("Level");
            string maxStep = reader.GetAttribute("MaxStep");

            Map map = new Map((Level)Enum.Parse(typeof(Level), level, true));
            map.ReadOnly = false;
            map.MaxStep = Int32.Parse(maxStep);

            for (int x = 0; x < map.Size.Width; x++)
            {
                for (int y = 0; y < map.Size.Height; y++)
                {
                    reader.ReadToFollowing("Tag");
                    string value = reader.ReadElementContentAsString();
                    //reader.ReadEndElement();

                    map[x, y] = (Tag)Enum.Parse(typeof(Tag), value, true);
                }
            }

            //reader.ReadEndElement();
            reader.Close();
            map.ReadOnly = true;
            return map;
        }

        private void MapChanged()
        {
            if (this.Map != null)
            {
                this.toolStripMaxStep.Text = this.Map.MaxStep.ToString();

                this.toolStripEdit.Enabled = true;
                this.toolStripSave.Enabled = true;
            }
            else
            {
                this.toolStripEdit.Enabled = false;
                this.toolStripSave.Enabled = false;

            }
            this.CurrentEditor = null;
            this.Result = null;
            this.MapPropertyChanged();
       }

        private void MapPropertyChanged()
        {
            if (this.Map != null)
            {
                this.toolStripEdit.Checked = !this.Map.ReadOnly;
                this.toolStripMaxStep.Text = this.Map.MaxStep.ToString();
                this.toolStripMapInfo.Text = this.Map.ToString();
                this.Result = null;
            }
        }
        #endregion

        #region Edit
        private void toolStripEdit_CheckedChanged(object sender, EventArgs e)
        {
            if (this.Map == null)
            {
                return;
            }

            if (this.toolStripEdit.Checked)
            {
                this.Map.ReadOnly = false;

                this.toolStripAuto.Enabled = true;
                this.toolStripClear.Enabled = true;
                this.toolStripWall.Enabled = true;

                switch (this.Map.Level)
                {
                    case Level.Easy:
                        this.toolStripCoin.Enabled = true;
                        break;
                    case Level.Normal:
                        this.toolStripCoin.Enabled = true;
                        this.toolStripGem.Enabled = true;
                        break;
                    case Level.Hard:
                        this.toolStripCoin.Enabled = true;
                        this.toolStripGem.Enabled = true;
                        this.toolStripHolyA.Enabled = true;
                        this.toolStripHolyB.Enabled = true;
                        this.toolStripHolyC.Enabled = true;
                        break;
                    default:
                        break;
                }

                this.toolStripMaxStep.ReadOnly = false;

                this.CurrentEditor = GrapeCity.Competition.TreasureHouse.Tag.Wall;
            }
            else
            {
                this.Map.ReadOnly = true;

                this.toolStripAuto.Enabled = false;
                this.toolStripClear.Enabled = false;
                this.toolStripWall.Enabled = false;
                this.toolStripCoin.Enabled = false;
                this.toolStripGem.Enabled = false;
                this.toolStripHolyA.Enabled = false;
                this.toolStripHolyB.Enabled = false;
                this.toolStripHolyC.Enabled = false;

                this.toolStripMaxStep.ReadOnly = true;

                this.CurrentEditor = null;
            }
        }

        private Tag? _currentEditor;
        private Tag? CurrentEditor
        {
            get
            {
                return this._currentEditor;
            }
            set
            {
                this._currentEditor = value;
                this.UpdateMapEditor(value);

                if (value != null)
                {
                    this.mapViewer1.Cursor = Cursors.Hand;
                }
                else
                {
                    this.mapViewer1.Cursor = null;
                }
            }
        }
        private void UpdateMapEditor(Tag? tag)
        {
            this.toolStripClear.Checked = tag == GrapeCity.Competition.TreasureHouse.Tag.Path;
            this.toolStripWall.Checked = tag == GrapeCity.Competition.TreasureHouse.Tag.Wall;
            this.toolStripCoin.Checked = tag == GrapeCity.Competition.TreasureHouse.Tag.Coin;
            this.toolStripGem.Checked = tag == GrapeCity.Competition.TreasureHouse.Tag.Gem;
            this.toolStripHolyA.Checked = tag == GrapeCity.Competition.TreasureHouse.Tag.HolyA;
            this.toolStripHolyB.Checked = tag == GrapeCity.Competition.TreasureHouse.Tag.HolyB;
            this.toolStripHolyC.Checked = tag == GrapeCity.Competition.TreasureHouse.Tag.HolyC;
        }
        private void toolStripAuto_Click(object sender, EventArgs e)
        {

⌨️ 快捷键说明

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