所以我有一個playerID
和numwalls
爲每個玩家在我正在做的棋盤遊戲。 當每個玩家使用牆壁時,現在每個人都基本上共用牆壁。如何減少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牆看到這是我的問題。我想將指定的playerID
numwall
減1。我現在只適用於單人遊戲。我需要這個爲最多4名球員工作。
此代碼應該(可能)被同步。或者使用[AtomicInteger](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicInteger.html)作爲值。 – user949300 2015-03-31 23:47:15