2012-09-04 139 views
0

嘿,我有點卡住我一直在尋找最好的方式來創建這種類型的迷你遊戲 基本上玩家將進入一個大廳(他們現在得到存儲在地圖或數組),然後當遊戲開始遊戲會從地圖或陣列中隨機選擇1人受到感染(現在將該玩家從地圖/陣列中移除到受感染的地圖/陣列)創建一個迷你遊戲

我將如何去做這件事我已經嘗試過,但是我最終失敗了。

到目前爲止,我有這樣的設置只是爲了測試它

private static HashMap<String, Integer> infected = new HashMap<String, Integer>(); 

private static HashMap<String, Integer> survivors = new HashMap<String, Integer>(); 

private static HashMap<String, Integer> lobby = new HashMap<String, Integer>(); 

我試圖存儲在那裏隨機的名字,因爲我們說話,並對其進行測試

+0

「Map」中'String'和'Integer'之間的連接是什麼? – MadProgrammer

回答

0

我可能會繼續所有可用玩家的「主」列表。從那裏我只是創建一個「感染」球員的參考清單。

您只需在「感染」列表中調用contains即可確定玩家是否感染了病毒。

需要,雖然詳細信息...

0

我不認爲HashMap真的是正確的數據結構,可能考慮做的一類含有「迷」,集合你的Game既可以維護多播放器受感染玩家名單,但在單一名單中找到該玩家可能會更簡單。

interface Moveable { 
    public void forward; 
    public void back; 
    public void left; 
    public void right; 
} 

interface Buffable { 
    public void addBuff(Buff buff); 
    public void removeBuff(Buff buff); 
    public boolean hasBuff(Buff buff); 
} 

class Player implements Moveable, Buffable { 
    private String name; 
    private int health; 
    private List<Buff> buffs; 

    /* 
    contructor etc 
    */ 

    public void addBuff(Buff buff) { 
     buffs.add(buff); 
    } 

    public void removeBuff(Buff buff) { 
     buffs.remove(buff); 
    } 

    public void hasBuff(Buff buff) { 
     return buffs.contains(buff); 
    } 
} 

class Game { 
    private List<Player> players; 
    private List<Player> infected; 

    /* 
    constructor etc 
    */ 

    public void infect(int player) { 
     Player p = players.get(player); 
     p.addBuff(new Infection()); 
     infected.add(p); // see or 
    } 
    // or 
    public List<Player> getInfectedPlayers() { 
     List<Player> ret = new List<Player>(); 
     Buff b = new Infection(); 

     for (Player p : players) { 
      if (p.hasBuff(b)) { 
       ret.add(p); 
      } 
     } 

     return ret; 
    } 
}