将武器添加到库存

时间:2012-12-23 16:29:11

标签: java class interface

我正在尝试将武器添加到玩家库存中。这很难解释,所以我会尽我所能。我拥有的是每个武器的类,Combat的类和Player的类。我试图将它带到当随机数等于某个数字的地方时,它会为玩家库存添加武器。我将把我的代码放在下面。

战斗等级:

public class Combat {

M4 m4 = new M4();
M16 m16 = new M16();
M9 m9 = new M9();
Glock glock = new Glock();
SCAR Scar = new SCAR();

Player player = new Player();
final int chanceOfDrop = 3;


static boolean[] hasWeapon = {false, true};



public static int  ranNumberGen(int chanceOfDrop) {
    return (int) (Math.random()*5); 
}

private void enemyDead() {
    boolean canDrop = false;
    if(ranNumberGen(chanceOfDrop)==0){
        canDrop = true;

    }

    if(canDrop == true){

        if(ranNumberGen(0) == 1) {


            Player.addInvetory(m4.weaponName(wepName), m4.weaponAmmo(wepAmmo)); //Issues here.  wepName & wepAmmo cannot be resolved into variable
            //Should I just delete the line?
            //Trying to get it to add the weapon M4 to the player inventory.  
            //Maybe use an ArrayList? If so I need a couple pointers on how to implement this.  
        }


    }
    }
}

M4班级:

public class M4 implements Armory {
//Weapon classes are practically identical except for differences in the name wepDamage and wepAmmo.
public Integer weaponAmmo(int wepAmmo) {
    wepAmmo = 10;
    return wepAmmo;
}

public Integer weaponDamage(int wepDamage) {
    wepDamage = 5;
    return wepDamage;
}

public String weaponName(String wepName) {
    wepName = "M4";
    return wepName;
}

玩家等级:

public class Player {
public static int health = 100;

//Player Class.

public static void addInvetory(String wepName, int wepAmmo) {

    Player.addInvetory(wepName, wepAmmo);
}

public static void removeInventory(String wepName, int wepAmmo) {

    Player.addInvetory(wepName, wepAmmo);
}


public static void removeAll(String wepName, int wepAmmo) {
    Player.removeAll(wepName, wepAmmo);
}

界面:

public interface Armory {

//Interface implemented by all of the weapons classes.
public Integer weaponAmmo(int wepAmmo);
public Integer weaponDamage(int wepDamage);
public String weaponName(String wepName);

希望你能帮忙!

3 个答案:

答案 0 :(得分:1)

 class Weapon {
     private final String name;
     private final int damage;
     private final int ammo;
     public Weapon(final String name,final  int damage,final  int ammo) {
         this.name = name;
         this.damage = damage;
         this.ammo = ammo;
     }
     public Weapon clone() {
         return new Weapon(this.name,this.damage,this.ammo);
     }
     public String getName() {
         return this.name;
     }
     public int getAmmo() {
         return this.ammo;
     }
     public int getDamage() {
         return this.damage;
     }
 }

 class WeaponFactory {
      static WeaponFactory factory;
      public static WeaponFactory getWeaponFactory() {
           if(factory == null) {
               factory = new WeaponFactory();
           }
           return factory;
      }
      private ArrayList<Weapon> weapons = new ArrayList<Weapon>();
      private Random random;
      private WeaponFactory() {
           //TODO: Fix Ammo and Damage
           weapons.add(new Weapon("M4",0,0));
           weapons.add(new Weapon("M16",0,0));
           weapons.add(new Weapon("M9",0,0));
           weapons.add(new Weapon("Glock",0,0));
           weapons.add(new Weapon("SCAR",0,0));
      }
      public Weapon getWeapon() {
          int w = random.nextInt(weapons.length);
          return weapons.get(w).clone();
      }
 }

 class Combat {
      ...
      private void enemyDead() {
           if(ranNumberGen(chanceOfDrop)==0){
                 Player.addInventory(WeaponFactory.getWeaponFactory().getWeapon());
           }
      }
 }

答案 1 :(得分:0)

您可以使用Armory数组,并生成一个0到数组大小的随机数作为数组的索引,以决定要添加哪个武器。

答案 2 :(得分:0)

好的,伙计,因为关于创建编程语言的问题已经结束,我在这里回答:

我认为你的想法很棒!不要放弃它,但不要太兴奋。我会尝试你所听到的所有选项(解释路线和编译路线)。如果您可以使用其中任何一个,那么您可以继续进一步了解语言创建。这需要一段时间。请耐心等待!