RedFlys

Weapon v4

Mar 23rd, 2024 (edited)
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Weapon
  2. {
  3.     private readonly int _damage;
  4.     private int _bullets;
  5.  
  6.     public Weapon(int damage, int bullets)
  7.     {
  8.         if (damage < 0)
  9.             throw new ArgumentOutOfRangeException();
  10.  
  11.         if (bullets < 0)
  12.             throw new ArgumentOutOfRangeException();
  13.  
  14.         _damage = damage ;
  15.         _bullets = bullets;
  16.     }
  17.  
  18.     public bool CanFire => _bullets > 0;
  19.  
  20.     public void Fire(IDamagable target)
  21.     {
  22.         if (target == null)
  23.             throw new ArgumentNullException("target");
  24.  
  25.         if (CanFire)
  26.         {
  27.             target.TakeDamage(_damage);
  28.             _bullets--;
  29.         }
  30.     }
  31. }
  32.  
  33. class Player : IDamagable
  34. {
  35.     private int _health;
  36.  
  37.     public Player(int health)
  38.     {
  39.         if (health < 0)
  40.             throw new ArgumentOutOfRangeException();
  41.  
  42.         _health = health;
  43.     }
  44.  
  45.     public bool IsAlive => _health > 0;
  46.  
  47.     public void TakeDamage(int damage)
  48.     {
  49.         if (damage < 0)
  50.             throw new ArgumentOutOfRangeException();
  51.  
  52.         if (IsAlive)
  53.             _health = Math.Max(_health - damage, 0);
  54.     }
  55. }
  56.  
  57. class Bot
  58. {
  59.     private readonly Weapon _weapon;
  60.  
  61.     public Bot(Weapon weapon) => _weapon = weapon ?? throw new ArgumentNullException();
  62.  
  63.     public void OnSee(Player player)
  64.     {
  65.         if (player == null)
  66.             throw new ArgumentNullException();
  67.  
  68.         if (player.IsAlive && _weapon.CanFire)
  69.             _weapon.Fire(player);
  70.     }
  71. }
  72.  
  73. interface IDamagable
  74. {
  75.     public void TakeDamage(int damage) { }
  76. }
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment