@Override
public void setPlayerPosition(final PlayerPosition playerPosition)
throws GameException {
if(playerPosition.getPlayer()==null){
throw new GameException("player must be set");
}
if(playerPosition.getPosition()==null){
throw new GameException("position can not be null");
}
if(playerPosition.getPosition().getBoardMap()==null){
throw new GameException("position map must be set first");
}
if(!playerPosition.getPosition().getBoardMap().equals(this.getBoardMap())){
throw new GameException("the map of the position ["+playerPosition.getPosition().getBoardMap().getName()+"] is different from the map of the game ["+this.getBoardMap().getName()+"]");
}
final Coordinate coordinate = playerPosition.getPosition();
final Player player = this.getWhoOccupiesPosition(coordinate);
if(player!=null){
if(!getMrX().equals(player)){
throw new GameException("this position has been already occupied by other detective");
}else if(getMrX().equals(player) && !getMrX().equals(playerPosition.getPlayer())){
//has Mr X been caught?
this.playerPositions.put(playerPosition.getPlayer(), playerPosition);
this.setGameStatus(GameStatus.FINISHED);
throw new GameException("Game is finished, detective ["+playerPosition.getPlayer().getName()+"] has captured Mr X");
}
}
playerPosition.getPlayer().setPosition(this, playerPosition.getPosition());
this.playerPositions.put(playerPosition.getPlayer(), playerPosition);