2012-12-23 64 views
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); 

希望你能幫助!

+5

什麼問題? –

+1

我在這裏沒有找到問題 –

+4

爲什麼你只有一個擁有Name屬性的武器類,爲什麼你必須每個武器有一個類? –

回答

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()); 
      } 
     } 
} 
+0

非常感謝!現在它更有意義。 – user1925218

0

您可以使用Armory的數組,並從0到數組的大小生成一個隨機數作爲數組的索引,以決定添加哪個武器。

0

好吧,夥計,因爲你的有關創建一個編程語言問題被關閉,我回答它通過這裏:

我認爲你的想法是偉大的!不要放棄它,但不要太激動。我會嘗試你聽過的所有選項(解釋路線和編譯路線)。如果你能讓其中任何一個人工作,那麼你可以着手進一步細化語言創建。但這需要一段時間。耐心一點!

+0

謝謝!我會做的。 – user1925218

+0

是的...讓我更新它的進展 – user1706978

相關問題