private void moveGamePiece() { Position newPosition = findNewPosition(); while (position2Piece.containsKey(newPosition)) { newPosition = findNewPosition(); } moveTo(newPosition, gamePiece); }
private GameState invokeMove(Piece piece, Position position) { if (position2Piece.containsKey(position)) { throw new IllegalArgumentException("Position " + position + " is already occupied"); } moveTo(position, piece); moveGamePiece(); return createBoard(); }
@Override public void move(Piece piece, Position position, ResultHandler<GameState> resultHandler) { try { final Position oldPosition = piece2Position.get(piece); final GameState gameState = invokeMove(piece, position); resultHandler.result(gameState); nofityListeners(new Move(oldPosition, position)); } catch (Exception e) { handleException(e, resultHandler); } }
private void clear() { piece2Position.clear(); position2Piece.clear(); }
private void moveTo(Position position, Piece piece) { Position oldPosition = piece2Position.get(piece); position2Piece.remove(oldPosition); position2Piece.put(position, piece); piece2Position.put(piece, position); }