Created
December 28, 2016 08:34
-
-
Save nielsutrecht/339dd79e01bd02ca1c220e338e93d765 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
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; | |
} | |
} |
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
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()); | |
} | |
} |
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
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); | |
} | |
} |
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
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