@Override public Field move( Moveable moveable, SetterOfPosition setterOfPosition, Field target, ResourceHolder resourceHolder) throws FieldsNotConnectedException, MoveNotPossibleException, NotEnoughResourceException { if (!moveable.getPosition().isConnected(target)) { throw new FieldsNotConnectedException(moveable.getPosition(), target); } CanMoveReport canMoveReport = canMove(moveable, target, resourceHolder); if (canMoveReport.isPossible()) { resourceHolder.pay(movePointCollector.collect(moveable, target).asPayment()); setterOfPosition.setPosition(target); return target; } else if (!canMoveReport.canPay()) { throw new NotEnoughResourceException( movePointCollector.collect(moveable, target).asPayment(), resourceHolder.amountOf(MovePoint.class)); } else if (canMoveReport.isBlocked() || !canMoveReport.isAbleToEnd()) { throw new MoveNotPossibleException(canMoveReport); } else { throw new MoveNotPossibleException("move is not possible - unknown reason"); } }
@Override public boolean canMove(Moveable moveable, MovePath movePath, ResourceHolder resourceHolder) { if (moveable == null || movePath == null || resourceHolder == null) { return false; } Moveable testMoveable = moveable.cloneMoveable(); ResourceHolder testResourceHolder = resourceHolder.cloneResourceHolder(); boolean canMove = true; for (Field field : movePath.getPathFields()) { try { testMoveable.move(field, testResourceHolder); } catch (Exception e) { canMove = false; break; } } return canMove; }
@Override public CanMoveReport canMove(Moveable moveable, Field target, ResourceHolder resourceHolder) { if (moveable == null || target == null || resourceHolder == null) { return new CanMoveReportBasic.Builder().buildNull(); } return new CanMoveReportBasic.Builder() .setCost(movePointCollector.collect(moveable, target)) .setMaxPayment(resourceHolder.get(MovePoint.class)) .setMoveIsBlockedBy(checkMoveBlocks(moveable, target)) .setMoveIsUnableToEndBy(checkMoveUnableToEnd(moveable, target)) .setSource(moveable.getPosition()) .setTarget(target) .build(); }