2015-03-31 75 views
1

所以我有一個playerIDnumwalls爲每個玩家在我正在做的棋盤遊戲。 當每個玩家使用牆壁時,現在每個人都基本上共用牆壁。如何減少HashMap中的值?

所以我想我應該做一個hashmap來保存playerID作爲關鍵和numwalls作爲價值。

但我不知道如何減少密鑰值,當它應該使用牆。

我會展示一個有問題的代碼。

public int getWallsRemaining(int i) { 
    return numWalls; 
} 

public void lastMove(PlayerMove playerMove) { 
    System.out.println("in lastMove... " + playerMove); 
    /** 
    * if piece moves, update its position 
    */ 
    if(playerMove.isMove() == true){ 

     Integer player = playerMove.getPlayerId(); 

     Coordinate newLoc = new Coordinate(playerMove.getEndRow(), playerMove.getEndCol()); 
     playerHomes.put(player, newLoc); 

    } 
    /** 
    * if a wall is placed, subtract the wall form the player who placed it 
    * and subtract the appropriate neighbors. 
    */ 
    if(playerMove.isMove() == false){ 
     numWalls-=1; 
     removeNeighbor(playerMove.getStart(), playerMove.getEnd()); 

    } 


} 

這裏就是我的一切初始化,walls是我的地圖我想要做的事:

private Map<Coordinate, HashSet<Coordinate>> graph; 

private int PlayerID; 
private int numWalls; 
private Map<Integer, Coordinate> playerHomes; 
private Map<Integer, Integer> walls; 



@Override 
public void init(Logger logger, int playerID, int numWalls, Map<Integer, Coordinate> playerHomes) { 


    this.PlayerID = playerID; 
    this.walls = new HashMap<Integer, Integer>(); 
    this.numWalls = numWalls; 
    this.playerHomes = playerHomes; 
    this.graph = new HashMap<Coordinate, HashSet<Coordinate>>(); 
    walls.put(playerID,numWalls); 

    for(int r = 0; r <= 10; r++){ 
     for(int c = 0; c <= 10; c++){ 
      HashSet<Coordinate> neighbors = new HashSet<Coordinate>(); 
       if(r > 0){ 
        neighbors.add(new Coordinate(r - 1, c)); 
       } 
       if(r < 8){ 
        neighbors.add(new Coordinate(r + 1, c)); 
       } 
       if(c > 0){ 
        neighbors.add(new Coordinate(r, c - 1)); 
       } 
       if(c < 8){ 
        neighbors.add(new Coordinate(r, c + 1)); 
       } 
      graph.put((new Coordinate(r,c)), neighbors); 
     } 
    } 
} 

你可以在我的lastMove的方法,我減1牆看到這是我的問題。我想將指定的playerIDnumwall減1。我現在只適用於單人遊戲。我需要這個爲最多4名球員工作。

回答

2

A HashMap只能包含對象(不是基元),所以您必須插入Integer作爲映射的值。

由於一個Integer是你不能直接修改的值的不可變類,你需要拋棄舊值來取代它,像:

HashMap<Player, Integer> walls = new HashMap<Player,Integer>(); 

int currentWalls = walls.get(player); 
walls.put(player, currentWalls-1); 
+0

此代碼應該(可能)被同步。或者使用[AtomicInteger](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicInteger.html)作爲值。 – user949300 2015-03-31 23:47:15

0

要改變數值存儲用鑰匙你應刪除舊值並添加新值。

這就是說,你是否考慮過創建一個Player類來封裝playerId和玩家擁有的牆的數量?它可能對您的計劃有更好的效果。

1

我會用AtomicInteger來保存你的值。這是線程安全的,以防多個玩家同時碰到牆壁。而且它比每次都重新創建一個新的整數(如@Jack答案)簡單

HashMap<Player, AtomicInteger> walls = new HashMap<Player,AtomicInteger>(); 

... 

walls.get(player).decrementAndGet(); 

如果需要的話,可以從decrementAndGet返回值()調用來檢索牆壁的新號碼。

+0

嗨,謝謝你的迴應。我現在通過使用Integer鍵和值的映射工作,但是您的AtomicInteger想法對我來說很有趣,也是新的。所以我在init中創建了一個循環來填充我的牆圖,但是當我做walls.put(我,numwalls)時,我在numWalls下面發現一個錯誤,說它需要一個AtomicInteger,而不是一個Integer。我有點困惑,但我喜歡你的想法 – FatFockFrank 2015-04-01 00:20:17

+0

Java會將int整型爲Integer,而不是AtomicInteger。在你的循環中去'walls.put(theKey,new AtomicInteger(numwalls));' – user949300 2015-04-01 03:16:18