Skip to content

Instantly share code, notes, and snippets.

@GigaOrts
Forked from HolyMonkey/Weapon.cs
Last active August 30, 2023 19:32
Show Gist options
  • Save GigaOrts/f7c8e39518fa399b7fdad4a4c03d5df0 to your computer and use it in GitHub Desktop.
Save GigaOrts/f7c8e39518fa399b7fdad4a4c03d5df0 to your computer and use it in GitHub Desktop.
using System;
class Weapon
{
private readonly int _damage;
private int _bullets;
public Weapon(int damage, int bullets)
{
if (damage < 0)
throw new ArgumentOutOfRangeException(nameof(damage));
if (bullets < 0)
throw new ArgumentOutOfRangeException(nameof(bullets));
_damage = damage;
_bullets = bullets;
}
public bool CanShoot => _bullets > 0;
public void Fire(Player player)
{
if (CanShoot)
{
_bullets--;
player.TakeDamage(_damage);
}
}
}
class Player
{
private int _health;
public Player(int health)
{
if (health <= 0)
throw new ArgumentOutOfRangeException(nameof(health));
_health = health;
}
public bool IsAlive => _health > 0;
public void TakeDamage(int damage)
{
if (IsAlive)
_health -= damage;
}
}
class Bot
{
private readonly Weapon _weapon;
public Bot(Weapon weapon)
{
_weapon = weapon;
}
public void OnSeePlayer(Player player)
{
_weapon.Fire(player);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment