萌新柿子 2021-08-18 11:23 采纳率: 77.8%
浏览 69
已结题

一道面向对象练习题,能请各位提供一下代码思路吗?

一道面向对象练习题,能请各位提供一下代码思路吗?目前只学了一些基础,map类里面不能使用For构建地图吗,用控制台实现,地图大概10x10就好了
img

  • 写回答

1条回答 默认 最新

  • CSDN专家-showbo 2021-08-18 11:36
    关注

    大概这样,有帮助麻烦点个采纳【本回答右上角】,谢谢~~

    img

    using System;
    namespace ConsoleApplication1
    {
        class Map
        {
            public int Row { get; set; }
            public int Column { get; set; }
            public void ShowMap(Hero hero)
            {
                Console.WriteLine("===========地图显示===========");
                for (var i = 1; i <= Row; i++)
                {
                    for (var j = 1; j <= Column; j++)
                    {
                        if (i == hero.Row && j == hero.Column) Console.Write("1");
                        else Console.Write("0");
                    }
                    Console.WriteLine();
                }
            }
        }
    
        class HM
        {
            public string Name { get; set; }
            /// <summary>
            /// 血量
            /// </summary>
            public int Blood { get; set; }
            /// <summary>
            /// 最大攻击输出
            /// </summary>
            public int Max { get; set; }
            public bool Attack(HM hm)
            {
                var s = "【"+Name+ ",血量:" + Blood + "】攻击【" + hm.Name+",血量:"+hm.Blood+"】,掉血:";
                var r = new Random(Guid.NewGuid().GetHashCode());
                var v = r.Next(0, Max + 1);
                hm.Blood -= v;
                s += v + "【" + hm.Name + "】当前血量:" + hm.Blood;
                Console.WriteLine(s);
                return hm.Blood < 1;
            }
        }
        class Hero:HM
        {
            /// <summary>
            /// 玩家位置所在行
            /// </summary>
            public int Row { get; set; }
            /// <summary>
            /// 玩家位置所在列
            /// </summary>
            public int Column { get; set; }
            public bool Move(string direction,Map map)
            {
                switch (direction)
                {
                    case "w": 
                    case "s":
                        if ((direction == "w" && Row < 2) || (direction == "s" && Row >= map.Row)) { Console.WriteLine("超出地图范围,请重新输入移动命令"); return false; }
                        if (direction == "w") Row--;
                        else Row++;
                        break;
                    case "a": 
                    case "d":
                        if ((direction == "a" && Column < 2) || (direction == "d" && Column >= map.Column)) { Console.WriteLine("超出地图范围,请重新输入移动命令"); return false; }
                        if (direction == "a") Column--;
                        else Column++;
                        break;
                }
                map.ShowMap(this);
                return true;
            }
            public bool Escape()
            {
                var r = new Random();
                var v = r.Next(1, 11);
                if (v < 4)
                {
                    Console.WriteLine("战斗结束,玩家跑了");
                    return true;
                }
                Console.WriteLine("很遗憾,逃跑失败");
                return false;
            }
        }
        class Monster:HM
        {
            public static Monster[] monsters = new Monster[] {
                new Monster{ Name="钢铁侠",Blood=30,Max=10},
                new Monster{ Name="蜘蛛侠",Blood=20,Max=6},
                new Monster{ Name="雷神",Blood=40,Max=15}};
            /// <summary>
            /// 生成怪物
            /// </summary>
            public static Monster Create()
            {
                var r = new Random();
                var m = Monster.monsters[r.Next(0, Monster.monsters.Length)];
    
                return new Monster
                {
                    Name = m.Name,
                    Blood = m.Blood,
                    Max = m.Max
                };
            }
        }
        class Game
        {
            public Map map { get; set; }
            public Hero hero { get; set; }
            public Monster monster { get; set; }
    
            private string GetAttacCmd()
            {
                Console.WriteLine("=========进入战斗系统=========\n"
                    +"选择操作\n"
                    + "1:攻击\n"
                    + "其他:逃跑");
                var cmd = Console.ReadLine();
                return cmd;
            }
            private string GetMoveCmd()
            {
                Console.WriteLine("请输入w/a/s/d移动玩家");
                while (true)
                {
                    var cmd = Console.ReadLine();
                    if (cmd == "w" || cmd == "a" || cmd == "s" || cmd == "d") return cmd;
                    else Console.WriteLine("输入错误,请重新输入移动指令");
                }
            }
            private Monster GetMonster() {
                var r = new Random(Guid.NewGuid().GetHashCode());
                var v = r.Next(1, 11);
                if (v < 7) return Monster.Create();
                return null;
            }
            public void Start()
            {
                map = new Map { Row = 3, Column = 3 };
                hero = new Hero { Name = "玩家", Blood = 100, Max = 20, Row = 1, Column = 1 };
                map.ShowMap(hero);
                while (true)
                {
                    var cmd = GetMoveCmd();
                    if (hero.Move(cmd, map))
                    {
                        monster = GetMonster();
                        if (monster != null)
                        {
                            cmd = GetAttacCmd();
                            if (cmd == "1" || !hero.Escape())
                            {//攻击模式
                                while (hero.Blood > 0 && monster.Blood > 0)
                                {
                                    if (hero.Attack(monster))
                                    {
                                        Console.WriteLine("怪物被打死");
                                        break;
                                    }
                                    if (monster.Attack(hero))
                                    {
                                        Console.WriteLine("玩家被打死,按任意键退出程序..........");
                                        return;
                                    }
                                }
                            }
                        }
                        else Console.WriteLine("未碰到怪物~");
                    }
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                new Game().Start();
                Console.ReadKey();
            }
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月7日
  • 已采纳回答 8月30日
  • 修改了问题 8月18日
  • 创建了问题 8月18日

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大