在游戏编程项目中,如何处理多个实例的多个项目?

时间:2017-02-19 07:02:47

标签: java oop design-patterns object-oriented-analysis

假设我有一个拥有多个武器的2人游戏。在游戏开始时,玩家可以选择自己的武器并将其装备到角色中。所以如果现在,我有以下代码来实现上述场景:

public class Weapon {
    private String name;
    private int attackPoints;
    private int effectivity = 100;

    public Weapon(String name, int attackPoints) {
        this.name = name;
        this.attackPoints = attackPoints;
    }
    // ...
}

public class Character {
    private String name;
    private Weapon weapon;

    public Character(String name, Weapon weapon) {
        this.name = name;
        this.weapon = weapon;
    }
    // ...
}

public class Game {
    Scanner scanner = new Scanner(System.in);
    public void createCharacter() {
        String namePlayer = "Knight";
        String nameOpponent = "Thief";

        //Weapon Menu
        System.out.println("Select weapon");
        System.out.println("1. Dagger - Attack: 10");
        System.out.println("2. Sword - Attack 20");

        Character player;
        Character opponent;
        int choice = scanner.nextInt();

        //Weapon assignment
        switch (choice) {
        case 1:
            player = new Character(name, new Weapon("Dagger", 10));
            break;
        case 2:
            player = new Character(name, new Weapon("Sword", 20));
            break;
        }
    }
}

重新提出问题

我担心的是,如果我必须添加一个额外的武器,说" Ax有15点伤害"例如,然后我必须修改武器菜单,同时在我的switch语句中添加一个额外的案例。是否有可能我只需要修改某种类型的列表,并且它会处理可供玩家选择的武器列表,同时使武器的实例不同于球员和对手角色?

旧问题

我担心的是,每次有新武器时,我都要修改武器菜单,同时修改武器分配部分。是否有更好的实施方式?

3 个答案:

答案 0 :(得分:1)

您基本上需要一个WeaponFactory,它可以了解所有可用的Weapon。该工厂还可以根据用户输入创建特定武器的新实例。这样,我认为,你将实现你所需要的一切

  • 所有武器都在一个地方创建,
  • 添加新的一项需要更改单个班级。

这是一个简化的例子。

public static class WeaponFactory {
    public List<Weapon> availableWeapons() {
        return Arrays.asList(new Dagger(), new Rifle());
    }

    public Weapon create(int type) {
        return availableWeapons().get(type);
    }
}

public interface Weapon {
    public abstract double attack();
    public abstract String name();
}

public static abstract class WeaponAdapter implements Weapon {
    @Override public String toString() {  return name() + " - " + attack(); }
}

public static class Dagger extends WeaponAdapter {
    @Override public double attack() { return 10; }
    @Override public String name() { return "Dagger"; }
}

public static class Rifle extends WeaponAdapter {
    @Override public double attack() { return 80; }
    @Override public String name() { return "Rifle"; }
}

由于在Weapon中显示Menu s的新实例并不是真的需要创建它们,所以有一点效率低下,但我认为这对于这个例子来说已经足够了。

为了缓解这种情况,您可以使用一些反射或使用枚举。你基本上需要的是一个函数,它会根据它的类型创建一个武器,所以它可能像是

List<Class<? extends Weapon>> weapons = Arrays.asList(Dagger.class, Rifle.class);

public Weapon create(Class<Weapon> weaponType) {
    try {
        return weaponType.newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
        throw new RuntimeException("Not possible");
    }
}

同样,所有这些都是WeaponFactory的一部分,Weapon是所有table#search.table.table-striped.search thead tr th Header th Header th Header th Header th Header th Header th Header th Header th Header th Header th Header tbody - @search_result.each do |result| tr td = result[data] td = result[data] td = result[data] td = result[data] td = result[data] td = result[data] td = result[data] td = result[data] td = result[data] td = result[data] td = result[data] 相关的单个地方。

答案 1 :(得分:0)

你的角色类可以有一个像这样的武器列表:

public class Character {
private String name;
private List<Weapon> weapons = new ArrayList<Weapon>();

 public Character(String name) { //keep this constructor. I know I have omitted weapons variable from this constructor.
    this.name = name;        
}
// down below you have getters and setters for name and weapons
}

然后在初始化一个新角色时,执行以下操作:

Character player1;
...

switch (choice) {
    case 1:
        Weapon w = new Weapon("Dagger", 10));
        player = new Character(name);
        player.getWeapons().add(w);
        break;

答案 2 :(得分:0)

是的,你可以......

..创建每个武器作为对象将其添加到数组(或列表等),所以你不必编码

System.out.println("Select weapon");
System.out.println("1. Dagger - Attack: 10");
System.out.println("2. Sword - Attack 20");

你可以去

    Weapon[] weapons = new Weapon[2];
    weapons[0] = new Weapon ("Dagger",10);
    weapons[1] = new Weapon ("Sword",20);
    for (int i = 0; i < weapons.length; i++) {
        System.out.println(i+1+". "weapons[i].getName+" - "+weapons[i].getAttackPoints);
    }
    int choice = scanner.nextInt();
    player = new Character(namePlayer, weapons[choice-1]);

只是为了照顾

  • 类武器具有名称和攻击点的getter
  • 并且数组的开头如下所示,索引为0