Skip to content

Instantly share code, notes, and snippets.

@nielsutrecht
Created December 28, 2016 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nielsutrecht/339dd79e01bd02ca1c220e338e93d765 to your computer and use it in GitHub Desktop.
Save nielsutrecht/339dd79e01bd02ca1c220e338e93d765 to your computer and use it in GitHub Desktop.
public class Enemy {
private int health;
public Enemy(int health) {
this.health = health;
}
public void hit(int damage) {
health -= damage;
}
public int getHealth() {
return health;
}
}
public class Player {
private Weapon weapon;
private int damageBase;
public Player(int damageBase, Weapon weapon) {
this.damageBase = damageBase;
this.weapon = weapon;
}
public void attack(Enemy enemy) {
int damage = damageBase + weapon.rollDamage();
enemy.hit(damage);
System.out.printf("Player hit enemy for %s damage, enemy is at %s health\n", damage, enemy.getHealth());
}
}
public class RPG {
public static void main(String... argv) {
Weapon weapon = new Weapon(5, 10);
Player player = new Player(5, weapon);
Enemy enemy = new Enemy(100);
do {
player.attack(enemy);
}
while(enemy.getHealth() > 0);
}
}
public class Weapon {
private int damageMin;
private int damageMax;
public Weapon(int damageMin, int damageMax) {
this.damageMax = damageMax;
this.damageMin = damageMin;
}
public int rollDamage() {
int range = damageMax - damageMin;
return damageMin + (int)(Math.random() * range);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment