Advertisement
TwinFrame

OOP_War

Dec 15th, 2020 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Clight_37_OOP_War
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int warriorsCount = 7;
  11.  
  12. Country odinburg = new Country("Одинбург", warriorsCount);
  13. Country dvack = new Country("Двацк", warriorsCount);
  14.  
  15. ShowStats(odinburg, dvack);
  16. Console.CursorVisible = false;
  17. Console.ReadKey(true);
  18.  
  19. while (odinburg.IsAliveArmy && dvack.IsAliveArmy)
  20. {
  21. odinburg.TakeDamage(dvack.GiveDamage());
  22.  
  23. if (!odinburg.IsAliveArmy || !dvack.IsAliveArmy) { break; }
  24.  
  25. dvack.TakeDamage(odinburg.GiveDamage());
  26. System.Threading.Thread.Sleep(100);
  27. }
  28.  
  29. if (odinburg.IsAliveArmy && !dvack.IsAliveArmy) ShowWinner(odinburg);
  30. else ShowWinner(dvack);
  31.  
  32. Console.ReadKey();
  33. }
  34.  
  35. public static void ShowStats(Country country1, Country country2)
  36. {
  37. Console.WriteLine($"Битва между странами {country1.Name} и {country2.Name} и их армиями.");
  38. Console.WriteLine($"\nАрмия страны {country1.Name}:");
  39. country1.ShowArmy();
  40. Console.WriteLine($"\nАрмия страны {country2.Name}:");
  41. country2.ShowArmy();
  42. Console.WriteLine($"\nПо нажатию на любую клавишу начнётся битва между странами.\n");
  43. }
  44.  
  45. public static void ShowWinner(Country country)
  46. {
  47. Console.WriteLine($"\n\nВойну выиграла Страна {country.Name}");
  48. Console.WriteLine("\nОставшиеся бойцы Армии:");
  49. country.ShowArmy();
  50. }
  51. }
  52.  
  53. class Country
  54. {
  55. private Random random = new Random();
  56.  
  57. public string Name { get; private set; }
  58. public bool IsAliveArmy { get { return _army.Count > 0; } }
  59.  
  60. private List<Warrior> _army;
  61.  
  62. public Country(string name, int numberWarriorsInArmy)
  63. {
  64. Random random = new Random();
  65.  
  66. Name = name;
  67.  
  68. List<Warrior> tempArmy = new List<Warrior>();
  69.  
  70. for (int i = 0; i < numberWarriorsInArmy; i++)
  71. {
  72. switch (random.Next(0, 3))
  73. {
  74. case 0:
  75. Warrior cannonFolder = new CannonFolder();
  76. tempArmy.Add(cannonFolder);
  77. break;
  78. case 1:
  79. Warrior soldier = new Soldier();
  80. tempArmy.Add(soldier);
  81. break;
  82. case 2:
  83. Warrior lieutenant = new Lieutenant();
  84. tempArmy.Add(lieutenant);
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90.  
  91. _army = tempArmy;
  92. }
  93.  
  94. public void TakeDamage(int damage)
  95. {
  96. int warrior = random.Next(0, _army.Count);
  97.  
  98. int takeHealth = 0;
  99. bool isAliveWarrior = _army[warrior].TakeDamage(damage, ref takeHealth);
  100.  
  101. if (!isAliveWarrior)
  102. {
  103. Console.WriteLine($"боец страны {Name} умер.");
  104. _army.RemoveAt(warrior);
  105. }
  106. else
  107. {
  108. Console.WriteLine($"боец страны {Name} потерял {takeHealth} здоровья. Осталось {_army[warrior].Health}");
  109. }
  110. }
  111.  
  112. public int GiveDamage()
  113. {
  114. int warrior = random.Next(0, _army.Count);
  115.  
  116. return _army[warrior].Damage;
  117. }
  118.  
  119. public void ShowArmy()
  120. {
  121. foreach (var warrior in _army)
  122. {
  123. warrior.ShowInfo();
  124. }
  125. }
  126. }
  127.  
  128. abstract class Warrior
  129. {
  130. public int Health { get; private set; }
  131. public int Damage { get; private set; }
  132.  
  133. private int _armor;
  134.  
  135. public Warrior(int health, int damage, int armor)
  136. {
  137. Health = health;
  138. Damage = damage;
  139. _armor = armor;
  140. }
  141.  
  142. public void ShowInfo()
  143. {
  144. Console.WriteLine($"Здоровье: {Health}, Урон: {Damage}, Защита {_armor}");
  145. }
  146.  
  147. public bool TakeDamage(int damage, ref int healthLoss)
  148. {
  149. healthLoss = damage - _armor;
  150. Health -= healthLoss;
  151.  
  152. return Health > 0;
  153. }
  154. }
  155.  
  156. class CannonFolder : Warrior
  157. {
  158. public CannonFolder() : base(100, 60, 20) { }
  159.  
  160. }
  161.  
  162. class Soldier : Warrior
  163. {
  164. public Soldier() : base(100, 80, 40) { }
  165.  
  166. }
  167.  
  168. class Lieutenant : Warrior
  169. {
  170. public Lieutenant() : base(100, 90, 30) { }
  171.  
  172. }
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement