Ι postaram się dać ci przykład, jak zaprojektować swój arsenał i zbrojownię.
Naszym celem jest oddzielenie bytów, dlatego broń powinna być interfejsem.
interface Weapon {
public int getDamage();
}
Załóżmy, że każdy gracz może posiadać tylko jedną broń, możemy jej użyć Strategy pattern
w celu łatwej zmiany broni.
class Knife implements Weapon {
private int damage = 10;
@Override
public int getDamage() {
return this.damage;
}
}
class Sword implements Weapon {
private int damage = 40;
@Override
public int getDamage() {
return this.damage;
}
}
Innym przydatnym wzorem byłby wzorzec zerowy na wypadek, gdyby gracz nie był uzbrojony.
class Weaponless implements Weapon {
private int damage = 0;
@Override
public int getDamage() {
return this.damage;
}
}
Jeśli chodzi o zbrojownię, możemy nosić wiele ekwipunku obronnego.
// Defence classes,interfaces
interface Armor {
public int defend();
}
class Defenseless implements Armor {
@Override
public int defend() {
return 0;
}
}
abstract class Armory implements Armor {
private Armor armory;
protected int defence;
public Armory() {
this(new Defenseless());
}
public Armory(Armor force) {
this.armory = force;
}
@Override
public int defend() {
return this.armory.defend() + this.defence;
}
}
// Defence implementations
class Helmet extends Armory {
{
this.defence = 30;
}
}
class Gloves extends Armory {
{
this.defence = 10;
}
}
class Boots extends Armory {
{
this.defence = 10;
}
}
Dla oddzielenia stworzyłem interfejs dla obrońcy.
interface Defender {
int getDefended();
}
I Player
klasa.
class Player implements Defender {
private String title;
private int health = 100;
private Weapon weapon = new Weaponless();
private List<Armor> armory = new ArrayList<Armor>(){{ new Defenseless(); }};
public Player(String name) {
this.title = name;
}
public Player() {
this("John Doe");
}
public String getName() {
return this.title;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public void attack(Player enemy) {
System.out.println(this.getName() + " attacked " + enemy.getName());
int attack = enemy.getDefended() + enemy.getHealth()- this.weapon.getDamage();
int health = Math.min(enemy.getHealth(),attack);
System.out.println("After attack " + enemy.getName() + " health is " + health);
enemy.setHealth(health);
}
public int getHealth() {
return health;
}
private void setHealth(int health) {
/* Check for die */
this.health = health;
}
public void addArmory(Armor armor) {
this.armory.add(armor);
}
@Override
public int getDefended() {
int defence = this.armory.stream().mapToInt(armor -> armor.defend()).sum();
System.out.println(this.getName() + " defended , armory points are " + defence);
return defence;
}
}
Dodajmy trochę rozgrywki.
public class Game {
public static void main(String[] args) {
Player yannis = new Player("yannis");
Player sven = new Player("sven");
yannis.setWeapon(new Knife());
sven.setWeapon(new Sword());
sven.addArmory(new Helmet());
sven.addArmory(new Boots());
yannis.attack(sven);
sven.attack(yannis);
}
}
Voila!