Advertisement
NS2A2

Bài tập lớn C#

Jan 11th, 2021
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Test
  8. {
  9.     class Skill
  10.     {
  11.         private int _damage;
  12.         private int _mp;
  13.         private int _reArmor;
  14.         private string _name;
  15.  
  16.         public int Damage { get => _damage; set => _damage = value; }
  17.         public int Mp { get => _mp; set => _mp = value; }
  18.         public int ReArmor { get => _reArmor; set => _reArmor = value; }
  19.         public string Name { get => _name; set => _name = value; }
  20.  
  21.         public Skill()
  22.         {
  23.  
  24.         }
  25.  
  26.         public Skill(string name,int damage, int mp, int rearmor)
  27.         {
  28.             Damage = damage;
  29.             Name = name;
  30.             Mp = mp;
  31.             ReArmor = rearmor;
  32.         }
  33.         public void ShowInfoSKill()
  34.         {
  35.             Console.WriteLine(Name + "(mana = " + Mp + ", dame = " + Damage + ", reduce armor = " + ReArmor + ")");
  36.         }
  37.     }
  38.    
  39.     class Player
  40.     {
  41.         private string _name;
  42.         private int _MP;
  43.         private int _HP;
  44.         private int _armor;
  45.         private List<Skill> _SkillList;
  46.  
  47.         public string Name { get => _name; set => _name = value; }
  48.         public int MP { get => _MP; set => _MP = value; }
  49.         public int HP { get => _HP; set => _HP = value; }
  50.         public int Armor { get => _armor; set => _armor = value; }
  51.         public List<Skill> SkillList { get => _SkillList; set => _SkillList = value; }
  52.  
  53.         public Player()
  54.         {
  55.  
  56.         }
  57.         public Player(string name, int hp, int mp, int armor,List<Skill> skillList)
  58.         {
  59.             Name = name;
  60.             HP = hp;
  61.             MP = mp;
  62.             Armor = armor;
  63.             SkillList = skillList;
  64.         }
  65.         public void ShowInfoPlayer()
  66.         {
  67.             Console.WriteLine("HP: " + HP);
  68.             Console.WriteLine("MP: " + MP);
  69.             Console.WriteLine("Armor: " + Armor);
  70.         }
  71.         public void get_dame(Skill skill)
  72.         {
  73.             HP -= skill.Damage + Math.Max(0, Armor - skill.ReArmor);
  74.         }
  75.         public bool dead()
  76.         {
  77.             return HP <= 0;
  78.         }
  79.         public bool CheckMana(Skill skill)
  80.         {
  81.             return skill.Mp <= MP;
  82.         }
  83.         public void ShowAllSkill(List<Skill> SkillList)
  84.         {
  85.             for (int i = 0; i < SkillList.Count; i++)
  86.             {
  87.                 Console.WriteLine("Skill thu " + i);
  88.                 SkillList[i].ShowInfoSKill();
  89.             }
  90.         }
  91.         public Skill UseSKill()
  92.         {
  93.             int i;
  94.             do
  95.             {
  96.                 this.ShowAllSkill(SkillList);
  97.                 Console.WriteLine("choose skill");
  98.                 i = int.Parse(Console.ReadLine());
  99.                 if (CheckMana(SkillList[i]))
  100.                 {
  101.                     MP -= SkillList[i].Mp;
  102.                     return SkillList[i];
  103.                 }
  104.                 else
  105.                 {
  106.                     i = -1;
  107.                     Console.WriteLine("Not enough mana");
  108.                 }
  109.             }
  110.             while (i < 0 || i >= SkillList.Count);
  111.             return null;
  112.         }
  113.     }
  114.     class Monster
  115.     {
  116.         private string _name;
  117.         private int _HP;
  118.         private int _Armor;
  119.         private List<Skill> sKillList;
  120.  
  121.         public int HP { get => _HP; set => _HP = value; }
  122.         public int Armor { get => _Armor; set => _Armor = value; }
  123.         internal List<Skill> SkillList { get => sKillList; set => sKillList = value; }
  124.         public string Name { get => _name; set => _name = value; }
  125.  
  126.         public Monster()
  127.         {
  128.  
  129.         }
  130.         public Monster(string name,int hp,int mp, int armor,List<Skill> skilllist)
  131.         {
  132.             HP = hp;
  133.             Armor = armor;
  134.             SkillList = skilllist;
  135.             Name = name;
  136.         }
  137.  
  138.         public bool Dead()
  139.         {
  140.             return this.HP <= 0;
  141.         }
  142.         public void get_dame(Skill skill)
  143.         {
  144.             HP -= skill.Damage + Math.Max(0, this.Armor - skill.ReArmor);
  145.         }
  146.         public void ShowInfoMonster()
  147.         {
  148.             Console.WriteLine("HP:" + HP);
  149.             Console.WriteLine("Armor: " + Armor);
  150.         }
  151.         public void ShowAllSkill(List<Skill> SkillList)
  152.         {
  153.             for (int i = 0; i < SkillList.Count; i++)
  154.             {
  155.                 Console.WriteLine("Skill thu " + i);
  156.                 SkillList[i].ShowInfoSKill();
  157.             }
  158.         }
  159.         public Skill UseSKill()
  160.         {
  161.             int i;
  162.             do
  163.             {
  164.                 this.ShowAllSkill(SkillList);
  165.                 Console.WriteLine("choose skill");
  166.                 i = int.Parse(Console.ReadLine());
  167.                 if (i == 0)
  168.                 {
  169.                     return SkillList[i];
  170.                 }
  171.             }
  172.             while (i != 0);
  173.             return null;
  174.         }
  175.     }
  176.     class BattleField
  177.     {
  178.         private Player _a;
  179.         private Monster _b;
  180.         private bool _isPlayerTurn;
  181.  
  182.         public bool IsPlayerTurn { get => _isPlayerTurn; set => _isPlayerTurn = value; }
  183.         internal Player A { get => _a; set => _a = value; }
  184.         internal Monster B { get => _b; set => _b = value; }
  185.  
  186.         public BattleField()
  187.         {
  188.  
  189.         }
  190.         public BattleField(Player a, Monster b, bool isplayerturn)
  191.         {
  192.             A = a;
  193.             B = b;
  194.             IsPlayerTurn = isplayerturn;
  195.         }
  196.         public void ChangeTurn()
  197.         {
  198.             IsPlayerTurn = !IsPlayerTurn;
  199.         }
  200.         public string Winner()
  201.         {
  202.             if (A.dead())
  203.             {
  204.                 return B.Name;
  205.             }
  206.             else if (B.Dead())
  207.             {
  208.                 return A.Name;
  209.             }
  210.             return null;
  211.         }
  212.         public void ShowResult()
  213.         {
  214.             Console.WriteLine("Winner is " + Winner());
  215.         }
  216.         public void Fight()
  217.         {
  218.             while (A.HP > 0 && B.HP > 0)
  219.             {
  220.                 if (IsPlayerTurn)
  221.                 {
  222.                     Console.WriteLine("===================");
  223.                     Console.WriteLine("Luot nguoi choi");
  224.                     A.ShowInfoPlayer();
  225.                     Console.WriteLine("===================");
  226.                     B.get_dame(A.UseSKill());
  227.                     Console.WriteLine("===================");
  228.                     ChangeTurn();
  229.                 }
  230.                 else
  231.                 {
  232.                     Console.WriteLine("===================");
  233.                     Console.WriteLine("Luot quai vat");
  234.                     B.ShowInfoMonster();
  235.                     Console.WriteLine("===================");
  236.                     A.get_dame(B.UseSKill());
  237.                     Console.WriteLine("===================");
  238.                     ChangeTurn();
  239.                 }
  240.             }
  241.             ShowResult();
  242.         }
  243.     }
  244.     class RunMain
  245.     {
  246.         static void Main(string[] args)
  247.         {
  248.             Skill skill1 = new Skill("Danh chay", 30, 0, 0);
  249.             Skill skill2 = new Skill("Ban ten", 100, 30, 10);
  250.             Player a = new Player("Cung thu", 200, 150, 10,new List<Skill>{ skill1, skill2 });
  251.  
  252.             Skill skill3 = new Skill("Danh chay", 40, 0, 0);
  253.             Skill skill4 = new Skill("Ban ten", 100, 50, 30);
  254.             Player b = new Player("Hiep si", 150, 100, 20, new List<Skill> { skill3, skill4 });
  255.  
  256.             Skill skill5 = new Skill("Thoi lua", 30, 0, 0);
  257.             Monster c = new Monster("Ho", 500,0, 20,new List<Skill> { skill5 });
  258.  
  259.             BattleField War = new BattleField(a, c, true);
  260.             War.Fight();
  261.             Console.ReadKey();
  262.         }
  263.     }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement