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 に答える