Skip to content

Instantly share code, notes, and snippets.

@shahnawaz
Created July 31, 2025 09:19
Show Gist options
  • Select an option

  • Save shahnawaz/6d46f83ced3081f106ba47766ba16ec6 to your computer and use it in GitHub Desktop.

Select an option

Save shahnawaz/6d46f83ced3081f106ba47766ba16ec6 to your computer and use it in GitHub Desktop.
humanvszombie.ts
interface IZombie {
health: number; // 20 | 60 | 80
takeDamage: (value: number) => void;
}
interface IHuman {
weapons: IWeapon[];
activeWeaponIndex: number;
switchWeapon: () => void;
reload: () => void;
}
interface IWeapon {
magazineSize: number;
damage: number;
bulletCount: number;
reload: () => void;
shoot: (target: IZombie) => void;
}
class Zombie implements IZombie {
constructor(public health: 20 | 60 | 80){}
takeDamage(value: number) {
this.health -= value;
// TODO: what if health is -ve
}
}
class Weapon implements IWeapon {
magazineSize: number;
damage: number;
bulletCount: number = 0;
constructor(weaponType: 'rifle' | 'pistol'){
if (weaponType === 'rifle') {
this.magazineSize = 50;
this.damage = 40;
} else {
this.magazineSize = 10;
this.damage = 15;
}
this.reload();
}
reload() {
this.bulletCount = this.magazineSize;
};
shoot(zombie: IZombie) {
if (this.bulletCount <= 0) {
return;
}
zombie.takeDamage(this.damage);
this.bulletCount--;
};
}
const createWeapon = (weaponType: 'rifle' | 'pistol'): Weapon => {
// TODO: create weapon based on weapon type
}
/*
const pistol: Weapon = {
magazineSize: 10,
damage: 15,
bulletCount: 10
}
const rifle: Weapon = {
magazineSize: 50,
damage: 40,
bulletCount: 50
}
*/
// create human, zombie, create pistol, create rifle
// shooting a zombie, reload weapon, changing weapon
// damage zombie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment