0

我正在尝试将武器添加到玩家库存中。这有点难以解释,所以我会尽力而为。我拥有的是每种武器的类、战斗类和玩家类。我试图让它到达随机数等于某个数字时的位置,它将向玩家库存添加武器。我将把我的代码放在下面。

战斗等级:

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);

希望你能帮忙!

4

3 回答 3

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());
           }
      }
 }
于 2012-12-24T00:19:59.940 回答
0

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

于 2012-12-23T16:31:37.890 回答
0

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

我觉得你的想法很棒!不要放弃它,但不要太兴奋。我会尝试您听说过的所有选项(解释路线和编译路线)。如果您可以让其中任何一个工作,那么您可以继续深入了解语言创建的细节。不过这需要一段时间。耐心点!

于 2012-12-24T00:07:12.733 回答