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

📄 map.cs

📁 编写一个“密室寻宝”算法(以下简称算法)
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Drawing;

namespace GrapeCity.Competition.TreasureHouse
{
    public class Map : IMap, INotifyPropertyChanged
    {
        public Map(Level level)
        {
            this._level = level;
            this._maxStep = this.MaxMaxStep;
        }

        private Level _level;

        private int _maxStep;

        private Tag[,] _tags;

        private bool _readOnly = true;

        public Level Level
        {
            get
            {
                return this._level;
            }
        }

        #region IMap Members

        public System.Drawing.Size Size
        {
            get
            {
                switch (this._level)
                {
                    case Level.Easy:
                        return new Size(10, 10);
                    case Level.Normal:
                        return new Size(15, 15);
                    case Level.Hard:
                        return new Size(20, 20);
                    default:
                        throw new InvalidOperationException();
                }
            }
        }

        public int MaxStep
        {
            get { return this._maxStep; }
            set
            {
                if (this._readOnly)
                {
                    throw new InvalidOperationException();
                }
                if (value < 0)
                {
                    value = 0;
                }
                if (value > this.MaxMaxStep)
                {
                    value = this.MaxMaxStep;
                }
                if (value != _maxStep)
                {
                    this._maxStep = value;
                    this.OnPropertyChanged("MaxStep");
                }
            }
        }

        public Tag this[int x, int y]
        {
            get 
            {
                if (this._tags == null)
                {
                    this._tags = new Tag[this.Size.Width, this.Size.Height];
                }
                return this._tags[x, y];
            }
            set
            {
                if (this._readOnly)
                {
                    throw new InvalidOperationException();
                }
                if (!Enum.IsDefined(typeof(Tag), value))
                {
                    throw new InvalidEnumArgumentException();
                }
                if (x < 0 || x >= this.Size.Width || y < 0 || y >= this.Size.Height)
                {
                    throw new ArgumentOutOfRangeException();
                }
                if (x == 0 && y == 0)
                {
                    value = Tag.Path;
                }
                if (this._tags == null)
                {
                    this._tags = new Tag[this.Size.Width, this.Size.Height];
                }
                if (this._tags[x, y] != value)
                {
                    this._tags[x, y] = value;
                    this.OnPropertyChanged("Item");
                }
            }
        }

        #endregion

        public bool ReadOnly
        {
            get { return this._readOnly; }
            set 
            {
                if (this._readOnly != value)
                {
                    this._readOnly = value;
                    this.OnPropertyChanged("ReadOnly");
                }
            }
        }

        public int MaxMaxStep
        {
            get 
            {
                switch (this._level)
                {
                    case Level.Easy:
                        return 50;
                    case Level.Normal:
                        return 100;
                    case Level.Hard:
                        return 200;
                    default:
                        throw new InvalidOperationException();
                }
            }
        }
        public int MaxTreasureCount
        {
            get
            {
                switch (this._level)
                {
                    case Level.Easy:
                        return 10;
                    case Level.Normal:
                        return 30;
                    case Level.Hard:
                        return 50;
                    default:
                        throw new InvalidOperationException();
                }
            }
        }
        public int MaxTreasureValue
        {
            get
            {
                switch (this._level)
                {
                    case Level.Easy:
                        return 10;
                    case Level.Normal:
                        return 50;
                    case Level.Hard:
                        return 150;
                    default:
                        throw new InvalidOperationException();
                }
            }
        }

        public int GetTagCount(Tag tag)
        {
            int count = 0;
            for (int x = 0; x < this.Size.Width; x++)
            {
                for (int y = 0; y < this.Size.Height; y++)
			    {
                    if (this[x, y] == tag)
                    {
                        count++;
                    }
			    }
            }
            return count;
        }

        public int TreasureCount
        {
            get
            {
                int count = 0;
                for (int x = 0; x < this.Size.Width; x++)
                {
                    for (int y = 0; y < this.Size.Height; y++)
                    {
                        if (this[x, y] != Tag.Path && this[x, y] != Tag.Wall)
                        {
                            count++;
                        }
                    }
                }
                return count;
            }
        }
        public int TreasureValue
        {
            get
            {
                int value = 0;
                int a = 0;
                int b = 0;
                int c = 0;
                for (int x = 0; x < this.Size.Width; x++)
                {
                    for (int y = 0; y < this.Size.Height; y++)
                    {
                        switch (this[x, y])
                        {
                            case Tag.Coin:
                                value++;
                                break;
                            case Tag.Gem:
                                value += 4;
                                break;
                            case Tag.HolyA:
                                a++;
                                break;
                            case Tag.HolyB:
                                b++;
                                break;
                            case Tag.HolyC:
                                c++;
                                break;
                            default:
                                break;
                        }
                    }
                }
                int holy = Math.Min(Math.Min(a, b), c);
                value += holy * 24;
                value += (a + b + c - holy * 3) * 2;
                return value;
            }
        }

        public override string ToString()
        {
            return string.Format("Level={0}, Size=({1}x{2}), TreasureCount={3}, TreasureValue={4}", this.Level, this.Size.Width, this.Size.Height, this.TreasureCount, this.TreasureValue);
        }


        #region INotifyPropertyChanged Members
        private void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    public enum Level
    {
        Easy,
        Normal,
        Hard
    }
}

⌨️ 快捷键说明

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