一道面向对象练习题,能请各位提供一下代码思路吗?
问题描述:
一道面向对象练习题,能请各位提供一下代码思路吗?目前只学了一些基础,map类里面不能使用For构建地图吗,用控制台实现,地图大概10x10就好了
答
大概这样,有帮助麻烦点个采纳【本回答右上角】,谢谢~~
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();
}
}
}