Created
November 11, 2025 09:19
-
-
Save sunmeat/bcb5638782dae89c73cee39960329348 to your computer and use it in GitHub Desktop.
вибірка об'єктів за умовами
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Linq; | |
| using System.Text; | |
| public class Creature | |
| { | |
| public string Name { get; set; } // назва істоти | |
| public bool Flying { get; set; } // літає? | |
| public bool Ranged { get; set; } // стріляє на відстані? | |
| public int Attack { get; set; } // атака | |
| public int Defense { get; set; } // захист | |
| public int Health { get; set; } // здоров'я (HP) | |
| public int Speed { get; set; } // швидкість | |
| } | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Console.OutputEncoding = Encoding.UTF8; | |
| var creatures = new List<Creature> | |
| { | |
| new Creature { Name = "Стрілець", Flying = false, Ranged = true, Attack = 6, Defense = 3, Health = 10, Speed = 6 }, | |
| new Creature { Name = "Арбалетник", Flying = false, Ranged = true, Attack = 6, Defense = 3, Health = 10, Speed = 6 }, | |
| new Creature { Name = "Грифон", Flying = true, Ranged = false, Attack = 6, Defense = 6, Health = 25, Speed = 7 }, | |
| new Creature { Name = "Королівський грифон", Flying = true, Ranged = false, Attack = 9, Defense = 8, Health = 30, Speed = 9 }, | |
| new Creature { Name = "Монах", Flying = false, Ranged = true, Attack = 13, Defense = 11, Health = 40, Speed = 6 }, | |
| new Creature { Name = "Фанатик", Flying = false, Ranged = true, Attack = 14, Defense = 14, Health = 40, Speed = 7 }, | |
| new Creature { Name = "Ангел", Flying = true, Ranged = false, Attack = 20, Defense = 20, Health = 200, Speed = 12 }, | |
| new Creature { Name = "Архангел", Flying = true, Ranged = true, Attack = 30, Defense = 30, Health = 250, Speed = 18 } // літає та стріляє для прикладу | |
| }; | |
| var flyingOrRanged = creatures | |
| .Where(c => c.Flying || c.Ranged) // лямбда: фільтрує за умовою (літає АБО стріляє) | |
| .OrderByDescending(c => c.Attack) // лямбда: сортує за атакою (за спаданням) | |
| .Select(c => new // лямбда в Select для проекції | |
| { | |
| Name = c.Name, | |
| Attack = c.Attack, | |
| Defense = c.Defense, | |
| Health = c.Health, | |
| Speed = c.Speed, | |
| Type = c.Flying ? (c.Ranged ? "Літаючий + Стріляючий" : "Літаючий") : "Стріляючий" | |
| }) | |
| .ToList(); // матеріалізація результату | |
| Console.WriteLine("Літаючі або стріляючі істоти фракції Замок з HoMM 3 (відсортовані за атакою):"); | |
| foreach (var unit in flyingOrRanged) | |
| { | |
| Console.WriteLine($"- {unit.Name}: Атака {unit.Attack}, Захист {unit.Defense}, HP {unit.Health}, Швидкість {unit.Speed} ({unit.Type})"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment