【发布时间】:2015-03-31 23:29:49
【问题描述】:
所以我有一个playerID 和numwalls 用于我正在制作的棋盘游戏中的每个玩家。
现在要在每个玩家都用墙的时候拆墙,基本上每个人都在共享墙。
所以我想我应该创建一个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。我现在只适用于 1 人。我需要它来为最多 4 名玩家工作。
【问题讨论】: