zoukankan      html  css  js  c++  java
  • 写着玩

    http://hero.pongo.cn/Question/Details?ID=168&ExamID=163

        /// <summary>
        /// 单词博弈游戏
        /// </summary>
        public class Game
        {
            /// <summary>
            /// 字母序列
            /// </summary>
            List<char> _List;
            /// <summary>
            /// 游戏历史记录
            /// </summary>
            List<KeyValuePair<int, char>> _History;
            /// <summary>
            /// 游戏参与者
            /// </summary>
            string[] _Playrs;
            /// <summary>
            /// 当前游戏者
            /// </summary>
            int _CurrentPlayerIndex;
    
            /// <summary>
            /// 使用一起单词开始游戏
            /// </summary>
            public Game(string str)
            {
                Console.WriteLine("游戏单词:" + str);
                Console.WriteLine("=============游戏开始==============");
                _List = str.ToLower().ToList();
                _History = new List<KeyValuePair<int, char>>();
                _Playrs = new string[2];
                _CurrentPlayerIndex = 0;
            }
            /// <summary>
            /// 获取或设定1号游戏者名字
            /// </summary>
            public string Player1
            {
                get
                {
                    return _Playrs[0];
                }
                set
                {
                    _Playrs[0] = value;
                }
            }
            /// <summary>
            /// 获取或设定2号游戏者名字
            /// </summary>
            public string Player2
            {
                get
                {
                    return _Playrs[1];
                }
                set
                {
                    _Playrs[1] = value;
                }
            }
            /// <summary>
            /// 切换游戏者
            /// </summary>
            private void SwapPlayr()
            {
                _CurrentPlayerIndex = _CurrentPlayerIndex ^ 1;
            }
            /// <summary>
            /// 当前游戏者名字
            /// </summary>
            public string CurrentPlayer
            {
                get
                {
                    return _Playrs[_CurrentPlayerIndex];
                }
            }
            /// <summary>
            /// 移除指定索引的字母
            /// </summary>
            public void Remove(int index)
            {
                if (index < 0 || index >= _List.Count)
                {
                    throw new IndexOutOfRangeException();
                }
                Console.Write(new string(' ', _History.Count * 2));
                Console.Write(CurrentPlayer + "抽取'" + _List[index] + "'");
                _History.Add(new KeyValuePair<int, char>(index, _List[index]));
                _List.RemoveAt(index);
                Console.Write(" 剩余: " + this);
                SwapPlayr();
            }
            /// <summary>
            /// 悔一步
            /// </summary>
            public void Undo()
            {
                if (_History.Count == 0)
                {
                    throw new NotImplementedException();
                }
                var kv = _History.Last();
                _History.RemoveAt(_History.Count - 1);
                _List.Insert(kv.Key, kv.Value);
                SwapPlayr();
            }
            /// <summary>
            /// 当前剩余字母数
            /// </summary>
            public int Count
            {
                get { return _List.Count; }
            }
    
            /// <summary>
            /// 判断当前剩余字母是否是升序序列
            /// </summary>
            public bool IsAscending()
            {
                if (Count <= 1)
                {
                    return true;
                }
                var ee = _List.GetEnumerator();
                ee.MoveNext();
                var pre = ee.Current;
                while (ee.MoveNext())
                {
                    if (ee.Current > pre)
                    {
                        pre = ee.Current;
                    }
                    else
                    {
                        return false;
                    }
                }
                return true;
            }
            /// <summary>
            /// 返回当前单词被抽离字母以_(下划线)代替的字符串
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                var list = new List<char>(_List);
                for (int i = _History.Count - 1; i >= 0; i--)
                {
                    list.Insert(_History[i].Key, '_');
                }
                return string.Concat(list);
            }
            /// <summary>
            /// 继续游戏,返回胜利者编号 0:一号游戏者 1:二号游戏者
            /// </summary>
            public int Play()
            {
                if (IsAscending())
                {
                    Console.Write(" " + _Playrs[_CurrentPlayerIndex ^ 1] + "");
                    return _CurrentPlayerIndex ^ 1;
                }
                for (int i = 0; i < Count; i++)
                {
                    Console.WriteLine();
                    Remove(i);
                    if (Play() != _CurrentPlayerIndex)
                    {
                        Undo();
                        Console.WriteLine(" " + CurrentPlayer + "放弃");
                        return _CurrentPlayerIndex;
                    }
                    Undo();
                    Console.Write(new string(' ', _History.Count * 2));
                    Console.Write(CurrentPlayer + "重来");
                }
                Console.Write(" " + CurrentPlayer + "没字母可抽了 请求再悔一步");
                return _CurrentPlayerIndex ^ 1;
            }
        }
    Game类
    public static int who(string word)
    {
        Game game = new Game(word);
        game.Player1 = "";
        game.Player2 = "";
    
        for (int i = 0; i < game.Count; i++)
        {
            game.Remove(i);
            var winIndex = game.Play();
            if (winIndex == 0)
            {
                Console.WriteLine("
    
    甲不同意 " + game.Player1 + "赢了");
                return 1;
            }
            game.Undo();
        }
    
        Console.WriteLine(game.Player1 + "输了");
        return 1;
    }

     输出结果

    游戏单词:money
    =============游戏开始==============
    甲抽取'm' 剩余: _oney
      乙抽取'o' 剩余: __ney
        甲抽取'n' 剩余: ___ey 甲胜 甲放弃
      乙重来
      乙抽取'n' 剩余: _o_ey
        甲抽取'o' 剩余: ___ey 甲胜 甲放弃
      乙重来
      乙抽取'e' 剩余: _on_y
        甲抽取'o' 剩余: __n_y 甲胜 甲放弃
      乙重来
      乙抽取'y' 剩余: _one_
        甲抽取'o' 剩余: __ne_
          乙抽取'n' 剩余: ___e_ 乙胜 乙放弃
        甲重来
        甲抽取'n' 剩余: _o_e_
          乙抽取'o' 剩余: ___e_ 乙胜 乙放弃
        甲重来
        甲抽取'e' 剩余: _on__
          乙抽取'o' 剩余: __n__ 乙胜 乙放弃
        甲重来 甲没字母可抽了 请求再悔一步 乙放弃
    甲抽取'o' 剩余: m_ney
      乙抽取'm' 剩余: __ney
        甲抽取'n' 剩余: ___ey 甲胜 甲放弃
      乙重来
      乙抽取'n' 剩余: m__ey
        甲抽取'm' 剩余: ___ey 甲胜 甲放弃
      乙重来
      乙抽取'e' 剩余: m_n_y 乙胜 乙放弃
    甲抽取'n' 剩余: mo_ey
      乙抽取'm' 剩余: _o_ey
        甲抽取'o' 剩余: ___ey 甲胜 甲放弃
      乙重来
      乙抽取'o' 剩余: m__ey
        甲抽取'm' 剩余: ___ey 甲胜 甲放弃
      乙重来
      乙抽取'e' 剩余: mo__y 乙胜 乙放弃
    甲抽取'e' 剩余: mon_y
      乙抽取'm' 剩余: _on_y
        甲抽取'o' 剩余: __n_y 甲胜 甲放弃
      乙重来
      乙抽取'o' 剩余: m_n_y 乙胜 乙放弃
    甲抽取'y' 剩余: mone_
      乙抽取'm' 剩余: _one_
        甲抽取'o' 剩余: __ne_
          乙抽取'n' 剩余: ___e_ 乙胜 乙放弃
        甲重来
        甲抽取'n' 剩余: _o_e_
          乙抽取'o' 剩余: ___e_ 乙胜 乙放弃
        甲重来
        甲抽取'e' 剩余: _on__
          乙抽取'o' 剩余: __n__ 乙胜 乙放弃
        甲重来 甲没字母可抽了 请求再悔一步 乙放弃
    甲输了
    游戏单词:baidu
    =============游戏开始==============
    甲抽取'b' 剩余: _aidu
      乙抽取'a' 剩余: __idu
        甲抽取'i' 剩余: ___du 甲胜 甲放弃
      乙重来
      乙抽取'i' 剩余: _a_du 乙胜 乙放弃
    甲抽取'a' 剩余: b_idu
      乙抽取'b' 剩余: __idu
        甲抽取'i' 剩余: ___du 甲胜 甲放弃
      乙重来
      乙抽取'i' 剩余: b__du 乙胜 乙放弃
    甲抽取'i' 剩余: ba_du
      乙抽取'b' 剩余: _a_du 乙胜 乙放弃
    甲抽取'd' 剩余: bai_u
      乙抽取'b' 剩余: _ai_u 乙胜 乙放弃
    甲抽取'u' 剩余: baid_
      乙抽取'b' 剩余: _aid_
        甲抽取'a' 剩余: __id_
          乙抽取'i' 剩余: ___d_ 乙胜 乙放弃
        甲重来
        甲抽取'i' 剩余: _a_d_ 甲胜 甲放弃
      乙重来
      乙抽取'a' 剩余: b_id_
        甲抽取'b' 剩余: __id_
          乙抽取'i' 剩余: ___d_ 乙胜 乙放弃
        甲重来
        甲抽取'i' 剩余: b__d_ 甲胜 甲放弃
      乙重来
      乙抽取'i' 剩余: ba_d_
        甲抽取'b' 剩余: _a_d_ 甲胜 甲放弃
      乙重来
      乙抽取'd' 剩余: bai__
        甲抽取'b' 剩余: _ai__ 甲胜 甲放弃
      乙重来 乙没字母可抽了 请求再悔一步
    甲不同意 甲赢了

     上传之后变成这样了 太失望了  无爱了~~~~~~

     

  • 相关阅读:
    (转)Delphi写COM+的心得体会
    delphi透明组件(控件)开发
    Delphi 常用组件常见属性说明
    DELPHI方面输入EDIT
    BYTE 转字符串
    椭圆按纽制作
    数据库实例学生名册管理系统(DAO的使用实验)
    数据库如何快速创建连接字符串
    数据库使用DataReader的简单实例(两种办法)
    数据库ADO.NET的结构
  • 原文地址:https://www.cnblogs.com/blqw/p/3478258.html
Copyright © 2011-2022 走看看